]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/News.pl
really support negative booleans and and return -1 for IsChanConf() when they are...
[infobot.git] / src / Modules / News.pl
index fcf62b9ca28bba6a357fedff093902811cbb291d..25b049c28d5fb1f4aa4a4a47d0706700a933a4d1 100644 (file)
 #      Text    - Actual text.
 ###
 
-use vars qw($who $chan);
-
 package News;
 
+use strict;
+
+use vars qw($who $chan);
+
 sub Parse {
     my($what)  = @_;
     $chan      = undef;
@@ -38,7 +40,7 @@ sub Parse {
     }
 
     if (defined $what and $what =~ s/^($::mask{chan})\s*//) {
-       # todo: check if the channel exists aswell.
+       # TODO: check if the channel exists aswell.
        $chan   = lc $1;
 
        if (!&::IsNickInChan($who, $chan)) {
@@ -116,8 +118,8 @@ sub Parse {
     } elsif ($what =~ /^(un)?notify$/i) {
        my $state = ($1) ? 0 : 1;
 
-       # todo: don't notify even if "news" is called.
-       if (!&::IsChanConf("newsNotifyAll")) {
+       # TODO: don't notify even if "News" is called.
+       if (&::IsChanConf("newsNotifyAll") <= 0) {
            &::DEBUG("news: chan => $chan, ::chan => $::chan.");
            &::notice($who, "not available for this channel or disabled altogether.");
            return;
@@ -165,7 +167,7 @@ sub readNews {
     while (<NEWS>) {
        chop;
 
-       # todo: allow commands.
+       # TODO: allow commands.
 
        if (/^[\s\t]+(\S+):[\s\t]+(.*)$/) {
            if (!defined $item) {
@@ -216,7 +218,7 @@ sub writeNews {
        return;
     }
 
-    # todo: add commands to output file.
+    # TODO: add commands to output file.
     my $c = 0;
     my($cc,$ci,$cu) = (0,0,0);
 
@@ -225,6 +227,7 @@ sub writeNews {
        $c = scalar keys %{ $::news{$chan} };
        next unless ($c);
        $cc++;
+       my $item;
 
        foreach $item (sort keys %{ $::news{$chan} }) {
            $c = scalar keys %{ $::news{$chan}{$item} };
@@ -232,6 +235,7 @@ sub writeNews {
            $ci++;
 
            print NEWS "$chan $item\n";
+           my $what;
            foreach $what (sort keys %{ $::news{$chan}{$item} }) {
                print NEWS "    $what: $::news{$chan}{$item}{$what}\n";
            }
@@ -239,7 +243,7 @@ sub writeNews {
        }
     }
 
-    # todo: show how many users we wrote down.
+    # TODO: show how many users we wrote down.
     if (&::getChanConfList("newsKeepRead")) {
        # old users are removed in newsFlush(), perhaps it should be
        # done here.
@@ -277,17 +281,17 @@ sub add {
     }
 
     $::news{$chan}{$str}{Time} = time();
-    my $expire = &::getChanConfDefault("newsDefaultExpire",7);
+    my $expire = &::getChanConfDefault("newsDefaultExpire",7, $chan);
     $::news{$chan}{$str}{Expire}       = time() + $expire*60*60*24;
     $::news{$chan}{$str}{Author}       = $::who;       # case!
 
     my $agestr = &::Time2String($::news{$chan}{$str}{Expire} - time() );
     my $item   = &newsS2N($str);
-    &::notice($who, "Added '\037$str\037' at [".localtime(time).
+    &::notice($who, "Added '\037$str\037' at [".gmtime(time).
                "] by \002$::who\002 for item #\002$item\002.");
     &::notice($who, "Now do 'news text $item <your_description>'");
     &::notice($who, "This item will expire at \002".
-       localtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
+       gmtime($::news{$chan}{$str}{Expire})."\002 [$agestr from now] "
     );
 
     &writeNews();
@@ -349,7 +353,7 @@ sub del {
        $auth++ if (&::IsFlag("o"));
 
        if (!$auth) {
-           # todo: show when it'll expire.
+           # TODO: show when it'll expire.
            &::notice($who, "Sorry, you cannot remove items; just let them expire on their own.");
            return;
        }
@@ -363,11 +367,11 @@ sub del {
 
 sub list {
     if (!scalar keys %{ $::news{$chan} }) {
-       &::notice($who, "No News for \002$chan\002.");
+       &::notice($who, "No news for \002$chan\002.");
        return;
     }
 
-    if (&::IsChanConf("newsKeepRead")) {
+    if (&::IsChanConf("newsKeepRead") > 0) {
        my $x = $::newsuser{$chan}{$who};
 
        if (defined $x and ($x == 0 or $x == -1)) {
@@ -393,7 +397,9 @@ sub list {
        $newest = $t if ($t > $newest);
        if ($e > 1 and $e < $expire) {
            $expire     = $e;
-           $eno        = &newsS2N($item);
+           &::DEBUG("before newsS2N($_)");
+           $eno        = &newsS2N($_);
+           &::DEBUG("after newsS2N($_) == $eno");
        }
     }
     my $timestr = &::Time2String(time() - $newest);
@@ -414,7 +420,7 @@ sub list {
            next;
        }
 
-       # todo: show request stats aswell.
+       # TODO: show request stats aswell.
        &::msg($who, sprintf("\002[\002%2d\002]\002%s %s",
                                $i, $chr, $subtopic));
        $i++;
@@ -440,13 +446,13 @@ sub read {
     }
 
     if (!scalar keys %{ $::news{$chan} }) {
-       &::notice($who, "No News for \002$chan\002.");
+       &::notice($who, "No news for \002$chan\002.");
        return;
     }
 
     my $item   = &getNewsItem($str);
     if (!defined $item or !scalar keys %{ $::news{$chan}{$item} }) {
-       # todo: numerical check.
+       # TODO: numerical check.
        if ($str =~ /^(\d+)[-, ](\d+)$/ or
            $str =~ /^-(\d+)$/ or
            $str =~ /^(\d+)-$/ or 0
@@ -464,7 +470,7 @@ sub read {
        return;
     }
 
-    my $t      = localtime( $::news{$chan}{$item}{Time} );
+    my $t      = gmtime( $::news{$chan}{$item}{Time} );
     my $a      = $::news{$chan}{$item}{Author};
     my $text   = $::news{$chan}{$item}{Text};
     my $num    = &newsS2N($item);
@@ -486,7 +492,7 @@ sub read {
     my $e;
     if ($_) {
        $e = sprintf("\037%s\037  [%s from now]",
-               scalar(localtime($_)),
+               scalar(gmtime($_)),
                &::Time2String($_ - time())
        );
     }
@@ -530,13 +536,13 @@ sub mod {
        }
 
        ### TODO: use m### to make code safe!
-       # todo: make code safer.
+       # TODO: make code safer.
        my $done = 0;
-       # todo: use eval to deal with flags easily.
+       # TODO: use eval to deal with flags easily.
        if ($flags eq "") {
            $done++ if (!$done and $mod_news  =~ s/\Q$op\E/$np/);
            $done++ if (!$done and $mod_nnews =~ s/\Q$op\E/$np/);
-        } elsif ($flags eq "g") {
+       } elsif ($flags eq "g") {
            $done++ if ($mod_news  =~ s/\Q$op\E/$np/g);
            $done++ if ($mod_nnews =~ s/\Q$op\E/$np/g);
        }
@@ -636,7 +642,7 @@ sub set {
     }
 
     if ($what eq "Expire") {
-       # todo: use do_set().
+       # TODO: use do_set().
 
        my $time = 0;
        my $plus = ($value =~ s/^\+//g);
@@ -674,7 +680,7 @@ sub set {
            return;
        } else {
            &::notice($who, "Set expire for \002$item\002, to ".
-               localtime($time) ." [".&::Time2String($time - time())."]" );
+               gmtime($time) ." [".&::Time2String($time - time())."]" );
 
            if (time() > $time) {
                &::DEBUG("news: hrm... time() > $time, should expire.");
@@ -700,12 +706,12 @@ sub set {
     }
 
     if (!$auth) {
-       # todo: show when it'll expire.
+       # TODO: show when it'll expire.
        &::notice($who, "Sorry, you cannot set items. (author $author owns it)");
        return;
     }
 
-    # todo: clean this up.
+    # TODO: clean this up.
     my $old = $::news{$chan}{$news}{$what};
     if (defined $old) {
        &::DEBUG("news: old => $old.");
@@ -715,13 +721,13 @@ sub set {
 }
 
 sub latest {
-    my($tchan, $flag) = @_;
+    my ($tchan, $flag) = @_;
 
-    # hack hack hack.
-    $chan ||= $tchan;
-    $who    = $::who;
+    # hack hack hack.  fix later.
+    $chan = $tchan;
+    $who  = $::who;
 
-    # todo: if chan = undefined, guess.
+    # TODO: if chan = undefined, guess.
 #    if (!exists $::news{$chan}) {
     if (!exists $::channels{$chan}) {
        &::notice($who, "invalid chan $chan") if ($flag);
@@ -745,15 +751,14 @@ sub latest {
     }
 
     $::chan    = $chan;
-    my $x = &::IsChanConf("newsNotifyAll");
-    if (&::IsChanConf("newsNotifyAll") and !defined $t) {
-       $t = 1;
-    }
+    return if (&::IsChanConf("newsNotifyAll") <= 0);
+
+    # I don't understand this code ;)
+    $t = 1 if (!defined $t);
 
     if (!defined $t) {
-       &::DEBUG("news: something went really wrong.");
-       &::DEBUG("news: chan => $chan, ::chan => $::chan");
-#      &::notice($who, "something went really wrong.");
+#      &::msg($who, "News is disabled for $chan");
+       &::DEBUG("news: $chan: something went really wrong.");
        return;
     }
 
@@ -786,10 +791,14 @@ sub latest {
     # scalar @new, !$flag
     my $unread = scalar @new;
     my $total  = scalar keys %{ $::news{$chan} };
+    if (!$flag && &::IsChanConf("newsTellUnread") <= 0) {
+       return;
+    }
+
     if (!$flag) {
        return unless ($unread);
 
-       # just a temporary measure not to flood ourself off the 
+       # just a temporary measure not to flood ourself off the
        # network with news until we get global notice() and msg()
        # throttling.
        if (time() - ($::cache{newsTime} || 0) < 5) {
@@ -821,20 +830,20 @@ sub latest {
            $i   = &newsS2N($_);
            $sorted[$i] = $_;
        }
-       
+
        for ($i=0; $i<=scalar(@sorted); $i++) {
            my $news = $sorted[$i];
            next unless (defined $news);
 
-           my $age = time() - $::news{$chan}{$news}{Time};
+#          my $age = time() - $::news{$chan}{$news}{Time};
+           my $msg = sprintf("\002[\002%2d\002]\002 %s", $i, $news);
+###                    $i, $_, &::Time2String($age)
            $::conn->schedule(int((2+$i)/2), sub {
-               &::notice($who, sprintf("\002[\002%2d\002]\002 %s",
-                       $i, $news) );
-#                      $i, $_, &::Time2String($age) ) );
+               &::notice($who, $msg);
            } );
        }
 
-       # todo: implement throttling via schedule into &notice() / &msg().
+       # TODO: implement throttling via schedule into &notice() / &msg().
        $::conn->schedule(int((2+$i)/2), sub {
            &::notice($who, "|= to read, do \002news $chan read <#>\002 or \002news $chan read <keyword>\002");
        } );
@@ -898,6 +907,8 @@ sub getNewsItem {
     my($what)  = @_;
     my $item   = 0;
 
+    $what =~ s/^\#//;  # '#1' for example.
+
     my %time;
     foreach (keys %{ $::news{$chan} }) {
        my $t = $::news{$chan}{$_}{Time};
@@ -977,7 +988,7 @@ sub do_set {
        return;
     }
 
-    &::DEBUG("news: do_set: TODO...");
+    &::TODO("news: do_set:");
 }
 
 sub stats {