changeset 55:f7ccab2a5811

quotedb: Add qdb_split function for splitting long strings nicely to substrings of specified length as a list. Use the function to send overly long quotes correctly to IRC.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 Sep 2011 17:49:50 +0300
parents 301df7d15861
children aff22ba168b0
files quotedb.tcl
diffstat 1 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/quotedb.tcl	Thu Sep 08 17:48:37 2011 +0300
+++ b/quotedb.tcl	Thu Sep 08 17:49:50 2011 +0300
@@ -125,6 +125,32 @@
   return 1
 }
 
+proc qdb_split {str maxlen} {
+  set pos 0 
+  set len [string length $str]
+  set ulen 0
+  set ustr ""
+  set result {}
+  while {$pos < $len} {
+    set end [string wordend $str $pos]
+    set new [expr $end - $pos + 1]
+    if {$ulen + $new < $maxlen} {
+      set ustr "$ustr[string range $str $pos $end]"
+      set ulen [expr $ulen + $new]
+    } else {
+      set ustr "$ustr[string range $str $pos $end]"
+      lappend result $ustr
+      set ustr ""
+      set ulen 0
+    }
+    set pos [expr $end + 1]
+  }
+  if {$ustr != ""} {
+    lappend result $ustr
+  }
+  return $result
+}
+
 
 #-------------------------------------------------------------------------
 proc qdb_add {utable unick uhost uhand uchan utext upublic} {
@@ -292,7 +318,10 @@
   }
 
   quotedb eval $usql {
-    qdb_msg $upublic $unick $uchan "#${quoteID}: $utext ($uuser, $uvote)"
+    set qtmp [qdb_split "#${quoteID}: $utext ($uuser, $uvote)" 450]
+    foreach qstr $qtmp {
+      qdb_msg $upublic $unick $uchan $qstr
+    }
     return 1
   }