All pastes #802558 Raw Edit

tzo

public text v1 · immutable
#802558 ·published 2007-11-30 12:04 UTC
rendered paste body
# --------------------------------------------------------------------
#              ______________________________________
#             |                                      |
#             | Quote v1.1 by mortician              |
#             |                                      |
#             | IRC:   irc.undernet.org - #mortician |
#             | email: mortician@linux.be            |
#             | www:   www.mortician.tk              |
#             |______________________________________|

# --------------------------------------------------------------------
# Usage
# --------------------------------------------------------------------
# !quote(s) <num/string>  - Displays a quote
# !quotehelp
# !addquote <string>      - Adds a quote
# !delquote <num>         - Deletes a quote
# !quotestats             - Shows how many quotes there are and
#                           how big the quote storage file is.
#
# ".chanset +quotes" to set the channels to store quotes in

# --------------------------------------------------------------------
# History
# --------------------------------------------------------------------
# 03 Jan 2003  v1.0  - Initial release
# 10 Jan 2003  v1.1  - bugfix in !quotestats and !quote <match>

# --------------------------------------------------------------------
# notes
# --------------------------------------------------------------------
# inspired on Quote tcl v3.52 by Stigmata
# todo: add a flood protection

# --------------------------------------------------------------------
# VARIABLES
# you have to change these
# --------------------------------------------------------------------
        # the char that marks public commands
set mqt(char) "!"
        # flag(s) needed to read quotes
set mqt(readflag) "."
        # flag(s) needed to add quotes
set mqt(addflag) "!"
        # flag(s) needed to delete quotes
set mqt(delflag) "!"
        # use a 1 general quote file? (1=yes/0=no)
        # if 0, channels will have different quote files
set mqt(gen) 0

# --------------------------------------------------------------------
# please email any bugs or comments (mortician@linux.be) or use forum
# on http://www.mortician.tk
# --------------------------------------------------------------------

# --------------------------------------------------------------------
# MAGIC BORDER
# Don't cross this line unless you feel prepared to code
# --------------------------------------------------------------------

set mqt(version) "v1.1"
putlog "Quote $mqt(version) by mortician loaded..."
setudef flag quotes

# --------------------------------------------------------------------
# bindings
# --------------------------------------------------------------------
bind pub $mqt(readflag) $mqt(char)quote mqt:quote
bind pub $mqt(readflag) $mqt(char)quotes mqt:quote
bind pub $mqt(readflag) $mqt(char)quotehelp mqt:quotehelp
bind pub $mqt(addflag) $mqt(char)addquote mqt:addquote
bind pub $mqt(delflag) $mqt(char)delquote mqt:delquote
bind pub $mqt(readflag) $mqt(char)quotestats mqt:quotestats

# --------------------------------------------------------------------
# procedures
# --------------------------------------------------------------------

# replace and mirc_strip thanks to More Tools v3.0.4 by MC_8
# (http://mc.purehype.net:81/)
proc replace {{args ""}} {
        set switches ""
        for {set i 0} {[string match -* [set arg [lindex $args $i]]]} {incr i} {
                if {![regexp -- {^-(nocase|-)$} $arg -> switch]} {
                        error "bad switch \"$arg\": must be -nocase, or --"
                }
                if {$switch == "-"} {
                        incr i
                        break
                }; lappend switches $switch
        }
        set nocase [expr {([lsearch -exact $switches "nocase"] >= "0") ? 1 : 0}]
        set text [lindex $args $i]
        set substitutions [lindex $args [expr $i+1]]

        # Check to see if $substitutions is in list format, if not make it so.
        set substitutions [split $substitutions]

        if {[info tclversion] >= "8.1"} {
                return [expr {($nocase)?
                        [string map -nocase $substitutions $text]:
                        [string map $substitutions $text]}]
        }

        set re_syntax {([][\\\*\+\?\{\}\,\(\)\:\.\^\$\=\!\|])}
        foreach {a b} $substitutions {
                regsub -all -- $re_syntax $a {\\\1} a
                if {$nocase} {regsub -all -nocase -- $a $text $b text} \
                else {regsub -all -- $a $text $b text}
        }; return $text
}
proc mirc_strip {{args ""}} {
        set switches ""
        for {set i 0} {[string match -* [set arg [lindex $args $i]]]} {incr i} {
                if {![regexp -- {^-(all|bold|color|reverse|underline|-)$} $arg -> switch]} {
                        set valid "-all -bold -color -reverse -underline {or --}"
                        error "bad switch \"$arg\": must be [join $valid ", "]"
                }
                if {$switch == "-"} {
                        incr i
                        break
                }; lappend switches $switch
        }
        if {$switches == ""} {set switches all}
        set arg [lindex $args $i]
        set all [expr {([lsearch -exact $switches all] >= 0) ? 1 : 0}]
        set list [list \002 "" \017 "" \026 "" \037 ""]
        regsub -all -- "\003(\[0-9\]\[0-9\]?(,\[0-9\]\[0-9\]?)?)?" $arg "" arg
        set arg [replace -- $arg [join $list]]

        return $arg
}

