comparison utillib.tcl @ 539:c9dc79874939

utillib: Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 07 Jul 2020 22:08:43 +0300
parents 20a1b0f39f71
children a57822226ba0
comparison
equal deleted inserted replaced
538:f07c972e269a 539:c9dc79874939
77 77
78 78
79 # Send IRC message with given message type, splitting the 79 # Send IRC message with given message type, splitting the
80 # string to fit to IRCNet (etc.) max message length. 80 # string to fit to IRCNet (etc.) max message length.
81 proc utl_msg_do {upreferredmsg upublic unick uchan umsg} { 81 proc utl_msg_do {upreferredmsg upublic unick uchan umsg} {
82 foreach uline [utl_str_split $umsg 450] { 82 # Split message per line
83 if {$upublic == 1} { 83 foreach qmsg [split $umsg "\n"] {
84 putserv "$upreferredmsg $uchan :$uline" 84 # Split each line to fit max message limit
85 } else { 85 foreach uline [utl_str_split $qmsg 445] {
86 putserv "$upreferredmsg $unick :$uline" 86 if {$upublic == 1} {
87 putserv "$upreferredmsg $uchan :$uline"
88 } else {
89 putserv "$upreferredmsg $unick :$uline"
90 }
87 } 91 }
88 } 92 }
89 } 93 }
90 94
91 95
96 # Substitute @n@ -> uvalues{n} in the ustr
97 proc utl_str_map_values {ustr {uvalues {}}} {
98 set narg 1
99 foreach marg $uvalues {
100 set ustr [string map [list "@$narg@" $marg] $ustr]
101 incr narg
102 }
103 return $ustr
104 }
105
106
92 proc utl_msg_args {upreferredmsg upublic unick uchan umsg {uargs {}}} { 107 proc utl_msg_args {upreferredmsg upublic unick uchan umsg {uargs {}}} {
93 108
94 # Map constant tokens 109 # Replace named tokens
95 set umsg [string map [list "@nick@" $unick] $umsg] 110 set umsg [string map [list "@nick@" $unick] $umsg]
96 set umsg [string map [list "@chan@" $uchan] $umsg] 111 set umsg [string map [list "@chan@" $uchan] $umsg]
97 112
98 # Map numeric tokens 113 # Replace numeric tokens
99 set narg 1 114 set umsg [utl_str_map_values $umsg $uargs]
100 foreach marg $uargs {
101 set umsg [string map [list "@$narg@" $marg] $umsg]
102 incr narg
103 }
104 115
105 utl_msg_do $upreferredmsg $upublic $unick $uchan $umsg 116 utl_msg_do $upreferredmsg $upublic $unick $uchan $umsg
106 } 117 }
107 118
108 119
109 # Return formatted time for given UNIX timestamp 120 # Return formatted time string for given UNIX timestamp
110 proc utl_ctime {utime} { 121 proc utl_ctime {utime} {
111 if {$utime == "" || $utime == "*"} { 122 if {$utime == "" || $utime == "*"} {
112 set utime 0 123 set utime 0
113 } 124 }
114 return [clock format $utime -format "%d.%m.%Y %H:%M"] 125 return [clock format $utime -format "%d.%m.%Y %H:%M"]