ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/irc.ext
(Generate patch)

Comparing deliantra/server/ext/irc.ext (file contents):
Revision 1.8 by elmex, Wed Nov 7 13:35:32 2007 UTC vs.
Revision 1.21 by root, Sat Nov 10 03:43:01 2012 UTC

1#! perl 1#! perl
2 2
3use Time::HiRes; 3use Time::HiRes;
4use Net::IRC3::Client::Connection; 4use AnyEvent::IRC::Client;
5use AnyEvent::IRC::Util qw/filter_colors/;
5 6
6# requires: commands.ext 7# requires: commands.ext
7 8
8return unless exists $cf::CFG{irc_server}; 9return unless exists $cf::CFG{irc_server};
9 10
10my $BOTSERVER = $cf::CFG{irc_server}; 11CONF BOTSERVER : irc_server = undef;
11my $BOTPORT = $cf::CFG{irc_port}; 12CONF BOTPORT : irc_port = undef;
12my $BOTNAME = $cf::CFG{irc_nick}; 13CONF BOTNAME : irc_nick = undef;
13my $BOTCHAN = $cf::CFG{irc_chan}; 14CONF BOTCHAN : irc_chan = undef;
14 15
15my $CON; # the connection 16our $CON; # the connection
16 17
17sub unload { 18sub unload {
18 $CON->disconnect if $CON; 19 $CON->disconnect if $CON;
19 undef $CON; 20 undef $CON;
20} 21}
21 22
22sub do_notice { 23sub do_notice {
23 my ($msg) = @_; 24 my ($msg) = @_;
24 25
25 utf8::encode $msg; 26 utf8::encode $msg;
26 $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN) 27 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, $msg)
27 if $CON; 28 if $CON;
28} 29}
29 30
30sub users { 31sub users {
31 $CON 32 $CON
32 ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } 33 ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} }
33 : () 34 : ()
34} 35}
36
37# for nicklist monitoring, #d# should be done event-based
38our %NICKLIST;
39our $NICKLIST = AE::timer 20, 59.17, sub {
40 return unless defined &ext::nickmon::NT_OTHER;
41
42 my %NEXTLIST;
43 for (users) {
44 &ext::nickmon::upd ("irc/$_", &ext::nickmon::NT_OTHER, "irc")
45 unless exists $NICKLIST{$_};
46
47 delete $NICKLIST{$_};
48 undef $NEXTLIST{$_};
49 }
50
51 &ext::nickmon::del ("irc/$_")
52 for keys %NICKLIST;
53
54 %NICKLIST = %NEXTLIST;
55};
35 56
36sub handle_fcmd { 57sub handle_fcmd {
37 my ($name, $me, $msg) = @_; 58 my ($name, $me, $msg) = @_;
38 59
39 if ($msg eq "!who") { 60 if ($msg eq "!who") {
47 68
48 if (my $other = cf::player::find_active $target) { 69 if (my $other = cf::player::find_active $target) {
49 70
50 if ($tmsg) { 71 if ($tmsg) {
51 if ($me eq $target) { 72 if ($me eq $target) {
52 $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); 73 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!");
53 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { 74 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) {
54 $CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN); 75 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it.");
55 } else { 76 } else {
56 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; 77 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg;
57 78
58 $other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); 79 $other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF);
59 } 80 }
66} 87}
67 88
68sub check_connection { 89sub check_connection {
69 return if $CON; 90 return if $CON;
70 91
71 $CON = Net::IRC3::Client::Connection->new; 92 $CON = AnyEvent::IRC::Client->new;
93 $CON->set_exception_cb (sub {
94 my ($exp, $ev) = @_;
95 cf::error "IRC: IRC EXCEPTION (event $ev): $exp\n";
96 });
72 $CON->connect ($BOTSERVER, $BOTPORT); 97 $CON->connect ($BOTSERVER, $BOTPORT, {
98 nick => $BOTNAME,
99 user => $BOTNAME,
100 real => 'deliantra server'
101 });
73 $CON->send_srv (JOIN => undef, $BOTCHAN); 102 $CON->send_srv (JOIN => undef, $BOTCHAN);
74 $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection');
75 $CON->reg_cb ( 103 $CON->reg_cb (
76 #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 },
77 irc_privmsg => sub { 104 irc_privmsg => sub {
78 my ($con, $msg) = @_; 105 my ($con, $msg) = @_;
79 my $name = 'irc'; 106 my $name = 'irc';
80 my $nick = Net::IRC3::Util::prefix_nick ($msg); 107 my $nick = AnyEvent::IRC::Util::prefix_nick ($msg);
81 my $NOW = Time::HiRes::time; 108 my $NOW = Time::HiRes::time;
82 109
83 my $tmsg = $msg->{trailing}; 110 my $tmsg = filter_colors ($msg->{params}->[-1]);
84 $tmsg =~ s/\x01[^\x01]*\x01//g; 111 $tmsg =~ s/\x01[^\x01]*\x01//g;
85 $tmsg =~ s/\015?\012/ /g; 112 $tmsg =~ s/\015?\012/ /g;
86 113
87 utf8::decode $tmsg; 114 utf8::decode $tmsg;
88 115
89 if ($tmsg =~ /^\!/) { 116 if ($tmsg =~ /^\!/) {
90 handle_fcmd ($name, $nick, $tmsg); 117 handle_fcmd ($name, $nick, $tmsg);
91 } elsif ($tmsg =~ m/\S/) { 118 } elsif ($tmsg =~ m/\S/) {
92 $_->ns->send_msg ($ext::chat::CHAT_CHANNEL, 119 $_->ns->send_msg ($cf::CHAT_CHANNEL,
93 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF 120 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF
94 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; 121 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list;
122 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg;
95 } 123 }
96 1;
97 }, 124 },
125 connect => sub {
126 my ($con, $error) = @_;
127
128 if ($error) {
129 cf::error "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n";
130 undef $CON;
131
132 } else {
133 cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n";
134 }
135 },
98# registered => sub { 136 registered => sub {
99# 1; 137 cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n";
100# }, 138 },
139 error => sub {
140 my ($con, $code, $message) = @_;
141 cf::error "IRC: IRC ERROR ($code) $message\n";
142 },
101 disconnect => sub { 143 disconnect => sub {
102 my ($con, $reason) = @_; 144 my ($con, $reason) = @_;
103 warn "CFBOT: disconnect: $reason\n"; 145 cf::warn "IRC: disconnect: $reason\n";
104 undef $CON; 146 undef $CON;
105 0;
106 } 147 }
107 ); 148 );
108} 149}
109 150
110Event->timer ( 151our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub {
111 reentrant => 0, 152 check_connection;
112 after => 1, 153};
113 interval => 30,
114 data => cf::WF_AUTOCANCEL,
115 cb => Coro::unblock_sub { check_connection },
116);
117 154

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines