[Richard Suchenwirth] 2007-10-21 - In the [Tcl chatroom] we discussed IRC, and one user was not happy with [picoIRC 0.2] - he didn't want the GUI, only the bot behavior which I modelled as a first shot to * connect to channel #tcl on IRC * listen to what is said * if someone mentions its name (minibot), tries to parse the message and answer So, in several iterations, this script ''minibot.tcl'' came out, which has some rudimentary knowledge of Chinese and Russian, can do math, and [string toupper]: #!/usr/bin/env tclsh set ::server irc.freenode.org set ::chan #tcl set ::me minibot proc recv {} { gets $::fd line puts $line # handle PING messages from server if {[lindex [split $line] 0] eq "PING"} { send "PONG [info hostname] [lindex [split $line] 1]"; return } if {[regexp {:([^!]*)![^ ].* +PRIVMSG ([^ :]+) +(.*[Mm]inibot)(.+)} $line -> \ nick target msg cmd]} { if {$nick eq "ijchain"} {regexp {<([^>]+)>(.+)} $msg -> nick msg} foreach pattern [array names ::patterns] { if [string match "*$pattern*" $cmd] { if [catch {eval $::patterns($pattern) $cmd} res] { append res \n$::errorInfo } foreach line [split $res \n] { send "PRIVMSG $::chan :$line" } break } } } } #----------- Patterns for response: set patterns(time) {clock format [clock sec] ;#} set patterns(expr) safeexpr proc safeexpr args {expr [string map {\[ ( \] ) expr ""} $args]} set patterns(eggdrop) {set _ "Please check http://wiki.tcl.tk/6601" ;#} set patterns(toupper) string set patterns(Windows) {set _ "I'd prefer not to discuss Windows..." ;#} set {patterns(translate "good" to Russian)} {set _ \u0425\u043E\u0440\u043E\u0448\u043E ;#} set patterns(Beijing) {set _ \u5317\u4EAC ;#} set patterns(Tokyo) {set _ \u4E1C\u4EAC ;#} set {patterns(your Wiki page)} {set _ http://wiki.tcl.tk/20205 ;#} set patterns(zzz) {set _ "zzz well!" ;#} set patterns(man) safeman proc safeman args {return http://www.tcl.tk/man/tcl8.4/TclCmd/[lindex $args 1].htm} set {patterns(where can I read about)} gotowiki proc gotowiki args {return "Try http://wiki.tcl.tk/[lindex $args end]"} set patterns(thank) {set _ "You're welcome." ;#} proc in {list element} {expr {[lsearch -exact $list $element]>=0}} proc send str {puts $::fd $str;flush $::fd} #-- ok, let the show begin :^) set ::fd [socket $::server 6667] fconfigure $fd -encoding utf-8 -buffering line send "NICK minibot" send "USER $::me 0 * :PicoIRC user" send "JOIN $::chan" fileevent $::fd readable recv vwait forever Examples from the chat: suchenwi minibot, which is your Wiki page? http://wiki.tcl.tk/20205 suchenwi ah, thanks suchenwi minibot expr 6*7 42 suchenwi minibot, what's your local time? Sun Oct 21 01:26:59 (MEZ) - Mitteleurop. Sommerzeit 2007 ---- | [Category Example] |