Weixin/Plugin/IRCShell.pm untested

filter: Statementstmt 0.0% 0 / 348 Branchbran 0.0% 0 / 194 Conditioncond 0.0% 0 / 129 MC/DCmcdc 0.0% 0 / 86 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 765 CC 98 SCAR 91.0
CC
98
Cov
0%
call
9506.0
BEGIN
6.0
BEGIN
2.0
CRAP 8952.4
1 package Mojo::Weixin::Plugin::IRCShell;
2 $Mojo::Weixin::Plugin::IRCShell::PRIORITY = 99;
3 use strict;
4 use List::Util qw(first);
5 use File::Spec ();
6 BEGIN{
7 $Mojo::Weixin::Plugin::IRCShell::has_mojo_irc_server = 0;
8 eval{
9 require Mojo::IRC::Server::Chinese;
10 };
11 $Mojo::Weixin::Plugin::IRCShell::has_mojo_irc_server = 1 if not $@;
12 }
13  
14 my $ircd;
15 sub call{
16 my $client = shift;
17 my $data = shift;
18 $client->die("请先安装模块 Mojo::IRC::Server::Chinese") if not $Mojo::Weixin::Plugin::IRCShell::has_mojo_irc_server;
19 my $master_irc_nick = $data->{master_irc_nick};
20 my $upload_api = $data->{upload_api}; #'http://img.vim-cn.com/';
21 my $media_dir = $data->{media_dir} // $client->media_dir;
22 my $is_load_friend = defined $data->{load_friend}?$data->{load_friend}:0;
23 my %mode = ref($data->{mode}) eq "HASH"?%{$data->{mode}}:();
24 $data->{auto_join_channel} = 1 if not defined $data->{auto_join_channel};
25 $data->{create_chanserv_user} = 1 if not defined $data->{create_chanserv_user};
26 $ircd = Mojo::IRC::Server::Chinese->new(listen=>$data->{listen},log=>$client->log,auth=>$data->{auth});
27 if($data->{create_chanserv_user}){
28 my $chanserv= $ircd->new_user(id=>"__ChanServ__",name=>"ChanServ:虚拟用户",user=>"__ChanServ__",nick=>"ChanServ",virtual=>1);
29 $ircd->on(new_channel=>sub{
30 my ($ircd,$channel) = @_;
31 return if index( $channel->mode ,"v" ) == -1;
32 $chanserv->join_channel($channel) if not $chanserv->is_join_channel($channel);
33 });
34 }
35 $ircd->on(privmsg=>sub{
36 my($ircd,$user,$msg) = @_;
37 if(substr($msg->{params}[0],0,1) eq "#" ){
38 my $channel_name = $msg->{params}[0];
39 my $content = $msg->{params}[1];
40 my $raw_content = $content;
41 my $channel = $ircd->search_channel(name=>$channel_name);
42 return if not defined $channel;
43 my $group = $client->search_group(id=>$channel->id);
44 return if not defined $group;
45 if($content=~/^([^\s]+?): /){
46 my $at_nick = $1;
47 $content =~s/^([^\s]+?): /\@$at_nick / if $ircd->search_user(nick=>$at_nick);
48 $raw_content = $content;
49 }
50 if($user->nick ne $master_irc_nick and !$user->is_localhost){
51 #$content = $user->nick . ": $content";
52 $content .= "\n(来自irc用户 - ".$user->nick.")";
53 }
54  
55 # 群组会话里面发送文件的二级命令解析 i.e. sendFile /tmp/dog.png
56 #if ($content =~ m/^(?:sendFile |!!)([^\s]+)$/i) {
57 if ($content =~ m/^(?:sendFile |!!)(?|"([^"]+)"|'([^']+)'|([^\s"']+))/i) {
58 my $media_path = $1;
59 if($media_path !~ /^(\/|\.)/){#没有使用绝对路径或者相对路径
60 $media_path = File::Spec->catfile($media_dir,$media_path);
61 }
62 if(-f $media_path or $media_path=~/^https?:\/\//){
63 $group->send_media($media_path);
64 }
65 else{
66 $user->send($user->ident,"PRIVMSG",$channel->name,"[系统提示]无法发送,无效媒体:$media_path");
67 }
68 }
69 else {
70 $group->send($content,sub{
71 $_[1]->from("irc");
72 $_[1]->cb(sub{
73 my($client,$msg)=@_;
74 if($msg->is_success){
75 if($msg->content ne $raw_content){
76 $msg->content($raw_content);
77 }
78 }
79 else{
80 $user->send($user->ident,"PRIVMSG",$channel_name,$content . "[发送失败]");
81 }
82 });
83 });
84 }
85 }
86 elsif($user->nick eq $master_irc_nick or $user->is_localhost){
87 my $nick = $msg->{params}[0];
88 my $content = $msg->{params}[1];
89 my $u = $ircd->search_user(nick=>$nick);
90 #return if not defined $u;
91 if(not defined $u){
92 my $friend = $client->search_friend(displayname=>$nick);
93 return if not defined $friend;
94 my $channel = $ircd->search_channel(name=>'#我的好友') || $ircd->new_channel(name=>'#我的好友',mode=>"Pivs");
95 return if not defined $channel;
96 $u = $ircd->new_user(
97 id =>$friend->id,
98 name =>$friend->displayname . ":虚拟用户",,
99 user =>$friend->id,
100 nick =>$friend->displayname,
101 virtual => 1,
102 );
103 $u->join_channel($channel);
104 $user->send($user->ident,"PRIVMSG",$nick,"[系统提示]已从微信好友中搜索到对应昵称好友并生成irc用户,现在可以继续和好友聊天了");
105  
106 }
107 my $friend = $client->search_friend(id=>$u->id);
108 if(defined $friend){
109 # 好友会话里面发送文件二级命令解析 sendFile /tmp/dog.png
110 #if ($content =~ m/^(?:sendFile |!!)([^\s]+)$/i) {
111 if ($content =~ m/^(?:sendFile |!!)(?|"([^"]+)"|'([^']+)'|([^\s"']+))/i) {
112 my $media_path = $1;
113 if($media_path !~ /^(\/|\.)/){#没有使用绝对路径或者相对路径
114 $media_path = File::Spec->catfile($media_dir,$media_path);
115 }
116 if(-f $media_path or $media_path=~/^https?:\/\//){
117 $friend->send_media($media_path);
118 }
119 else{
120 $user->send($user->ident,"PRIVMSG",$nick,"[系统提示]无法发送,无效媒体:$media_path");
121 }
122 }
123 else {
124 $friend->send($content,sub{
125 $_[1]->from("irc");
126 $_[1]->cb(sub{
127 my($client,$msg)=@_;
128 return if $msg->is_success;
129 $user->send($user->ident,"PRIVMSG",$nick,$content . "[发送失败]");
130 });
131 });
132 }
133 }
134 else{
135 $user->send($user->ident,"PRIVMSG",$nick,$content . "[发送失败]");
136 $user->send($user->ident,"PRIVMSG",$nick,"你和 [$nick] 并非好友关系");
137 }
138 }
139 });
140  
141 my $callback = sub{
142 my %delete_channel = map {$_->id => $_} grep {$_->name ne "#我的好友"} $ircd->channels;
143 $ircd->remove_user($_) for grep {$_->is_virtual} $ircd->users;
144 #$ircd->remove_user($_) for grep {$_->id ne '__ChanServ__' and $_->is_virtual} $ircd->users;
145 my $friend_channel = $ircd->new_channel(name=>'#我的好友',mode=>"Pivs");
146 if($is_load_friend){
147 $client->each_friend(sub{
148 my($client,$friend) = @_;
149 my $user = $ircd->search_user(nick=>$friend->displayname,virtual=>0);
150 if(defined $user){
151 $user->once(close=>sub{
152 my $virtual_user = $ircd->new_user(
153 id => $friend->id,
154 name => $friend->displayname . ":虚拟用户",
155 nick => $friend->displayname,
156 user => $friend->id,
157 virtual => 1,
158 );
159 $virtual_user->join_channel($friend_channel);
160 });
161 }
162 else{
163 my $virtual_user = $ircd->new_user(
164 id => $friend->id,
165 name => $friend->displayname . ":虚拟用户",
166 nick => $friend->displayname,
167 user => $friend->id,
168 virtual => 1,
169 );
170 $virtual_user->join_channel($friend_channel);
171 }
172 });
173 }
174 $client->each_group(sub{
175 my($client,$group) = @_;
176 return if ref $data->{ban_group} eq "ARRAY" and first {$group->displayname eq $_} @{$data->{ban_group}};
177 return if ref $data->{allow_group} eq "ARRAY" and !first {$group->displayname eq $_} @{$data->{allow_group}};
178 my $mode = defined $mode{$group->displayname}?$mode{$group->displayname}:"Piv";
179 $mode .= "v" if index($mode,"v")==-1;
180 my $channel_name = '#'.$group->displayname;$channel_name=~s/\s|,|&//g;
181 my $channel = $ircd->search_channel(name=>$channel_name);
182 if(defined $channel){
183 delete $delete_channel{$channel->id};
184 $channel->id($group->id);
185 $channel->remove_user($_) for grep {$_->is_virtual} $channel->users;
186 }
187 else{ $ircd->new_channel(id=>$group->id,name=>'#'.$group->displayname,mode=>$mode);}
188 });
189 $ircd->remove_channel($_) for values %delete_channel;
190 };
191 $client->on(new_group=>sub{
192 my($client,$group) = @_;
193 return if ref $data->{ban_group} eq "ARRAY" and first {$group->displayname eq $_} @{$data->{ban_group}};
194 return if ref $data->{allow_group} eq "ARRAY" and !first {$group->displayname eq $_} @{$data->{allow_group}};
195 my $mode = defined $mode{$group->displayname}?$mode{$group->displayname}:"Piv";
196 $mode .= "v" if index($mode,"v")==-1;
197 my $channel_name = '#'.$group->displayname;$channel_name=~s/\s|,|&//g;
198 my $channel = $ircd->search_channel(name=>$channel_name);
199 if(defined $channel){
200 $channel->id($group->id);
201 $channel->remove_user($_) for grep {$_->is_virtual} $channel->users;
202 }
203 else{ $ircd->new_channel(id=>$group->id,name=>'#'.$group->displayname,mode=>$mode);}
204 });
205 $client->on(lose_group=>sub{
206 my($client,$group) = @_;
207 return if ref $data->{ban_group} eq "ARRAY" and first {$group->displayname eq $_} @{$data->{ban_group}};
208 return if ref $data->{allow_group} eq "ARRAY" and !first {$group->displayname eq $_} @{$data->{allow_group}};
209 my $channel_name = '#'.$group->displayname;$channel_name=~s/\s|,|&//g;
210 my $channel = $ircd->search_channel(name=>$channel_name);
211 $ircd->remove_channel($channel) if defined $channel;
212 });
213 $client->on(ready=>sub{
214 $master_irc_nick //= $client->user->displayname;
215 $callback->();
216 $client->on(login=>$callback);
217 });
218 $client->on(receive_message=>sub{
219 my($client,$msg) = @_;
220 if($msg->type eq "friend_message"){
221 return if $data->{is_ban_official} and $msg->sender->category eq '公众号';
222 my $friend = $msg->sender;
223 my $user = $ircd->search_user(id=>$friend->id,virtual=>1) || $ircd->search_user(nick=>$friend->displayname,virtual=>0);
224 my $channel = $ircd->search_channel(name=>'#我的好友') || $ircd->new_channel(name=>'#我的好友',mode=>"Pivs");
225 return if not defined $channel;
226 if(not defined $user){
227 $user = $ircd->new_user(
228 id =>$friend->id,
229 name =>$friend->displayname . ":虚拟用户",,
230 user =>$friend->id,
231 nick =>$friend->displayname,
232 virtual => 1,
233 );
234 $user->join_channel($channel);
235 }
236 else{
237 $user->join_channel($channel) if $user->is_virtual and !$user->is_join_channel($channel);
238 }
239  
240 if(defined $upload_api and $msg->format eq 'media'){
241 my $content = $msg->content;
242 #接收的图片上传到图床
243 $client->http_post($upload_api,form=>{image=>{content=>$msg->media_data}},sub{
244 my($url,$ua,$tx)=@_;
245 if(not defined $url or $url!~/https?:\/\//){
246 $client->warn("二维码图片上传云存储失败: 响应数据异常");
247 }
248 else{$content =~ s/^\[(.+?)\]\(.+?\)/[$1]($url)/g;}
249 for (grep { $_->nick eq $master_irc_nick or $_->is_localhost} grep {!$_->is_virtual} $ircd->users){
250 for my $line (split /\r?\n/,$content){
251 $_->send($user->ident,"PRIVMSG",$_->nick,$line);
252 $user->send($user->ident,"PRIVMSG",$_->nick,$line);
253 }
254 }
255 });
256 }
257 else{
258 for (grep { $_->nick eq $master_irc_nick or $_->is_localhost} grep {!$_->is_virtual} $ircd->users){
259 for my $line (split /\r?\n/,$msg->content){
260 $_->send($user->ident,"PRIVMSG",$_->nick,$line);
261 $user->send($user->ident,"PRIVMSG",$_->nick,$line);
262 }
263 }
264 }
265 }
266  
267 elsif($msg->type eq "group_message"){
268 my $member = $msg->sender;
269 #return if not defined $member;
270 return if ref $data->{ban_group} eq "ARRAY" and first {$msg->group->displayname eq $_} @{$data->{ban_group}};
271 return if ref $data->{allow_group} eq "ARRAY" and !first {$msg->group->displayname eq $_} @{$data->{allow_group}};
272 my $user = $ircd->search_user(id=>$member->id,virtual=>1) || $ircd->search_user(nick=>$member->displayname,virtual=>0);
273 my $channel = $ircd->search_channel(id=>$member->group->id) ||
274 $ircd->new_channel(id=>$member->group->id,name=>'#'.$member->group->displayname,);
275 return if not defined $channel;
276 if(not defined $user){
277 $user=$ircd->new_user(
278 id =>$member->id,
279 name =>$member->displayname . ":虚拟用户",
280 user =>$member->id,
281 nick =>$member->displayname,
282 virtual => 1,
283 );
284  
285 $user->join_channel($channel);
286 }
287 elsif($user->is_virtual){
288 $user->join_channel($channel) if not $user->is_join_channel($channel);
289 }
290 else{
291 $user->join_channel($channel) if not $user->is_join_channel($channel);
292 }
293  
294 #master用户如果没有加入到该频道就自动加入,防止漏收消息
295 if($data->{auto_join_channel}){
296 for(grep {$_->nick eq $master_irc_nick or $_->is_localhost}
297 grep {!$_->is_virtual} $ircd->users){
298 $_->join_channel($channel) if not $_->is_join_channel($channel);
299 }
300 }
301  
302 if(defined $upload_api and $msg->format eq 'media'){
303 my $content = $msg->content;
304 #接收的图片上传到图床
305 $client->http_post($upload_api,form=>{image=>{content=>$msg->media_data}},sub{
306 my($url,$ua,$tx)=@_;
307 if(not defined $url or $url!~/https?:\/\//){
308 $client->warn("二维码图片上传云存储失败: 响应数据异常");
309 }
310 else{$content =~ s/^\[(.+?)\]\(.+?\)/[$1]($url)/g;}
311 for(grep {!$_->is_virtual} $channel->users){
312 my @content = split /\r?\n/,$content;
313 if($content[0]=~/^\@([^\s]+?) /){
314 my $at_nick = $1;
315 if($ircd->search_user(nick=>$at_nick)){
316 $content[0] =~s/^\@([^\s]+?) //;
317 $_ = "$at_nick: " . $_ for @content;
318 }
319 }
320 for my $line (@content){
321 $_->send($user->ident,"PRIVMSG",$channel->name,$line);
322 }
323 }
324  
325 });
326 }
327 else{
328 for(grep {!$_->is_virtual} ($channel->users)){
329 my @content = split /\r?\n/,$msg->content;
330 if($content[0]=~/^\@([^\s]+?) /){
331 my $at_nick = $1;
332 if($ircd->search_user(nick=>$at_nick)){
333 $content[0] =~s/^\@([^\s]+?) //;
334 $_ = "$at_nick: " . $_ for @content;
335 }
336 }
337 for my $line (@content){
338 $_->send($user->ident,"PRIVMSG",$channel->name,$line);
339 }
340 }
341 }
342 }
343  
344 });
345 $client->on(send_message=>sub{
346 my($client,$msg) = @_;
347 return if $msg->from eq "irc";
348 if($msg->type eq "friend_message"){
349 my $friend = $msg->receiver;
350 my $user = $ircd->search_user(id=>$friend->id,virtual=>1) || $ircd->search_user(nick=>$friend->displayname,virtual=>0);
351 my $channel = $ircd->search_channel(name=>'#我的好友') || $ircd->new_channel(name=>'#我的好友',mode=>"Pivs");
352 if(not defined $user){
353 $user=$ircd->new_user(
354 id =>$friend->id,
355 name =>$friend->displayname . ":虚拟用户",
356 user =>$friend->id,
357 nick =>$friend->displayname,
358 virtual => 1,
359 );
360 $user->join_channel($channel);
361 }
362 elsif($user->is_virtual){
363 $user->join_channel($channel) if not $user->is_join_channel($channel);
364 }
365  
366 if(defined $upload_api and $msg->format eq 'media'){
367 my $content = $msg->content;
368 #发送的图片上传到图床
369 $client->http_post($upload_api,form=>{image=>{content=>$msg->media_data}},sub{
370 my($url,$ua,$tx)=@_;
371 if(not defined $url or $url!~/https?:\/\//){
372 $client->warn("二维码图片上传云存储失败: 响应数据异常");
373 }
374 else{$content =~ s/^\[(.+?)\]\(.+?\)/[$1]($url)/g;}
375 for(
376 grep {$_->nick eq $master_irc_nick or $_->is_localhost}
377 grep {!$_->is_virtual} $ircd->users
378 )
379 {
380 for my $line (split /\r?\n/,$content){
381 $_->send($_->ident,"PRIVMSG",$user->nick,$line);
382 $user->send($_->ident,"PRIVMSG",$user->nick,$line);
383 }
384 }
385 });
386 }
387 else{
388 for(
389 grep {$_->nick eq $master_irc_nick or $_->is_localhost}
390 grep {!$_->is_virtual} $ircd->users
391 )
392 {
393 for my $line (split /\r?\n/,$msg->content){
394 $_->send($_->ident,"PRIVMSG",$user->nick,$line);
395 $user->send($_->ident,"PRIVMSG",$user->nick,$line);
396 }
397 }
398 }
399 }
400 elsif($msg->type eq "group_message"){
401 return if ref $data->{ban_group} eq "ARRAY" and first {$msg->group->displayname eq $_} @{$data->{ban_group}};
402 return if ref $data->{allow_group} eq "ARRAY" and !first {$msg->group->displayname eq $_} @{$data->{allow_group}};
403 my $channel = $ircd->search_channel(id=>$msg->group->id);
404 return unless defined $channel;
405  
406 #master用户如果没有加入到该频道就自动加入,防止漏收消息
407 if($data->{auto_join_channel}){
408 for(grep {$_->nick eq $master_irc_nick or $_->is_localhost}
409 grep {!$_->is_virtual} $ircd->users){
410 $_->join_channel($channel) if not $_->is_join_channel($channel);
411 }
412 }
413  
414 if(defined $upload_api and $msg->format eq 'media'){
415 my $content = $msg->content;
416 $client->http_post($upload_api,form=>{image=>{content=>$msg->media_data}},sub{
417 my($url,$ua,$tx)=@_;
418 if(not defined $url or $url!~/https?:\/\//){
419 $client->warn("二维码图片上传云存储失败: 响应数据异常");
420 }
421 else{$content =~ s/^\[(.+?)\]\(.+?\)/[$1]($url)/g;}
422 for my $master_irc_client (
423 grep {$_->nick eq $master_irc_nick or $_->is_localhost}
424 grep {!$_->is_virtual} $ircd->users
425 ){
426 for(grep {!$_->{virtual}} $channel->users){
427 my @content = split /\r?\n/,$content;
428 if($content[0]=~/^\@([^\s]+?) /){
429 my $at_nick = $1;
430 if($ircd->search_user(nick=>$at_nick)){
431 $content[0] =~s/^\@([^\s]+?) //;
432 map {$_ = "$at_nick: " . $_} @content;
433 }
434 }
435 for my $line (@content){
436 $_->send($master_irc_client->ident,"PRIVMSG",$channel->name,$line);
437 }
438 }
439 }
440 });
441 }
442 else{
443 for my $master_irc_client (
444 grep {$_->nick eq $master_irc_nick or $_->is_localhost}
445 grep {!$_->is_virtual} $ircd->users
446 ){
447 for(grep {!$_->{virtual}} $channel->users){
448 my @content = split /\r?\n/,$msg->content;
449 if($content[0]=~/^\@([^\s]+?) /){
450 my $at_nick = $1;
451 if($ircd->search_user(nick=>$at_nick)){
452 $content[0] =~s/^\@([^\s]+?) //;
453 map {$_ = "$at_nick: " . $_} @content;
454 }
455 }
456 for my $line (@content){
457 $_->send($master_irc_client->ident,"PRIVMSG",$channel->name,$line);
458 }
459 }
460 }
461 }
462 }
463 });
464  
465 my $property_change_callback = sub{
466 my($client,$object,$property,$old,$new)=@_;
467 if($object->is_friend){
468 return if $property ne "name" and $property ne "markname";
469 my $user = $ircd->search_user(id=>$object->id,virtual=>1);
470 return unless defined $user;
471 my $displayname = $object->displayname;
472 $displayname=~s/\s|\@|!//g;
473 $user->set_nick($displayname) if $displayname ne $user->nick;
474 }
475 elsif($object->is_group_member){
476 return if $property ne "name" and $property ne "markname";
477 my $user = $ircd->search_user(id=>$object->id,virtual=>1);
478 return unless defined $user;
479 my $displayname = $object->displayname;
480 $displayname=~s/\s|\@|!//g;
481 $user->set_nick($displayname) if $displayname ne $user->nick;
482 }
483 elsif($object->is_group){
484 return if $property ne "name";
485 my $channel = $ircd->search_channel(id=>$object->id);
486 return unless defined $channel;
487 my $channel_name = '#'.$object->displayname;$channel_name=~s/\s|,|&//g;
488 $channel->name($channel_name);
489 }
490 };
491 $client->on("friend_property_change"=>$property_change_callback,
492 "group_member_property_change"=>$property_change_callback,
493 "group_property_change"=>$property_change_callback,
494 );
495  
496 $ircd->ready();
497 }
498 1;