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