| 1 | package Webqq::Client::Plugin::SendMsgFromSocket; | ||
| 2 | use Webqq::Client::Util qw(console); | ||
| 3 | use AnyEvent::Socket; | ||
| 4 | use AnyEvent::Handle; | ||
| 5 | my %connection; | ||
| 6 | sub call{ | ||
| 7 | my $client = shift; | ||
| 8 | tcp_server "127.0.0.1", 2014, sub { | ||
| 9 | my($fh,$host,$port) = @_; | ||
| 10 | my $client_id = rand(); | ||
| 11 | $connection{$client_id}[0] = $fh; | ||
| 12 | my $hdl; $hdl = AnyEvent::Handle->new( | ||
| 13 | fh => $fh , | ||
| 14 | on_error => sub { | ||
| 15 | my ($hdl, $fatal, $msg) = @_; | ||
| 16 | $hdl->destroy; | ||
| 17 | delete $client{$client_id}; | ||
| 18 | }, | ||
| 19 | ); | ||
| 20 | $connection{$client_id}[1] = $hdl; | ||
| 21 | $hdl->push_read (line => sub { | ||
| 22 | my ($hdl, $line) = @_; | ||
| 23 | delete $connection{$client_id}; | ||
| 24 | console "从socket接收到发送消息指令: $line\n"; | ||
| 25 | my($type,$uin,$content) = split(/\s+/,$line,3); | ||
| 26 | if($type eq 'group'){ | ||
| 27 | my $gcode = $client->get_group_code_from_gid($uin); | ||
| 28 | unless(defined $gcode){ | ||
| 29 | console "指定的群不存在,指令无效: $line\n"; | ||
| 30 | return; | ||
| 31 | } | ||
| 32 | $client->send_group_message(to_uin=>$uin,content=>$content); | ||
| 33 | } | ||
| 34 | elsif($type eq 'friend'){ | ||
| 35 | my $f = $client->search_friend($uin); | ||
| 36 | unless(defined $f){ | ||
| 37 | console "指定的好友不存在,指令无效: $line\n"; | ||
| 38 | return; | ||
| 39 | } | ||
| 40 | $client->send_message(to_uin=>$uin,content=>$content); | ||
| 41 | } | ||
| 42 | elsif($type eq 'discuss'){ | ||
| 43 | my $d = $client->search_discuss($uin); | ||
| 44 | unless(defined $d){ | ||
| 45 | console "指定的讨论组不存在,指令无效: $line\n"; | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | $client->send_discuss_message(to_uin=>$uin,content=>$content); | ||
| 49 | } | ||
| 50 | elsif($type eq 'group_sess'){ | ||
| 51 | my($to_uin,$gid) = split /:/,$uin,2; | ||
| 52 | my $gcode = $client->get_group_code_from_gid($gid); | ||
| 53 | my $m = $client->search_member_in_group($gcode,$to_uin); | ||
| 54 | unless(defined $m){ | ||
| 55 | console "指定的群或群成员不存在,指令无效: $line\n"; | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | $client->send_sess_message(to_uin=>$to_uin,content=>$content,gid=>$gid); | ||
| 59 | } | ||
| 60 | elsif($type eq 'discuss_sess'){ | ||
| 61 | my($to_uin,$did) = split /:/,$uin,2; | ||
| 62 | my $m = $client->search_member_in_discuss($did,$to_uin); | ||
| 63 | unless(defined $m){ | ||
| 64 | console "指定的讨论组或讨论组成员不存在,指令无效: $line\n"; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | $client->send_sess_message(to_uin=>$to_uin,content=>$content,did=>$did); | ||
| 68 | } | ||
| 69 | }); | ||
| 70 | |||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | 1; |