ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/irc.ext
Revision: 1.2
Committed: Fri May 18 21:24:31 2007 UTC (17 years ago) by root
Branch: MAIN
CVS Tags: rel-2_1
Changes since 1.1: +4 -3 lines
Log Message:
likely fix irc double utf-8 encoding

File Contents

# Content
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
25 utf8::encode $msg;
26 $CON->send_chan ($BOTCHAN, NOTICE => $msg, $BOTCHAN)
27 if $CON;
28 }
29
30 sub users {
31 $CON
32 ? grep $_ ne $CON->nick, keys %{ $CON->channel_list->{$BOTCHAN} || {} }
33 : ()
34 }
35
36 sub handle_fcmd {
37 my ($name, $me, $msg) = @_;
38
39 if ($msg eq "!who") {
40 do_notice $_
41 for ext::commands::who_listing ();
42
43 } elsif ($msg =~ /^\!tell/) {
44 my (undef, $target, $tmsg) = split / /, $msg, 3;
45
46 if (my $other = cf::player::find_active $target) {
47
48 if ($tmsg) {
49 if ($me eq $target) {
50 $CON->send_chan ($BOTCHAN, NOTICE => "$me: You are talking to yourself, you freak!", $BOTCHAN);
51 } 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);
53 } else {
54 cf::LOG cf::llevDebug, sprintf "TELL [%s/%s>%s] %s\n", $name, $me, $target, $tmsg;
55
56 $other->ob->message ("$name/$me tells you: $tmsg");
57 $other->ob->{ext_last_tell} = "$name/$me";
58 }
59 } else {
60 do_notice "$me: What do you want to tell $target?";
61 }
62
63 }
64 }
65 }
66
67 sub check_connection {
68 return if $CON;
69
70 $CON = Net::IRC3::Client::Connection->new;
71 $CON->connect ($BOTSERVER, $BOTPORT);
72 $CON->send_srv (JOIN => undef, $BOTCHAN);
73 $CON->register ($BOTNAME, $BOTNAME, 'crossfire connection');
74 $CON->reg_cb (
75 #d# 'irc_*' => sub { warn "IRC $_[1]->{trailing}\n"; 1 },
76 irc_privmsg => sub {
77 my ($con, $msg) = @_;
78 my $name = 'irc';
79 my $nick = Net::IRC3::Util::prefix_nick ($msg);
80 my $NOW = Time::HiRes::time;
81
82 my $tmsg = $msg->{trailing};
83 $tmsg =~ s/\x01[^\x01]*\x01//g;
84 $tmsg =~ s/\015?\012/ /g;
85
86 utf8::decode $tmsg;
87
88 if ($tmsg =~ /^\!/) {
89 handle_fcmd ($name, $nick, $tmsg);
90 } elsif ($tmsg =~ m/\S/) {
91 $_->ob->message (
92 "$name/".$nick." chats: $tmsg", cf::NDI_BLUE
93 ) for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
94 }
95 1;
96 },
97 # registered => sub {
98 # 1;
99 # },
100 disconnect => sub {
101 my ($con, $reason) = @_;
102 warn "CFBOT: disconnect: $reason\n";
103 undef $CON;
104 0;
105 }
106 );
107 }
108
109 Event->timer (
110 reentrant => 0,
111 after => 1,
112 interval => 30,
113 data => cf::WF_AUTOCANCEL,
114 cb => Coro::unblock_sub { check_connection },
115 );
116