Files

106 lines
3.1 KiB
Tcl
Raw Permalink Normal View History

#!/usr/bin/env tclsh
# tiiview: view ii/idec messages from the local text db
2024-10-23 20:27:03 +03:00
# Usage: tiiview.tcl [echo_name] [filter_string] [termwidth] [dbfile]
# Created by Luxferre in 2024, released into public domain
2024-10-23 20:27:03 +03:00
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 ".." ".." ".." ]]
}
2024-10-23 20:27:03 +03:00
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} {
2024-10-23 20:27:03 +03:00
set localdb [lindex $argv 3]
}
set echoname [string trim [lindex $argv 0]]
}
if {$twidth < 20} {set twidth 80}
if {$filterstr eq ""} {set filterstr "h0"}
2024-10-23 20:27:03 +03:00
# 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
2024-10-23 20:27:03 +03:00
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]
}
2024-10-23 20:27:03 +03:00
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)"
}
2024-10-23 20:27:03 +03:00
puts "Subj: $msg(subj)"
puts $hdrline
2024-10-23 21:23:26 +03:00
set msg(body) [lmap s $msg(body) {string trimright $s}]
2024-10-23 20:27:03 +03:00
puts "[tiiflow $msg(body) $twidth]\n\n$globalline\n"
}
2024-10-23 20:27:03 +03:00
}
}
2024-10-23 20:27:03 +03:00
# close the db
msgdb close