changeset 644:77c9feb5dcee

tj: Add numerous comments.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 16 Feb 2021 13:06:50 +0200
parents 1b743b80524b
children 6a56df3437d1
files tj.tcl
diffstat 1 files changed, 23 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/tj.tcl	Tue Feb 16 13:01:29 2021 +0200
+++ b/tj.tcl	Tue Feb 16 13:06:50 2021 +0200
@@ -247,6 +247,7 @@
       set uid [tj_get_default_id $uhand]
     }
 
+    # Do we have any arguments left?
     if {![utl_arg_get $rarglist rindex rarg 1]} {
       tj_msg $upublic $unick $uchan "help_set"
       return 1
@@ -276,6 +277,7 @@
       # Check for days
       if {$dsign == "-"} { set rdays [expr -$rdays] }
 
+      # Check for hours
       set rhours 0
       if {[utl_arg_get $rarglist rindex rarg 0]} {
         if {[regexp {(\+|\-)?(\d+)} $rarg -> hsign rhours]} {
@@ -283,8 +285,10 @@
           incr rindex
         }
 
+        # Get description, if any
         tj_arg_rest $rarglist $rindex rdesc
 
+        # Validate hours parameters a bit
         if {$rhours < -24 || $rhours > 24} {
           tj_msg $upublic $unick $uchan "err_invalid_hours" [list $rhours]
           return 1
@@ -294,7 +298,7 @@
       # Compute target timestamp
       set rstamp [expr $uclock + ($rdays * 24 * 60 * 60) + ($rhours * 60 * 60)]
     } else {
-      # Only description, if any
+      # Only description was specified
       incr rindex -1
       tj_arg_rest $rarglist $rindex rdesc
       set rstamp "invalid"
@@ -308,14 +312,18 @@
       set umode 0
       set cid $id
       set ctarget $utarget
+
+      # If description has not been set, fetch previous
       if {$rdesc == "*"} {
         set rdesc $udesc
       }
       incr nres
     }
 
-    # Check for DB sanity
+    # Check for DB sanity at this point
     if {$nres > 1} {
+      # If we have more than one result for this ID, there's been
+      # some kind of mistake at some point.
       tj_msg $upublic $unick $uchan "err_db_corrupt" [list $nres]
       tj_log "too many $nres fatal error piip"
       return 1
@@ -323,18 +331,22 @@
 
     # Check if we are trying to add a new entry without valid timestamp
     if {$rstamp == "invalid"} {
+      # Yes, if mode is "new", we should error out
       if {$umode} {
         tj_msg $upublic $unick $uchan "err_missing_timestamp"
         return 1
       }
 
+      # Otherwise we are updating, so just use the old timestamp
       set rdate $ctarget
       set ustamp [utl_sql_datetime_to_stamp $ctarget]
     } else {
+      # Timestamp was okay
       set rdate [utl_sql_stamp_to_datetime $rstamp]
       set ustamp $rstamp
     }
 
+    # If description has not been set, use default
     if {$rdesc == "*"} {
       set rdesc $tj_default_desc
     }
@@ -342,7 +354,7 @@
     set utjstr [tj_get_tj_str_delta $ustamp $uclock]
     set udate [tj_ctime $ustamp]
 
-    # Max reminders ..
+    # Check against max reminders ..
     set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uhand]'"
     if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} {
       tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg]
@@ -350,8 +362,8 @@
       return 1
     }
 
-    # Check for maximum reminders
     if {$nitems >= $tj_max_items} {
+      # User has too many set already
       tj_msg $upublic $unick $uchan "err_too_many" [list $nitems $tj_max_items]
       return 1
     }
@@ -389,6 +401,7 @@
         }
       }
 
+      # Do a query
       set usql "SELECT * FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'"
       tj_dbh eval $usql {
         set utjstr [tj_get_tj_str_delta [utl_sql_datetime_to_stamp $utarget] $uclock]
@@ -398,6 +411,7 @@
     }
 
   } elseif {[tj_cmd_match "remind" $rcmd]} {
+    # XXX TODO MAYBE .. reminder functionality .. perhaps some day.
     # !tj remind #<id> <<dd.mm.yyyy [hh:mm]>|<message>>
     # !tj remind #<id> <<dd.mm [hh:mm]>|<message>>
     if {[llength $rarglist] < 3} {
@@ -414,6 +428,7 @@
       set uname $uhand
     }
 
+    # First, get count of items
     set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uname]'"
     if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} {
       tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg]
@@ -421,6 +436,7 @@
       return 1
     }
 
+    # Then, list items
     set nitem 0
     set usql "SELECT * FROM tj WHERE uuser='[utl_escape $uname]' ORDER BY ucreated"
     tj_dbh eval $usql {
@@ -441,6 +457,7 @@
       return 1
     }
 
+    # Check if the desired item exists
     set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'"
     if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} {
       tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg]
@@ -449,10 +466,12 @@
     }
 
     if {$nitems == 0} {
+      # No, error out
       tj_msg $upublic $unick $uchan "err_no_such_id" [list $uid]
       return 1
     }
 
+    # Delete it
     set usql "DELETE FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'"
     if {[catch {set ndone [tj_dbh onecolumn $usql]} uerrmsg]} {
       tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg]