Posted to tcl by ace at Sat Nov 03 01:47:44 GMT 2007view raw
- bind pub - !define learn_it
- bind pub - !learn learn_learn
- bind pub - "!whatis" learn_explain
- bind pub - "!search" learn_search
- bind dcc n|n sort learn_sortFile:dcc
- bind dcc - allwords learn_alldef:dcc
- #bind time - "5 0 1 * *" learn_sortFile
- set learn_whodid "*"
- proc learn_alldef:dcc {hand idx args} {
- global learn_db
- putdcc $idx "\[ ° ° Available definitions ° ° \]"
- set lista ""
- set fp [open $learn_db r]
- while {![eof $fp]} {lappend lista [lindex [split [gets $fp] " "] 1]}
- close $fp
- foreach {a b c d e f g h i j} [lsort -unique $lista] {putdcc $idx " \0037$a $b $c $d $e $f $g $h $i $j\003"}
- putdcc $idx "\[ ° ° === END === ° ° \]"
- }
- proc learn_it { nick uhost hand chan args } {
- global chan_ign
- set args [lindex $args 0]
- set args [split $args " "]
- if {[lindex $args 1] == ""} {
- if {[lsearch $chan_ign $chan] < "0" } {
- puthelp "NOTICE $nick :Try !define <keyword> defenition!"
- }
- } else {
- if {[lindex $args 1] == "*"} {
- learn_addEntry [lindex $args 1] [lindex $args 0] [lrange $args 2 end]
- learn_flood "[lindex $args 0]" $chan
- } else {
- learn_addEntry $nick [lindex $args 0] [lrange $args 1 end]
- if {[lsearch $chan_ign $chan] < "0" } {
- puthelp "NOTICE $nick :Defenition added"
- }
- learn_flood "[lindex $args 0]" $chan
- }
- }
- }
- proc learn_learn { nick uhost hand chan args } {
- global chan_ign
- if {[lsearch $chan_ign $chan] < "0" } {
- set args [lindex $args 0]
- set args [split $args " "]
- switch [lindex $args 0] {
- "add" {
- if {[lindex $args 2] == ""} {
- puthelp "NOTICE $nick :Try !learn add <keyword> defenition!"
- } else {
- learn_addEntry $nick [lindex $args 1] [lrange $args 2 end]
- puthelp "NOTICE $nick :Defenition added"
- learn_flood "[lindex $args 1]" $chan
- }
- }
- "del" {
- if {[lindex $args 2]!=""} {
- learn_delEntry [lindex $args 1] [lindex $args 2]
- puthelp "NOTICE $nick :Defenition removed"
- learn_flood "[lindex $args 1]" $chan
- } else {
- learn_delEntry [lindex $args 1]
- puthelp "NOTICE $nick :Defenition removed"
- }
- }
- "ins" {
- if {[lindex $args 3]!=""} {
- learn_insEntry [lindex $args 1] [lindex $args 2] [lrange $args 3 end]
- puthelp "NOTICE $nick :Defenition inserted"
- learn_flood "[lindex $args 1]" $chan
- } else {puthelp "NOTICE $nick :Insert Syntax: !learn ins <keyword> num text" }
- }
- "put" {
- if {[lindex $args 3]!=""} {
- learn_putEntry [lindex $args 1] [lindex $args 2] [lrange $args 3 end] $nick
- puthelp "NOTICE $nick :Defenition inserted"
- learn_flood "[lindex $args 1]" $chan
- } else {puthelp "NOTICE $nick :Put Syntax: !learn put <keyword> num text" }
- }
- "rep" {
- if {[lindex $args 3]!=""} {
- learn_repEntry [lindex $args 1] [lindex $args 2] [lrange $args 3 end]
- puthelp "NOTICE $nick :Defenition replaced"
- learn_flood "[lindex $args 1]" $chan
- } else {puthelp "NOTICE $nick :Replace Syntax: !learn rep <keyword> num text" }
- }
- "status" {
- learn_status $nick $chan
- learn_flood "[lindex $args 0]" $chan
- }
- default { puthelp "NOTICE $nick :\0037Syntax: !learn <add|del|info|ins|rep> <keyword> <num> text\003" }
- }
- }
- }
- proc learn_explain { nick uhost hand chan args } {
- global chan_ign
- if {[lsearch $chan_ign $chan] < "0" } {
- global learn_db learn_whodid;if {![info exists learn_whodid]} {set learn_whodid ""}
- set real_chan $chan
- set args [split [lindex $args 0] " "]
- set nogo [split [lindex $chan_ign 0] " "]
- if {$args == ""} {
- puthelp "PRIVMSG $chan :\0037Say what?: Usage: !whatis <keyword> or !whatis <keyword> > <yournick>\003"
- return
- }
- set explain [string tolower [lindex $args 0]];
- set chan [string tolower $chan]
- if {([lindex $args 1] == ">" || [lindex $args 1] == ">>") && [lindex $args 2] != ""} {
- set chan [lindex $args 2]
- }
- set fp [open $learn_db r]
- set allEntrys ""
- while {![eof $fp]} {
- gets $fp curEntry
- set curEntry [split $curEntry " "]
- if {[string equal -nocase [lindex $curEntry 1] $explain]} {
- lappend allEntrys [join $curEntry " "]
- }
- }
- close $fp
- set count 0
- if {[llength $allEntrys]==1} {
- # Single line output
- if {[lsearch -exact $learn_whodid [list $explain [string tolower $chan]]] != -1} {
- puthelp "PRIVMSG $real_chan :\0027Hey ${nick}\002, i've already told $chan about \"$explain\"... no need to repeat (i think)"
- return 0
- } else {
- set dump [learn_filterstr [join [lrange [split [lindex $allEntrys 0] " "] 2 end]]]
- puthelp "PRIVMSG $chan :\00311${explain}\003:\0037 ${dump}\003";incr count
- }
- } else {
- # Chan flood protection
- if {[llength $allEntrys]>17&&[string index $chan 0]=="#"&&!(([lindex $args 1] == ">" || [lindex $args 1] == ">>") && [lindex $args 2] != "")} {
- set chan $nick;
- puthelp "PRIVMSG $real_chan :\00311\037${explain}?\002\003 \0037What a huge defenition... i'll tell you in private instead...\003"
- }
- if {[lsearch -exact $learn_whodid [list $explain [string tolower $chan]]] != -1} {
- puthelp "PRIVMSG $real_chan :\002Hey ${nick}\002, i've already told you about \"$explain\"... no need to repeat (i think)"
- return 0
- }
- # Multi line output
- foreach curEntry $allEntrys {
- incr count
- set dump [learn_filterstr [join [lrange [learn_filterstr $curEntry] 2 end]]]
- putquick "PRIVMSG $chan :\00311${explain} \037\|\037${count}\037|\037:\003\0037 ${dump}\003"
- }
- }
- # No ouput found and more
- if {$count == 0} { puthelp "PRIVMSG $chan :\00311${explain} \037\|\037\002x\002\037\|\037: \003\0037No defenition found for word.\003" }
- if {([lindex $args 1] == ">" || [lindex $args 1] == ">>") && [lindex $args 2] != ""} {
- #puthelp "NOTICE $nick :Ok, done."
- }
- lappend learn_whodid [list $explain [string tolower $chan]]
- #set explain [learn_filterstr $explain] ;# Don't allow code to be executed
- set chan [learn_filterstr $chan] ;# ensure []s are properly handled...
- utimer 60 "learn_flood \"$explain\" \"$chan\""
- }
- }
- proc learn_flood {word target} {
- # removes from "already told so" list
- global learn_whodid
- set word [string tolower $word]; set target [string tolower $target]
- set lin [lsearch -exact $learn_whodid [list $word $target]]
- if {$lin == -1} {
- return
- } else {
- set learn_whodid [lreplace $learn_whodid $lin $lin]
- }
- }
- proc learn_addEntry { nick word defenition } {
- global learn_db;set word [string tolower $word]
- if {![file exists $learn_db]} {file mkdir [lindex [split $learn_db /] 0];set fp [open $learn_db w+]
- puts $fp "Infodb."
- } else {set fp [open $learn_db a]};puts $fp "$nick $word [join $defenition]";close $fp
- }
- proc learn_delEntry { word {remnum "all"}} {
- global learn_db
- set word [string tolower $word]
- set fp [open $learn_db r]
- set allEntrys ""
- set count 1
- if {$remnum == "all"} {
- while {![eof $fp]} {gets $fp curEntry;if {![string equal -nocase [lindex [split $curEntry] 1] $word]} {lappend allEntrys $curEntry}}
- } else {
- while {![eof $fp]} {
- gets $fp curEntry
- if {![string equal -nocase [lindex [split $curEntry] 1] $word] || ($count != $remnum && $remnum != -2)} {
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys $curEntry}
- if {[string equal -nocase [lindex [split $curEntry] 1] $word]} {incr count}
- } else {incr count}
- }
- }
- close $fp;set fp [open $learn_db w];foreach curEntry $allEntrys {puts $fp $curEntry};close $fp
- }
- proc learn_insEntry {word num text} {
- global learn_db
- set word [string tolower $word]
- set fp [open $learn_db r]
- set allEntrys ""
- set count 1
- while {![eof $fp]} {
- gets $fp curEntry
- if {![string equal -nocase [lindex [split $curEntry] 1] $word] || ($count != $num && $num != -2)} {
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys $curEntry}
- if {[string equal -nocase [lindex [split $curEntry] 1] $word]} {incr count}
- } else {incr count;lappend allEntrys "$curEntry [join $text]"}
- }
- close $fp;set fp [open $learn_db w];foreach curEntry $allEntrys {puts $fp $curEntry};close $fp
- }
- proc learn_putEntry {word num text {whodid "."}} {
- global learn_db
- set word [string tolower $word]
- set fp [open $learn_db r]
- set allEntrys ""
- set count 1
- if {$num==1} {lappend allEntrys "$whodid $word [join $text]"}
- while {![eof $fp]} {
- gets $fp curEntry
- if {[string equal -nocase [lindex [split $curEntry] 1] $word]} {incr count}
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys $curEntry}
- if {$count==$num&&$num!=1} {lappend allEntrys "$whodid $word [join $text]"}
- }
- close $fp;set fp [open $learn_db w];foreach curEntry $allEntrys {puts $fp $curEntry};close $fp
- }
- proc learn_repEntry {word num text} {
- global learn_db
- set word [string tolower $word]
- set fp [open $learn_db r]
- set allEntrys ""
- set count 1
- while {![eof $fp]} {
- gets $fp curEntry
- if {![string equal -nocase [lindex [split $curEntry] 1] $word] || ($count != $num && $num != -2)} {
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys $curEntry}
- if {[string equal -nocase [lindex [split $curEntry] 1] $word]} {incr count}
- } else {incr count;lappend allEntrys "[lrange [split $curEntry] 0 1] [join $text]"}
- }
- close $fp;set fp [open $learn_db w];foreach curEntry $allEntrys {puts $fp $curEntry};close $fp
- }
- proc learn_status {nick chan} {
- global learn_db
- set fp [open $learn_db r]
- set allEntrys ""
- set cache ""
- set count 0
- set lines 0
- while {![eof $fp]} {
- set curEntry [gets $fp]
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys [split $curEntry " "]}
- }
- close $fp
- set lines [llength $allEntrys]
- foreach line $allEntrys {
- set word [join [lindex $line 1]]
- if {![string match -nocase "*$word*" $cache]} {
- append cache "$word "
- incr count
- }
- }
- puthelp "PRIVMSG $chan :\00311Status\003: \0037There's \037\002$lines\002\037 enties and \037\002$count\002\037 unique definitions in the DataBase.\003"
- }
- proc learn_sortFile {a c d e f} {
- global learn_db
- set t_count [clock clicks -milliseconds]
- set fp [open $learn_db r]
- set allEntrys ""
- while {![eof $fp]} {
- set curEntry [gets $fp]
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys [split $curEntry " "]}
- }
- close $fp; set allEntrys [lsort -index 1 $allEntrys]
- set fp [open $learn_db w];foreach curEntry $allEntrys {puts $fp [join $curEntry " "]};close $fp
- putlog "\[!learn\]-> Auto sorted data in the database ($learn_db)->[expr double([clock clicks -milliseconds]-$t_count)/1000]s"
- }
- proc learn_sortFile:dcc {hand idx args} {
- global learn_db
- set t_count [clock clicks -milliseconds]
- set allEntrys ""
- set fp [open $learn_db r]
- while {![eof $fp]} {
- set curEntry [gets $fp]
- if {[info exists curEntry]&&$curEntry!=""} {lappend allEntrys [split $curEntry " "]}
- }
- close $fp; set allEntrys [lsort -index 1 $allEntrys]
- set fp [open $learn_db w];foreach curEntry $allEntrys {puts $fp [join $curEntry " "]};close $fp
- putdcc $idx "\[!learn\]-> Sorted data in the database ($learn_db)->[expr double([clock clicks -milliseconds]-$t_count)/1000]s"
- }
- proc learn_filterstr { data } {
- regsub -all -- \\\\ $data \\\\\\\\ data
- regsub -all -- \\\[ $data \\\\\[ data
- regsub -all -- \\\] $data \\\\\] data
- regsub -all -- \\\} $data \\\\\} data
- regsub -all -- \\\{ $data \\\\\{ data
- regsub -all -- \\\" $data \\\\\" data
- return $data
- }
- proc learn_search { nick uhost hand chan args } {
- global chan_ign
- if {[lsearch $chan_ign $chan] < "0" } {
- global learn_db
- set args [string tolower [lindex $args 0]]
- if {$args==""} {
- puthelp "PRIVMSG $chan :\0037Not enough arguments.\003"
- return
- }
- set init_t [clock clicks -milliseconds]
- set fp [open $learn_db r]
- set matches ""
- set allEntrys ""
- while {![eof $fp]} {
- gets $fp curEntry;
- if {[info exists curEntry]&&$curEntry!=""} {
- set curEntry [split [string tolower $curEntry] " "]
- set thisEntry [join [lrange $curEntry 1 end] " "]
- if {[string match -nocase "*${args}*" $thisEntry]} {
- # putserv "PRIVMSG #meta :worked $curEntry"
- if {[lsearch $allEntrys [lindex curEntry 1]]==-1} {
- lappend allEntrys [lindex $curEntry 1]
- set allEntrys [lsort -unique $allEntrys]
- }
- }
- }
- }
- set init_t [expr double(([clock clicks -milliseconds] - $init_t))/1000]
- if {$allEntrys==""} {
- puthelp "PRIVMSG $chan :\0037\002No matches\002 for \037\002$args\002\037 in the DataBase, sorry. (Search tok: \002\037${init_t}s\037\002)\003"
- } else {
- if {[llength $allEntrys]>20} {
- puthelp "PRIVMSG $chan :\0037\002Too many matches.\002 Please try a more complex search.\003"
- } else {
- puthelp "PRIVMSG $chan :\0037\002Found \037[llength $allEntrys]\037 matches.\002 Sorted: \037[join [lsort -dictionary $allEntrys] "\037, \037"]\037. (\002\037${init_t}\037s)\003"
- }
- }
- }
- }