[Tkabber-dev] r260 - trunk/plugins/reminder

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Tue Dec 29 05:01:40 MSK 2009


Author: bigote
Date: 2009-12-29 05:01:40 +0300 (Tue, 29 Dec 2009)
New Revision: 260

Modified:
   trunk/plugins/reminder/reminder.tcl
Log:
Timeout parser's been rewritten and seems to be neater and more correct now.
"/remind help" command added that prints out examples of usage.


Modified: trunk/plugins/reminder/reminder.tcl
===================================================================
--- trunk/plugins/reminder/reminder.tcl	2009-12-28 03:16:19 UTC (rev 259)
+++ trunk/plugins/reminder/reminder.tcl	2009-12-29 02:01:40 UTC (rev 260)
@@ -1,44 +1,16 @@
-# "Reminder" plugin for Tkabber. 2009-12-28 v. 0.4
+# "Reminder" plugin for Tkabber. 2009-12-29 v. 0.5
 # Written by Serge Yudin xmpp:bigote at jabber.ru
 # Its purpose is to remind about your contact birthdays automatically
 # and about some tasks that you assign by means of "/remind" command.
 #
-# USAGE (note the difference between seconds, minutes and hours):
+# USAGE
+# Type "/remind help" when being in Tkabber
+# either take a look at reminder::help procedure here.
 #####################################
-#/remind 180
-#Turn off boiling eggs
-# This will remind you in three minutes that your eggs
-# are probably ready :)
-#####################################
-#/remind 30:15
-#Go to the work
-# This will remind you in 30 min 15 sec that you should go to your work.
-# Please note that seconds value should not exceed 60.
-#####################################
-#/remind 1:2:05
-#Call my Dad
-# Will remind you in an hour, 2 min and 5 sec to call to your Dad.
-# Please note that minutes and/or seconds value should not exceed 60.
-#####################################
-#/remind tomorrow 10:30:00
-#Call to my office.
-# It is a _relative_ timeout, not an absolute time.
-# Also supported some other words like "day", "week", "now", "next", etc.
-# Should be used with caution. Quite buggy. You can get warnings in some cases.
-# TODO: Fix it or drop it?
-#####################################
-#/remind 12/25/2009 10:00:00
-#Merry Christmas!
-# Sets absolute date and time for a reminder.
-# Please note the date must be in the format MM/DD/YYYY HH:MM:SS
-# Otherwise, there could be errors or wrong timing calculation.
-# TODO: Think about saving and restoring such reminders.
-#####################################
-#/listrems
-# This will show you a list of all running reminders you have set.
-#####################################
 #
 # TODO: Add a possibility to delete some reminders.
+# TODO: Think about saving and restoring long term reminders.
+# TODO: Add some sound reminder notification.
 # TODO: Birthday reminding isn't ready yet, at all.
 #
 
