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})";
189 $new = &topicCipher(@prev, sprintf("%s||%s", $args, $who));
191 &topicNew($chan, $new, "", $topicUpdate);
193 } elsif ($cmd =~ /^(del|delete|rm|remove|kill|purge)$/i) {
195 my @subtopics = &topicDecipher($chan);
196 my $topiccount = scalar @subtopics;
198 if ($topiccount == 0) {
199 &msg($who, "No topic set.");
208 $args = ",".$args.",";
210 $args =~ s/(first|1st)/1/i;
211 $args =~ s/last/$topiccount/i;
212 $args =~ s/,-(\d+)/,1-$1/;
213 $args =~ s/(\d+)-,/,$1-$topiccount/;
215 if ($args !~ /[\,\-\d]/) {
216 &msg($who, "error: Invalid argument ($args).");
220 foreach (split ",", $args) {
224 # change to hash list instead of array?
225 if (/^(\d+)-(\d+)$/) {
226 my ($from,$to) = ($1,$2);
227 ($from,$to) = ($2,$1) if ($from > $to);
229 push(@delete, $1..$2);
230 } elsif (/^(\d+)$/) {
233 &msg($who, "error: Invalid sub-argument ($_).");
238 if ($_ > $topiccount || $_ < 1) {
239 &msg($who, "error: argument out of range. (max: $topiccount)");
242 # skip if already deleted.
243 # only checked if x-y range is given.
244 next unless (defined($subtopics[$_-1]));
246 my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
247 $whoby = "unknown" if ($whoby eq "");
248 &msg($who, "Deleting topic: $subtopic ($whoby)");
249 undef $subtopics[$_-1];
254 foreach (@subtopics) {
255 next unless (defined $_);
256 push(@newtopics, $_);
259 &topicNew($chan, &topicCipher(@newtopics), "", $topicUpdate);
261 } elsif ($cmd =~ /^list$/i) {
263 my @topics = &topicDecipher($chan);
264 if (!scalar @topics) {
265 &msg($who, "No topics for \002$chan\002.");
269 &msg($who, "Topics for \002$chan\002:");
270 &msg($who, "No \002[\002 Set by \002]\002 Topic");
274 my ($subtopic, $setby) = split /\|\|/;
276 &msg($who, sprintf(" %d. \002[\002%-10s\002]\002 %s",
277 $i, $setby, $subtopic));
280 &msg($who, "End of Topics.");
282 } elsif ($cmd =~ /^(mod|modify|change|alter)$/i) {
290 # a warning message instead of halting. we kind of trust the user now.
291 if ($args =~ /\|\|/) {
292 &msg($who, "warning: adding double pipes manually == evil. be warned.");
296 if ($args =~ m|^\s*s([/,#])(.+?)\1(.*?)\1([a-z]*);?\s*$|) {
297 my ($delim, $op, $np, $flags) = ($1,quotemeta $2,$3,$4);
299 if ($flags !~ /^(g)?$/) {
300 &msg($who, "error: Invalid flags to regex.");
304 my $topic = $topic{$chan}{'Current'};
306 if (($flags eq "g" and $topic =~ s/$op/$np/g) ||
307 ($flags eq "" and $topic =~ s/$op/$np/)) {
309 $_ = "Modifying topic with sar s/$op/$np/.";
310 &topicNew($chan, $topic, $_, $topicUpdate);
312 &msg($who, "warning: regex not found in topic.");
317 &msg($who, "error: Invalid regex. Try s/1/2/, s#3#4#...");
319 } elsif ($cmd =~ /^(mv|move)$/i) {
327 if ($args =~ /^(first|last|\d+)\s+(before|after|swap)\s+(first|last|\d+)$/i) {
328 my ($from, $action, $to) = ($1,$2,$3);
329 my @subtopics = &topicDecipher($chan);
331 my $topiccount = scalar @subtopics;
333 if ($topiccount == 1) {
334 &msg($who, "error: impossible to move the only subtopic, dumbass.");
338 # Is there an easier way to do this?
339 $from =~ s/first/1/i;
341 $from =~ s/last/$topiccount/i;
342 $to =~ s/last/$topiccount/i;
344 if ($from > $topiccount || $to > $topiccount || $from < 1 || $to < 1) {
345 &msg($who, "error: <from> or <to> is out of range.");
350 &msg($who, "error: <from> and <to> are the same.");
354 if ($action =~ /^(swap)$/i) {
355 my $tmp = $subtopics[$to - 1];
356 $subtopics[$to - 1] = $subtopics[$from - 1];
357 $subtopics[$from - 1] = $tmp;
359 $_ = "Swapped #\002$from\002 with #\002$to\002.";
360 &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
365 # Is there a better way to do this? guess not.
367 my $subtopic = $subtopics[$from - 1];
368 foreach (@subtopics) {
370 $newtopics[$j] = $_ if ($i != $from);
374 if ($action =~ /^(before|b4)$/i) {
375 $newtopics[$to*2-2] = $subtopic;
378 $newtopics[$to*2] = $subtopic;
381 undef @subtopics; # lets reuse this array.
382 foreach (@newtopics) {
384 push(@subtopics, $_);
387 $_ = "Moved #\002$from\002 $action #\002$to\002.";
388 &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
393 &msg($who, "Invalid arguments.");
395 } elsif ($cmd =~ /^shuffle$/i) {
397 my @subtopics = &topicDecipher($chan);
400 foreach (&makeRandom(scalar @subtopics)) {
401 push(@newtopics, $subtopics[$_]);
404 $_ = "Shuffling the bag of lollies.";
405 &topicNew($chan, &topicCipher(@newtopics), $_, $topicUpdate);
407 } elsif ($cmd =~ /^(history)$/i) {
409 if (!scalar @{$topic{$chan}{'History'}}) {
410 &msg($who, "Sorry, no topics in history list.");
414 &msg($who, "History of topics on \002$chan\002:");
415 for (1 .. scalar @{$topic{$chan}{'History'}}) {
416 my $topic = ${$topic{$chan}{'History'}}[$_-1];
417 &msg($who, " #\002$_\002: $topic");
419 # To prevent excess floods.
420 sleep 1 if (length($topic) > 160);
422 &msg($who, "End of list.");
424 } elsif ($cmd =~ /^restore$/i) {
427 &help("topic restore");
431 # following needs to be verified.
432 if ($args =~ /^last$/i) {
433 if (${$topic{$chan}{'History'}}[0] eq $topic{$chan}{'Current'}) {
434 &msg($who,"error: cannot restore last topic because it's mine.");
440 if ($args =~ /\d+/) {
441 if ($args > $#{$topic{$chan}{'History'}} || $args < 1) {
442 &msg($who, "error: argument is out of range.");
446 $_ = "Changing topic according to request.";
447 &topicNew($chan, ${$topic{$chan}{'History'}}[$args-1], $_, $topicUpdate);
452 &msg($who, "error: argument is not positive integer.");
454 } elsif ($cmd =~ /^rehash$/i) {
456 $_ = "Rehashing topic...";
457 &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
459 } elsif ($cmd =~ /^info$/i) {
461 my $reply = "no topic info.";
462 if (exists $topic{$chan}{'Who'} and exists $topic{$chan}{'Time'}) {
463 $reply = "topic on \002$chan\002 was last set by ".
464 $topic{$chan}{'Who'}. ". This was done ".
465 &Time2String(time() - $topic{$chan}{'Time'}) ." ago.";
468 &performStrictReply($reply);
471 if ($cmd ne "" and $cmd !~ /^help/i) {
472 &msg($who, "Invalid command [$cmd].");
473 &msg($who, "Try 'help topic'.");