chachin

Untitled

Mar 10th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 11.69 KB | None | 0 0
  1. #################################################################################################
  2. # CodeNinja Quote System v1.0.1                                                                 #
  3. #                                                                                               #
  4. # Commands (<required> [optional]):                                                             #
  5. #  !addquote <quote> (adds a quote)                                                             #
  6. #  !delquote <quote id> (deletes a quote)                                                       #
  7. #  !quote [quote id] (gets a specific or random quote)                                          #
  8. #  !findquote <regexp string> (find quotes matching regexp string)                              #
  9. #  !lastquote (get the last quote added to the database)                                        #
  10. #  !quoteauth <nick> (find quotes added by nick)                                                #
  11. #  !quotechan <#chan> (find quotes added in #chan)                                              #
  12. #                                                                                               #
  13. # Changes                                                                                       #
  14. #  v1.0.1                                                                                       #
  15. #  -Quote file is automatically added upon adding first quote                                   #
  16. #  -Fixed a bug with random quotes                                                              #
  17. #  v1.0.2                                                                                       #
  18. #  -Changed how search commands (e.g. !findquote, !quoteauth, and !quotechan) handle            #
  19. #   excess results                                                                              #
  20. #  -Fixed a var mix-up                                                                          #
  21. #                                                                                               #
  22. # For updates, check out https://sites.google.com/site/codeninjacodes/                          #
  23. #                                                                                               #
  24. # To report bugs, email me at [email protected]                                             #
  25. #                                                                                               #
  26. #################################################################################################
  27.  
  28. namespace eval codeninja {
  29.     namespace eval quotesys {
  30.  
  31.         # File to store quotes in
  32.         variable qfile "data/quote.txt"
  33.  
  34.         # Limit of lines before quote is sent to private instead of the channel
  35.         variable plines 6
  36.  
  37.         # Use notice for private instead of message?
  38.         variable notice 1
  39.  
  40.         # Maximum number of search results to display (0 for no limit)
  41.         # WARNING: if you do not enable this there is a chance the bot will get booted from the
  42.         # server if there are too many results
  43.         variable flimit 15
  44.  
  45.         # Line divider to use when adding quotes
  46.         variable div "|"
  47.  
  48.         # Command character
  49.         variable cmdchar "!"
  50.  
  51.         # Flags required to delete quotes
  52.         variable dflags "m"
  53.     }
  54. }
  55.  
  56. # END CONFIG - DO NOT EDIT BELOW THIS
  57.  
  58. namespace eval codeninja {
  59.     namespace eval quotesys {
  60.         variable ver "v1.0.1"
  61.         proc add {nick uhost hand chan text} {
  62.             if {[string trim $text] == ""} { putserv "PRIVMSG $chan :No quote specified"
  63.                 return
  64.             }
  65.             set filesock [open ${codeninja::quotesys::qfile} {WRONLY APPEND CREAT}]
  66.             puts $filesock "\{$text\} $nick $chan [unixtime]"
  67.             close $filesock
  68.  
  69.             putserv "PRIVMSG $chan :Quote added to storage file"
  70.         }
  71.         proc del {nick uhost hand chan text} {
  72.             if {![regexp {^([0-9]+)$} $text]} { putserv "PRIVMSG $chan :Invalid quote ID"
  73.                 return
  74.             }
  75.             set filesock [open ${codeninja::quotesys::qfile} r]
  76.             set quotelist [split [string trimright [read $filesock] "\n"] "\n"]
  77.             close $filesock
  78.  
  79.             if {[lindex $quotelist [expr $text - 1]] == ""} { putserv "PRIVMSG $chan :Invalid quote ID"
  80.                 return
  81.             }
  82.             set filesock [open ${codeninja::quotesys::qfile} w]
  83.             set x 0
  84.             foreach quote $quotelist {
  85.                 if {$x != [expr $text - 1]} { puts $filesock [string trim $quote "\n"] }
  86.                 incr x
  87.             }
  88.             close $filesock
  89.             putserv "PRIVMSG $chan :Quote $text deleted"
  90.         }
  91.         proc get {nick uhost hand chan text} {
  92.             if {![file exists ${codeninja::quotesys::qfile}]} { putserv "PRIVMSG $chan :No quotes available."
  93.                 return
  94.             }
  95.             if {[string trim $text] == ""} {
  96.                 set filesock [open ${codeninja::quotesys::qfile} r]
  97.                 set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  98.                 close $filesock
  99.  
  100.                 set qi(id)  [rand [llength $quotes]]
  101.                 set qi(all) [lindex $quotes $qi(id)]
  102.                 incr qi(id)
  103.                 set qi(quote) [lindex $qi(all) 0]
  104.                 set qi(nick) [lindex $qi(all) 1]
  105.                 set qi(chan) [lindex $qi(all) 2]
  106.                 set qi(time) [lindex $qi(all) 3]
  107.             } elseif {[regexp {^([0-9]+)$} $text]} {
  108.                 set filesock [open ${codeninja::quotesys::qfile} r]
  109.                 set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  110.                 close $filesock
  111.  
  112.                 if {$text > [llength $quotes] || $text < 1} { putserv "PRIVMSG $chan :That quote does not exist" }
  113.  
  114.                 set qi(id) $text
  115.                 set qi(all) [lindex $quotes [expr $text - 1]]
  116.                 set qi(quote) [lindex $qi(all) 0]
  117.                 set qi(nick) [lindex $qi(all) 1]
  118.                 set qi(chan) [lindex $qi(all) 2]
  119.                 set qi(time) [lindex $qi(all) 3]
  120.             } else { putserv "PRIVMSG $chan :Invalid quote ID"
  121.                 return
  122.             }
  123.             if {[llength [split $qi(quote) ${codeninja::quotesys::div}]] > 5} { set sendmeth "[pmnot] $nick" } else { set sendmeth "PRIVMSG $chan" }
  124.             foreach line [split $qi(quote) ${codeninja::quotesys::div}] {
  125.                 putserv "$sendmeth :Quote $qi(id): $line"
  126.             }
  127.             putserv "$sendmeth :Added by $qi(nick) in $qi(chan) on [clock format $qi(time)]"
  128.         }
  129.         proc find {nick uhost hand chan text} {
  130.             if {[string trim $text] == ""} { putserv "PRIVMSG $chan :What do you want to search for?"
  131.                 return
  132.             }
  133.             set filesock [open ${codeninja::quotesys::qfile} r]
  134.             set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  135.             close $filesock
  136.  
  137.             set x 1
  138.             set found ""
  139.             foreach quote $quotes {
  140.                 if {[regexp -nocase $text [lindex $quote 0]]} { lappend found $x }
  141.                 incr x
  142.             }
  143.             if {$found == ""} { putserv "PRIVMSG $chan :No quotes found"
  144.             } elseif {[llength $found] > ${codeninja::quotesys::flimit}} { putserv "PRIVMSG $chan :The following quotes matched your query: [lrange $found 0 [expr ${codeninja::quotesys::flimit} - 1]] (first ${codeninja::quotesys::flimit} shown)"
  145.             } else { putserv "PRIVMSG $chan :The following quotes matched your query: $found" }
  146.         }
  147.         proc last {nick uhost hand chan text} {
  148.             set filesock [open ${codeninja::quotesys::qfile} r]
  149.             set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  150.             close $filesock
  151.  
  152.             set qi(id) [llength $quotes]
  153.             set qi(all) [lindex $quotes [expr $qi(id) - 1]]
  154.             set qi(quote) [lindex $qi(all) 0]
  155.             set qi(nick) [lindex $qi(all) 1]
  156.             set qi(chan) [lindex $qi(all) 2]
  157.             set qi(time) [clock format [lindex $qi(all) 3]]
  158.  
  159.             if {[llength [split $qi(quote) ${codeninja::quotesys::div}]] > ${codeninja::quotesys::plines}} { set sendmeth "NOTICE $nick" } else { set sendmeth "PRIVMSG $chan" }
  160.             foreach line [split $qi(quote) ${codeninja::quotesys::div}] {
  161.                 putserv "$sendmeth :Quote ${qi(id)}: $line"
  162.             }
  163.             putserv "$sendmeth :Added by $qi(nick) in $qi(chan) on $qi(time)"
  164.         }
  165.         proc total {nick uhost hand chan text} {
  166.             set filesock [open ${codeninja::quotesys::qfile} r]
  167.             set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  168.             close $filesock
  169.  
  170.             set total [llength $quotes]
  171.  
  172.             putserv "PRIVMSG $chan :There are $total quotes in my database"
  173.         }
  174.         proc listauth {nick uhost hand chan text} {
  175.             if {$text == ""} { putserv "PRIVMSG $chan :Invalid syntax"
  176.                 return
  177.             }
  178.             set filesock [open ${codeninja::quotesys::qfile} r]
  179.             set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  180.             close $filesock
  181.  
  182.             set match ""
  183.             set qid 1
  184.             foreach quote $quotes {
  185.                 if {[string match -nocase [lindex $quote 1] [lindex $text 0]]} {
  186.                     lappend match $qid
  187.                 }
  188.                 incr qid
  189.             }
  190.             if {$match == ""} { putserv "PRIVMSG $chan :No quotes found by $text"
  191.             } elseif {[llength $match] > ${codeninja::quotesys::flimit}} { putserv "PRIVMSG $chan :$text added the following quotes: [lrange $match 0 [expr ${codeninja::quotesys::flimit} - 1]] (first ${codeninja::quotesys::flimit} shown)"
  192.             } else { putserv "PRIVMSG $chan :$text added the following quotes: $match" }
  193.         }
  194.         proc listchan {nick uhost hand chan text} {
  195.             if {$text == ""} { set arg $chan } else { set arg [lindex $text 0] }
  196.             set filesock [open ${codeninja::quotesys::qfile} r]
  197.             set quotes [split [string trimright [read $filesock] "\n"] "\n"]
  198.             close $filesock
  199.  
  200.             set match ""
  201.             set qid 1
  202.  
  203.             foreach quote $quotes {
  204.                 if {[string match -nocase [lindex $quote 2] $arg]} {
  205.                     lappend match $qid
  206.                 }
  207.                 incr qid
  208.             }
  209.             if {$match == ""} { putserv "PRIVMSG $chan :No quotes were added in $arg"
  210.             } elseif {[llength $match] > ${codeninja::quotesys::plines}} { putserv "PRIVMSG $chan :The following quotes were added in $arg: [lrange $match 0 [expr ${codeninja::quotesys::flimit} - 1]] (first ${codeninja::quotesys::flimit} shown)"
  211.             } else { putserv "PRIVMSG $chan :The following quotes were added in $arg: $match" }
  212.         }
  213.         proc pmnot {} {
  214.             if {${codeninja::quotesys::notice} == 1} { return "NOTICE"
  215.             } else { return "PRIVMSG" }
  216.         }
  217.     }
  218. }
  219.  
  220. bind pub -|- ${codeninja::quotesys::cmdchar}addquote codeninja::quotesys::add
  221. bind pub ${codeninja::quotesys::dflags} ${codeninja::quotesys::cmdchar}delquote codeninja::quotesys::del
  222. bind pub -|- ${codeninja::quotesys::cmdchar}quote codeninja::quotesys::get
  223. bind pub -|- ${codeninja::quotesys::cmdchar}findquote codeninja::quotesys::find
  224. bind pub -|- ${codeninja::quotesys::cmdchar}lastquote codeninja::quotesys::last
  225. bind pub -|- ${codeninja::quotesys::cmdchar}totalquotes codeninja::quotesys::total
  226. bind pub -|- ${codeninja::quotesys::cmdchar}quoteauth codeninja::quotesys::listauth
  227. bind pub -|- ${codeninja::quotesys::cmdchar}quotechan codeninja::quotesys::listchan
  228.  
  229. putlog "CodeNinja Quote System ${codeninja::quotesys::ver} loaded"
Advertisement
Add Comment
Please, Sign In to add comment