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'}) {
23 &DEBUG("Topic: does not exist for $chan.");
27 &DEBUG("Topic: hrm => '$topic{$chan}{'Current'}'.");
29 foreach (split /\|\|/, $topic{$chan}{'Current'}) {
33 # very nice fix to solve the null subtopic problem.
34 ### if nick contains a space, treat topic as ownerless.
36 next unless ($1 =~ /\s/);
40 my $owner = "Unknown";
41 if (/(.*)\s+\((.*?)\)$/) {
46 push(@results, "$subtopic||$owner");
53 # Usage: &topicCipher(@topics);
56 &DEBUG("topicCipher: topic is NULL.");
62 my ($subtopic, $setby) = split /\|\|/;
64 $result .= " || $subtopic";
65 next if ($setby eq "" or $setby =~ /unknown/i);
67 $result .= " (" . $setby . ")";
70 return substr($result, 4);
74 # Usage: &topicNew($chan, $topic, $updateMsg, $topicUpdate);
76 my ($chan, $topic, $updateMsg, $topicUpdate) = @_;
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 # notification that the topic was altered.
99 if (!$topicUpdate) { # for cached changes with '-'.
100 &performReply("okay");
104 if ($updateMsg ne "") {
105 &msg($who, $updateMsg);
108 $topic{$chan}{'Last'} = $topic;
109 $topic{$chan}{'Who'} = $orig{who}."!".$uh;
110 $topic{$chan}{'Time'} = time();
111 rawout("TOPIC $chan :$topic");
112 &topicAddHistory($chan,$topic);
117 # Usage: &topicAddHistory($chan,$topic);
118 sub topicAddHistory {
119 my ($chan, $topic) = @_;
122 return 1 if ($topic eq ""); # required fix.
124 foreach (@{ $topic{$chan}{'History'} }) {
125 next if ($_ ne "" and $_ ne $topic);
126 # checking length is required.
134 my @topics = @{ $topic{$chan}{'History'} };
135 unshift(@topics, $topic);
136 pop(@topics) while (scalar @topics > 6);
137 $topic{$chan}{'History'} = \@topics;
142 ###############################
143 ##### HELPER FUNCTIONS
144 ###############################
158 ###############################
160 ###############################
163 # Usage: &Topic($cmd, $args);
165 my ($chan, $cmd, $args) = @_;
168 if ($cmd =~ /^-(\S+)/) {
173 if ($cmd =~ /^(add)$/i) {
180 # heh, joeyh. 19990819. -xk
181 if ($who =~ /\|\|/) {
182 &msg($who, "error: you have an invalid nick, loser!");
186 my @prev = &topicDecipher($chan);
187 my $new = "$args ($orig{who})";
188 $topic{$chan}{'What'} = "Added '$args'.";
190 $new = &topicCipher(@prev, sprintf("%s||%s", $args, $who));
192 &topicNew($chan, $new, "", $topicUpdate);
194 } elsif ($cmd =~ /^(del|delete|rm|remove|kill|purge)$/i) {
196 my @subtopics = &topicDecipher($chan);
197 my $topiccount = scalar @subtopics;
199 if ($topiccount == 0) {
200 &msg($who, "No topic set.");
209 $args = ",".$args.",";
211 $args =~ s/(first|1st)/1/i;
212 $args =~ s/last/$topiccount/i;
213 $args =~ s/,-(\d+)/,1-$1/;
214 $args =~ s/(\d+)-,/,$1-$topiccount/;
216 if ($args !~ /[\,\-\d]/) {
217 &msg($who, "error: Invalid argument ($args).");
221 foreach (split ",", $args) {
225 # change to hash list instead of array?
226 if (/^(\d+)-(\d+)$/) {
227 my ($from,$to) = ($1,$2);
228 ($from,$to) = ($2,$1) if ($from > $to);
230 push(@delete, $1..$2);
231 } elsif (/^(\d+)$/) {
234 &msg($who, "error: Invalid sub-argument ($_).");
238 $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
241 if ($_ > $topiccount || $_ < 1) {
242 &msg($who, "error: argument out of range. (max: $topiccount)");
245 # skip if already deleted.
246 # only checked if x-y range is given.
247 next unless (defined($subtopics[$_-1]));
249 my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
250 $whoby = "unknown" if ($whoby eq "");
251 &msg($who, "Deleting topic: $subtopic ($whoby)");
252 undef $subtopics[$_-1];
257 foreach (@subtopics) {
258 next unless (defined $_);
259 push(@newtopics, $_);
262 &topicNew($chan, &topicCipher(@newtopics), "", $topicUpdate);
264 } elsif ($cmd =~ /^list$/i) {
266 my @topics = &topicDecipher($chan);
267 if (!scalar @topics) {
268 &msg($who, "No topics for \002$chan\002.");
272 &msg($who, "Topics for \002$chan\002:");
273 &msg($who, "No \002[\002 Set by \002]\002 Topic");
277 my ($subtopic, $setby) = split /\|\|/;
279 &msg($who, sprintf(" %d. \002[\002%-10s\002]\002 %s",
280 $i, $setby, $subtopic));
283 &msg($who, "End of Topics.");
285 } elsif ($cmd =~ /^(mod|modify|change|alter)$/i) {
293 # a warning message instead of halting. we kind of trust the user now.
294 if ($args =~ /\|\|/) {
295 &msg($who, "warning: adding double pipes manually == evil. be warned.");
298 $topic{$chan}{'What'} = "SAR $args";
301 if ($args =~ m|^\s*s([/,#])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
302 my ($delim, $op, $np, $flags) = ($1,quotemeta $2,$3,$4);
304 if ($flags !~ /^(g)?$/) {
305 &msg($who, "error: Invalid flags to regex.");
309 my $topic = $topic{$chan}{'Current'};
311 if (($flags eq "g" and $topic =~ s/$op/$np/g) ||
312 ($flags eq "" and $topic =~ s/$op/$np/)) {
314 $_ = "Modifying topic with sar s/$op/$np/.";
315 &topicNew($chan, $topic, $_, $topicUpdate);
317 &msg($who, "warning: regex not found in topic.");
322 &msg($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
324 } elsif ($cmd =~ /^(mv|move)$/i) {
332 if ($args =~ /^(first|last|\d+)\s+(before|after|swap)\s+(first|last|\d+)$/i) {
333 my ($from, $action, $to) = ($1,$2,$3);
334 my @subtopics = &topicDecipher($chan);
336 my $topiccount = scalar @subtopics;
338 if ($topiccount == 1) {
339 &msg($who, "error: impossible to move the only subtopic, dumbass.");
343 # Is there an easier way to do this?
344 $from =~ s/first/1/i;
346 $from =~ s/last/$topiccount/i;
347 $to =~ s/last/$topiccount/i;
349 if ($from > $topiccount || $to > $topiccount || $from < 1 || $to < 1) {
350 &msg($who, "error: <from> or <to> is out of range.");
355 &msg($who, "error: <from> and <to> are the same.");
359 $topic{$chan}{'What'} = "Move $from to $to";
361 if ($action =~ /^(swap)$/i) {
362 my $tmp = $subtopics[$to - 1];
363 $subtopics[$to - 1] = $subtopics[$from - 1];
364 $subtopics[$from - 1] = $tmp;
366 $_ = "Swapped #\002$from\002 with #\002$to\002.";
367 &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
372 # Is there a better way to do this? guess not.
374 my $subtopic = $subtopics[$from - 1];
375 foreach (@subtopics) {
377 $newtopics[$j] = $_ if ($i != $from);
381 if ($action =~ /^(before|b4)$/i) {
382 $newtopics[$to*2-2] = $subtopic;
385 $newtopics[$to*2] = $subtopic;
388 undef @subtopics; # lets reuse this array.
389 foreach (@newtopics) {
391 push(@subtopics, $_);
394 $_ = "Moved #\002$from\002 $action #\002$to\002.";
395 &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
400 &msg($who, "Invalid arguments.");
402 } elsif ($cmd =~ /^shuffle$/i) {
404 my @subtopics = &topicDecipher($chan);
407 $topic{$chan}{'What'} = "shuffled";
409 foreach (&makeRandom(scalar @subtopics)) {
410 push(@newtopics, $subtopics[$_]);
413 $_ = "Shuffling the bag of lollies.";
414 &topicNew($chan, &topicCipher(@newtopics), $_, $topicUpdate);
416 } elsif ($cmd =~ /^(history)$/i) {
418 if (!scalar @{$topic{$chan}{'History'}}) {
419 &msg($who, "Sorry, no topics in history list.");
423 &msg($who, "History of topics on \002$chan\002:");
424 for (1 .. scalar @{$topic{$chan}{'History'}}) {
425 my $topic = ${$topic{$chan}{'History'}}[$_-1];
426 &msg($who, " #\002$_\002: $topic");
428 # To prevent excess floods.
429 sleep 1 if (length($topic) > 160);
431 &msg($who, "End of list.");
433 } elsif ($cmd =~ /^restore$/i) {
436 &help("topic restore");
440 $topic{$chan}{'What'} = "Restore topic $args";
442 # following needs to be verified.
443 if ($args =~ /^last$/i) {
444 if (${$topic{$chan}{'History'}}[0] eq $topic{$chan}{'Current'}) {
445 &msg($who,"error: cannot restore last topic because it's mine.");
451 if ($args =~ /\d+/) {
452 if ($args > $#{$topic{$chan}{'History'}} || $args < 1) {
453 &msg($who, "error: argument is out of range.");
457 $_ = "Changing topic according to request.";
458 &topicNew($chan, ${$topic{$chan}{'History'}}[$args-1], $_, $topicUpdate);
463 &msg($who, "error: argument is not positive integer.");
465 } elsif ($cmd =~ /^rehash$/i) {
467 $_ = "Rehashing topic...";
468 $topic{$chan}{'What'} = "Rehash";
469 &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
471 } elsif ($cmd =~ /^info$/i) {
473 my $reply = "no topic info.";
474 if (exists $topic{$chan}{'Who'} and exists $topic{$chan}{'Time'}) {
475 $reply = "topic on \002$chan\002 was last set by ".
476 $topic{$chan}{'Who'}. ". This was done ".
477 &Time2String(time() - $topic{$chan}{'Time'}) ." ago.";
478 my $change = $topic{$chan}{'What'};
479 $reply .= "Change => $change" if (defined $change);
482 &performStrictReply($reply);
485 if ($cmd ne "" and $cmd !~ /^help/i) {
486 &msg($who, "Invalid command [$cmd].");
487 &msg($who, "Try 'help topic'.");