#!/usr/bin/env tclsh # tiiview: view ii/idec messages from the local text db # Usage: tiiview.tcl [echo_name] [filter_string] [termwidth] [dbfile] # Created by Luxferre in 2024, released into public domain package require sqlite3 # basic text reflow helper # list in, string out proc tiiflow {lines width} { set outtext {} foreach inl $lines { set l [string length $inl] if {$l <= $width} { append outtext "$inl\n" } else { set wordset "" set words [split $inl " "] foreach w $words { set candidate "$wordset $w" if {[string length $candidate] <= $width} { set wordset $candidate } else { append outtext "$wordset\n" set wordset $w } } append outtext "$wordset\n" } } return $outtext } # entry point set scriptpath [file normalize [info script]] set appdir [file dirname $scriptpath] # check if we're running from a starpack if [string match *app-tiiview $appdir] { set appdir [file normalize [file join $appdir ".." ".." ".." ]] } set localdb [file join $appdir "tii.db"] set echoname "" set filterstr "" set twidth 80 if {$argc > 0} { if {$argc > 1} { set filterstr [string trim [lindex $argv 1]] } if {$argc > 2} { set twidth [expr {int([lindex $argv 2])}] } if {$argc > 3} { set localdb [lindex $argv 3] } set echoname [string trim [lindex $argv 0]] } if {$twidth < 20} {set twidth 80} if {$filterstr eq ""} {set filterstr "h0"} # open the message db sqlite3 msgdb $localdb -readonly true if {$echoname eq ""} { # list the echonames set echos [msgdb eval {SELECT DISTINCT `echoname` FROM `msg`;}] puts [join [lsort $echos] "\n"] } else { # fetch the actual contents set filters [split $filterstr "/"] set basicmod [string trim [lindex $filters 0]] set filterregex {} if {[llength $filters] > 1} { set filterregex [string trim [lindex $filters 1]] } set doreverse 0 if {[string first r $basicmod] > -1} {set doreverse 1} set numitems 0 if {[regexp {\d+} $basicmod foundnum]} {set numitems $foundnum} set query {SELECT * FROM `msg` WHERE `echoname` = $echoname} if {$filterregex ne {}} { append query { AND (`body` LIKE $filterregex OR `subj` LIKE $filterregex) } } append query { ORDER BY `timestamp` } if {$doreverse eq 1} {append query DESC} else {append query ASC} if {$numitems > 0} {append query { LIMIT $numitems}} append query ";" msgdb eval $query msg { set globalline [string repeat = $twidth] set hdrline [string repeat - $twidth] set tz "" set renderedts "" catch { # because some servers don't provide timestamps set renderedts [clock format $msg(timestamp) -format {%Y-%m-%d %H:%M:%S} -timezone $tz] } catch { # because pipe can be broken anytime puts "\[$renderedts\] $msg(msgid)" puts "$msg(echoname) - $msg(msgfrom) ($msg(msgfromaddr)) to $msg(msgto)" if {$msg(repto) ne ""} { puts "Replied to: $msg(repto)" } puts "Subj: $msg(subj)" puts $hdrline set msg(body) [lmap s $msg(body) {string trimright $s}] puts "[tiiflow $msg(body) $twidth]\n\n$globalline\n" } } } # close the db msgdb close