2 # Topic.pl: Advanced topic management (maxtopiclen>=512)
4 # Version: v0.8 (19990919).
9 use vars qw(%topiccmp);
10 no strict "refs"; ### FIXME!!!
12 ###############################
13 ##### INTERNAL FUNCTIONS
14 ###############################
17 # Usage: &topicDecipher(chan);
22 if (!exists $topic{$chan}{'Current'}) {
26 foreach (split /\|\|/, $topic{$chan}{'Current'}) {
30 # very nice fix to solve the null subtopic problem.
31 ### if nick contains a space, treat topic as ownerless.
33 next unless ($1 =~ /\s/);
37 my $owner = "Unknown";
38 if (/(.*)\s+\((.*?)\)$/) {
43 if (grep /^\Q$subtopic\E\|\|\Q$owner\E$/, @results) {
44 &status("Topic: we have found a dupe in the topic, not adding.");
48 push(@results, "$subtopic||$owner");
55 # Usage: &topicCipher(@topics);
58 &WARN("topicCipher: topic is NULL for $chan.");
64 my ($subtopic, $setby) = split /\|\|/;
66 $result .= " || $subtopic";
67 next if ($setby eq "" or $setby =~ /unknown/i);
69 $result .= " (" . $setby . ")";
72 return substr($result, 4);
76 # Usage: &topicNew($chan, $topic, $updateMsg, $topicUpdate);
78 my ($chan, $topic, $updateMsg, $topicUpdate) = @_;
81 if ($channels{$chan}{t} and !$channels{$chan}{o}{$ident}) {
82 &msg($who, "error: cannot change topic without ops. (channel is +t) :(");
86 if (defined $topiccmp{$chan} and $topiccmp{$chan} eq $topic) {
87 &msg($who, "warning: action had no effect on topic; no change required.");
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)");
98 $topic{$chan}{'Current'} = $topic;
100 # notification that the topic was altered.
101 if (!$topicUpdate) { # for cached changes with '-'.
106 if ($updateMsg ne "") {
107 &msg($who, $updateMsg);
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);
119 # Usage: &topicAddHistory($chan,$topic);
120 sub topicAddHistory {
121 my ($chan, $topic) = @_;
124 return 1 if ($topic eq ""); # required fix.
126 foreach (@{ $topic{$chan}{'History'} }) {
127 next if ($_ ne "" and $_ ne $topic);
128 # checking length is required.
136 my @topics = @{ $topic{$chan}{'History'} };
137 unshift(@topics, $topic);
138 pop(@topics) while (scalar @topics > 6);
139 $topic{$chan}{'History'} = \@topics;
144 ###############################
145 ##### HELPER FUNCTIONS
146 ###############################
160 ###############################
162 ###############################
165 # Usage: &Topic($cmd, $args);
167 my ($chan, $cmd, $args) = @_;
170 if ($cmd =~ /^-(\S+)/) {
175 if ($cmd =~ /^(add)$/i) {
182 # heh, joeyh. 19990819. -xk
183 if ($who =~ /\|\|/) {
184 &msg($who, "error: you have an invalid nick, loser!");
188 my @prev = &topicDecipher($chan);
189 my $new = "$args ($orig{who})";
190 $topic{$chan}{'What'} = "Added '$args'.";
192 $new = &topicCipher(@prev, sprintf("%s||%s", $args, $who));
194 &topicNew($chan, $new, "", $topicUpdate);
196 } elsif ($cmd =~ /^(del|delete|rm|remove|kill|purge)$/i) {
198 my @subtopics = &topicDecipher($chan);
199 my $topiccount = scalar @subtopics;
201 if ($topiccount == 0) {
202 &msg($who, "No topic set.");
211 $args = ",".$args.",";
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/;
218 if ($args !~ /[\,\-\d]/) {
219 &msg($who, "error: Invalid argument ($args).");
223 foreach (split ",", $args) {
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);
232 push(@delete, $1..$2);
233 } elsif (/^(\d+)$/) {
236 &msg($who, "error: Invalid sub-argument ($_).");
240 $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
243 if ($_ > $topiccount || $_ < 1) {
244 &msg($who, "error: argument out of range. (max: $topiccount)");
247 # skip if already deleted.
248 # only checked if x-y range is given.
249 next unless (defined($subtopics[$_-1]));
251 my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
252 $whoby = "unknown" if ($whoby eq "");
253 &msg($who, "Deleting topic: $subtopic ($whoby)");
254 undef $subtopics[$_-1];
259 foreach (@subtopics) {
260 next unless (defined $_);
261 push(@newtopics, $_);
264 &topicNew($chan, &topicCipher(@newtopics), "", $topicUpdate);
266 } elsif ($cmd =~ /^list$/i) {
268 my @topics = &topicDecipher($chan);
269 if (!scalar @topics) {
270 &msg($who, "No topics for \002$chan\002.");
274 &msg($who, "Topics for \002$chan\002:");
275 &msg($who, "No \002[\002 Set by \002]\002 Topic");
279 my ($subtopic, $setby) = split /\|\|/;
281 &msg($who, sprintf(" %d. \002[\002%-10s\002]\002 %s",
282 $i, $setby, $subtopic));
285 &msg($who, "End of Topics.");
287 } elsif ($cmd =~ /^(mod|modify|change|alter)$/i) {
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.");
300 $topic{$chan}{'What'} = "SAR $args";
303 if ($args =~ m|^\s*s([/,#])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
304 my ($delim, $op, $np, $flags) = ($1,$2,$3,$4);
306 if ($flags !~ /^(g)?$/) {
307 &msg($who, "error: Invalid flags to regex.");
311 my $topic = $topic{$chan}{'Current'};
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/)) {
317 $_ = "Modifying topic with sar s/$op/$np/.";
318 &topicNew($chan, $topic, $_, $topicUpdate);
320 &msg($who, "warning: regex not found in topic.");
325 &msg($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
327 } elsif ($cmd =~ /^(mv|move)$/i) {
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);
339 my $topiccount = scalar @subtopics;
341 if ($topiccount == 1) {
342 &msg($who, "error: impossible to move the only subtopic, dumbass.");
346 # Is there an easier way to do this?
347 $from =~ s/first/1/i;
349 $from =~ s/last/$topiccount/i;
350 $to =~ s/last/$topiccount/i;
352 if ($from > $topiccount || $to > $topiccount || $from < 1 || $to < 1) {
353 &msg($who, "error: <from> or <to> is out of range.");
358 &msg($who, "error: <from> and <to> are the same.");
362 $topic{$chan}{'What'} = "Move $from to $to";
364 if ($action =~ /^(swap)$/i) {
365 my $tmp = $subtopics[$to - 1];
366 $subtopics[$to - 1] = $subtopics[$from - 1];
367 $subtopics[$from - 1] = $tmp;
369 $_ = "Swapped #\002$from\002 with #\002$to\002.";
370 &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
375 # Is there a better way to do this? guess not.
377 my $subtopic = $subtopics[$from - 1];
378 foreach (@subtopics) {
380 $newtopics[$j] = $_ if ($i != $from);
384 if ($action =~ /^(before|b4)$/i) {
385 $newtopics[$to*2-2] = $subtopic;
388 $newtopics[$to*2] = $subtopic;
391 undef @subtopics; # lets reuse this array.
392 foreach (@newtopics) {
393 next if (!defined $_ or $_ eq "");
394 push(@subtopics, $_);
397 $_ = "Moved #\002$from\002 $action #\002$to\002.";
398 &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
403 &msg($who, "Invalid arguments.");
405 } elsif ($cmd =~ /^shuffle$/i) {
407 my @subtopics = &topicDecipher($chan);
410 $topic{$chan}{'What'} = "shuffled";
412 foreach (&makeRandom(scalar @subtopics)) {
413 push(@newtopics, $subtopics[$_]);
416 $_ = "Shuffling the bag of lollies.";
417 &topicNew($chan, &topicCipher(@newtopics), $_, $topicUpdate);
419 } elsif ($cmd =~ /^(history)$/i) {
421 if (!scalar @{ $topic{$chan}{'History'} }) {
422 &msg($who, "Sorry, no topics in history list.");
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");
431 # To prevent excess floods.
432 sleep 1 if (length($topic) > 160);
434 &msg($who, "End of list.");
436 } elsif ($cmd =~ /^restore$/i) {
439 &help("topic restore");
443 $topic{$chan}{'What'} = "Restore topic $args";
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.");
454 if ($args =~ /\d+/) {
455 if ($args > $#{ $topic{$chan}{'History'} } || $args < 1) {
456 &msg($who, "error: argument is out of range.");
460 $_ = "Changing topic according to request.";
461 &topicNew($chan, ${ $topic{$chan}{'History'} }[$args-1], $_, $topicUpdate);
466 &msg($who, "error: argument is not positive integer.");
468 } elsif ($cmd =~ /^rehash$/i) {
470 $_ = "Rehashing topic...";
471 $topic{$chan}{'What'} = "Rehash";
472 &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
474 } elsif ($cmd =~ /^info$/i) {
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);
486 &performStrictReply($reply);
489 if ($cmd ne "" and $cmd !~ /^help/i) {
490 &msg($who, "Invalid command [$cmd].");
491 &msg($who, "Try 'help topic'.");