Mercurial > hg > egg-tcls
comparison tj.tcl @ 644:77c9feb5dcee
tj: Add numerous comments.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 16 Feb 2021 13:06:50 +0200 |
parents | 5cbff6baebef |
children | cf62cdc44568 |
comparison
equal
deleted
inserted
replaced
643:1b743b80524b | 644:77c9feb5dcee |
---|---|
245 set rdesc "*" | 245 set rdesc "*" |
246 if {![tj_get_id $rarg rindex uid]} { | 246 if {![tj_get_id $rarg rindex uid]} { |
247 set uid [tj_get_default_id $uhand] | 247 set uid [tj_get_default_id $uhand] |
248 } | 248 } |
249 | 249 |
250 # Do we have any arguments left? | |
250 if {![utl_arg_get $rarglist rindex rarg 1]} { | 251 if {![utl_arg_get $rarglist rindex rarg 1]} { |
251 tj_msg $upublic $unick $uchan "help_set" | 252 tj_msg $upublic $unick $uchan "help_set" |
252 return 1 | 253 return 1 |
253 } | 254 } |
254 | 255 |
274 | 275 |
275 } elseif {[regexp {(\+|\-)?(\d+)} $rarg -> dsign rdays]} { | 276 } elseif {[regexp {(\+|\-)?(\d+)} $rarg -> dsign rdays]} { |
276 # Check for days | 277 # Check for days |
277 if {$dsign == "-"} { set rdays [expr -$rdays] } | 278 if {$dsign == "-"} { set rdays [expr -$rdays] } |
278 | 279 |
280 # Check for hours | |
279 set rhours 0 | 281 set rhours 0 |
280 if {[utl_arg_get $rarglist rindex rarg 0]} { | 282 if {[utl_arg_get $rarglist rindex rarg 0]} { |
281 if {[regexp {(\+|\-)?(\d+)} $rarg -> hsign rhours]} { | 283 if {[regexp {(\+|\-)?(\d+)} $rarg -> hsign rhours]} { |
282 if {$hsign == "-"} { set rhours [expr -$rhours] } | 284 if {$hsign == "-"} { set rhours [expr -$rhours] } |
283 incr rindex | 285 incr rindex |
284 } | 286 } |
285 | 287 |
288 # Get description, if any | |
286 tj_arg_rest $rarglist $rindex rdesc | 289 tj_arg_rest $rarglist $rindex rdesc |
287 | 290 |
291 # Validate hours parameters a bit | |
288 if {$rhours < -24 || $rhours > 24} { | 292 if {$rhours < -24 || $rhours > 24} { |
289 tj_msg $upublic $unick $uchan "err_invalid_hours" [list $rhours] | 293 tj_msg $upublic $unick $uchan "err_invalid_hours" [list $rhours] |
290 return 1 | 294 return 1 |
291 } | 295 } |
292 } | 296 } |
293 | 297 |
294 # Compute target timestamp | 298 # Compute target timestamp |
295 set rstamp [expr $uclock + ($rdays * 24 * 60 * 60) + ($rhours * 60 * 60)] | 299 set rstamp [expr $uclock + ($rdays * 24 * 60 * 60) + ($rhours * 60 * 60)] |
296 } else { | 300 } else { |
297 # Only description, if any | 301 # Only description was specified |
298 incr rindex -1 | 302 incr rindex -1 |
299 tj_arg_rest $rarglist $rindex rdesc | 303 tj_arg_rest $rarglist $rindex rdesc |
300 set rstamp "invalid" | 304 set rstamp "invalid" |
301 } | 305 } |
302 | 306 |
306 set usql "SELECT * FROM tj WHERE uid LIKE '[utl_escape $uid]'" | 310 set usql "SELECT * FROM tj WHERE uid LIKE '[utl_escape $uid]'" |
307 tj_dbh eval $usql { | 311 tj_dbh eval $usql { |
308 set umode 0 | 312 set umode 0 |
309 set cid $id | 313 set cid $id |
310 set ctarget $utarget | 314 set ctarget $utarget |
315 | |
316 # If description has not been set, fetch previous | |
311 if {$rdesc == "*"} { | 317 if {$rdesc == "*"} { |
312 set rdesc $udesc | 318 set rdesc $udesc |
313 } | 319 } |
314 incr nres | 320 incr nres |
315 } | 321 } |
316 | 322 |
317 # Check for DB sanity | 323 # Check for DB sanity at this point |
318 if {$nres > 1} { | 324 if {$nres > 1} { |
325 # If we have more than one result for this ID, there's been | |
326 # some kind of mistake at some point. | |
319 tj_msg $upublic $unick $uchan "err_db_corrupt" [list $nres] | 327 tj_msg $upublic $unick $uchan "err_db_corrupt" [list $nres] |
320 tj_log "too many $nres fatal error piip" | 328 tj_log "too many $nres fatal error piip" |
321 return 1 | 329 return 1 |
322 } | 330 } |
323 | 331 |
324 # Check if we are trying to add a new entry without valid timestamp | 332 # Check if we are trying to add a new entry without valid timestamp |
325 if {$rstamp == "invalid"} { | 333 if {$rstamp == "invalid"} { |
334 # Yes, if mode is "new", we should error out | |
326 if {$umode} { | 335 if {$umode} { |
327 tj_msg $upublic $unick $uchan "err_missing_timestamp" | 336 tj_msg $upublic $unick $uchan "err_missing_timestamp" |
328 return 1 | 337 return 1 |
329 } | 338 } |
330 | 339 |
340 # Otherwise we are updating, so just use the old timestamp | |
331 set rdate $ctarget | 341 set rdate $ctarget |
332 set ustamp [utl_sql_datetime_to_stamp $ctarget] | 342 set ustamp [utl_sql_datetime_to_stamp $ctarget] |
333 } else { | 343 } else { |
344 # Timestamp was okay | |
334 set rdate [utl_sql_stamp_to_datetime $rstamp] | 345 set rdate [utl_sql_stamp_to_datetime $rstamp] |
335 set ustamp $rstamp | 346 set ustamp $rstamp |
336 } | 347 } |
337 | 348 |
349 # If description has not been set, use default | |
338 if {$rdesc == "*"} { | 350 if {$rdesc == "*"} { |
339 set rdesc $tj_default_desc | 351 set rdesc $tj_default_desc |
340 } | 352 } |
341 | 353 |
342 set utjstr [tj_get_tj_str_delta $ustamp $uclock] | 354 set utjstr [tj_get_tj_str_delta $ustamp $uclock] |
343 set udate [tj_ctime $ustamp] | 355 set udate [tj_ctime $ustamp] |
344 | 356 |
345 # Max reminders .. | 357 # Check against max reminders .. |
346 set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uhand]'" | 358 set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uhand]'" |
347 if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} { | 359 if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} { |
348 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] | 360 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] |
349 tj_log "$uerrmsg on SQL:\n$usql" | 361 tj_log "$uerrmsg on SQL:\n$usql" |
350 return 1 | 362 return 1 |
351 } | 363 } |
352 | 364 |
353 # Check for maximum reminders | |
354 if {$nitems >= $tj_max_items} { | 365 if {$nitems >= $tj_max_items} { |
366 # User has too many set already | |
355 tj_msg $upublic $unick $uchan "err_too_many" [list $nitems $tj_max_items] | 367 tj_msg $upublic $unick $uchan "err_too_many" [list $nitems $tj_max_items] |
356 return 1 | 368 return 1 |
357 } | 369 } |
358 | 370 |
359 set ucreated [utl_sql_stamp_to_datetime $uclock] | 371 set ucreated [utl_sql_stamp_to_datetime $uclock] |
387 tj_log "$uerrmsg on SQL:\n$usql" | 399 tj_log "$uerrmsg on SQL:\n$usql" |
388 return 1 | 400 return 1 |
389 } | 401 } |
390 } | 402 } |
391 | 403 |
404 # Do a query | |
392 set usql "SELECT * FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'" | 405 set usql "SELECT * FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'" |
393 tj_dbh eval $usql { | 406 tj_dbh eval $usql { |
394 set utjstr [tj_get_tj_str_delta [utl_sql_datetime_to_stamp $utarget] $uclock] | 407 set utjstr [tj_get_tj_str_delta [utl_sql_datetime_to_stamp $utarget] $uclock] |
395 tj_msg $upublic $unick $uchan "tj_updated" [list $id $uid $uhand $udesc $udate $utjstr] | 408 tj_msg $upublic $unick $uchan "tj_updated" [list $id $uid $uhand $udesc $udate $utjstr] |
396 tj_log "tj_updated $id:$uid:$uhand:$udesc:$utarget" | 409 tj_log "tj_updated $id:$uid:$uhand:$udesc:$utarget" |
397 } | 410 } |
398 } | 411 } |
399 | 412 |
400 } elseif {[tj_cmd_match "remind" $rcmd]} { | 413 } elseif {[tj_cmd_match "remind" $rcmd]} { |
414 # XXX TODO MAYBE .. reminder functionality .. perhaps some day. | |
401 # !tj remind #<id> <<dd.mm.yyyy [hh:mm]>|<message>> | 415 # !tj remind #<id> <<dd.mm.yyyy [hh:mm]>|<message>> |
402 # !tj remind #<id> <<dd.mm [hh:mm]>|<message>> | 416 # !tj remind #<id> <<dd.mm [hh:mm]>|<message>> |
403 if {[llength $rarglist] < 3} { | 417 if {[llength $rarglist] < 3} { |
404 tj_msg $upublic $unick $uchan "help_add" | 418 tj_msg $upublic $unick $uchan "help_add" |
405 return 1 | 419 return 1 |
412 set uname [tj_correct_handle $uname] | 426 set uname [tj_correct_handle $uname] |
413 } else { | 427 } else { |
414 set uname $uhand | 428 set uname $uhand |
415 } | 429 } |
416 | 430 |
431 # First, get count of items | |
417 set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uname]'" | 432 set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uname]'" |
418 if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} { | 433 if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} { |
419 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] | 434 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] |
420 tj_log "$uerrmsg on SQL:\n$usql" | 435 tj_log "$uerrmsg on SQL:\n$usql" |
421 return 1 | 436 return 1 |
422 } | 437 } |
423 | 438 |
439 # Then, list items | |
424 set nitem 0 | 440 set nitem 0 |
425 set usql "SELECT * FROM tj WHERE uuser='[utl_escape $uname]' ORDER BY ucreated" | 441 set usql "SELECT * FROM tj WHERE uuser='[utl_escape $uname]' ORDER BY ucreated" |
426 tj_dbh eval $usql { | 442 tj_dbh eval $usql { |
427 incr nitem | 443 incr nitem |
428 set qtarget [utl_sql_datetime_to_stamp $utarget] | 444 set qtarget [utl_sql_datetime_to_stamp $utarget] |
439 if {![utl_arg_get $rarglist rindex rarg 0] || ![tj_get_id $rarg rindex uid]} { | 455 if {![utl_arg_get $rarglist rindex rarg 0] || ![tj_get_id $rarg rindex uid]} { |
440 tj_msg $upublic $unick $uchan "help_delete" | 456 tj_msg $upublic $unick $uchan "help_delete" |
441 return 1 | 457 return 1 |
442 } | 458 } |
443 | 459 |
460 # Check if the desired item exists | |
444 set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'" | 461 set usql "SELECT COUNT(*) FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'" |
445 if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} { | 462 if {[catch {set nitems [tj_dbh onecolumn $usql]} uerrmsg]} { |
446 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] | 463 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] |
447 tj_log "$uerrmsg on SQL:\n$usql" | 464 tj_log "$uerrmsg on SQL:\n$usql" |
448 return 1 | 465 return 1 |
449 } | 466 } |
450 | 467 |
451 if {$nitems == 0} { | 468 if {$nitems == 0} { |
469 # No, error out | |
452 tj_msg $upublic $unick $uchan "err_no_such_id" [list $uid] | 470 tj_msg $upublic $unick $uchan "err_no_such_id" [list $uid] |
453 return 1 | 471 return 1 |
454 } | 472 } |
455 | 473 |
474 # Delete it | |
456 set usql "DELETE FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'" | 475 set usql "DELETE FROM tj WHERE uuser='[utl_escape $uhand]' AND uid LIKE '[utl_escape $uid]'" |
457 if {[catch {set ndone [tj_dbh onecolumn $usql]} uerrmsg]} { | 476 if {[catch {set ndone [tj_dbh onecolumn $usql]} uerrmsg]} { |
458 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] | 477 tj_msg $upublic $unick $uchan "err_sql" [list $uerrmsg] |
459 tj_log "$uerrmsg on SQL:\n$usql" | 478 tj_log "$uerrmsg on SQL:\n$usql" |
460 return 1 | 479 return 1 |