]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
added '.mode' for Netsnipe
[infobot.git] / src / Modules / UserDCC.pl
1 #
2 #  UserDCC.pl: User Commands, DCC CHAT.
3 #      Author: dms
4 #     Version: v0.1 (20000707)
5 #     Created: 20000707 (from UserExtra.pl)
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9
10 sub userDCC {
11     # hrm...
12     $message =~ s/\s+$//;
13
14     ### for all users.
15     # quit.
16     if ($message =~ /^(exit|quit)$/i) {
17         # do ircII clients support remote close? if so, cool!
18         &status("userDCC: quit called. FIXME");
19 ###     $irc->removeconn($dcc{'CHAT'}{lc $who});
20
21         return $noreply;
22     }
23
24     # who.
25     if ($message =~ s/^who//i) {
26         my $count = scalar(keys %{$dcc{'CHAT'}});
27         my $dccCHAT = $message;
28
29         &performStrictReply("Start of who ($count users).");
30         foreach (keys %{$dcc{'CHAT'}}) {
31             &performStrictReply("=> $_");
32         }
33         &performStrictReply("End of who.");
34
35         if ($message) { foreach (`$dccCHAT`) {
36             &performStrictReply("OUT: $_"); }
37         }
38
39         return $noreply;
40     }
41
42     ### for those users with enough flags.
43
44     # 4op.
45     if ($message =~ /^4op(\s+($mask{chan}))?$/i) {
46         return $noreply unless (&hasFlag("o"));
47
48         my $chan = $2;
49
50         if ($chan eq "") {
51             &help("4op");
52             return $noreply;
53         }
54
55         if (!$channels{$chan}{'o'}{$ident}) {
56             &msg($who, "i don't have ops on $chan to do that.");
57             return $noreply;
58         }
59
60         # on non-4mode(<4) servers, this may be exploited.
61         if ($channels{$chan}{'o'}{$who}) {
62             rawout("MODE $chan -o+o-o+o". (" $who" x 4));
63         } else {
64             rawout("MODE $chan +o-o+o-o". (" $who" x 4));
65         }
66
67         return $noreply;
68     }
69
70     # backlog.
71     if ($message =~ /^backlog(\s+(.*))?$/i) {
72         return $noreply unless (&hasFlag("o"));
73         return $noreply unless (&hasParam("backlog"));
74         my $num = $2;
75         my $max = $param{'backlog'};
76
77         if (!defined $num) {
78             &help("backlog");
79             return $noreply;
80         } elsif ($num !~ /^\d+/) {
81             &msg($who, "error: argument is not positive integer.");
82             return $noreply;
83         } elsif ($num > $max or $num < 0) {
84             &msg($who, "error: argument is out of range (max $max).");
85             return $noreply;
86         }
87
88         &msg($who, "Start of backlog...");
89         for (0..$num-1) {
90             sleep 1 if ($_ % 4 == 0 and $_ != 0);
91             $conn->privmsg($who, "[".($_+1)."]: $backlog[$max-$num+$_]");
92         }
93         &msg($who, "End of backlog.");
94
95         return $noreply;
96     }
97
98     # dump variables.
99     if ($message =~ /^dumpvars$/i) {
100         return $noreply unless (&hasFlag("o"));
101         return '' unless (&IsParam("dumpvars"));
102
103         &status("Dumping all variables...");
104         &dumpallvars();
105
106         return $noreply;
107     }
108
109     # kick.
110     if ($message =~ /^kick(\s+(\S+)(\s+(\S+))?)?/) {
111         return $noreply unless (&hasFlag("o"));
112         my ($nick,$chan) = (lc $2,lc $4);
113
114         if ($nick eq "") {
115             &help("kick");
116             return $noreply;
117         }
118
119         if (&validChan($chan) == 0) {
120             &msg($who,"error: invalid channel \002$chan\002");
121             return $noreply;
122         }
123
124         if (&IsNickInChan($nick,$chan) == 0) {
125             &msg($who,"$nick is not in $chan.");
126             return $noreply;
127         }
128
129         &kick($nick,$chan);
130
131         return $noreply;
132     }
133
134     # kick.
135     if ($message =~ /^mode(\s+(.*))?$/) {
136         return $noreply unless (&hasFlag("m"));
137         my ($chan,$mode) = split /\s+/,$2,2;
138
139         if ($chan eq "") {
140             &help("mode");
141             return $noreply;
142         }
143
144         if (&validChan($chan) == 0) {
145             &msg($who,"error: invalid channel \002$chan\002");
146             return $noreply;
147         }
148
149         if (!$channels{$chan}{o}{$ident}) {
150             &msg($who,"error: don't have ops on \002$chan\002");
151             return $noreply;
152         }
153
154         &mode($chan, $mode);
155
156         return $noreply;
157     }
158
159     # part.
160     if ($message =~ /^part(\s+(\S+))?$/i) {
161         return $noreply unless (&hasFlag("o"));
162         my $jchan = $2;
163
164         if ($jchan !~ /^$mask{chan}$/) {
165             &msg($who, "error, invalid chan.");
166             &help("part");
167             return $noreply;
168         }
169
170         if (!&validChan($jchan)) {
171             &msg($who, "error, I'm not on that chan.");
172             return $noreply;
173         }
174
175         &msg($jchan, "Leaving. (courtesy of $who).");
176         &part($jchan);
177         return $noreply;
178     }
179
180     # ignore.
181     if ($message =~ /^ignore(\s+(\S+))?$/i) {
182         return $noreply unless (&hasFlag("o"));
183         my $what = lc $2;
184
185         if ($what eq "") {
186             &help("ignore");
187             return $noreply;
188         }
189
190         my $expire = $param{'ignoreTempExpire'} || 60;
191         $ignoreList{$what} = time() + ($expire * 60);
192         &status("ignoring $what at $who's request");
193         &msg($who, "added $what to the ignore list");
194
195         return $noreply;
196     }
197
198     # unignore.
199     if ($message =~ /^unignore(\s+(\S+))?$/i) {
200         return $noreply unless (&hasFlag("o"));
201         my $what = $2;
202
203         if ($what eq "") {
204             &help("unignore");
205             return $noreply;
206         }
207
208         if ($ignoreList{$what}) {
209             &status("unignoring $what at $userHandle's request");
210             delete $ignoreList{$what};
211             &msg($who, "removed $what from the ignore list");
212         } else {
213             &status("unignore FAILED for $1 at $who's request");
214             &msg($who, "no entry for $1 on the ignore list");
215         }
216         return $noreply;
217     }
218
219     # clear unignore list.
220     if ($message =~ /^clear ignorelist$/i) {
221         return $noreply unless (&hasFlag("o"));
222         undef %ignoreList;
223         &status("unignoring all ($who said the word)");
224
225         return $noreply;
226     }
227
228     # lobotomy. sometimes we want the bot to be _QUIET_.
229     if ($message =~ /^(lobotomy|bequiet)$/i) {
230         return $noreply unless (&hasFlag("o"));
231
232         if ($lobotomized) {
233             &performReply("i'm already lobotomized");
234         } else {
235             &performReply("i have been lobotomized");
236             $lobotomized = 1;
237         }
238
239         return $noreply;
240     }
241
242     # unlobotomy.
243     if ($message =~ /^(unlobotomy|benoisy)$/i) {
244         return $noreply unless (&hasFlag("o"));
245         if ($lobotomized) {
246             &performReply("i have been unlobotomized, woohoo");
247             $lobotomized = 0;
248         } else {
249             &performReply("i'm not lobotomized");
250         }
251         return $noreply;
252     }
253
254     # op.
255     if ($message =~ /^op(\s+(.*))?$/i) {
256         return $noreply unless (&hasFlag("o"));
257         my ($opee) = lc $2;
258         my @chans;
259
260         if ($opee =~ / /) {
261             if ($opee =~ /^(\S+)\s+(\S+)$/) {
262                 $opee  = $1;
263                 @chans = ($2);
264                 if (!&validChan($2)) {
265                     &msg($who,"error: invalid chan ($2).");
266                     return $noreply;
267                 }
268             } else {
269                 &msg($who,"error: invalid params.");
270                 return $noreply;
271             }
272         } else {
273             @chans = keys %channels;
274         }
275
276         my $found = 0;
277         my $op = 0;
278         foreach (@chans) {
279             next unless (&IsNickInChan($opee,$_));
280             $found++;
281             if ($channels{$_}{'o'}{$opee}) {
282                 &status("op: $opee already has ops on $_");
283                 next;
284             }
285             $op++;
286
287             &status("opping $opee on $_ at ${who}'s request");
288             &performStrictReply("opping $opee on $_");
289             &op($_, $opee);
290         }
291
292         if ($found != $op) {
293             &status("op: opped on all possible channels.");
294         } else {
295             &DEBUG("found => '$found'.");
296             &DEBUG("op => '$op'.");
297         }
298
299         return $noreply;
300     }
301
302     # deop.
303     if ($message =~ /^deop(\s+(.*))?$/i) {
304         return $noreply unless (&hasFlag("o"));
305         my ($opee) = lc $2;
306         my @chans;
307
308         if ($opee =~ / /) {
309             if ($opee =~ /^(\S+)\s+(\S+)$/) {
310                 $opee  = $1;
311                 @chans = ($2);
312                 if (!&validChan($2)) {
313                     &msg($who,"error: invalid chan ($2).");
314                     return $noreply;
315                 }
316             } else {
317                 &msg($who,"error: invalid params.");
318                 return $noreply;
319             }
320         } else {
321             @chans = keys %channels;
322         }
323
324         my $found = 0;
325         my $op = 0;
326         foreach (@chans) {
327             next unless (&IsNickInChan($opee,$_));
328             $found++;
329             if (!exists $channels{$_}{'o'}{$opee}) {
330                 &status("deop: $opee already has no ops on $_");
331                 next;
332             }
333             $op++;
334
335             &status("deopping $opee on $_ at ${who}'s request");
336             &deop($_, $opee);
337         }
338
339         if ($found != $op) {
340             &status("deop: deopped on all possible channels.");
341         } else {
342             &DEBUG("deop: found => '$found'.");
343             &DEBUG("deop: op => '$op'.");
344         }
345
346         return $noreply;
347     }
348
349     # say.
350     if ($message =~ s/^say\s+(\S+)\s+(.*)//) {
351         return $noreply unless (&hasFlag("o"));
352         my ($chan,$msg) = (lc $1, $2);
353         &DEBUG("chan => '$1', msg => '$msg'.");
354
355         if (&validChan($chan)) {
356             &msg($chan, $2);
357         } else {
358             &msg($who,"i'm not on \002$1\002, sorry.");
359         }
360         return $noreply;
361     }
362
363     # die.
364     if ($message =~ /^die$/) {
365         return $noreply unless (&hasFlag("n"));
366
367         &doExit();
368
369         &status("Dying by $who\'s request");
370         exit 0;
371     }
372
373     # global factoid substitution.
374     if ($message =~ m|^s([/,#])(.+?)\1(.*?)\1;?\s*$|) {
375         my ($delim,$op,$np) = ($1, $2, $3);
376         return $noreply unless (&hasFlag("n"));
377         ### TODO: support flags to do full-on global.
378
379         # incorrect format.
380         if ($np =~ /$delim/) {
381             &performReply("looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
382             return $noreply;
383         }
384
385         ### TODO: fix up $op to support mysql/pgsql/dbm(perl)
386         ### TODO: => add db/sql specific function to fix this.
387         my @list = &searchTable("factoids", "factoid_key",
388                         "factoid_value", $op);
389
390         if (!scalar @list) {
391             &performReply("Expression didn't match anything.");
392             return $noreply;
393         }
394
395         if (scalar @list > 100) {
396             &performReply("regex found more than 100 matches... not doing.");
397             return $noreply;
398         }
399
400         &status("gsubst: going to alter ".scalar(@list)." factoids.");
401         &performReply("going to alter ".scalar(@list)." factoids.");
402
403         my $error = 0;
404         foreach (@list) {
405             my $faqtoid = $_;
406
407             next if (&IsLocked($faqtoid) == 1);
408             my $result = &getFactoid($faqtoid);
409             my $was = $result;
410             &DEBUG("was($faqtoid) => '$was'.");
411
412             # global global
413             # we could support global local (once off).
414             if ($result =~ s/\Q$op/$np/gi) {
415                 if (length $result > $param{'maxDataSize'}) {
416                     &performReply("that's too long (or was long)");
417                     return $noreply;
418                 }
419                 &setFactInfo($faqtoid, "factoid_value", $result);
420                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
421             } else {
422                 &WARN("subst: that's weird... thought we found the string ($op) in '$faqtoid'.");
423                 $error++;
424             }
425         }
426
427         if ($error) {
428             &ERROR("Some warnings/errors?");
429         }
430
431         &performReply("Ok... did s/$op/$np/ for ".
432                                 (scalar(@list) - $error)." factoids");
433
434         return $noreply;
435     }
436
437     # jump.
438     if ($message =~ /^jump(\s+(\S+))?$/i) {
439         return $noreply unless (&hasFlag("n"));
440
441         if ($2 eq "") {
442             &help("jump");
443             return $noreply;
444         }
445
446         my ($server,$port);
447         if ($2 =~ /^(\S+)(:(\d+))?$/) {
448             $server = $1;
449             $port   = $3 || 6667;
450         } else {
451             &msg($who,"invalid format.");
452             return $noreply;
453         }
454
455         &status("jumping servers... $server...");
456         &rawout("QUIT :jumping to $server");
457
458         if (&irc($server,$port) == 0) {
459             &ircloop();
460         }
461     }
462
463     # reload.
464     if ($message =~ /^reload$/i) {
465         return $noreply unless (&hasFlag("n"));
466
467         &status("USER reload $who");
468         &msg($who,"reloading...");
469         &reloadAllModules();
470         &msg($who,"reloaded.");
471
472         return $noreply;
473     }
474
475     # rehash.
476     if ($message =~ /^rehash$/) {
477         return $noreply unless (&hasFlag("n"));
478
479         &msg($who,"rehashing...");
480         &restart("REHASH");
481         &status("USER rehash $who");
482         &msg($who,"rehashed");
483
484         return $noreply;
485     }
486
487     # set.
488     if ($message =~ /^set(\s+(\S+)?(\s+(.*))?)?$/i) {
489         return $noreply unless (&hasFlag("n"));
490         my ($param,$what) = ($2,$4);
491
492         if ($param eq "" and $what eq "") {
493             &msg($who,"\002Usage\002: set <param> [what]");
494             return $noreply;
495         }
496
497         if (!exists $param{$param}) {
498             &msg($who,"error: param{$param} cannot be set");
499             return $noreply;
500         }
501
502         if ($what eq "") {
503             if ($param{$param} eq "") {
504                 &msg($who,"param{$param} has \002no value\002.");
505             } else {
506                 &msg($who,"param{$param} has value of '\002$param{$param}\002'.");
507             }
508             return $noreply;
509         }
510
511         if ($param{$param} eq $what) {
512             &msg($who,"param{$param} already has value of '\002$what\002'.");
513             return $noreply;
514         }
515
516         $param{$param} = $what;
517         &msg($who,"setting param{$param} to '\002$what\002'.");
518
519         return $noreply;
520     }
521
522     # unset.
523     if ($message =~ /^unset(\s+(\S+))?$/i) {
524         return $noreply unless (&hasFlag("n"));
525         my ($param) = $2;
526
527         if ($param eq "") {
528             &msg($who,"\002Usage\002: unset <param>");
529             return $noreply;
530         }
531
532         if (!exists $param{$param}) {
533             &msg($who,"error: \002$param\002 cannot be unset");
534             return $noreply;
535         }
536
537         if ($param{$param} == 0) {
538             &msg($who,"\002param{$param}\002 has already been unset.");
539             return $noreply;
540         }
541
542         $param{$param} = 0;
543         &msg($who,"unsetting \002param{$param}\002.");
544
545         return $noreply;
546     }
547
548     # more...
549 }
550
551 1;