2 # Topic.pl: Advanced topic management (maxtopiclen>=512)
4 # Version: v0.8 (19990919).
9 use vars qw(%topiccmp %topic %channels %cache %orig);
10 use vars qw($who $chan $conn $uh $ident);
12 ###############################
13 ##### INTERNAL FUNCTIONS
14 ###############################
17 # Usage: &topicDecipher(chan);
22 return if (!exists $topic{$chan});
23 return if (!exists $topic{$chan}{'Current'});
25 foreach (split /\|\|/, $topic{$chan}{'Current'}) {
29 # very nice fix to solve the null subtopic problem.
30 # if nick contains a space, treat topic as ownerless.
32 next unless ($1 =~ /\s/);
36 my $owner = "Unknown";
38 if (/(.*)\s+\((.*?)\)$/) {
43 if (grep /^\Q$subtopic\E\|\|\Q$owner\E$/, @results) {
44 &status("Topic: we have found a dupe ($subtopic) in the topic, not adding.");
48 push(@results, "$subtopic||$owner");
55 # Usage: &topicCipher(@topics);
61 my ($subtopic, $setby) = split /\|\|/;
63 if ($setby =~ /^(unknown|)$/i) {
64 push(@topic, $subtopic);
66 push(@topic, "$subtopic ($setby)");
70 return join(' || ', @topic);
74 # Usage: &topicNew($chan, $topic, $updateMsg);
76 my ($chan, $topic, $updateMsg) = @_;
79 if ($channels{$chan}{t} and !$channels{$chan}{o}{$ident}) {
80 &msg($who, "error: cannot change topic without ops. (channel is +t) :(");
84 if (defined $topiccmp{$chan} and $topiccmp{$chan} eq $topic) {
85 &msg($who, "warning: action had no effect on topic; no change required.");
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)");
96 $topic{$chan}{'Current'} = $topic;
98 if ($cache{topicNotUpdate}{$chan}) {
99 &msg($who, "done. 'flush' to finalize changes.");
100 delete $cache{topicNotUpdate}{$chan};
104 if (defined $updateMsg && $updateMsg ne "") {
105 &msg($who, $updateMsg);
108 $topic{$chan}{'Last'} = $topic;
109 $topic{$chan}{'Who'} = $orig{who}."!".$uh;
110 $topic{$chan}{'Time'} = time();
113 $conn->topic($chan, $topic);
114 &topicAddHistory($chan, $topic);
116 $conn->topic($chan, " ");
123 # Usage: &topicAddHistory($chan,$topic);
124 sub topicAddHistory {
125 my ($chan, $topic) = @_;
128 return 1 if ($topic eq ""); # required fix.
130 foreach (@{ $topic{$chan}{'History'} }) {
131 next if ($_ ne "" and $_ ne $topic);
132 # checking length is required.
134 # slightly weird to put a return statement in a loop.
140 my @topics = @{ $topic{$chan}{'History'} };
141 unshift(@topics, $topic);
142 pop(@topics) while (scalar @topics > 6);
143 $topic{$chan}{'History'} = \@topics;
148 ###############################
149 ##### HELPER FUNCTIONS
150 ###############################
154 my ($chan, $args) = @_;
161 # heh, joeyh. 19990819. -xk
162 if ($who =~ /\|\|/) {
163 &msg($who, "error: you have an invalid nick, loser!");
167 return if ($channels{$chan}{t} and !&hasFlag("T"));
169 my @prev = &topicDecipher($chan);
170 my $new = "$args ($orig{who})";
171 $topic{$chan}{'What'} = "Added '$args'.";
174 my $str = sprintf("%s||%s", $args, $who);
175 $new = &topicCipher(@prev, $str);
178 &topicNew($chan, $new, "");
183 my ($chan, $args) = @_;
184 my @subtopics = &topicDecipher($chan);
185 my $topiccount = scalar @subtopics;
187 if ($topiccount == 0) {
188 &msg($who, "No topic set.");
198 $_ = sprintf(",%s,", $args);
201 s/last/$topiccount/i;
203 s/(\d+)-,/,$1-$topiccount/;
206 if ($args !~ /[\,\-\d]/) {
207 &msg($who, "error: Invalid argument ($args).");
212 foreach (split ",", $args) {
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);
220 push(@delete, $1..$2);
221 } elsif (/^(\d+)$/) {
224 &msg($who, "error: Invalid sub-argument ($_).");
228 $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
232 if ($_ > $topiccount || $_ < 1) {
233 &msg($who, "error: argument out of range. (max: $topiccount)");
237 # skip if already deleted.
238 # only checked if x-y range is given.
239 next unless (defined($subtopics[$_-1]));
241 my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
243 $whoby = "unknown" if ($whoby eq "");
245 &msg($who, "Deleting topic: $subtopic ($whoby)");
246 undef $subtopics[$_-1];
250 foreach (@subtopics) {
251 next unless (defined $_);
252 push(@newtopics, $_);
255 &topicNew($chan, &topicCipher(@newtopics), "");
260 my ($chan, $args) = @_;
261 my @topics = &topicDecipher($chan);
263 if (!scalar @topics) {
264 &msg($who, "No topics for \002$chan\002.");
268 &msg($who, "Topics for \002$chan\002:");
269 &msg($who, "No \002[\002 Set by \002]\002 Topic");
273 my ($subtopic, $setby) = split /\|\|/;
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;
284 &msg($who, "End of Topics.");
289 my ($chan, $args) = @_;
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.");
301 $topic{$chan}{'What'} = "SAR $args";
304 if ($args =~ m|^\s*s([/,#])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
305 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
307 if ($flags !~ /^(g)?$/) {
308 &msg($who, "error: Invalid flags to regex.");
312 my $topic = $topic{$chan}{'Current'};
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/)
319 $_ = "Modifying topic with sar s/$op/$np/.";
320 &topicNew($chan, $topic, $_);
322 &msg($who, "warning: regex not found in topic.");
328 &msg($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
333 my ($chan, $args) = @_;
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);
345 &msg($who, "Invalid arguments.");
349 my @subtopics = &topicDecipher($chan);
351 my $topiccount = scalar @subtopics;
353 if ($topiccount == 1) {
354 &msg($who, "error: impossible to move the only subtopic, dumbass.");
358 # Is there an easier way to do this?
359 $from =~ s/first/1/i;
361 $from =~ s/last/$topiccount/i;
362 $to =~ s/last/$topiccount/i;
364 if ($from > $topiccount || $to > $topiccount || $from < 1 || $to < 1) {
365 &msg($who, "error: <from> or <to> is out of range.");
370 &msg($who, "error: <from> and <to> are the same.");
374 $topic{$chan}{'What'} = "Move $from to $to";
376 if ($action =~ /^(swap)$/i) {
377 my $tmp = $subtopics[$to - 1];
378 $subtopics[$to - 1] = $subtopics[$from - 1];
379 $subtopics[$from - 1] = $tmp;
381 $_ = "Swapped #\002$from\002 with #\002$to\002.";
382 &topicNew($chan, &topicCipher(@subtopics), $_);
387 # Is there a better way to do this? guess not.
389 my $subtopic = $subtopics[$from - 1];
390 foreach (@subtopics) {
392 $newtopics[$j] = $_ if ($i != $from);
396 if ($action =~ /^(before|b4)$/i) {
397 $newtopics[$to*2-2] = $subtopic;
400 $newtopics[$to*2] = $subtopic;
403 undef @subtopics; # lets reuse this array.
404 foreach (@newtopics) {
405 next if (!defined $_ or $_ eq "");
406 push(@subtopics, $_);
409 $_ = "Moved #\002$from\002 $action #\002$to\002.";
410 &topicNew($chan, &topicCipher(@subtopics), $_);
415 my ($chan, $args) = @_;
416 my @subtopics = &topicDecipher($chan);
419 $topic{$chan}{'What'} = "shuffled";
421 foreach (&makeRandom(scalar @subtopics)) {
422 push(@newtopics, $subtopics[$_]);
425 $_ = "Shuffling the bag of lollies.";
426 &topicNew($chan, &topicCipher(@newtopics), $_);
431 my ($chan, $args) = @_;
433 if (!scalar @{ $topic{$chan}{'History'} }) {
434 &msg($who, "Sorry, no topics in history list.");
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");
443 # To prevent excess floods.
444 sleep 1 if (length($topic) > 160);
447 &msg($who, "End of list.");
452 my ($chan, $args) = @_;
455 &help("topic restore");
459 $topic{$chan}{'What'} = "Restore topic $args";
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.");
470 if ($args !~ /\d+/) {
471 &msg($who, "error: argument is not positive integer.");
475 if ($args > $#{ $topic{$chan}{'History'} } || $args < 1) {
476 &msg($who, "error: argument is out of range.");
480 $_ = "Changing topic according to request.";
481 &topicNew($chan, ${ $topic{$chan}{'History'} }[$args-1], $_);
488 $_ = "Rehashing topic...";
489 $topic{$chan}{'What'} = "Rehash";
490 &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
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);
507 &performStrictReply($reply);
510 ###############################
512 ###############################
515 # Usage: &Topic($cmd, $args);
517 my ($chan, $cmd, $args) = @_;
519 if ($cmd =~ /^-(\S+)/) {
520 $cache{topicNotUpdate}{$chan} = 1;
524 if ($cmd =~ /^(add)$/i) {
525 &do_add($chan, $args);
527 } elsif ($cmd =~ /^(del|delete|rm|remove|kill|purge)$/i) {
528 &do_delete($chan, $args);
530 } elsif ($cmd =~ /^list$/i) {
531 &do_list($chan, $args);
533 } elsif ($cmd =~ /^(mod|modify|change|alter)$/i) {
534 &do_modify($chan, $args);
536 } elsif ($cmd =~ /^(mv|move)$/i) {
537 &do_move($chan, $args);
539 } elsif ($cmd =~ /^shuffle$/i) {
540 &do_shuffle($chan, $args);
542 } elsif ($cmd =~ /^(history)$/i) {
543 &do_history($chan, $args);
545 } elsif ($cmd =~ /^restore$/i) {
546 &do_restore($chan, $args);
548 } elsif ($cmd =~ /^(flush|rehash)$/i) {
551 } elsif ($cmd =~ /^info$/i) {
556 if ($cmd ne "" and $cmd !~ /^help/i) {
557 &msg($who, "Invalid command [$cmd].");
558 &msg($who, "Try 'help topic'.");