]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/UserDCC.pl
Minor cleanup
[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     # part.
135     if ($message =~ /^part(\s+(\S+))?$/i) {
136         return $noreply unless (&hasFlag("o"));
137         my $jchan = $2;
138
139         if ($jchan !~ /^$mask{chan}$/) {
140             &msg($who, "error, invalid chan.");
141             &help("part");
142             return $noreply;
143         }
144
145         if (!&validChan($jchan)) {
146             &msg($who, "error, I'm not on that chan.");
147             return $noreply;
148         }
149
150         &msg($jchan, "Leaving. (courtesy of $who).");
151         &part($jchan);
152         return $noreply;
153     }
154
155     # ignore.
156     if ($message =~ /^ignore(\s+(\S+))?$/i) {
157         return $noreply unless (&hasFlag("o"));
158         my $what = lc $2;
159
160         if ($what eq "") {
161             &help("ignore");
162             return $noreply;
163         }
164
165         my $expire = $param{'ignoreTempExpire'} || 60;
166         $ignoreList{$what} = time() + ($expire * 60);
167         &status("ignoring $what at $who's request");
168         &msg($who, "added $what to the ignore list");
169
170         return $noreply;
171     }
172
173     # unignore.
174     if ($message =~ /^unignore(\s+(\S+))?$/i) {
175         return $noreply unless (&hasFlag("o"));
176         my $what = $2;
177
178         if ($what eq "") {
179             &help("unignore");
180             return $noreply;
181         }
182
183         if ($ignoreList{$what}) {
184             &status("unignoring $what at $userHandle's request");
185             delete $ignoreList{$what};
186             &msg($who, "removed $what from the ignore list");
187         } else {
188             &status("unignore FAILED for $1 at $who's request");
189             &msg($who, "no entry for $1 on the ignore list");
190         }
191         return $noreply;
192     }
193
194     # clear unignore list.
195     if ($message =~ /^clear ignorelist$/i) {
196         return $noreply unless (&hasFlag("o"));
197         undef %ignoreList;
198         &status("unignoring all ($who said the word)");
199
200         return $noreply;
201     }
202
203     # lobotomy. sometimes we want the bot to be _QUIET_.
204     if ($message =~ /^(lobotomy|bequiet)$/i) {
205         return $noreply unless (&hasFlag("o"));
206
207         if ($lobotomized) {
208             &performReply("i'm already lobotomized");
209         } else {
210             &performReply("i have been lobotomized");
211             $lobotomized = 1;
212         }
213
214         return $noreply;
215     }
216
217     # unlobotomy.
218     if ($message =~ /^(unlobotomy|benoisy)$/i) {
219         return $noreply unless (&hasFlag("o"));
220         if ($lobotomized) {
221             &performReply("i have been unlobotomized, woohoo");
222             $lobotomized = 0;
223         } else {
224             &performReply("i'm not lobotomized");
225         }
226         return $noreply;
227     }
228
229     # op.
230     if ($message =~ /^op(\s+(.*))?$/i) {
231         return $noreply unless (&hasFlag("o"));
232         my ($opee) = lc $2;
233         my @chans;
234
235         if ($opee =~ / /) {
236             if ($opee =~ /^(\S+)\s+(\S+)$/) {
237                 $opee  = $1;
238                 @chans = ($2);
239                 if (!&validChan($2)) {
240                     &msg($who,"error: invalid chan ($2).");
241                     return $noreply;
242                 }
243             } else {
244                 &msg($who,"error: invalid params.");
245                 return $noreply;
246             }
247         } else {
248             @chans = keys %channels;
249         }
250
251         my $found = 0;
252         my $op = 0;
253         foreach (@chans) {
254             next unless (&IsNickInChan($opee,$_));
255             $found++;
256             if ($channels{$_}{'o'}{$opee}) {
257                 &status("op: $opee already has ops on $_");
258                 next;
259             }
260             $op++;
261
262             &status("opping $opee on $_ at ${who}'s request");
263             &performStrictReply("opping $opee on $_");
264             &op($_, $opee);
265         }
266
267         if ($found != $op) {
268             &status("op: opped on all possible channels.");
269         } else {
270             &DEBUG("found => '$found'.");
271             &DEBUG("op => '$op'.");
272         }
273
274         return $noreply;
275     }
276
277     # deop.
278     if ($message =~ /^deop(\s+(.*))?$/i) {
279         return $noreply unless (&hasFlag("o"));
280         my ($opee) = lc $2;
281         my @chans;
282
283         if ($opee =~ / /) {
284             if ($opee =~ /^(\S+)\s+(\S+)$/) {
285                 $opee  = $1;
286                 @chans = ($2);
287                 if (!&validChan($2)) {
288                     &msg($who,"error: invalid chan ($2).");
289                     return $noreply;
290                 }
291             } else {
292                 &msg($who,"error: invalid params.");
293                 return $noreply;
294             }
295         } else {
296             @chans = keys %channels;
297         }
298
299         my $found = 0;
300         my $op = 0;
301         foreach (@chans) {
302             next unless (&IsNickInChan($opee,$_));
303             $found++;
304             if (!exists $channels{$_}{'o'}{$opee}) {
305                 &status("deop: $opee already has no ops on $_");
306                 next;
307             }
308             $op++;
309
310             &status("deopping $opee on $_ at ${who}'s request");
311             &deop($_, $opee);
312         }
313
314         if ($found != $op) {
315             &status("deop: deopped on all possible channels.");
316         } else {
317             &DEBUG("deop: found => '$found'.");
318             &DEBUG("deop: op => '$op'.");
319         }
320
321         return $noreply;
322     }
323
324     # say.
325     if ($message =~ s/^say\s+(\S+)\s+(.*)//) {
326         return $noreply unless (&hasFlag("o"));
327         my ($chan,$msg) = (lc $1, $2);
328         &DEBUG("chan => '$1', msg => '$msg'.");
329
330         if (&validChan($chan)) {
331             &msg($chan, $2);
332         } else {
333             &msg($who,"i'm not on \002$1\002, sorry.");
334         }
335         return $noreply;
336     }
337
338     # die.
339     if ($message =~ /^die$/) {
340         return $noreply unless (&hasFlag("n"));
341
342         &doExit();
343
344         &status("Dying by $who\'s request");
345         exit 0;
346     }
347
348     # global factoid substitution.
349     if ($message =~ m|^s([/,#])(.+?)\1(.*?)\1;?\s*$|) {
350         my ($delim,$op,$np) = ($1, $2, $3);
351         return $noreply unless (&hasFlag("n"));
352         ### TODO: support flags to do full-on global.
353
354         # incorrect format.
355         if ($np =~ /$delim/) {
356             &performReply("looks like you used the delimiter too many times. You may want to use a different delimiter, like ':' or '#'.");
357             return $noreply;
358         }
359
360         ### TODO: fix up $op to support mysql/pgsql/dbm(perl)
361         ### TODO: => add db/sql specific function to fix this.
362         my @list = &searchTable("factoids", "factoid_key",
363                         "factoid_value", $op);
364
365         if (!scalar @list) {
366             &performReply("Expression didn't match anything.");
367             return $noreply;
368         }
369
370         if (scalar @list > 100) {
371             &performReply("regex found more than 100 matches... not doing.");
372             return $noreply;
373         }
374
375         &status("gsubst: going to alter ".scalar(@list)." factoids.");
376         &performReply("going to alter ".scalar(@list)." factoids.");
377
378         my $error = 0;
379         foreach (@list) {
380             my $faqtoid = $_;
381
382             next if (&IsLocked($faqtoid) == 1);
383             my $result = &getFactoid($faqtoid);
384             my $was = $result;
385             &DEBUG("was($faqtoid) => '$was'.");
386
387             # global global
388             # we could support global local (once off).
389             if ($result =~ s/\Q$op/$np/gi) {
390                 if (length $result > $param{'maxDataSize'}) {
391                     &performReply("that's too long (or was long)");
392                     return $noreply;
393                 }
394                 &setFactInfo($faqtoid, "factoid_value", $result);
395                 &status("update: '$faqtoid' =is=> '$result'; was '$was'");
396             } else {
397                 &WARN("subst: that's weird... thought we found the string ($op) in '$faqtoid'.");
398                 $error++;
399             }
400         }
401
402         if ($error) {
403             &ERROR("Some warnings/errors?");
404         }
405
406         &performReply("Ok... did s/$op/$np/ for ".
407                                 (scalar(@list) - $error)." factoids");
408
409         return $noreply;
410     }
411
412     # jump.
413     if ($message =~ /^jump(\s+(\S+))?$/i) {
414         return $noreply unless (&hasFlag("n"));
415
416         if ($2 eq "") {
417             &help("jump");
418             return $noreply;
419         }
420
421         my ($server,$port);
422         if ($2 =~ /^(\S+)(:(\d+))?$/) {
423             $server = $1;
424             $port   = $3 || 6667;
425         } else {
426             &msg($who,"invalid format.");
427             return $noreply;
428         }
429
430         &status("jumping servers... $server...");
431         &rawout("QUIT :jumping to $server");
432
433         if (&irc($server,$port) == 0) {
434             &ircloop();
435         }
436     }
437
438     # reload.
439     if ($message =~ /^reload$/i) {
440         return $noreply unless (&hasFlag("n"));
441
442         &status("USER reload $who");
443         &msg($who,"reloading...");
444         &reloadAllModules();
445         &msg($who,"reloaded.");
446
447         return $noreply;
448     }
449
450     # rehash.
451     if ($message =~ /^rehash$/) {
452         return $noreply unless (&hasFlag("n"));
453
454         &msg($who,"rehashing...");
455         &restart("REHASH");
456         &status("USER rehash $who");
457         &msg($who,"rehashed");
458
459         return $noreply;
460     }
461
462     # set.
463     if ($message =~ /^set(\s+(\S+)?(\s+(.*))?)?$/i) {
464         return $noreply unless (&hasFlag("n"));
465         my ($param,$what) = ($2,$4);
466
467         if ($param eq "" and $what eq "") {
468             &msg($who,"\002Usage\002: set <param> [what]");
469             return $noreply;
470         }
471
472         if (!exists $param{$param}) {
473             &msg($who,"error: param{$param} cannot be set");
474             return $noreply;
475         }
476
477         if ($what eq "") {
478             if ($param{$param} eq "") {
479                 &msg($who,"param{$param} has \002no value\002.");
480             } else {
481                 &msg($who,"param{$param} has value of '\002$param{$param}\002'.");
482             }
483             return $noreply;
484         }
485
486         if ($param{$param} eq $what) {
487             &msg($who,"param{$param} already has value of '\002$what\002'.");
488             return $noreply;
489         }
490
491         $param{$param} = $what;
492         &msg($who,"setting param{$param} to '\002$what\002'.");
493
494         return $noreply;
495     }
496
497     # unset.
498     if ($message =~ /^unset(\s+(\S+))?$/i) {
499         return $noreply unless (&hasFlag("n"));
500         my ($param) = $2;
501
502         if ($param eq "") {
503             &msg($who,"\002Usage\002: unset <param>");
504             return $noreply;
505         }
506
507         if (!exists $param{$param}) {
508             &msg($who,"error: \002$param\002 cannot be unset");
509             return $noreply;
510         }
511
512         if ($param{$param} == 0) {
513             &msg($who,"\002param{$param}\002 has already been unset.");
514             return $noreply;
515         }
516
517         $param{$param} = 0;
518         &msg($who,"unsetting \002param{$param}\002.");
519
520         return $noreply;
521     }
522
523     # more...
524 }
525
526 1;