X-Git-Url: https://git.distorted.org.uk/~mdw/ircbot/blobdiff_plain/574abac6430df672a6ad566fffcce7422933c68a..8c7d57b0f8de4eeee27ecb6a5b7cb95824626d7f:/bot.tcl diff --git a/bot.tcl b/bot.tcl index da2a472..e021af7 100755 --- a/bot.tcl +++ b/bot.tcl @@ -1,36 +1,22 @@ -# Core bot code +# Actual IRC bot code + +set helpfile helpinfos source irccore.tcl +source parsecmd.tcl +source stdhelp.tcl -proc unlogged_content_msg {prefix params} { - if {![regexp {^[&#+!]} [lindex $params 0]] || +proc privmsg_unlogged {prefix ischan params} { + if {!$ischan || [regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} { - return 1 + return 0 } # on-channel message, ignore set chan [lindex $params 0] upvar #0 chan_lastactivity([irctolower $chan]) la set la [clock seconds] catch { recordlastseen_p $prefix "talking on $chan" 1 } - return -} - -proc usererror {emsg} { error $emsg {} {BLIGHT USER} } - -proc prefix_none {} { - upvar 1 p p - if {[string length $p]} { error "prefix specified" } -} - -proc prefix_nick {} { - global nick - upvar 1 p p - upvar 1 n n - if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" } - check_nick $n - if {"[irctolower $n]" == "[irctolower $nick]"} { - error "from myself" {} {} - } + return 1 } proc showintervalsecs {howlong abbrev} { @@ -390,9 +376,10 @@ proc nick_case {user} { } proc msg_NICK {p c newnick} { - global nick_arys nick_case + global nick_arys nick_case calling_nick prefix_nick recordlastseen_n $n "changing nicks to $newnick" 0 + set calling_nick $newnick recordlastseen_n $newnick "changing nicks from $n" 1 set luser [irctolower $n] lnick_marktime_cancel $luser @@ -435,9 +422,14 @@ proc msg_JOIN {p c chan} { lappend nlist $nl nick_ishere $n } -proc msg_PART {p c chan} { +proc msg_PART {p c chan args} { prefix_nick - recordlastseen_n $n "leaving $chan" 1 + set msg "leaving $chan" + if {[llength $args]} { + set why [lindex $args 0] + if {"[irctolower $why]" != "[irctolower $n]"} { append msg " ($why)" } + } + recordlastseen_n $n $msg 1 process_kickpart $chan $n } proc msg_QUIT {p c why} { @@ -459,30 +451,7 @@ proc msg_PRIVMSG {p c dest text} { } nick_case $n - if {[catch { - regsub {^! *} $text {} text - set ucmd [ta_word] - set procname ucmd/[string tolower $ucmd] - if {[catch { info body $procname }]} { - usererror "Unknown command; try help for help." - } - $procname $p $dest - } rv]} { - if {"$errorCode" != "BLIGHT USER"} { set rv "error: $rv" } - sendprivmsg $n $rv - } else { - manyset $rv priv_msgs pub_msgs priv_acts pub_acts - foreach {td val} [list $n $priv_acts $output $pub_acts] { - foreach l [split $val "\n"] { - sendaction_priority 0 $td $l - } - } - foreach {td val} [list $n $priv_msgs $output $pub_msgs] { - foreach l [split $val "\n"] { - sendprivmsg $td $l - } - } - } + execute_usercommand $p $c $n $output $dest $text } proc msg_INVITE {p c n chan} { @@ -537,111 +506,6 @@ proc msg_366 {p c args} { unset names_chans } -proc ta_anymore {} { - upvar 1 text text - return [expr {!![string length $text]}] -} - -proc ta_nomore {} { - upvar 1 text text - if {[string length $text]} { error "too many parameters" } -} - -proc ta_word {} { - upvar 1 text text - if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} { - error "too few parameters" - } - return $firstword -} - -proc ta_nick {} { - upvar 1 text text - set v [ta_word] - check_nick $v - return $v -} - -proc def_ucmd {cmdname body} { - proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body" -} - -proc ucmdr {priv pub args} { - return -code return [concat [list $priv $pub] $args] -} - -proc loadhelp {} { - global help_topics errorInfo - - catch { unset help_topics } - set f [open helpinfos r] - try_except_finally { - set lno 0 - while {[gets $f l] >= 0} { - incr lno - if {[regexp {^#.*} $l]} { - } elseif {[regexp {^ *$} $l]} { - if {[info exists topic]} { - set help_topics($topic) [join $lines "\n"] - unset topic - unset lines - } - } elseif {[regexp {^\:\:} $l]} { - } elseif {[regexp {^\:([-+._0-9a-z]*)$} $l dummy newtopic]} { - if {[info exists topic]} { - error "help $newtopic while in $topic" - } - set topic $newtopic - set lines {} - } elseif {[regexp {^[^:#]} $l]} { - set topic - regsub -all {([^\\])\!\$?} _$l {\1} l - regsub -all {\\(.)} $l {\1} l - regsub {^_} $l {} l - lappend lines [string trimright $l] - } else { - error "eh ? $lno: $l" - } - } - if {[info exists topic]} { error "unfinished topic $topic" } - } { - set errorInfo "in helpinfos line $lno\n$errorInfo" - } { - close $f - } -} - -def_ucmd help { - upvar 1 n n - - set topic [irctolower [string trim $text]] - if {[string length $topic]} { - set ontopic " on `$topic'" - } else { - set ontopic "" - } - if {[set lag [out_lagged]]} { - if {[ischan $dest]} { set replyto $dest } else { set replyto $n } - if {$lag > 1} { - sendaction_priority 1 $replyto \ - "is very lagged. Please ask for help$ontopic again later." - ucmdr {} {} - } else { - sendaction_priority 1 $replyto \ - "is lagged. Your help$ontopic will arrive shortly ..." - } - } - - upvar #0 help_topics($topic) info - if {![info exists info]} { ucmdr "No help on $topic, sorry." {} } - ucmdr $info {} -} - -def_ucmd ? { - global help_topics - ucmdr $help_topics() {} -} - proc check_username {target} { if { [string length $target] > 8 || @@ -1168,6 +1032,9 @@ def_ucmd register { ucmdr {} "This is fine, but bear in mind that people will be able to mess with your settings. Channel management features need a secure registration." "makes an insecure registration for your nick." } } + default { + error "you mean register / register delete / register insecure" + } } } @@ -1417,7 +1284,7 @@ proc lnick_pingstring {why oc apstring} { catch { exec uptime } uptime set nnicks [llength [array names nick_onchans]] if {[regexp \ - {^ *([0-9:apm]+) +up.*, +(\d+) users, +load average: +([0-9., ]+) *$} \ + {^ *([0-9:apm]+) +up.*, +(\d+) users?, +load average: +([0-9., ]+) *$} \ $uptime dummy time users load]} { regsub -all , $load {} load set uptime "$time $nnicks/$users $load" @@ -1473,50 +1340,12 @@ proc ensure_globalsecret {} { unset gsfile } -proc ensure_outqueue {} { - out__vars - if {[info exists out_queue]} return - set out_creditms 0 - set out_creditat [clock seconds] - set out_queue {} - set out_lag_reported 0 - set out_lag_reportwhen $out_creditat -} - -proc fail {msg} { - logerror "failing: $msg" - exit 1 -} - -proc ensure_connecting {} { - global sock ownfullname host port nick socketargs - global musthaveping_ms musthaveping_after - - if {[info exists sock]} return - set sock [eval socket $socketargs [list $host $port]] - fconfigure $sock -buffering line - fconfigure $sock -translation crlf - - sendout USER blight 0 * $ownfullname - sendout NICK $nick - fileevent $sock readable onread - - set musthaveping_after [after $musthaveping_ms \ - {fail "no ping within timeout"}] -} - proc connected {} { - global musthaveping_after - - after cancel $musthaveping_after - unset musthaveping_after - foreach chan [chandb_list] { if {[chandb_get $chan autojoin]} { dojoin $chan } } } ensure_globalsecret -ensure_outqueue loadhelp ensure_connecting