aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB. Watson <yalhcru@gmail.com>2018-08-28 22:44:25 -0400
committerB. Watson <yalhcru@gmail.com>2018-08-28 22:44:25 -0400
commit2c000060858e03dccde02d5e8454b95de0caaedc (patch)
treedad2247d5de1b8d2cf19484c15a20dd8f4ff9aef
parent5d86ccd8cf07f1afac8734112635bdd200bb572c (diff)
downloadmisc-scripts-2c000060858e03dccde02d5e8454b95de0caaedc.tar.gz
slacktopic.pl: only cache last result. add a way to manually force-set the date.
-rw-r--r--slacktopic.pl51
1 files changed, 37 insertions, 14 deletions
diff --git a/slacktopic.pl b/slacktopic.pl
index 256db09..a44ba5e 100644
--- a/slacktopic.pl
+++ b/slacktopic.pl
@@ -20,9 +20,9 @@
# Only the date (inside []) is changed, all the other stuff is left as-is.
# Assumptions made:
-# - Every new ChangeLog entry is a security fix. This is almost
-# 100% true for stable releases (which is what we track), and
-# the only other updates will be fixes for major regressions...
+# - Pat won't be updating the ChangeLog several times in the same
+# minute or so. If he did, we might get confused about whether
+# or not an update is a security fix update.
# - Client is set to autojoin ##slackware, or else the user will
# always manually join it. Script doesn't do anything until
# this happens.
@@ -178,7 +178,8 @@ sub init {
# This gets called any time an /exec finished.
signal_add_last("exec remove", "finish_update");
- # Command for manual updates.
+ # Command for manual update checks (without argument), or
+ # forcing the date (with an argument).
command_bind("slacktopic", "start_update");
# Check once at script load.
@@ -229,7 +230,16 @@ sub initial_update {
# Start the update process.
sub start_update {
- debugmsg("start_update() called");
+ my $force_date = shift || 0;
+ debugmsg("start_update() called, force_date==$force_date");
+
+ if($force_date) {
+ if($force_date !~ /^\d\d\d\d-\d\d-\d\d$/) {
+ errmsg("Invalid date '$force_date'");
+ } else {
+ set_topic_date(get_channel(), $force_date, 1);
+ }
+ }
# Don't do anything if we're not joined to the channel already.
exec_update() if get_channel();
@@ -275,8 +285,14 @@ sub finish_update {
return;
}
+ set_topic_date($chan, $new_date, 0);
+}
+
+sub set_topic_date {
+ my ($chan, $new_date, $force) = @_;
+
# Get old topic, replace the date with the new one.
- debugmsg("\$new_date is: $new_date");
+ debugmsg("set_topic_date() called, \$new_date is: $new_date");
my $t = $chan->{topic};
unless($t =~ s,\[\d\d\d\d-\d\d-\d\d\],[$new_date],) {
errmsg("topic doesn't contain [yyyy-mm-dd] date, fix it manually");
@@ -290,7 +306,9 @@ sub finish_update {
}
# Make sure this is a security fix update.
- return unless is_security_update($new_date);
+ if(!$force) {
+ return unless is_security_update($new_date);
+ }
# Ask ChanServ to change the topic for us. We don't need +o in
# the channel, so long as we're logged in to services and have +t.
@@ -325,23 +343,28 @@ sub exec_update {
0); # last arg is unused
}
-# Without %sec_check_cache, every non-security update would result in us
-# wasting bandwidth rechecking the ChangeLog every $update_frequency sec.
-our %sec_check_cache = ();
+# Without caching the last result, every non-security update would result
+# in us wasting bandwidth rechecking the ChangeLog every $update_frequency
+# sec.
+our $sup_last_date = "";
+our $sup_last_result;
# Return true if the first ChangeLog entry is a security update. Unlike
# the regular check-for-update that happens periodically, this one blocks
# for up to $cmd_timeout seconds. The updates only happen every few days,
# I don't see this as a real problem that needs extra complexity to solve.
+# Notice we don't check the $date argument against the date read from
+# the file.
sub is_security_update {
my $date = shift;
debugmsg("is_security_update($date) called");
- if(exists($sec_check_cache{$date})) {
- debugmsg("already checked & got '$sec_check_cache{$date}' for $date");
- return $sec_check_cache{$date};
+ if($date eq $sup_last_date) {
+ debugmsg("already checked & got '$sup_last_result' for $date");
+ return $sup_last_result;
}
+ $sup_last_date = $date;
debugmsg("getting start of ChangeLog");
my $result = 0;
@@ -377,7 +400,7 @@ sub is_security_update {
}
debugmsg("read $lines lines from ChangeLog, returning $result");
- $sec_check_cache{$date} = $result;
+ $sup_last_result = $result;
return $result;
}