1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | use Time::HiRes; |
3 | use Time::HiRes; |
4 | use Net::IRC3::Client::Connection; |
4 | use Net::IRC3::Client::Connection; |
5 | |
5 | |
6 | my $BOTSERVER = 'localhost'; |
6 | # requires: commands.ext |
7 | my $BOTPORT = '6667'; |
7 | |
8 | my $BOTNAME = 'cfbot'; |
8 | return unless exists $cf::CFG{irc_server}; |
9 | my $BOTCHAN = '#cf'; |
9 | |
|
|
10 | my $BOTSERVER = $cf::CFG{irc_server}; |
|
|
11 | my $BOTPORT = $cf::CFG{irc_port}; |
|
|
12 | my $BOTNAME = $cf::CFG{irc_nick}; |
|
|
13 | my $BOTCHAN = $cf::CFG{irc_chan}; |
10 | |
14 | |
11 | my $CON; # the connection |
15 | my $CON; # the connection |
12 | |
16 | |
13 | sub unload { |
17 | sub unload { |
14 | $CON->disconnect if $CON; |
18 | $CON->disconnect if $CON; |
… | |
… | |
19 | my ($msg) = @_; |
23 | my ($msg) = @_; |
20 | $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) |
24 | $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) |
21 | if $CON; |
25 | if $CON; |
22 | } |
26 | } |
23 | |
27 | |
|
|
28 | sub users { |
|
|
29 | $CON |
|
|
30 | ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } |
|
|
31 | : () |
|
|
32 | } |
|
|
33 | |
|
|
34 | sub handle_fcmd { |
|
|
35 | my ($name, $me, $msg) = @_; |
|
|
36 | |
|
|
37 | if ($msg eq "!who") { |
|
|
38 | $CON->send_chan ($BOTCHAN, NOTICE => $_, $BOTCHAN) |
|
|
39 | for ext::commands::who_listing (); |
|
|
40 | |
|
|
41 | } elsif ($msg =~ /^\!tell/) { |
|
|
42 | my (undef, $target, $tmsg) = split / /, $msg, 3; |
|
|
43 | |
|
|
44 | if (my $other = cf::player::find $target) { |
|
|
45 | |
|
|
46 | if ($tmsg) { |
|
|
47 | if ($me eq $target) { |
|
|
48 | $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); |
|
|
49 | } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
|
|
50 | $CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); |
|
|
51 | } else { |
|
|
52 | utf8::encode $tmsg; # ->message not yet utf8-ified |
|
|
53 | cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
|
|
54 | |
|
|
55 | $other->ob->message ("$name/$me tells you: $tmsg"); |
|
|
56 | $other->ob->{ext_last_tell} = "$name/$me"; |
|
|
57 | } |
|
|
58 | } else { |
|
|
59 | $CON->send_chan ($BOTCHAN, NOTICE => "$me: What do you want to tell $target?", cf::NDI_UNIQUE); |
|
|
60 | } |
|
|
61 | |
|
|
62 | } |
|
|
63 | } |
|
|
64 | } |
|
|
65 | |
24 | sub check_connection { |
66 | sub check_connection { |
25 | return if $CON; |
67 | return if $CON; |
26 | |
68 | |
27 | $CON = Net::IRC3::Client::Connection->new; |
69 | $CON = Net::IRC3::Client::Connection->new; |
28 | $CON->connect ($BOTSERVER, $BOTPORT); |
70 | $CON->connect ($BOTSERVER, $BOTPORT); |
… | |
… | |
31 | $CON->reg_cb ( |
73 | $CON->reg_cb ( |
32 | #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, |
74 | #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, |
33 | irc_privmsg => sub { |
75 | irc_privmsg => sub { |
34 | my ($con, $msg) = @_; |
76 | my ($con, $msg) = @_; |
35 | my $name = 'irc'; |
77 | my $name = 'irc'; |
|
|
78 | my $nick = Net::IRC3::Util::prefix_nick ($msg); |
36 | my $NOW = Time::HiRes::time; |
79 | my $NOW = Time::HiRes::time; |
37 | my $tmsg = $msg->{trailing}; |
80 | my $tmsg = $msg->{trailing}; |
38 | $tmsg =~ s/\x01[^\x01]*\x01//g; |
81 | $tmsg =~ s/\x01[^\x01]*\x01//g; |
|
|
82 | $tmsg =~ s/\015?\012/ /g; |
39 | if ($tmsg =~ m/\S/) { |
83 | if ($tmsg =~ /^\!/) { |
|
|
84 | handle_fcmd ($name, $nick, $tmsg); |
|
|
85 | } elsif ($tmsg =~ m/\S/) { |
40 | $_->ob->message ( |
86 | $_->ob->message ( |
41 | "$name/".Net::IRC3::Util::prefix_nick ($msg)." chats: $tmsg", |
87 | "$name/".$nick." chats: $tmsg", cf::NDI_BLUE |
42 | cf::NDI_BLUE |
|
|
43 | ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
88 | ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
44 | } |
89 | } |
45 | 1; |
90 | 1; |
46 | }, |
91 | }, |
47 | # registered => sub { |
92 | # registered => sub { |
… | |
… | |
52 | 0; |
97 | 0; |
53 | } |
98 | } |
54 | ); |
99 | ); |
55 | } |
100 | } |
56 | |
101 | |
57 | my $timer; |
102 | Event->timer (after => 1, interval => 30, data => cf::WF_AUTOCANCEL, cb => \&check_connection); |
58 | |
103 | |
59 | sub new_timer { |
|
|
60 | $timer = AnyEvent->timer (after => 10, cb => sub { |
|
|
61 | check_connection (); |
|
|
62 | &new_timer; # and restart the time |
|
|
63 | }); |
|
|
64 | } |
|
|
65 | |
|
|
66 | new_timer; # create first timer |
|
|
67 | check_connection (); |
|
|