1 |
#! perl |
2 |
|
3 |
use Time::HiRes; |
4 |
use Net::IRC3::Client::Connection; |
5 |
|
6 |
my $BOTSERVER = 'localhost'; |
7 |
my $BOTPORT = '6667'; |
8 |
my $BOTNAME = 'cfbot'; |
9 |
my $BOTCHAN = '#cf'; |
10 |
|
11 |
my $CON; # the connection |
12 |
|
13 |
sub unload { |
14 |
$CON->disconnect if $CON; |
15 |
undef $CON; |
16 |
} |
17 |
|
18 |
sub do_notice { |
19 |
my ($msg) = @_; |
20 |
$CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) |
21 |
if $CON; |
22 |
} |
23 |
|
24 |
sub handle_fcmd { |
25 |
my ($name, $me, $msg) = @_; |
26 |
|
27 |
if ($msg eq "!who") { |
28 |
my ($numplayers, $numwiz, @plist) = (0, 0); |
29 |
|
30 |
foreach my $pl (cf::player::list) { |
31 |
$numplayers++; |
32 |
$numwiz++ |
33 |
if ($pl->ob->flag (cf::FLAG_WIZ)); |
34 |
push (@plist, $pl); |
35 |
} |
36 |
|
37 |
$CON->send_chan ($BOTCHAN, NOTICE => "Total Players in The World. ($numplayers) -- WIZ($numwiz)", $BOTCHAN); |
38 |
|
39 |
if ($numplayers > 0) { |
40 |
foreach my $pl (@plist) { |
41 |
$CON->send_chan ($BOTCHAN, NOTICE => |
42 |
"* " . $pl->ob->name . "/" . $pl->ob->level . " " . ( length $pl->own_title ? $pl->own_title : "the " . $pl->title ) . " " |
43 |
.($pl->ob->flag (cf::FLAG_WIZ) ? " [WIZ] " : "") |
44 |
." [" . ($pl->ob->map ? $pl->ob->map->path : "NULL") . "]", $BOTCHAN); |
45 |
} |
46 |
} |
47 |
} elsif ($msg =~ /^\!tell/) { |
48 |
my (undef, $target, $tmsg) = split / /, $msg, 3; |
49 |
|
50 |
if (my $other = cf::player::find $target) { |
51 |
|
52 |
if ($tmsg) { |
53 |
if ($me eq $target) { |
54 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); |
55 |
} elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { |
56 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); |
57 |
} else { |
58 |
utf8::encode $tmsg; # ->message not yet utf8-ified |
59 |
cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; |
60 |
|
61 |
$other->ob->message ("$name/$me tells you: $tmsg"); |
62 |
$other->ob->{ext_last_tell} = "$name/$me"; |
63 |
} |
64 |
} else { |
65 |
$CON->send_chan ($BOTCHAN, NOTICE => "$me: What do you want to tell $target?", cf::NDI_UNIQUE); |
66 |
} |
67 |
|
68 |
} |
69 |
} |
70 |
} |
71 |
|
72 |
sub check_connection { |
73 |
return if $CON; |
74 |
|
75 |
$CON = Net::IRC3::Client::Connection->new; |
76 |
$CON->connect ($BOTSERVER, $BOTPORT); |
77 |
$CON->send_srv (JOIN => undef, $BOTCHAN); |
78 |
$CON->register ($BOTNAME, $BOTNAME, 'crossfire connection'); |
79 |
$CON->reg_cb ( |
80 |
#d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 }, |
81 |
irc_privmsg => sub { |
82 |
my ($con, $msg) = @_; |
83 |
my $name = 'irc'; |
84 |
my $nick = Net::IRC3::Util::prefix_nick ($msg); |
85 |
my $NOW = Time::HiRes::time; |
86 |
my $tmsg = $msg->{trailing}; |
87 |
$tmsg =~ s/\x01[^\x01]*\x01//g; |
88 |
$tmsg =~ s/\015?\012/ /g; |
89 |
if ($tmsg =~ /^\!/) { |
90 |
handle_fcmd ($name, $nick, $tmsg); |
91 |
} elsif ($tmsg =~ m/\S/) { |
92 |
$_->ob->message ( |
93 |
"$name/".$nick." chats: $tmsg", cf::NDI_BLUE |
94 |
) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; |
95 |
} |
96 |
1; |
97 |
}, |
98 |
# registered => sub { |
99 |
# 1; |
100 |
# }, |
101 |
disconnect => sub { |
102 |
undef $CON; |
103 |
0; |
104 |
} |
105 |
); |
106 |
} |
107 |
|
108 |
my $timer; |
109 |
|
110 |
sub new_timer { |
111 |
$timer = AnyEvent->timer (after => 10, cb => sub { |
112 |
check_connection (); |
113 |
&new_timer; # and restart the time |
114 |
}); |
115 |
} |
116 |
|
117 |
new_timer; # create first timer |
118 |
check_connection (); |