# add quotes
proc mqt:addquote {nick uhost handle chan arg} {
    global mqt
    if {![string match "*+quotes*" [channel info $chan]]} {return 0}
    if {$mqt(gen) == 1} { set file "quotes.mqt" } else { set file "[string tolower $chan].mqt" }
    set arg [mirc_strip $arg]
    if {$mqt(gen) == 1} {set arg "\[$chan\] $arg"}
    set mqt(fd) [open $file a+]
    puts $mqt(fd) $arg
    close $mqt(fd)
    putserv "PRIVMSG $chan :Quote has been added"
    putcmdlog "<<$nick>> !$handle! Added a quote in $chan."
    return 1
}
# quotehelp
proc mqt:quotehelp {nick uhost handle chan arg} {
    global mqt
    if {![string match "*+quotes*" [channel info $chan]]} {return 0}
    putserv "NOTICE $nick :Quote.tcl $mqt(version) by mortician.  Commands:"
    putserv "NOTICE $nick :$mqt(char)addquote <quote>   $mqt(char)quote(s) <num/string>"
    putserv "NOTICE $nick :$mqt(char)delquote <num>     $mqt(char)quotestats"
}
# read quotes
proc mqt:quote {nick uhost handle chan arg} {
    global mqt
    if {![string match "*+quotes*" [channel info $chan]]} {return 0}
    if {$mqt(gen) == 1} {set file "quotes.mqt"} else {set file "[string tolower $chan].mqt"}
    if {![file exists $file]} {putserv "PRIVMSG $chan :No quotes found, $file does not exist" ; return}
    set mqt(fd) [open $file r]
    set mqt(list) ""
    while { ![eof $mqt(fd)] } {
     set mqt_tmp [gets $mqt(fd)]
     if { $mqt_tmp != "" } {lappend mqt(list) $mqt_tmp}
    }
    close $mqt(fd)
    if {[llength $mqt(list)] == 0} {putserv "PRIVMSG $chan :No quotes found, $file is empty"; return}
    set arg [split $arg]
    if {$arg==""} {
       set mqt(print) [lindex $mqt(list) [rand [llength $mqt(list)]]]
       putserv "PRIVMSG $chan :Quote \002[expr [lsearch -exact $mqt(list) $mqt(print)] + 1]\002 of \002[llength $mqt(list)]\002: $mqt(print)"
    } else {
      if {[string is integer $arg]} {
         if {$arg > [llength $mqt(list)] || $arg <= 0} {return}
         set mqt(print) [lindex $mqt(list) [expr $arg - 1]]
         putserv "PRIVMSG $chan :Quote \002[expr [lsearch -exact $mqt(list) $mqt(print)] + 1]\002 of \002[llength $mqt(list)]\002: $mqt(print)"
      } else {
         if {$arg == "help"} { mqt:quotehelp $nick $uhost $handle $chan $arg ; return }
         set mqt(mlist) ""
         set arg "*[string tolower $arg]*"
                foreach mqt_tmp $mqt(list) {
                set mqt_low [string tolower $mqt_tmp]
                if { [string match $arg $mqt_low] } {
                lappend mqt(mlist) $mqt_tmp
                }
                }
         if {[llength $mqt(mlist)] == 0} {putserv "PRIVMSG $chan :no quotes found matching $arg"; return}
         set mqt(print) [lindex $mqt(mlist) [rand [llength $mqt(mlist)]]]
         putserv "PRIVMSG $chan :Quote \002[expr [lsearch -exact $mqt(mlist) $mqt(print)] + 1]\002 of \002[llength $mqt(mlist)]\002: $mqt(print)"
      }
     }
}

#del quotes
proc mqt:delquote {nick uhost handle chan arg} {
    global mqt
    if {![string match "*+quotes*" [channel info $chan]]} {return 0}
    if {$mqt(gen) == 1} { set file "quotes.mqt" } else { set file "[string tolower $chan].mqt" }
    if {![file exists $file]} { putserv "PRIVMSG $chan :No quotes found, $file does not exist" ; return}
    set mqt(fd) [open $file r]
    set mqt(list) ""
    while { ![eof $mqt(fd)] } {
     set mqt_tmp [gets $mqt(fd)]
     if { $mqt_tmp != "" } {lappend mqt(list) $mqt_tmp}
    }
    close $mqt(fd)
    if {[string is integer $arg]} {
    set index [expr $arg - 1]
    if {$arg > [llength $mqt(list)] || $arg <= 0} { putserv "PRIVMSG $chan :No quote deleted" ; return}
    set mqt(list) [lreplace $mqt(list) $index $index ]
    set mqt(fd) [open $file w]
    foreach mqt_tmp $mqt(list) {puts $mqt(fd) $mqt_tmp}
    close $mqt(fd)
    putserv "PRIVMSG $chan :Quote $arg deleted"
    putcmdlog "<<$nick>> !$handle! Deleted a quote in $chan."
    }
}

# stats quotes -- based on Quote tcl v3.52 by Stigmata
proc mqt:quotestats {nick uhost handle chan arg} {
    global mqt
    if {![string match "*+quotes*" [channel info $chan]]} {return 0}
    if {$mqt(gen) == 1} { set file "quotes.mqt" } else { set file "[string tolower $chan].mqt" }
    if {![file exists $file]} {
      putserv "PRIVMSG $chan :No quotes found, $file does not exist"
    } else {
      set mqt(size) [expr [file size $file] / 1024]
      set mqt(cnt) [exec grep -c "" $file]
      putserv "PRIVMSG $chan :$mqt(cnt) quotes total using $mqt(size)kb."
    }
}