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.20 by root, Fri Feb 3 03:01:44 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
47 48
48 if (my $other = cf::player::find_active $target) { 49 if (my $other = cf::player::find_active $target) {
49 50
50 if ($tmsg) { 51 if ($tmsg) {
51 if ($me eq $target) { 52 if ($me eq $target) {
52 $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN); 53 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: You are talking to yourself, you freak!");
53 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { 54 } 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); 55 $CON->send_chan ($BOTCHAN, NOTICE => $BOTCHAN, "$me: $target ignores what you say. Give up on it.");
55 } else { 56 } else {
56 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg; 57 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg;
57 58
58 $other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF); 59 $other->ns->send_msg (ext::chat::tell_channel ("$name/$me"), "$name/$me tells you: $tmsg", cf::NDI_DK_ORANGE | cf::NDI_DEF);
59 } 60 }
66} 67}
67 68
68sub check_connection { 69sub check_connection {
69 return if $CON; 70 return if $CON;
70 71
71 $CON = Net::IRC3::Client::Connection->new; 72 $CON = AnyEvent::IRC::Client->new;
73 $CON->set_exception_cb (sub {
74 my ($exp, $ev) = @_;
75 cf::error "IRC: IRC EXCEPTION (event $ev): $exp\n";
76 });
72 $CON->connect ($BOTSERVER, $BOTPORT); 77 $CON->connect ($BOTSERVER, $BOTPORT, {
78 nick => $BOTNAME,
79 user => $BOTNAME,
80 real => 'deliantra server'
81 });
73 $CON->send_srv (JOIN => undef, $BOTCHAN); 82 $CON->send_srv (JOIN => undef, $BOTCHAN);
74 $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection');
75 $CON->reg_cb ( 83 $CON->reg_cb (
76 #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 },
77 irc_privmsg => sub { 84 irc_privmsg => sub {
78 my ($con, $msg) = @_; 85 my ($con, $msg) = @_;
79 my $name = 'irc'; 86 my $name = 'irc';
80 my $nick = Net::IRC3::Util::prefix_nick ($msg); 87 my $nick = AnyEvent::IRC::Util::prefix_nick ($msg);
81 my $NOW = Time::HiRes::time; 88 my $NOW = Time::HiRes::time;
82 89
83 my $tmsg = $msg->{trailing}; 90 my $tmsg = filter_colors ($msg->{params}->[-1]);
84 $tmsg =~ s/\x01[^\x01]*\x01//g; 91 $tmsg =~ s/\x01[^\x01]*\x01//g;
85 $tmsg =~ s/\015?\012/ /g; 92 $tmsg =~ s/\015?\012/ /g;
86 93
87 utf8::decode $tmsg; 94 utf8::decode $tmsg;
88 95
89 if ($tmsg =~ /^\!/) { 96 if ($tmsg =~ /^\!/) {
90 handle_fcmd ($name, $nick, $tmsg); 97 handle_fcmd ($name, $nick, $tmsg);
91 } elsif ($tmsg =~ m/\S/) { 98 } elsif ($tmsg =~ m/\S/) {
92 $_->ns->send_msg ($ext::chat::CHAT_CHANNEL, 99 $_->ns->send_msg ($cf::CHAT_CHANNEL,
93 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF 100 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF
94 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; 101 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list;
102 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg;
95 } 103 }
96 1;
97 }, 104 },
105 connect => sub {
106 my ($con, $error) = @_;
107
108 if ($error) {
109 cf::error "IRC: CONNECT ERROR to IRC server: $BOTSERVER:$BOTPORT: $error\n";
110 undef $CON;
111
112 } else {
113 cf::info "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n";
114 }
115 },
98# registered => sub { 116 registered => sub {
99# 1; 117 cf::info "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n";
100# }, 118 },
119 error => sub {
120 my ($con, $code, $message) = @_;
121 cf::error "IRC: IRC ERROR ($code) $message\n";
122 },
101 disconnect => sub { 123 disconnect => sub {
102 my ($con, $reason) = @_; 124 my ($con, $reason) = @_;
103 warn "CFBOT: disconnect: $reason\n"; 125 cf::warn "IRC: disconnect: $reason\n";
104 undef $CON; 126 undef $CON;
105 0;
106 } 127 }
107 ); 128 );
108} 129}
109 130
110Event->timer ( 131our $RECONNECT = length $BOTSERVER && cf::periodic 30, Coro::unblock_sub {
111 reentrant => 0, 132 check_connection;
112 after => 1, 133};
113 interval => 30,
114 data => cf::WF_AUTOCANCEL,
115 cb => Coro::unblock_sub { check_connection },
116);
117 134

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines