]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Topic.pl
- strictify
[infobot.git] / src / Modules / Topic.pl
1 #
2 # Topic.pl: Advanced topic management (maxtopiclen>=512)
3 #   Author: dms
4 #  Version: v0.8 (19990919).
5 #  Created: 19990720
6 #
7
8 use strict;
9 use vars qw(%topiccmp %topic %channels %orig);
10 use vars qw($who $chan $conn $uh $ident);
11
12 ###############################
13 ##### INTERNAL FUNCTIONS
14 ###############################
15
16 ###
17 # Usage: &topicDecipher(chan);
18 sub topicDecipher {
19   my $chan      = shift;
20   my @results;
21
22   if (!exists $topic{$chan}{'Current'}) {
23     return;
24   }
25
26   foreach (split /\|\|/, $topic{$chan}{'Current'}) {
27     s/^\s+//;
28     s/\s+$//;
29
30     # very nice fix to solve the null subtopic problem.
31     ### if nick contains a space, treat topic as ownerless.
32     if (/^\(.*?\)$/) {
33         next unless ($1 =~ /\s/);
34     }
35
36     my $subtopic        = $_;
37     my $owner           = "Unknown";
38     if (/(.*)\s+\((.*?)\)$/) {
39         $subtopic       = $1;
40         $owner          = $2;
41     }
42
43     if (grep /^\Q$subtopic\E\|\|\Q$owner\E$/, @results) {
44         &status("Topic: we have found a dupe in the topic, not adding.");
45         next;
46     }
47
48     push(@results, "$subtopic||$owner");
49   }
50
51   return @results;
52 }
53
54 ###
55 # Usage: &topicCipher(@topics);
56 sub topicCipher {
57   if (!@_) {
58     &WARN("topicCipher: topic is NULL for $chan.");
59     return;
60   }
61
62   my $result;
63   foreach (@_) {
64     my ($subtopic, $setby) = split /\|\|/;
65
66     $result .= " || $subtopic";
67     next if ($setby eq "" or $setby =~ /unknown/i);
68
69     $result .= " (" . $setby . ")";
70   }
71
72   return substr($result, 4);
73 }
74
75 ###
76 # Usage: &topicNew($chan, $topic, $updateMsg, $topicUpdate);
77 sub topicNew {
78   my ($chan, $topic, $updateMsg, $topicUpdate) = @_;
79   my $maxlen = 470;
80
81   if ($channels{$chan}{t} and !$channels{$chan}{o}{$ident}) {
82     &msg($who, "error: cannot change topic without ops. (channel is +t) :(");
83     return 0;
84   }
85
86   if (defined $topiccmp{$chan} and $topiccmp{$chan} eq $topic) {
87     &msg($who, "warning: action had no effect on topic; no change required.");
88     return 0;
89   }
90
91   # bail out if the new topic is too long.
92   my $newlen = length($chan.$topic);
93   if ($newlen > $maxlen) {
94     &msg($who, "new topic will be too long. ($newlen > $maxlen)");
95     return 0;
96   }
97
98   $topic{$chan}{'Current'} = $topic;
99
100   # notification that the topic was altered.
101   if (!$topicUpdate) {          # for cached changes with '-'.
102     &msg($who, "okay");
103     return 1;
104   }
105
106   if ($updateMsg ne "") {
107     &msg($who, $updateMsg);
108   }
109
110   $topic{$chan}{'Last'} = $topic;
111   $topic{$chan}{'Who'}  = $orig{who}."!".$uh;
112   $topic{$chan}{'Time'} = time();
113   $conn->topic($chan, $topic);
114   &topicAddHistory($chan,$topic);
115   return 1;
116 }
117
118 ###
119 # Usage: &topicAddHistory($chan,$topic);
120 sub topicAddHistory {
121   my ($chan, $topic)    = @_;
122   my $dupe              = 0;
123
124   return 1 if ($topic eq "");                   # required fix.
125
126   foreach (@{ $topic{$chan}{'History'} }) {
127     next        if ($_ ne "" and $_ ne $topic);
128     # checking length is required.
129
130     $dupe++;
131     last;
132   }
133
134   return 1      if $dupe;
135
136   my @topics = @{ $topic{$chan}{'History'} };
137   unshift(@topics, $topic);
138   pop(@topics) while (scalar @topics > 6);
139   $topic{$chan}{'History'} = \@topics;
140
141   return $dupe;
142 }
143
144 ###############################
145 ##### HELPER FUNCTIONS
146 ###############################
147
148 ### TODO.
149 # sub topicNew {
150 # sub topicDelete {
151 # sub topicList {
152 # sub topicModify {
153 # sub topicMove {
154 # sub topicShuffle {
155 # sub topicHistory {
156 # sub topicRestore {
157 # sub topicRehash {
158 # sub topicHelp {
159
160 ###############################
161 ##### MAIN
162 ###############################
163
164 ###
165 # Usage: &Topic($cmd, $args);
166 sub Topic {
167   my ($chan, $cmd, $args) = @_;
168   my $topicUpdate = 1;
169
170   if ($cmd =~ /^-(\S+)/) {
171     $topicUpdate = 0;
172     $cmd = $1;
173   }
174
175   if ($cmd =~ /^(add)$/i) {
176     ### CMD: ADD:
177     if ($args eq "") {
178         &help("topic add");
179         return;
180     }
181
182     # heh, joeyh. 19990819. -xk
183     if ($who =~ /\|\|/) {
184         &msg($who, "error: you have an invalid nick, loser!");
185         return;
186     }
187
188     my @prev = &topicDecipher($chan);
189     my $new  = "$args ($orig{who})";
190     $topic{$chan}{'What'} = "Added '$args'.";
191     if (scalar @prev) {
192       $new = &topicCipher(@prev, sprintf("%s||%s", $args, $who));
193     }
194     &topicNew($chan, $new, "", $topicUpdate);
195
196   } elsif ($cmd =~ /^(del|delete|rm|remove|kill|purge)$/i) {
197     ### CMD: DEL:
198     my @subtopics       = &topicDecipher($chan);
199     my $topiccount      = scalar @subtopics;
200
201     if ($topiccount == 0) {
202         &msg($who, "No topic set.");
203         return;
204     }
205
206     if ($args eq "") {
207         &help("topic del");
208         return;
209     }
210
211     $args =  ",".$args.",";
212     $args =~ s/\s+//g;
213     $args =~ s/(first|1st)/1/i;
214     $args =~ s/last/$topiccount/i;
215     $args =~ s/,-(\d+)/,1-$1/;
216     $args =~ s/(\d+)-,/,$1-$topiccount/;
217
218     if ($args !~ /[\,\-\d]/) {
219         &msg($who, "error: Invalid argument ($args).");
220         return;
221     }
222
223     foreach (split ",", $args) {
224         next if ($_ eq "");
225         my @delete;
226
227         # change to hash list instead of array?
228         if (/^(\d+)-(\d+)$/) {
229             my ($from,$to) = ($1,$2);
230             ($from,$to) = ($2,$1)       if ($from > $to);
231
232             push(@delete, $1..$2);
233         } elsif (/^(\d+)$/) {
234             push(@delete, $1);
235         } else {
236             &msg($who, "error: Invalid sub-argument ($_).");
237             return;
238         }
239
240         $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
241
242         foreach (@delete) {
243           if ($_ > $topiccount || $_ < 1) {
244             &msg($who, "error: argument out of range. (max: $topiccount)");
245             return;
246           }
247           # skip if already deleted.
248           # only checked if x-y range is given.
249           next unless (defined($subtopics[$_-1]));
250
251           my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
252           $whoby                = "unknown"     if ($whoby eq "");
253           &msg($who, "Deleting topic: $subtopic ($whoby)");
254           undef $subtopics[$_-1];
255         }
256     }
257
258     my @newtopics;
259     foreach (@subtopics) {
260         next unless (defined $_);
261         push(@newtopics, $_);
262     }
263
264     &topicNew($chan, &topicCipher(@newtopics), "", $topicUpdate);
265
266   } elsif ($cmd =~ /^list$/i) {
267     ### CMD: LIST:
268     my @topics  = &topicDecipher($chan);
269     if (!scalar @topics) {
270         &msg($who, "No topics for \002$chan\002.");
271         return;
272     }
273
274     &msg($who, "Topics for \002$chan\002:");
275     &msg($who, "No  \002[\002  Set by  \002]\002 Topic");
276
277     my $i = 1;
278     foreach (@topics) {
279         my ($subtopic, $setby) = split /\|\|/;
280
281         &msg($who, sprintf(" %d. \002[\002%-10s\002]\002 %s",
282                                 $i, $setby, $subtopic));
283         $i++;
284     }
285     &msg($who, "End of Topics.");
286
287   } elsif ($cmd =~ /^(mod|modify|change|alter)$/i) {
288     ### CMD: MOD:
289
290     if ($args eq "") {
291         &help("topic mod");
292         return;
293     }
294
295     # a warning message instead of halting. we kind of trust the user now.
296     if ($args =~ /\|\|/) {
297         &msg($who, "warning: adding double pipes manually == evil. be warned.");
298     }
299
300     $topic{$chan}{'What'} = "SAR $args";
301
302     # SAR patch. mu++
303     if ($args =~ m|^\s*s([/,#])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
304         my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
305
306         if ($flags !~ /^(g)?$/) {
307           &msg($who, "error: Invalid flags to regex.");
308           return;
309         }
310
311         my $topic = $topic{$chan}{'Current'};
312
313         ### TODO: use m### to make code safe!
314         if (($flags eq "g" and $topic =~ s/\Q$op\E/$np/g) ||
315             ($flags eq ""  and $topic =~ s/\Q$op\E/$np/)) {
316
317           $_ = "Modifying topic with sar s/$op/$np/.";
318           &topicNew($chan, $topic, $_, $topicUpdate);
319         } else {
320           &msg($who, "warning: regex not found in topic.");
321         }
322         return;
323     }
324
325     &msg($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
326
327   } elsif ($cmd =~ /^(mv|move)$/i) {
328     ### CMD: MV:
329
330     if ($args eq "") {
331         &help("topic mv");
332         return;
333     }
334
335     if ($args =~ /^(first|last|\d+)\s+(before|after|swap)\s+(first|last|\d+)$/i) {
336         my ($from, $action, $to) = ($1,$2,$3);
337         my @subtopics  = &topicDecipher($chan);
338         my @newtopics;
339         my $topiccount = scalar @subtopics;
340
341         if ($topiccount == 1) {
342           &msg($who, "error: impossible to move the only subtopic, dumbass.");
343           return;
344         }
345
346         # Is there an easier way to do this?
347         $from =~ s/first/1/i;
348         $to   =~ s/first/1/i;
349         $from =~ s/last/$topiccount/i;
350         $to   =~ s/last/$topiccount/i;
351
352         if ($from > $topiccount || $to > $topiccount || $from < 1 || $to < 1) {
353           &msg($who, "error: <from> or <to> is out of range.");
354           return;
355         }
356
357         if ($from == $to) {
358           &msg($who, "error: <from> and <to> are the same.");
359           return;
360         }
361
362         $topic{$chan}{'What'} = "Move $from to $to";
363
364         if ($action =~ /^(swap)$/i) {
365           my $tmp                       = $subtopics[$to   - 1];
366           $subtopics[$to   - 1]         = $subtopics[$from - 1];
367           $subtopics[$from - 1]         = $tmp;
368
369           $_ = "Swapped #\002$from\002 with #\002$to\002.";
370           &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
371           return;
372         }
373
374         # action != swap:
375         # Is there a better way to do this? guess not.
376         my $i           = 1;
377         my $subtopic    = $subtopics[$from - 1];
378         foreach (@subtopics) {
379           my $j = $i*2 - 1;
380           $newtopics[$j] = $_   if ($i != $from);
381           $i++;
382         }
383
384         if ($action =~ /^(before|b4)$/i) {
385             $newtopics[$to*2-2] = $subtopic;
386         } else {
387             # action =~ /after/.
388             $newtopics[$to*2] = $subtopic;
389         }
390
391         undef @subtopics;                       # lets reuse this array.
392         foreach (@newtopics) {
393           next if (!defined $_ or $_ eq "");
394           push(@subtopics, $_);
395         }
396
397         $_ = "Moved #\002$from\002 $action #\002$to\002.";
398         &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
399
400         return;
401     }
402
403     &msg($who, "Invalid arguments.");
404
405   } elsif ($cmd =~ /^shuffle$/i) {
406     ### CMD: SHUFFLE:
407     my @subtopics  = &topicDecipher($chan);
408     my @newtopics;
409
410     $topic{$chan}{'What'} = "shuffled";
411
412     foreach (&makeRandom(scalar @subtopics)) {
413         push(@newtopics, $subtopics[$_]);
414     }
415
416     $_ = "Shuffling the bag of lollies.";
417     &topicNew($chan, &topicCipher(@newtopics), $_, $topicUpdate);
418
419   } elsif ($cmd =~ /^(history)$/i) {
420     ### CMD: HISTORY:
421     if (!scalar @{ $topic{$chan}{'History'} }) {
422         &msg($who, "Sorry, no topics in history list.");
423         return;
424     }
425
426     &msg($who, "History of topics on \002$chan\002:");
427     for (1 .. scalar @{ $topic{$chan}{'History'} }) {
428         my $topic = ${ $topic{$chan}{'History'} }[$_-1];
429         &msg($who, "  #\002$_\002: $topic");
430
431         # To prevent excess floods.
432         sleep 1 if (length($topic) > 160);
433     }
434     &msg($who, "End of list.");
435
436   } elsif ($cmd =~ /^restore$/i) {
437     ### CMD: RESTORE:
438     if ($args eq "") {
439         &help("topic restore");
440         return;
441     }
442
443     $topic{$chan}{'What'} = "Restore topic $args";
444
445     # following needs to be verified.
446     if ($args =~ /^last$/i) {
447         if (${ $topic{$chan}{'History'} }[0] eq $topic{$chan}{'Current'}) {
448             &msg($who,"error: cannot restore last topic because it's mine.");
449             return;
450         }
451         $args = 1;
452     }
453
454     if ($args =~ /\d+/) {
455         if ($args > $#{ $topic{$chan}{'History'} } || $args < 1) {
456             &msg($who, "error: argument is out of range.");
457             return;
458         }
459
460         $_ = "Changing topic according to request.";
461         &topicNew($chan, ${ $topic{$chan}{'History'} }[$args-1], $_, $topicUpdate);
462
463         return;
464     }
465
466     &msg($who, "error: argument is not positive integer.");
467
468   } elsif ($cmd =~ /^rehash$/i) {
469     ### CMD: REHASH.
470     $_ = "Rehashing topic...";
471     $topic{$chan}{'What'} = "Rehash";
472     &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
473
474   } elsif ($cmd =~ /^info$/i) {
475     ### CMD: INFO.
476     my $reply = "no topic info.";
477     if (exists $topic{$chan}{'Who'} and exists $topic{$chan}{'Time'}) {
478         $reply = "topic on \002$chan\002 was last set by ".
479                 $topic{$chan}{'Who'}. ".  This was done ".
480                 &Time2String(time() - $topic{$chan}{'Time'}) ." ago".
481                 ".  Length: ".length($topic{$chan}{'Current'});
482         my $change = $topic{$chan}{'What'};
483         $reply .= ".  Change => $change" if (defined $change);
484     }
485
486     &performStrictReply($reply);
487   } else {
488     ### CMD: HELP:
489     if ($cmd ne "" and $cmd !~ /^help/i) {
490         &msg($who, "Invalid command [$cmd].");
491         &msg($who, "Try 'help topic'.");
492         return;
493     }
494
495     &help("topic");
496   }
497
498   return;
499 }
500
501 1;