ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/schmorp-irc.ext
Revision: 1.16
Committed: Fri Sep 29 19:21:25 2006 UTC (17 years, 7 months ago) by root
Branch: MAIN
Changes since 1.15: +4 -18 lines
Log Message:
replace who command in pelr, use the same function for both irc and in-game listing

File Contents

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