Version 1 of minibot

Updated 2007-10-20 23:23:26 by suchenwi

Richard Suchenwirth 2001-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. 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 ;#} 

 proc in {list element} {expr {[lsearch -exact $list $element]>=0}}
 proc send str {puts $::fd $str;flush $::fd}

 set ::fd [socket $::server 6667]
 fconfigure $fd  -encoding utf-8
 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?
 <minibot>        http://wiki.tcl.tk/20205
 suchenwi        ah, thanks 
 suchenwi        minibot expr 6*7
 <minibot>        42

Category Example