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.14 by elmex, Tue Sep 23 14:41:26 2008 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 AnyEvent::IRC::Client; 4use AnyEvent::IRC::Client;
5use AnyEvent::IRC::Util; 5use AnyEvent::IRC::Util qw/filter_colors/;
6 6
7# requires: commands.ext 7# requires: commands.ext
8 8
9return unless exists $cf::CFG{irc_server}; 9return unless exists $cf::CFG{irc_server};
10 10
11my $BOTSERVER = $cf::CFG{irc_server} || "localhost"; 11CONF BOTSERVER : irc_server = undef;
12my $BOTPORT = $cf::CFG{irc_port} || 6667; 12CONF BOTPORT : irc_port = undef;
13my $BOTNAME = $cf::CFG{irc_nick} || "server"; 13CONF BOTNAME : irc_nick = undef;
14my $BOTCHAN = $cf::CFG{irc_chan} || "cf"; 14CONF BOTCHAN : irc_chan = undef;
15 15
16my $CON; # the connection 16our $CON; # the connection
17 17
18sub unload { 18sub unload {
19 $CON->disconnect if $CON; 19 $CON->disconnect if $CON;
20 undef $CON; 20 undef $CON;
21} 21}
29} 29}
30 30
31sub users { 31sub users {
32 $CON 32 $CON
33 ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} } 33 ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} }
34 : () 34 : ()
35} 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};
36 56
37sub handle_fcmd { 57sub handle_fcmd {
38 my ($name, $me, $msg) = @_; 58 my ($name, $me, $msg) = @_;
39 59
40 if ($msg eq "!who") { 60 if ($msg eq "!who") {
68 88
69sub check_connection { 89sub check_connection {
70 return if $CON; 90 return if $CON;
71 91
72 $CON = AnyEvent::IRC::Client->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 });
73 $CON->connect ($BOTSERVER, $BOTPORT, { 97 $CON->connect ($BOTSERVER, $BOTPORT, {
74 nick => $BOTNAME, 98 nick => $BOTNAME,
75 user => $BOTNAME, 99 user => $BOTNAME,
76 real => 'deliantra server' 100 real => 'deliantra server'
77 }); 101 });
81 my ($con, $msg) = @_; 105 my ($con, $msg) = @_;
82 my $name = 'irc'; 106 my $name = 'irc';
83 my $nick = AnyEvent::IRC::Util::prefix_nick ($msg); 107 my $nick = AnyEvent::IRC::Util::prefix_nick ($msg);
84 my $NOW = Time::HiRes::time; 108 my $NOW = Time::HiRes::time;
85 109
86 my $tmsg = $msg->{params}->[-1]; 110 my $tmsg = filter_colors ($msg->{params}->[-1]);
87 $tmsg =~ s/\x01[^\x01]*\x01//g; 111 $tmsg =~ s/\x01[^\x01]*\x01//g;
88 $tmsg =~ s/\015?\012/ /g; 112 $tmsg =~ s/\015?\012/ /g;
89 113
90 utf8::decode $tmsg; 114 utf8::decode $tmsg;
91 115
97 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list; 121 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list;
98 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg; 122 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg;
99 } 123 }
100 }, 124 },
101 connect => sub { 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 {
102 warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n"; 133 cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n";
134 }
103 }, 135 },
104 registered => sub { 136 registered => sub {
105 warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n"; 137 cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n";
106 }, 138 },
107 error => sub { 139 error => sub {
108 my ($con, $code, $message) = @_; 140 my ($con, $code, $message) = @_;
109 warn "IRC: IRC ERROR ($code) $message\n"; 141 cf::error "IRC: IRC ERROR ($code) $message\n";
110 }, 142 },
111 disconnect => sub { 143 disconnect => sub {
112 my ($con, $reason) = @_; 144 my ($con, $reason) = @_;
113 warn "IRC: disconnect: $reason\n"; 145 cf::warn "IRC: disconnect: $reason\n";
114 undef $CON; 146 undef $CON;
115 } 147 }
116 ); 148 );
117} 149}
118 150
119our $RECONNECT = cf::periodic 30, Coro::unblock_sub { 151our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub {
120 check_connection; 152 check_connection;
121}; 153};
122 154

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines