tiiview.tcl (3206B)
1 #!/usr/bin/env tclsh 2 # tiiview: view ii/idec messages from the local text db 3 # Usage: tiiview.tcl [echo_name] [filter_string] [termwidth] [dbfile] 4 # Created by Luxferre in 2024, released into public domain 5 6 package require sqlite3 7 8 # basic text reflow helper 9 # list in, string out 10 proc tiiflow {lines width} { 11 set outtext {} 12 foreach inl $lines { 13 set l [string length $inl] 14 if {$l <= $width} { 15 append outtext "$inl\n" 16 } else { 17 set wordset "" 18 set words [split $inl " "] 19 foreach w $words { 20 set candidate "$wordset $w" 21 if {[string length $candidate] <= $width} { 22 set wordset $candidate 23 } else { 24 append outtext "$wordset\n" 25 set wordset $w 26 } 27 } 28 append outtext "$wordset\n" 29 } 30 } 31 return $outtext 32 } 33 34 # entry point 35 set scriptpath [file normalize [info script]] 36 set appdir [file dirname $scriptpath] 37 # check if we're running from a starpack 38 if [string match *app-tiiview $appdir] { 39 set appdir [file normalize [file join $appdir ".." ".." ".." ]] 40 } 41 set localdb [file join $appdir "tii.db"] 42 set echoname "" 43 set filterstr "" 44 set twidth 80 45 if {$argc > 0} { 46 if {$argc > 1} { 47 set filterstr [string trim [lindex $argv 1]] 48 } 49 if {$argc > 2} { 50 set twidth [expr {int([lindex $argv 2])}] 51 } 52 if {$argc > 3} { 53 set localdb [lindex $argv 3] 54 } 55 set echoname [string trim [lindex $argv 0]] 56 } 57 if {$twidth < 20} {set twidth 80} 58 if {$filterstr eq ""} {set filterstr "h0"} 59 # open the message db 60 sqlite3 msgdb $localdb -readonly true 61 if {$echoname eq ""} { # list the echonames 62 set echos [msgdb eval {SELECT DISTINCT `echoname` FROM `msg`;}] 63 puts [join [lsort $echos] "\n"] 64 } else { # fetch the actual contents 65 set filters [split $filterstr "/"] 66 set basicmod [string trim [lindex $filters 0]] 67 set filterregex {} 68 if {[llength $filters] > 1} { 69 set filterregex [string trim [lindex $filters 1]] 70 } 71 set doreverse 0 72 if {[string first r $basicmod] > -1} {set doreverse 1} 73 set numitems 0 74 if {[regexp {\d+} $basicmod foundnum]} {set numitems $foundnum} 75 set query {SELECT * FROM `msg` WHERE `echoname` = $echoname} 76 if {$filterregex ne {}} { 77 append query { AND (`body` LIKE $filterregex OR `subj` LIKE $filterregex) } 78 } 79 append query { ORDER BY `timestamp` } 80 if {$doreverse eq 1} {append query DESC} else {append query ASC} 81 if {$numitems > 0} {append query { LIMIT $numitems}} 82 append query ";" 83 msgdb eval $query msg { 84 set globalline [string repeat = $twidth] 85 set hdrline [string repeat - $twidth] 86 set tz "" 87 set renderedts "" 88 catch { # because some servers don't provide timestamps 89 set renderedts [clock format $msg(timestamp) -format {%Y-%m-%d %H:%M:%S} -timezone $tz] 90 } 91 catch { # because pipe can be broken anytime 92 puts "\[$renderedts\] $msg(msgid)" 93 puts "$msg(echoname) - $msg(msgfrom) ($msg(msgfromaddr)) to $msg(msgto)" 94 if {$msg(repto) ne ""} { 95 puts "Replied to: $msg(repto)" 96 } 97 puts "Subj: $msg(subj)" 98 puts $hdrline 99 set msg(body) [lmap s $msg(body) {string trimright $s}] 100 puts "[tiiflow $msg(body) $twidth]\n\n$globalline\n" 101 } 102 } 103 } 104 # close the db 105 msgdb close