@@ -116,23 +88,26 @@
     }
     switch -- $cmd {
         /remind {
-            if {[string match */* $timeout] || [string match *:* $timeout] \
-             || [string match *\ * $timeout]} {
-                set timeout [[namespace current]::parse_timeout $timeout]
-            }
+            set timeout [[namespace current]::parse_timeout $timeout $chatid]
             set group [chat::get_jid $chatid]
             if {$timeout == -1} {
                 chat::add_message $chatid $group error "Wrong timeout format.\
                 Should be H\[H\]:MM:SS or M\[M\]:SS or S\[S\]" {}
                 return stop
             } elseif {$timeout == -2} {
-                chat::add_message $chatid $group error "Wrong date format.\
-                Must be MM/DD/YYYY HH:MM:SS" {}
+                chat::add_message $chatid $group error "Wrong timeout keyword.\
+                Must be one of: year, month, fortnight, week, day, hour,\
+                min (minute), tomorrow." {}
                 return stop
-            } elseif {$timeout < -2} {
-                chat::add_message $chatid $group error "It seems that you have set\
-                your reminder to some date in the past." {}
+            } elseif {$timeout == -3} {
+                chat::add_message $chatid $group error "You've set something really odd ;-)" {}
                 return stop
+            } elseif {[string equal -nocase $timeout help]} {
+                return stop
+            } elseif {$timeout < -3} {
+                chat::add_message $chatid $group error \
+                "It seems that you have set your reminder to some date in the past." {}
+                return stop
             }
             set timeout [string trimleft $timeout 0]
             set rid [after [expr {1000 * $timeout}] \
@@ -188,54 +163,99 @@
     set rem_list $rl_1
 }
 
-proc reminder::parse_timeout {to} {
-    switch -- [llength [split $to \ ]] {
-        1 {
-            if {[string match *:* $to]} {
-                set time [split $to :]
-                set to [lindex $time 0]
-                for {set i 1} {$i < [llength $time]} {incr i} {
-                    if {[string trimleft [lindex $time $i] 0] >= 60} {
-                        return -1
-                    }
-                    set to [expr {[string trimleft $to 0] * 60 + \
-                    [string trimleft [lindex $time $i] 0]}]
-                }
-            } elseif {[string match */* $to]} {
-                set to [clock scan "$to" -base 0 -gmt 0]
-                set to [expr $to - [clock seconds]]
-            } else {
-                return -2
-            }
-        }
-        2 {
-# Had to set -gmt 0 to avoid 1 hour shift
-# although if using only time scanning (without date) it should be -gmt 1
+proc reminder::parse_timeout {to chatid} {
+# Absolute date (and time). We don't check ONLY time here.
+    if {[string match */* $to]} {
+# I had to set -gmt 0 to avoid 1 hour shift.
 # I have to avoid using -format to be compatible with tcl8.4
-            set tmp [clock scan [join "$to"] -base 0 -gmt 0]
+        set tmp [clock scan [join "$to"] -base 0 -gmt 0]
 # So, when scanning date, it gives an absolute value,
 # so I have to calculate a timeout to pass it along
 # (which will be recalculated to an absolute value again :D)
-            if {[string match */* $to]} {
-                set to [expr $tmp - [clock seconds]]
-            } elseif  {[string match *year* $to] || [string match *month* $to]
-                    || [string match *week* $to] || [string match *day* $to]
-                    || [string match *hour* $to] || [string match *min* $to]
-                    || [string match *fortnight* $to] || [string match *sec* $to]
-                    || [string match *tomorrow* $to] || [string match *yesterday* $to]
-                    || [string match *today* $to] || [string match *now* $to]
-                    || [string match *last* $to] || [string match *this* $to]
-                    || [string match *next* $to] || [string match *ago* $to]} {
-                set to $tmp
-                puts $to
+        set to [expr $tmp - [clock seconds]]
+# Relative timeout such as 32:20:00 (32 hours 20 min) or 15:30 (15 min 30 sec).
+# We don't use clock scan here, as it doesn't accept hours value more than 24.
+    } elseif {[string match *:* $to] && ![string match *\ * $to]} {
+        foreach t [split $to :] {
+            set t [string trimleft $t]
+            if {$t == {}} {
+                set t 0
             }
+            lappend time $t
         }
-        3 {
-            set to [clock scan [join "$to"] -base 0 -gmt 0]
+        set to [lindex $time 0]
+        for {set i 1} {$i < [llength $time]} {incr i} {
+            if {[string trimleft [lindex $time $i] 0] >= 60} {
+                return -1
+            }
+            set to [expr {$to * 60 + [lindex $time $i]}]
         }
-        default {
-            return -2
+# Relative timeouts like "tomorrow 1:00:00" (in 25 hours)
+# or "2 years" (in 2 years), "fortnight 00:00:30" (in 2 weeks 30 sec), etc.
+# -gmt 1 should be used to avoid 1 hour shift.
+    } elseif {[string match *\ * $to]} {
+        set res [catch {clock scan "$to" -base 0 -gmt 1} to]
+        if {$res} {
+            set to -2
         }
+# A single word, either digital timeout or a word like "tomorrow" (in 24 hours).
+    } elseif {![string match *\ * $to]} {
+# Allowed timeout keywords.
+        if {[string match *year* $to] || [string match *month* $to]
+         || [string match *week* $to] || [string match *day* $to]
+         || [string match *hour* $to] || [string match *min* $to]
+         || [string match *fortnight* $to] || [string match *tomorrow* $to]} {
+            set res [catch {clock scan "$to" -base 0 -gmt 1} to]
+            if {$res} {
+                set to -2
+            }
+# If timeout is an integer, then it's in seconds.
+        } elseif {[string is integer $to]} {
+            set to [string trimleft $to 0]
+        } elseif {[string match *help* $to]} {
+            [namespace current]::help $chatid
+        } else {
+            set to -3
+        }
+    } else {
+        set to -3
     }
     return $to
 }
+
+proc reminder::help {chatid} {
+    set group [chat::get_jid $chatid]
+    lappend help_msg "\n/remind 180\
+        \nTurn off boiling eggs.\
+        \n\tThis will remind you in three minutes that your eggs are probably ready :)\
+        \n\n/remind 30:15\
+        \nGo to the work.\
+        \n\tThis will remind you in 30 min 15 sec that you should go to your work.\
+        \n\tPlease note that seconds value should not exceed 60.\
+        \n\n/remind 1:2:05\
+        \nCall my Dad.\
+        \n\tWill remind you in an hour, 2 min and 5 sec to call to your Dad.\
+        \n\tPlease note that minutes and/or seconds value should not exceed 60.\
+        \n\n/remind tomorrow 10:30:00\
+        \nCall to my office.\
+        \n\tIt is a _relative_ timeout, not absolute time.\
+        \n\tIn this case the reminder will shoot in 34 hours 30 minutes.\
+        \n\tKeywords: \'year\', \'month\', \'fortnight\', \'week\', \'day\', \'hour\', \'min (minute)\', \'tomorrow\'.\
+        \n\tShould be used with caution.\
+        \n\n/remind hour\
+        \nGo shopping.\
+        \n\tA short form to say 1:00:00.\
+        \n\n/remind 12/25/2009 10:00:00\
+        \nMerry Christmas!\
+        \n\tor\
+        \n/remind 12/25/2009\
+        \nMerry Christmas!\
+        \n\tSets an absolute date and time for a reminder.\
+        \n\tPlease note the date must be in the format MM/DD/YYYY HH:MM:SS\
+        \n\tOtherwise, there could be errors or wrong timing calculation.\
+        \n\n/listrems\
+        \n\tThis will show you a list of all running reminders you have set."
+    chat::add_message $chatid $group error "Some examples of using /remind command:" {}
+    chat::add_message $chatid $group info "[join $help_msg]" {}
+    return stop
+}



More information about the Tkabber-dev mailing list