]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/Topic.pl
- $topicUpdate was borked. replaced with $cache{}
[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     if (!@_) {
58         &WARN("topicCipher: topic is NULL for $chan.");
59         return;
60     }
61
62     my @topic;
63     foreach (@_) {
64         my ($subtopic, $setby) = split /\|\|/;
65
66         if ($setby =~ /^(unknown|)$/i) {
67             push(@topic, $subtopic);
68         } else {
69             push(@topic, "$subtopic ($setby)");
70         }
71     }
72
73     return join(' || ', @topic);
74 }
75
76 ###
77 # Usage: &topicNew($chan, $topic, $updateMsg);
78 sub topicNew {
79     my ($chan, $topic, $updateMsg) = @_;
80     my $maxlen = 470;
81
82     if ($channels{$chan}{t} and !$channels{$chan}{o}{$ident}) {
83         &msg($who, "error: cannot change topic without ops. (channel is +t) :(");
84         return 0;
85     }
86
87     if (defined $topiccmp{$chan} and $topiccmp{$chan} eq $topic) {
88         &msg($who, "warning: action had no effect on topic; no change required.");
89         return 0;
90     }
91
92     # bail out if the new topic is too long.
93     my $newlen = length($chan.$topic);
94     if ($newlen > $maxlen) {
95         &msg($who, "new topic will be too long. ($newlen > $maxlen)");
96         return 0;
97     }
98
99     $topic{$chan}{'Current'} = $topic;
100
101     if ($cache{topicNotUpdate}{$chan}) {
102         &msg($who, "done. 'flush' to finalize changes.");
103         delete $cache{topicNotUpdate}{$chan};
104         return 1;
105     }
106
107     if ($updateMsg ne "") {
108         &msg($who, $updateMsg);
109     }
110
111     $topic{$chan}{'Last'} = $topic;
112     $topic{$chan}{'Who'}  = $orig{who}."!".$uh;
113     $topic{$chan}{'Time'} = time();
114
115     $conn->topic($chan, $topic);
116     &topicAddHistory($chan, $topic);
117
118     return 1;
119 }
120
121 ###
122 # Usage: &topicAddHistory($chan,$topic);
123 sub topicAddHistory {
124     my ($chan, $topic)  = @_;
125     my $dupe            = 0;
126
127     return 1 if ($topic eq "");                 # required fix.
128
129     foreach (@{ $topic{$chan}{'History'} }) {
130         next if ($_ ne "" and $_ ne $topic);
131         # checking length is required.
132
133         # slightly weird to put a return statement in a loop.
134         return 1;
135     }
136
137     # WTF IS THIS FOR?
138
139     my @topics = @{ $topic{$chan}{'History'} };
140     unshift(@topics, $topic);
141     pop(@topics) while (scalar @topics > 6);
142     $topic{$chan}{'History'} = \@topics;
143
144     return $dupe;
145 }
146
147 ###############################
148 ##### HELPER FUNCTIONS
149 ###############################
150
151 # cmd: add.
152 sub do_add {
153     my ($chan, $args) = @_;
154
155     if ($args eq "") {
156         &help("topic add");
157         return;
158     }
159
160     # heh, joeyh. 19990819. -xk
161     if ($who =~ /\|\|/) {
162         &msg($who, "error: you have an invalid nick, loser!");
163         return;
164     }
165
166     return unless (&hasFlag("T"));
167
168     my @prev = &topicDecipher($chan);
169     my $new  = "$args ($orig{who})";
170     $topic{$chan}{'What'} = "Added '$args'.";
171
172     if (scalar @prev) {
173         my $str = sprintf("%s||%s", $args, $who);
174         $new = &topicCipher(@prev, $str);
175     }
176
177     &topicNew($chan, $new, "");
178 }
179
180 # cmd: delete.
181 sub do_delete {
182     my ($chan, $args)   = @_;
183     my @subtopics       = &topicDecipher($chan);
184     my $topiccount      = scalar @subtopics;
185
186     if ($topiccount == 0) {
187         &msg($who, "No topic set.");
188         return;
189     }
190
191     if ($args eq "") {
192         &help("topic del");
193         return;
194     }
195
196     for ($args) {
197         $_ = sprintf(",%s,", $args);
198         s/\s+//g;
199         s/(first|1st)/1/i;
200         s/last/$topiccount/i;
201         s/,-(\d+)/,1-$1/;
202         s/(\d+)-,/,$1-$topiccount/;
203     }
204
205     if ($args !~ /[\,\-\d]/) {
206         &msg($who, "error: Invalid argument ($args).");
207         return;
208     }
209
210     my @delete;
211     foreach (split ",", $args) {
212         next if ($_ eq "");
213
214         # change to hash list instead of array?
215         if (/^(\d+)-(\d+)$/) {
216             my ($from,$to) = ($1,$2);
217             ($from,$to) = ($2,$1)       if ($from > $to);
218
219             push(@delete, $1..$2);
220         } elsif (/^(\d+)$/) {
221             push(@delete, $1);
222         } else {
223             &msg($who, "error: Invalid sub-argument ($_).");
224             return;
225         }
226
227         $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
228     }
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         ### CMD: 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;