]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/Topic.pl
take a few more things literally
[infobot.git] / src / Modules / Topic.pl
index 6fccc440c48eda853c234e82a8ca2038e33db7ed..e12fc5d25761d8b9ff65208ad95beeb16bbc1e1f 100644 (file)
@@ -6,9 +6,8 @@
 #
 
 use strict;
-use vars qw(%topiccmp %topic %channels %orig);
-use vars qw($who $chan $conn $uh $ident $topicUpdate);
-# use cache{topicUpdate}?
+use vars qw(%topiccmp %topic %channels %cache %orig);
+use vars qw($who $chan $conn $uh $ident);
 
 ###############################
 ##### INTERNAL FUNCTIONS
@@ -34,7 +33,7 @@ sub topicDecipher {
        }
 
        my $subtopic    = $_;
-       my $owner       = "Unknown";
+       my $owner       = 'Unknown';
 
        if (/(.*)\s+\((.*?)\)$/) {
            $subtopic   = $1;
@@ -42,7 +41,7 @@ sub topicDecipher {
        }
 
        if (grep /^\Q$subtopic\E\|\|\Q$owner\E$/, @results) {
-           &status("Topic: we have found a dupe in the topic, not adding.");
+           &status("Topic: we have found a dupe ($subtopic) in the topic, not adding.");
            next;
        }
 
@@ -55,19 +54,16 @@ sub topicDecipher {
 ###
 # Usage: &topicCipher(@topics);
 sub topicCipher {
-    if (!@_) {
-       &WARN("topicCipher: topic is NULL for $chan.");
-       return;
-    }
+    return if (!@_);
 
     my @topic;
     foreach (@_) {
        my ($subtopic, $setby) = split /\|\|/;
 
-       if ($setby =~ /(unknown|)$/i) {
-           push(@topic, $subtopic);
-       } else {
+       if ($param{'topicAuthor'} eq '1' and (!$setby =~ /^(unknown|)$/i)) {
            push(@topic, "$subtopic ($setby)");
+       } else {
+           push(@topic, "$subtopic");
        }
     }
 
@@ -75,9 +71,9 @@ sub topicCipher {
 }
 
 ###
-# Usage: &topicNew($chan, $topic, $updateMsg, $topicUpdate);
+# Usage: &topicNew($chan, $topic, $updateMsg);
 sub topicNew {
-    my ($chan, $topic, $updateMsg, $topicUpdate) = @_;
+    my ($chan, $topic, $updateMsg) = @_;
     my $maxlen = 470;
 
     if ($channels{$chan}{t} and !$channels{$chan}{o}{$ident}) {
@@ -99,13 +95,13 @@ sub topicNew {
 
     $topic{$chan}{'Current'} = $topic;
 
-    # notification that the topic was altered.
-    if (!$topicUpdate) {               # for cached changes with '-'.
-       &msg($who, "okay");
+    if ($cache{topicNotUpdate}{$chan}) {
+       &msg($who, "done. 'flush' to finalize changes.");
+       delete $cache{topicNotUpdate}{$chan};
        return 1;
     }
 
-    if ($updateMsg ne "") {
+    if (defined $updateMsg && $updateMsg ne '') {
        &msg($who, $updateMsg);
     }
 
@@ -113,8 +109,12 @@ sub topicNew {
     $topic{$chan}{'Who'}  = $orig{who}."!".$uh;
     $topic{$chan}{'Time'} = time();
 
-    $conn->topic($chan, $topic);
-    &topicAddHistory($chan,$topic);
+    if ($topic) {
+       $conn->topic($chan, $topic);
+       &topicAddHistory($chan, $topic);
+    } else {
+       $conn->topic($chan, ' ');
+    }
 
     return 1;
 }
@@ -125,10 +125,10 @@ sub topicAddHistory {
     my ($chan, $topic) = @_;
     my $dupe           = 0;
 
-    return 1 if ($topic eq "");                        # required fix.
+    return 1 if ($topic eq '');                        # required fix.
 
     foreach (@{ $topic{$chan}{'History'} }) {
-       next if ($_ ne "" and $_ ne $topic);
+       next if ($_ ne '' and $_ ne $topic);
        # checking length is required.
 
        # slightly weird to put a return statement in a loop.
@@ -153,24 +153,27 @@ sub topicAddHistory {
 sub do_add {
     my ($chan, $args) = @_;
 
-    if ($args eq "") {
-       &help("topic add");
+    if ($args eq '') {
+       &help('topic add');
        return;
     }
 
     # heh, joeyh. 19990819. -xk
     if ($who =~ /\|\|/) {
-       &msg($who, "error: you have an invalid nick, loser!");
+       &msg($who, 'error: you have an invalid nick, loser!');
        return;
     }
 
-    if (!&hasFlag("T")) {
-       &msg($who, "you do not have enough flags to add topics");
-       return;
-    }
+    return if ($channels{$chan}{t} and !&hasFlag('T'));
 
     my @prev = &topicDecipher($chan);
-    my $new  = "$args ($orig{who})";
+    my $new;
+    # If bot new to chan and topic is blank, it still got a (owner). This is fix
+    if ($param{'topicAuthor'} eq '1') {
+       $new  = "$args ($orig{who})";
+    } else {
+       $new  = "$args";
+    }
     $topic{$chan}{'What'} = "Added '$args'.";
 
     if (scalar @prev) {
@@ -178,7 +181,7 @@ sub do_add {
        $new = &topicCipher(@prev, $str);
     }
 
-    &topicNew($chan, $new, "", $topicUpdate);
+    &topicNew($chan, $new, '');
 }
 
 # cmd: delete.
@@ -188,12 +191,12 @@ sub do_delete {
     my $topiccount     = scalar @subtopics;
 
     if ($topiccount == 0) {
-       &msg($who, "No topic set.");
+       &msg($who, 'No topic set.');
        return;
     }
 
-    if ($args eq "") {
-       &help("topic del");
+    if ($args eq '') {
+       &help('topic del');
        return;
     }
 
@@ -212,8 +215,8 @@ sub do_delete {
     }
 
     my @delete;
-    foreach (split ",", $args) {
-       next if ($_ eq "");
+    foreach (split ',', $args) {
+       next if ($_ eq '');
 
        # change to hash list instead of array?
        if (/^(\d+)-(\d+)$/) {
@@ -228,10 +231,9 @@ sub do_delete {
            return;
        }
 
-       $topic{$chan}{'What'} = "Deleted ".join("/",@delete);
+       $topic{$chan}{'What'} = 'Deleted '.join("/",@delete);
     }
 
-
     foreach (@delete) {
        if ($_ > $topiccount || $_ < 1) {
            &msg($who, "error: argument out of range. (max: $topiccount)");
@@ -244,7 +246,7 @@ sub do_delete {
 
        my ($subtopic,$whoby) = split('\|\|', $subtopics[$_-1]);
 
-       $whoby = "unknown" if ($whoby eq "");
+       $whoby = 'unknown' if ($whoby eq '');
 
        &msg($who, "Deleting topic: $subtopic ($whoby)");
        undef $subtopics[$_-1];
@@ -256,7 +258,7 @@ sub do_delete {
        push(@newtopics, $_);
     }
 
-    &topicNew($chan, &topicCipher(@newtopics), "", $topicUpdate);
+    &topicNew($chan, &topicCipher(@newtopics), '');
 }
 
 # cmd: list
@@ -292,8 +294,8 @@ sub do_list {
 sub do_modify {
     my ($chan, $args) = @_;
 
-    if ($args eq "") {
-       &help("topic mod");
+    if ($args eq '') {
+       &help('topic mod');
        return;
     }
 
@@ -316,12 +318,12 @@ sub do_modify {
        my $topic = $topic{$chan}{'Current'};
 
        ### TODO: use m### to make code safe!
-       if (($flags eq "g" and $topic =~ s/\Q$op\E/$np/g) ||
-           ($flags eq ""  and $topic =~ s/\Q$op\E/$np/)
+       if (($flags eq 'g' and $topic =~ s/\Q$op\E/$np/g) ||
+           ($flags eq ''  and $topic =~ s/\Q$op\E/$np/)
        ) {
 
            $_ = "Modifying topic with sar s/$op/$np/.";
-           &topicNew($chan, $topic, $_, $topicUpdate);
+           &topicNew($chan, $topic, $_);
        } else {
            &msg($who, "warning: regex not found in topic.");
        }
@@ -336,8 +338,8 @@ sub do_modify {
 sub do_move {
     my ($chan, $args) = @_;
 
-    if ($args eq "") {
-       &help("topic mv");
+    if ($args eq '') {
+       &help('topic mv');
        return;
     }
 
@@ -383,7 +385,7 @@ sub do_move {
        $subtopics[$from - 1]   = $tmp;
 
        $_ = "Swapped #\002$from\002 with #\002$to\002.";
-       &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
+       &topicNew($chan, &topicCipher(@subtopics), $_);
        return;
     }
 
@@ -406,12 +408,12 @@ sub do_move {
 
     undef @subtopics;                  # lets reuse this array.
     foreach (@newtopics) {
-       next if (!defined $_ or $_ eq "");
+       next if (!defined $_ or $_ eq '');
        push(@subtopics, $_);
     }
 
     $_ = "Moved #\002$from\002 $action #\002$to\002.";
-    &topicNew($chan, &topicCipher(@subtopics), $_, $topicUpdate);
+    &topicNew($chan, &topicCipher(@subtopics), $_);
 }
 
 # cmd: shuffle.
@@ -420,14 +422,14 @@ sub do_shuffle {
     my @subtopics      = &topicDecipher($chan);
     my @newtopics;
 
-    $topic{$chan}{'What'} = "shuffled";
+    $topic{$chan}{'What'} = 'shuffled';
 
     foreach (&makeRandom(scalar @subtopics)) {
        push(@newtopics, $subtopics[$_]);
     }
 
     $_ = "Shuffling the bag of lollies.";
-    &topicNew($chan, &topicCipher(@newtopics), $_, $topicUpdate);
+    &topicNew($chan, &topicCipher(@newtopics), $_);
 }
 
 # cmd: history.
@@ -455,8 +457,8 @@ sub do_history {
 sub do_restore {
     my ($chan, $args) = @_;
 
-    if ($args eq "") {
-       &help("topic restore");
+    if ($args eq '') {
+       &help('topic restore');
        return;
     }
 
@@ -482,7 +484,7 @@ sub do_restore {
     }
 
     $_ = "Changing topic according to request.";
-    &topicNew($chan, ${ $topic{$chan}{'History'} }[$args-1], $_, $topicUpdate);
+    &topicNew($chan, ${ $topic{$chan}{'History'} }[$args-1], $_);
 }
 
 # cmd: rehash.
@@ -490,7 +492,7 @@ sub do_rehash {
     my ($chan) = @_;
 
     $_ = "Rehashing topic...";
-    $topic{$chan}{'What'} = "Rehash";
+    $topic{$chan}{'What'} = 'Rehash';
     &topicNew($chan, $topic{$chan}{'Current'}, $_, 1);
 }
 
@@ -502,7 +504,7 @@ sub do_info {
     if (exists $topic{$chan}{'Who'} and exists $topic{$chan}{'Time'}) {
        $reply = "topic on \002$chan\002 was last set by ".
                $topic{$chan}{'Who'}. ".  This was done ".
-               &Time2String(time() - $topic{$chan}{'Time'}) ." ago".
+               &Time2String(time() - $topic{$chan}{'Time'}) .' ago'.
                ".  Length: ".length($topic{$chan}{'Current'});
        my $change = $topic{$chan}{'What'};
        $reply .= ".  Change => $change" if (defined $change);
@@ -519,10 +521,9 @@ sub do_info {
 # Usage: &Topic($cmd, $args);
 sub Topic {
     my ($chan, $cmd, $args) = @_;
-    my $topicUpdate = 1;
 
     if ($cmd =~ /^-(\S+)/) {
-       $topicUpdate = 0;
+       $cache{topicNotUpdate}{$chan} = 1;
        $cmd = $1;
     }
 
@@ -550,21 +551,21 @@ sub Topic {
     } elsif ($cmd =~ /^restore$/i) {
        &do_restore($chan, $args);
 
-    } elsif ($cmd =~ /^rehash$/i) {
+    } elsif ($cmd =~ /^(flush|rehash)$/i) {
        &do_rehash($chan);
 
     } elsif ($cmd =~ /^info$/i) {
        &do_info($chan);
 
     } else {
-       ### CMD: HELP:
-       if ($cmd ne "" and $cmd !~ /^help/i) {
+       ### HELP:
+       if ($cmd ne '' and $cmd !~ /^help/i) {
            &msg($who, "Invalid command [$cmd].");
            &msg($who, "Try 'help topic'.");
            return;
        }
 
-       &help("topic");
+       &help('topic');
     }
 
     return;