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.5 by elmex, Sat Aug 25 11:51:04 2007 UTC vs.
Revision 1.14 by elmex, Tue Sep 23 14:41:26 2008 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;
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}; 11my $BOTSERVER = $cf::CFG{irc_server} || "localhost";
11my $BOTPORT = $cf::CFG{irc_port}; 12my $BOTPORT = $cf::CFG{irc_port} || 6667;
12my $BOTNAME = $cf::CFG{irc_nick}; 13my $BOTNAME = $cf::CFG{irc_nick} || "server";
13my $BOTCHAN = $cf::CFG{irc_chan}; 14my $BOTCHAN = $cf::CFG{irc_chan} || "cf";
14 15
15my $CON; # the connection 16my $CON; # the connection
16 17
17sub unload { 18sub unload {
18 $CON->disconnect if $CON; 19 $CON->disconnect if $CON;
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
35 36
36sub handle_fcmd { 37sub handle_fcmd {
37 my ($name, $me, $msg) = @_; 38 my ($name, $me, $msg) = @_;
38 39
39 if ($msg eq "!who") { 40 if ($msg eq "!who") {
41 # clobbers irc, http is available
42 do_notice "see http://www.deliantra.net/userlist.crossfire.schmorp.de.html";
40 do_notice $_ 43# do_notice $_
41 for ext::commands::who_listing (0, "."); 44# for ext::commands::who_listing (0, ".");
42 45
43 } elsif ($msg =~ /^\!tell/) { 46 } elsif ($msg =~ /^\!tell/) {
44 my (undef, $target, $tmsg) = split / /, $msg, 3; 47 my (undef, $target, $tmsg) = split / /, $msg, 3;
45 48
46 if (my $other = cf::player::find_active $target) { 49 if (my $other = cf::player::find_active $target) {
47 50
48 if ($tmsg) { 51 if ($tmsg) {
49 if ($me eq $target) { 52 if ($me eq $target) {
50 $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!");
51 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) { 54 } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) {
52 $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.");
53 } else { 56 } else {
54 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;
55 58
56 $other->ns->send_msg (cf::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);
57 } 60 }
58 } else { 61 } else {
59 do_notice "$me: What do you want to tell $target?"; 62 do_notice "$me: What do you want to tell $target?";
60 } 63 }
61 64
64} 67}
65 68
66sub check_connection { 69sub check_connection {
67 return if $CON; 70 return if $CON;
68 71
69 $CON = Net::IRC3::Client::Connection->new; 72 $CON = AnyEvent::IRC::Client->new;
70 $CON->connect ($BOTSERVER, $BOTPORT); 73 $CON->connect ($BOTSERVER, $BOTPORT, {
74 nick => $BOTNAME,
75 user => $BOTNAME,
76 real => 'deliantra server'
77 });
71 $CON->send_srv (JOIN => undef, $BOTCHAN); 78 $CON->send_srv (JOIN => undef, $BOTCHAN);
72 $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection');
73 $CON->reg_cb ( 79 $CON->reg_cb (
74 #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 },
75 irc_privmsg => sub { 80 irc_privmsg => sub {
76 my ($con, $msg) = @_; 81 my ($con, $msg) = @_;
77 my $name = 'irc'; 82 my $name = 'irc';
78 my $nick = Net::IRC3::Util::prefix_nick ($msg); 83 my $nick = AnyEvent::IRC::Util::prefix_nick ($msg);
79 my $NOW = Time::HiRes::time; 84 my $NOW = Time::HiRes::time;
80 85
81 my $tmsg = $msg->{trailing}; 86 my $tmsg = $msg->{params}->[-1];
82 $tmsg =~ s/\x01[^\x01]*\x01//g; 87 $tmsg =~ s/\x01[^\x01]*\x01//g;
83 $tmsg =~ s/\015?\012/ /g; 88 $tmsg =~ s/\015?\012/ /g;
84 89
85 utf8::decode $tmsg; 90 utf8::decode $tmsg;
86 91
87 if ($tmsg =~ /^\!/) { 92 if ($tmsg =~ /^\!/) {
88 handle_fcmd ($name, $nick, $tmsg); 93 handle_fcmd ($name, $nick, $tmsg);
89 } elsif ($tmsg =~ m/\S/) { 94 } elsif ($tmsg =~ m/\S/) {
90 $_->ns->send_msg ($ext::chat::CHAT_CHANNEL, 95 $_->ns->send_msg ($cf::CHAT_CHANNEL,
91 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF 96 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE | cf::NDI_DEF
92 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list; 97 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW } cf::player::list;
98 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", "$name/$nick", $tmsg;
93 } 99 }
94 1;
95 }, 100 },
101 connect => sub {
102 warn "IRC: connected to IRC server: $BOTSERVER:$BOTPORT\n";
103 },
96# registered => sub { 104 registered => sub {
97# 1; 105 warn "IRC: successfully logged into IRC server: $BOTSERVER:$BOTPORT\n";
98# }, 106 },
107 error => sub {
108 my ($con, $code, $message) = @_;
109 warn "IRC: IRC ERROR ($code) $message\n";
110 },
99 disconnect => sub { 111 disconnect => sub {
100 my ($con, $reason) = @_; 112 my ($con, $reason) = @_;
101 warn "CFBOT: disconnect: $reason\n"; 113 warn "IRC: disconnect: $reason\n";
102 undef $CON; 114 undef $CON;
103 0;
104 } 115 }
105 ); 116 );
106} 117}
107 118
108Event->timer ( 119our $RECONNECT = cf::periodic 30, Coro::unblock_sub {
109 reentrant => 0, 120 check_connection;
110 after => 1, 121};
111 interval => 30,
112 data => cf::WF_AUTOCANCEL,
113 cb => Coro::unblock_sub { check_connection },
114);
115 122

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines