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);
65 # If topicAuthor is on then show it in topic, otherwise just topic -- troubled
66 } elsif ($param{'topicAuthor'} eq "1") {
67 push(@topic, "$subtopic ($setby)");
69 push(@topic, "$subtopic");
73 return join(' || ', @topic);
77 # Usage: &topicNew($chan, $topic, $updateMsg);
79 my ($chan, $topic, $updateMsg) = @_;
82 if ($channels{$chan}{t} and !$channels{$chan}{o}{$ident}) {
83 &msg($who, "error: cannot change topic without ops. (channel is +t) :(");
87 if (defined $topiccmp{$chan} and $topiccmp{$chan} eq $topic) {
88 &msg($who, "warning: action had no effect on topic; no change required.");
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)");
99 $topic{$chan}{'Current'} = $topic;
101 if ($cache{topicNotUpdate}{$chan}) {
102 &msg($who, "done. 'flush' to finalize changes.");
103 delete $cache{topicNotUpdate}{$chan};
107 if (defined $updateMsg && $updateMsg ne "") {
108 &msg($who, $updateMsg);
111 $topic{$chan}{'Last'} = $topic;
112 $topic{$chan}{'Who'} = $orig{who}."!".$uh;
113 $topic{$chan}{'Time'} = time();
116 $conn->topic($chan, $topic);
117 &topicAddHistory($chan, $topic);
119 $conn->topic($chan, " ");
126 # Usage: &topicAddHistory($chan,$topic);
127 sub topicAddHistory {
128 my ($chan, $topic) = @_;
131 return 1 if ($topic eq ""); # required fix.
133 foreach (@{ $topic{$chan}{'History'} }) {
134 next if ($_ ne "" and $_ ne $topic);
135 # checking length is required.
137 # slightly weird to put a return statement in a loop.
143 my @topics = @{ $topic{$chan}{'History'} };
144 unshift(@topics, $topic);
145 pop(@topics) while (scalar @topics > 6);
146 $topic{$chan}{'History'} = \@topics;
151 ###############################
152 ##### HELPER FUNCTIONS
153 ###############################
157 my ($chan, $args) = @_;
164 # heh, joeyh. 19990819. -xk
165 if ($who =~ /\|\|/) {
166 &msg($who, "error: you have an invalid nick, loser!");
170 return if ($channels{$chan}{t} and !&hasFlag("T"));
172 my @prev = &topicDecipher($chan);
173 my $new = "$args ($orig{who})";
174 $topic{$chan}{'What'} = "Added '$args'.";
177 my $str = sprintf("%s||%s", $args, $who);
178 $new = &topicCipher(@prev, $str);
181 &topicNew($chan, $new, "");
186 my ($chan, $args) = @_;
187 my @subtopics = &topicDecipher($chan);
188 my $topiccount = scalar @subtopics;
190 if ($topiccount == 0) {
191 &msg($who, "No topic set.");
201 $_ = sprintf(",%s,", $args);
204 s/last/$topiccount/i;
206 s/(\d+)-,/,$1-$topiccount/;
209 if ($args !~ /[\,\-\d]/) {
210 &msg($who, "error: Invalid argument ($args).");
215 foreach (split ",", $args) {
218 # change to hash list instead of array?
219 if (/^(\d+)-(\d+)$/) {
220 my ($from,$to) = ($1,$2);
221 ($from,$to) = ($2,$1) if ($from > $to);
223 push(@delete, $1..$2);
224 } elsif (/^(\d+)$/) {
227 &msg($who, "error: Invalid sub-argument ($_).");
231 $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
235 if ($_ > $topiccount || $_ < 1) {
236 &msg($who, "error: argument out of range. (max: $topiccount)");
240 # skip if already deleted.
241 # only checked if x-y range is given.
242 next unless (defined($subtopics[$_-1]));
244 my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
246 $whoby = "unknown" if ($whoby eq "");
248 &msg($who, "Deleting topic: $subtopic ($whoby)");
249 undef $subtopics[$_-1];
253 foreach (@subtopics) {
254 next unless (defined $_);
255 push(@newtopics, $_);
258 &topicNew($chan, &topicCipher(@newtopics), "");
263 my ($chan, $args) = @_;
264 my @topics = &topicDecipher($chan);
266 if (!scalar @topics) {
267 &msg($who, "No topics for \002$chan\002.");
271 &msg($who, "Topics for \002$chan\002:");
272 &msg($who, "No \002[\002 Set by \002]\002 Topic");
276 my ($subtopic, $setby) = split /\|\|/;
278 my $str = sprintf(" %d. [%-10s] %s", $i, $setby, $subtopic);
279 # is there a better way of doing this?
280 $str =~ s/ (\[)/ \002$1/g;
281 $str =~ s/ (\])/ \002$1/g;
287 &msg($who, "End of Topics.");
292 my ($chan, $args) = @_;
299 # a warning message instead of halting. we kind of trust the user now.
300 if ($args =~ /\|\|/) {
301 &msg($who, "warning: adding double pipes manually == evil. be warned.");
304 $topic{$chan}{'What'} = "SAR $args";
307 if ($args =~ m|^\s*s([/,#])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
308 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
310 if ($flags !~ /^(g)?$/) {
311 &msg($who, "error: Invalid flags to regex.");
315 my $topic = $topic{$chan}{'Current'};
317 ### TODO: use m### to make code safe!
318 if (($flags eq "g" and $topic =~ s/\Q$op\E/$np/g) ||
319 ($flags eq "" and $topic =~ s/\Q$op\E/$np/)
322 $_ = "Modifying topic with sar s/$op/$np/.";
323 &topicNew($chan, $topic, $_);
325 &msg($who, "warning: regex not found in topic.");
331 &msg($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
336 my ($chan, $args) = @_;
343 my ($from, $action, $to);
344 # better way of doing this?
345 if ($args =~ /^(first|last|\d+)\s+(before|after|swap)\s+(first|last|\d+)$/i) {
346 ($from, $action, $to) = ($1,$2,$3);
348 &msg($who, "Invalid arguments.");
352 my @subtopics = &topicDecipher($chan);
354 my $topiccount = scalar @subtopics;
356 if ($topiccount == 1) {
357 &msg($who, "error: impossible to move the only subtopic, dumbass.");
361 # Is there an easier way to do this?
362 $from =~ s/first/1/i;
364 $from =~ s/last/$topiccount/i;
365 $to =~ s/last/$topiccount/i;
367 if ($from > $topiccount || $to > $topiccount || $from < 1 || $to < 1) {
368 &msg($who, "error: <from> or <to> is out of range.");
373 &msg($who, "error: <from> and <to> are the same.");
377 $topic{$chan}{'What'} = "Move $from to $to";
379 if ($action =~ /^(swap)$/i) {
380 my $tmp = $subtopics[$to - 1];
381 $subtopics[$to - 1] = $subtopics[$from - 1];
382 $subtopics[$from - 1] = $tmp;
384 $_ = "Swapped #\002$from\002 with #\002$to\002.";
385 &topicNew($chan, &topicCipher(@subtopics), $_);
390 # Is there a better way to do this? guess not.
392 my $subtopic = $subtopics[$from - 1];
393 foreach (@subtopics) {
395 $newtopics[$j] = $_ if ($i != $from);
399 if ($action =~ /^(before|b4)$/i) {
400 $newtopics[$to*2-2] = $subtopic;
403 $newtopics[$to*2] = $subtopic;
406 undef @subtopics; # lets reuse this array.
407 foreach (@newtopics) {
408 next if (!defined $_ or $_ eq "");
409 push(@subtopics, $_);
412 $_ = "Moved #\002$from\002 $action #\002$to\002.";
413 &topicNew($chan, &topicCipher(@subtopics), $_);
418 my ($chan, $args) = @_;
419 my @subtopics = &topicDecipher($chan);
422 $topic{$chan}{'What'} = "shuffled";
424 foreach (&makeRandom(scalar @subtopics)) {
425 push(@newtopics, $subtopics[$_]);
428 $_ = "Shuffling the bag of lollies.";
429 &topicNew($chan, &topicCipher(@newtopics), $_);
434 my ($chan, $args) = @_;
436 if (!scalar @{ $topic{$chan}{'History'} }) {
437 &msg($who, "Sorry, no topics in history list.");
441 &msg($who, "History of topics on \002$chan\002:");
442 for (1 .. scalar @{ $topic{$chan}{'History'} }) {
443 my $topic = ${ $topic{$chan}{'History'} }[$_-1];
444 &msg($who, " #\002$_\002: $topic");
446 # To prevent excess floods.
447 sleep 1 if (length($topic) > 160);
450 &msg($who, "End of list.");
455 my ($chan, $args) = @_;
458 &help("topic restore");
462 $topic{$chan}{'What'} = "Restore topic $args";
464 # following needs to be verified.
465 if ($args =~ /^last$/i) {
466 if (${ $topic{$chan}{'History'} }[0] eq $topic{$chan}{'Current'}) {
467 &msg($who,"error: cannot restore last topic because it's mine.");
473 if ($args !~ /\d+/) {
474 &msg($who, "error: argument is not positive integer.");
478 if ($args > $#{ $topic{$chan}{'History'} } || $args < 1) {
479 &msg($who, "error: argument is out of range.");
483 $_ = "Changing topic according to request.";
484 &topicNew($chan, ${ $topic{$chan}{'History'} }[$args-1], $_);
491 $_ = "Rehashing topic...";
492 $topic{$chan}{'What'} = "Rehash";
493 &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
500 my $reply = "no topic info.";
501 if (exists $topic{$chan}{'Who'} and exists $topic{$chan}{'Time'}) {
502 $reply = "topic on \002$chan\002 was last set by ".
503 $topic{$chan}{'Who'}. ". This was done ".
504 &Time2String(time() - $topic{$chan}{'Time'}) ." ago".
505 ". Length: ".length($topic{$chan}{'Current'});
506 my $change = $topic{$chan}{'What'};
507 $reply .= ". Change => $change" if (defined $change);
510 &performStrictReply($reply);
513 ###############################
515 ###############################
518 # Usage: &Topic($cmd, $args);
520 my ($chan, $cmd, $args) = @_;
522 if ($cmd =~ /^-(\S+)/) {
523 $cache{topicNotUpdate}{$chan} = 1;
527 if ($cmd =~ /^(add)$/i) {
528 &do_add($chan, $args);
530 } elsif ($cmd =~ /^(del|delete|rm|remove|kill|purge)$/i) {
531 &do_delete($chan, $args);
533 } elsif ($cmd =~ /^list$/i) {
534 &do_list($chan, $args);
536 } elsif ($cmd =~ /^(mod|modify|change|alter)$/i) {
537 &do_modify($chan, $args);
539 } elsif ($cmd =~ /^(mv|move)$/i) {
540 &do_move($chan, $args);
542 } elsif ($cmd =~ /^shuffle$/i) {
543 &do_shuffle($chan, $args);
545 } elsif ($cmd =~ /^(history)$/i) {
546 &do_history($chan, $args);
548 } elsif ($cmd =~ /^restore$/i) {
549 &do_restore($chan, $args);
551 } elsif ($cmd =~ /^(flush|rehash)$/i) {
554 } elsif ($cmd =~ /^info$/i) {
559 if ($cmd ne "" and $cmd !~ /^help/i) {
560 &msg($who, "Invalid command [$cmd].");
561 &msg($who, "Try 'help topic'.");