ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/schmorp-irc.ext
Revision: 1.6
Committed: Wed Apr 25 23:53:50 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
hopefully properly treat irc channelsas utf-8

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     use Time::HiRes;
4     use Net::IRC3::Client::Connection;
5    
6     # requires: commands.ext
7    
8     return unless exists $cf::CFG{irc_server};
9    
10     my $BOTSERVER = $cf::CFG{irc_server};
11     my $BOTPORT = $cf::CFG{irc_port};
12     my $BOTNAME = $cf::CFG{irc_nick};
13     my $BOTCHAN = $cf::CFG{irc_chan};
14    
15     my $CON; # the connection
16    
17     sub unload {
18     $CON->disconnect if $CON;
19     undef $CON;
20     }
21    
22     sub do_notice {
23     my ($msg) = @_;
24     $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN)
25     if $CON;
26     }
27    
28     sub users {
29     $CON
30     ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} }
31     : ()
32     }
33    
34     sub handle_fcmd {
35     my ($name, $me, $msg) = @_;
36    
37     if ($msg eq "!who") {
38     $CON->send_chan ($BOTCHAN, NOTICE => $_, $BOTCHAN)
39     for ext::commands::who_listing ();
40    
41     } elsif ($msg =~ /^\!tell/) {
42     my (undef, $target, $tmsg) = split / /, $msg, 3;
43    
44 root 1.2 if (my $other = cf::player::find_active $target) {
45 root 1.1
46     if ($tmsg) {
47     if ($me eq $target) {
48     $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN);
49     } elsif ($other->ob->{ext_ignore_tell}{$me} >= time) {
50     $CON->send_chan ($BOTCHAN, NOTICE => "$me: $target ignores what you say. Give up on it.", $BOTCHAN);
51     } else {
52     cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg;
53    
54     $other->ob->message ("$name/$me tells you: $tmsg");
55     $other->ob->{ext_last_tell} = "$name/$me";
56     }
57     } else {
58     $CON->send_chan ($BOTCHAN, NOTICE => "$me: What do you want to tell $target?", cf::NDI_UNIQUE);
59     }
60    
61     }
62     }
63     }
64    
65     sub check_connection {
66     return if $CON;
67    
68     $CON = Net::IRC3::Client::Connection->new;
69     $CON->connect ($BOTSERVER, $BOTPORT);
70     $CON->send_srv (JOIN => undef, $BOTCHAN);
71     $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection');
72     $CON->reg_cb (
73     #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 },
74     irc_privmsg => sub {
75     my ($con, $msg) = @_;
76     my $name = 'irc';
77     my $nick = Net::IRC3::Util::prefix_nick ($msg);
78     my $NOW = Time::HiRes::time;
79     my $tmsg = $msg->{trailing};
80     $tmsg =~ s/\x01[^\x01]*\x01//g;
81     $tmsg =~ s/\015?\012/ /g;
82 root 1.6 utf8::encode $tmsg; # ->message not yet utf8-ified
83 root 1.1 if ($tmsg =~ /^\!/) {
84     handle_fcmd ($name, $nick, $tmsg);
85     } elsif ($tmsg =~ m/\S/) {
86     $_->ob->message (
87     "$name/".$nick." chats: $tmsg", cf::NDI_BLUE
88     ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
89     }
90     1;
91     },
92     # registered => sub {
93     # 1;
94     # },
95     disconnect => sub {
96 elmex 1.5 my ($con, $reason) = @_;
97     warn "CFBOT: disconnect: $reason\n";
98 root 1.1 undef $CON;
99     0;
100     }
101     );
102     }
103    
104 root 1.3 Event->timer (
105     reentrant => 0,
106     after => 1,
107     interval => 30,
108     data => cf::WF_AUTOCANCEL,
109 root 1.4 cb => Coro::unblock_sub { check_connection },
110 root 1.3 );
111 root 1.1