source: QuoteEngine/QuoteEngine.tcl @ 46

Revision 46, 17.7 KB checked in by notopic, 12 months ago (diff)

v1.3
Added readme, separate settings file for TCL script, SQL script, rearranged dir structure

Line 
1# $Id: QuoteEngine.tcl,v 1.20 2004/03/30 21:52:12 James Exp $
2
3###############################################################################
4# QuoteEngine for eggdrop bots
5# Copyright (C) James Michael Seward 2003
6#
7# This program is covered by the GPL, please refer the to LICENCE file in the
8# distribution.
9###############################################################################
10
11# load the extension
12package require mysqltcl
13
14# Make sure you edit the sample settings file and save it as "QuoteEngine-settings.tcl"
15# in the eggdrop scripts directory!
16source "scripts/QuoteEngine-settings.tcl"
17
18# bind commands CHANGE as needed to set who can use
19# use ".chanset #channel [+/-]quoteengine" to enable/disable individual
20# channels
21bind pub "m|fov" !addquote quote_add
22bind pub "m|fov" !randquote quote_rand
23bind pub "m|fov" !fetchquote quote_fetch
24bind pub "m|fov" !getquote quote_fetch
25bind pub "m|fov" !findquote quote_search
26bind pub "m|fov" !searchquote quote_search
27bind pub "m|fov" !urlquote quote_url
28bind pub "-|-" !quoteurl quote_url
29bind pub "m|ov" !delquote quote_delete
30bind pub "m|ov" !deletequote quote_delete
31bind pub "m|ov" !quotestats quote_stats
32bind pub "-|-" !quoteversion quote_version
33bind pub "-|-" !quotehelp quote_help
34bind pubm "-|ov" * quote_auto
35
36################################################################################
37#No need to edit beyond this point
38################################################################################
39
40set quote_version "1.3"
41set quote_auto_last(blah) 0
42
43#add setting to channel
44setudef flag quoteengine
45
46# connect to database
47proc quote_connect { } {
48        global db_handle quote_db
49               
50        set db_handle [mysqlconnect -host $quote_db(host) -user $quote_db(user) -password $quote_db(password) -db $quote_db(database)]
51
52        if {$db_handle != ""} {
53                return 1
54        } else {
55                return 0
56        }
57}
58
59################################################################################
60# quote_ping
61# Check we're still connected to mysql
62################################################################################
63proc quote_ping { } {
64        global db_handle
65
66        if [::mysql::ping $db_handle] {
67                return 1
68        } else {
69                return [quote_connect]
70        }
71}
72
73################################################################################
74# quote_add
75# !addquote <text>
76#   Adds a quote to the database
77################################################################################
78proc quote_add { nick host handle channel text } {
79  global db_handle quote_noflags
80
81  if {![channel get $channel quoteengine]} {
82    return 0
83  }
84
85  if [matchattr $handle $quote_noflags] { return 0 }
86
87  if {($handle == "") || ($handle == "*")} {
88    set handle $nick
89  }
90
91        if {![quote_ping]} {
92                putquick "PRIVMSG $channel :Sorry, lost database connection :("
93                return 0
94        }
95
96  set sql "INSERT INTO quotes VALUES(null, "
97  append sql "'$handle', "
98  append sql "'$nick!$host', "
99  set text [mysqlescape $text]
100  append sql "'$text', "
101  append sql "'$channel', "
102  append sql "'[clock seconds]')"
103
104  putloglev d * "QuoteEngine: executing $sql"
105
106  set result [mysqlexec $db_handle $sql]
107  if {$result != 1} {
108    putlog "An error occurred with the sql :("
109  } else {
110    set id [mysqlinsertid $db_handle]
111    puthelp "PRIVMSG $channel :Quote \002$id\002 added"
112                if [regexp {[^]> ]\|[[<0-9(]} $text] {
113                        puthelp "PRIVMSG $nick :It's possible you didn't split the lines quite right on the quote you just added. For best results, split lines in quotes using '|' with a space each side. To delete the quote you just added and fix it, do '!delquote $id' in the channel."
114                }
115  }
116}
117
118
119################################################################################
120# quote_rand
121# !randquote [--all|--channel #channel]
122#   Gets a random quote from the database for the current channel
123#     --all: Choose from entire database
124#     --channel: Choose from given channel
125#     -c: Shortcut for --channel
126################################################################################
127proc quote_rand { nick host handle channel text } {
128  global db_handle quote_noflags quote_shrinkspaces
129
130  if {![channel get $channel quoteengine]} {
131    return 0
132  }
133
134  if [matchattr $handle $quote_noflags] { return 0 }
135
136        if {![quote_ping]} {
137                putquick "PRIVMSG $channel :Sorry, lost database connection :("
138                return 0
139        }
140
141  set where_clause "WHERE channel='$channel'"
142  if [regexp -- "--?all" $text] {
143    set where_clause ""
144  }
145
146  if [regexp -- "--?c(hannel)?( |=)(.+)" $text matches skip1 skip2 newchan] {
147    set where_clause "WHERE channel='[mysqlescape $newchan]'"
148  }
149
150  set sql "SELECT * FROM quotes $where_clause ORDER BY RAND() LIMIT 1"
151  putloglev d * "QuoteEngine: executing $sql"
152
153  set result [mysqlquery $db_handle $sql]
154
155  if {[set row [mysqlnext $result]] != ""} {
156    set id [lindex $row 0]
157    set quote [lindex $row 3]
158    set by [lindex $row 1]
159    set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
160                catch {
161                        if {$quote_shrinkspaces == 1} {
162                                regsub -all "  +" $quote " " quote
163                        }
164                        set quote [stripcodes bcruag $quote]
165                }
166
167    puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
168  } else {
169    puthelp "PRIVMSG $channel :Couldn't find a quote :("
170  }
171  mysqlendquery $result
172}
173
174
175################################################################################
176# quote_fetch
177# !getquote <id>
178#   Fetches the given quote from the database
179################################################################################
180proc quote_fetch { nick host handle channel text } {
181  global db_handle quote_noflags quote_shrinkspaces
182
183  if {![channel get $channel quoteengine]} {
184    return 0
185  }
186
187  if [matchattr $handle $quote_noflags] { return 0 }
188
189        set verbose ""
190
191  if {![regexp {(-v )?([0-9]+)} $text matches verbose quote_id]} {
192    puthelp "PRIVMSG $channel: Use: !getquote \[-v\] <id>"
193    return 0
194  }
195
196        if {![quote_ping]} {
197                putquick "PRIVMSG $channel :Sorry, lost database connection :("
198                return 0
199        }
200
201       
202        set text [mysqlescape $quote_id]
203  set sql "SELECT * FROM quotes WHERE id='$text'"
204  putloglev d * "QuoteEngine: executing $sql"
205
206  set result [mysqlquery $db_handle $sql]
207
208  if {[set row [mysqlnext $result]] != ""} {
209    set id [lindex $row 0]
210                set quote [lindex $row 3]
211                catch {
212                        if {$quote_shrinkspaces == 1} {
213                                regsub -all "  +" $quote " " quote
214                        }
215                        set quote [stripcodes bcruag $quote]
216                }
217    set by [lindex $row 1]
218    set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
219    set chan [lindex $row 4]
220    if {$chan != $channel} {
221      puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
222      if {$verbose != ""} {
223                                puthelp "PRIVMSG $channel :\[\002$id\002\] From $chan, by added $by at $when."
224                        }
225    } else {
226      puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
227                        if {$verbose != ""} {
228                                puthelp "PRIVMSG $channel :\[\002$id\002\] Added by $by at $when."
229                        }
230    }
231  } else {
232    puthelp "PRIVMSG $channel :Couldn't find quote $text"
233  }
234
235  mysqlendquery $result
236}
237
238
239################################################################################
240# quote_search
241# !findquote [--all] [--channel #channel] [--count <int>] <text>
242#   Find all quotes with "text" in them. (in random order)
243#   The first 5 (by default) are listed in the channel. The rest are /msg'd to
244#   you up to the maximum (default 5).
245#     --all: Search all channels, not just current one
246#     --channel: Search given channel
247#     --count <int>: Find this many total quotes
248#     -c: Shortcut for --channel
249#     -n: Shortcut for --count
250#   Note this is a SQL search, so use % as the wildcard (instead of *)
251#   The script automatically puts %s around your text when searching.
252################################################################################
253proc quote_search { nick host handle channel text } {
254  global db_handle quote_webpage quote_noflags quote_chanmax
255
256  if {![channel get $channel quoteengine]} {
257    return 0
258  }
259
260  if [matchattr $handle $quote_noflags] { return 0 }
261
262  if {$text == ""} {
263    puthelp "PRIVMSG $channel :Use: !findquote <text>"
264    return 0
265  }
266
267        if {![quote_ping]} {
268                putquick "PRIVMSG $channel :Sorry, lost database connection :("
269                return 0
270        }
271
272  set where_clause "AND channel='[mysqlescape $channel]'"
273  if [regexp -- "--?all " $text matches skip1] {
274    set where_clause ""
275    regsub -- $matches $text "" text
276  }
277
278  if [regexp -- {--?c(hannel)?( |=)([^ ]+)} $text matches skip1 skip2 newchan] {
279    set where_clause "AND channel='[mysqlescape $newchan]'"
280    regsub -- $matches $text "" text
281  }
282
283  set limit 5
284  if [regexp -- {--?count( |=)([^ ]+)} $text matches skip1 count] {
285    set limit [mysqlescape $count]
286    regsub -- $matches $text "" text
287  }
288
289  if [regexp -- {-n( )?([^ ]+)} $text matches skip1 count] {
290    set limit [mysqlescape $count]
291    regsub -- $matches $text "" text
292  }
293
294  set sql "SELECT * FROM quotes WHERE quote LIKE '%[mysqlescape $text]%' $where_clause ORDER BY RAND()"
295
296  putloglev d * "QuoteEngine: executing $sql"
297
298  if {[mysqlsel $db_handle $sql] > 0} {
299
300    set count 0
301    mysqlmap $db_handle {id qnick qhost quote qchannel qts} {
302      if {$count == $limit} {
303        break
304      }
305
306      if {$count == $quote_chanmax} {
307        puthelp "PRIVMSG $nick :Rest of matches for your search '$text' follow in private:"
308      }
309
310      if {$count < $quote_chanmax} {
311        puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
312      } else {
313        puthelp "PRIVMSG $nick :\[\002$id\002\] $quote"
314      }
315      incr count
316    }
317
318    set remaining [mysqlresult $db_handle rows?]
319    if {$remaining > 0} {
320                        if {$count < $quote_chanmax} {
321                                set command "PRIVMSG $channel :"
322                        } else {
323                                set command "PRIVMSG $nick :"
324                        }
325
326      regsub "#" $channel "" chan
327      if {$quote_webpage != ""} {
328        puthelp "${command}(Plus $remaining more matches: $quote_webpage?filter=${text}&channel=${chan}&search=search)"
329      } else {
330        puthelp "${command}Plus $remaining other matches"
331      }
332    } else {
333                        if {$count < $quote_chanmax} {
334                                set command "PRIVMSG $channel :"
335                        } else {
336                                set command "PRIVMSG $nick :"
337                        }
338      if {$count == 1} {
339        puthelp "${command}(All of 1 match)"
340      } else {
341        puthelp "${command}(All of $count matches)"
342      }
343    }
344  } else {
345    puthelp "PRIVMSG $channel :No matches"
346  }
347}
348
349
350################################################################################
351# quote_url
352# !quoteurl
353#   Gives the web of the web interface
354################################################################################
355proc quote_url { nick host handle channel text } {
356  global quote_webpage quote_noflags
357
358  if {![channel get $channel quoteengine]} {
359    return 0
360  }
361
362  if [matchattr $handle $quote_noflags] { return 0 }
363
364  if {$quote_webpage != ""} {
365# changed for better url by dubkat
366  puthelp "PRIVMSG $channel :${quote_webpage}?channel=[string range $channel 1 end]"
367  } else {
368    puthelp "PRIVMSG $channel :Not available."
369  }
370}
371
372
373################################################################################
374# quote_stats
375# !quotestats
376#   Give some simple statistics about the db, channel, and user
377################################################################################
378proc quote_stats { nick host handle channel text } {
379  global db_handle quote_noflags
380
381  if {![channel get $channel quoteengine]} {
382    return 0
383  }
384
385  if [matchattr $handle $quote_noflags] { return 0 }
386
387        if {![quote_ping]} {
388                putquick "PRIVMSG $channel :Sorry, lost database connection :("
389                return 0
390        }
391
392  set sql "SELECT COUNT(*) AS total FROM quotes WHERE channel='$channel'"
393  putloglev d * "QuoteEngine: executing $sql"
394
395  set result [mysqlquery $db_handle $sql]
396  set total 0
397  set chan 0
398
399  if {[set row [mysqlnext $result]] != ""} {
400    set total [lindex $row 0]
401  }
402
403  mysqlendquery $result
404
405  set sql "SELECT COUNT(*) AS total FROM quotes"
406  putloglev d * "QuoteEngine: executing $sql"
407
408  set result [mysqlquery $db_handle $sql]
409
410  if {[set row [mysqlnext $result]] != ""} {
411    set chan [lindex $row 0]
412  }
413
414  mysqlendquery $result
415
416  set sql "SELECT COUNT(*) AS total FROM quotes WHERE nick='$handle' AND channel='$channel'"
417  putloglev d * "QuoteEngine: executing $sql"
418
419  set result [mysqlquery $db_handle $sql]
420
421  if {[set row [mysqlnext $result]] != ""} {
422    set by_handle [lindex $row 0]
423  }
424
425  mysqlendquery $result
426
427  puthelp "PRIVMSG $channel :Quotes for $channel: \002$total\002 (total: $chan). You have added \002$by_handle\002 quotes in this channel."
428}
429
430
431################################################################################
432# quote_delete
433# !delquote <id>
434#   Removes a quote from the database. You can only delete the quote if you
435#   are a bot/channel master, or if you're the person who added it.
436################################################################################
437proc quote_delete  { nick host handle channel text } {
438  global db_handle quote_noflags
439
440  if {![channel get $channel quoteengine]} {
441    return 0
442  }
443
444  if [matchattr $handle $quote_noflags] { return 0 }
445
446        if {![quote_ping]} {
447                putquick "PRIVMSG $channel :Sorry, lost database connection :("
448                return 0
449        }
450
451  set text [mysqlescape $text]
452  if {![matchattr $handle m|m $channel]} {
453    set sql "SELECT nick FROM quotes WHERE id='$text'"
454    putloglev d * "QuoteEngine: executing $sql"
455
456    set result [mysqlquery $db_handle $sql]
457    set owner [lindex [mysqlnext $result] 0]
458    mysqlendquery $result
459    if {$owner != $handle} {
460      puthelp "NOTICE $nick :You cannot delete that quote."
461      return 0
462    }
463  }
464
465  set sql "DELETE FROM quotes WHERE id='$text'"
466  putloglev d * "QuoteEngine: executing $sql"
467
468  set result [mysqlexec $db_handle $sql]
469  if {$result != 1} {
470    puthelp "PRIVMSG $channel :An error occurred deleting the quote :("
471    return 0
472  } else {
473    puthelp "PRIVMSG $channel :Deleted quote $text"
474  }
475}
476
477
478################################################################################
479# quote_version
480# !quoteversion
481#   Gives the version of the script
482################################################################################
483proc quote_version { nick host handle channel text } {
484  global quote_version quote_noflags
485
486  if [matchattr $handle $quote_noflags] { return 0 }
487
488  puthelp "PRIVMSG $channel :This is the QuoteEngine version $quote_version by JamesOff (http://www.jamesoff.net/go/quoteengine)"
489  return 0
490}
491
492
493################################################################################
494# quote_help
495# !quotehelp
496#   Handle help requests
497################################################################################
498 proc quote_help { nick host handle channel text } {
499  global quote_noflags
500
501  if [matchattr $handle $quote_noflags] { return 0 }
502
503  puthelp "PRIVMSG $nick :Commands for the QuoteEngine script:"
504  puthelp "PRIVMSG $nick :  !addquote <quote text> - adds a quote to the database"
505  puthelp "PRIVMSG $nick :  !delquote <id> - deletes a quote. You must be either a bot/channel master or the person who added the quote to delete it."
506  puthelp "PRIVMSG $nick :  !randquote \[--all\] \[--channel=#channel\] \[-c #channel\] - fetches a random quote from the current channel. --all chooses from all channels, not just the one the command is executed from. --channel and -c choose only from the given channel."
507  puthelp "PRIVMSG $nick :  !getquote \[-v\]<id> - fetches the quote with number <id>. Gives info of who added it if -v is specified."
508  puthelp "PRIVMSG $nick :  !findquote \[--all\] \[--channel=#channel\] \[-c #channel\] \[--count <int>\] \[-n <int>\] <text> - finds up to <int> (default 5) quotes containing 'text'. Optional parameters same as !randquote. -n is a shortcut for --count."
509  puthelp "PRIVMSG $nick :  !quoteurl - get the URL for the web interface to the quotes"
510  puthelp "PRIVMSG $nick :  !quotestats - get some information"
511  puthelp "PRIVMSG $nick :  !quoteversion - get the version of the script"
512  puthelp "PRIVMSG $nick :  Some commands have synonyms: !deletequote, !fetchquote, !urlquote, and !searchquote."
513  puthelp "PRIVMSG $nick :  (End of help)"
514  return 0
515}
516
517proc quote_auto { nick host handle channel text } {
518        global quote_automatic quote_shrinkspaces
519        if {$quote_automatic == 0} {
520                return
521        }
522
523  if {![channel get $channel quoteengine]} {
524                return
525        }
526
527        global quote_auto_last db_handle quote_automatic_minimum
528
529        if [info exists quote_auto_last($channel)] {
530                set diff [expr [clock seconds] - $quote_auto_last($channel)]
531                putloglev 1 * "diff for $channel is $diff"
532        } else {
533                set diff [expr $quote_automatic_minimum + 1]
534                putloglev d * "initialising diff for $channel"
535                set quote_auto_last($channel) 0
536        }
537
538        if {$diff < $quote_automatic_minimum} {
539                return
540        }
541
542        set words [split $text]
543        set newwords [list]
544
545        foreach word $words {
546                if [regexp -nocase {^[a-z0-9']{4,}$} $word] {
547                        if {[lsearch [list "yeah" "about" "hello" "their" "there" "that's" "can't" "morning" "won't"] $word] > -1} {
548                                continue
549                        }
550
551                        if [onchan $word] {
552                                continue
553                        }
554
555                        lappend newwords [mysqlescape $word]
556                }
557        }
558
559        if {[llength $newwords] == 0} {
560                return
561        }
562
563        putloglev d * "quoteengine: candidate words for random quote in $channel: $newwords"
564
565        if {![quote_ping]} {
566                return
567        }
568
569        set thisword [pickRandom $newwords]
570        putloglev d * "quoteengine: using $thisword"
571
572        if {[rand 100] < 95} {
573                putloglev d * "quoteengine: not random enough, ignoring"
574                return
575        }
576
577       
578        set where_clause "WHERE channel='[mysqlescape $channel]' AND quote LIKE '%$thisword%' ORDER BY RAND() LIMIT 1"
579        putloglev d * "quoteengine: $where_clause"
580        set sql "SELECT * FROM quotes $where_clause"
581
582        set result [mysqlquery $db_handle $sql]
583        if {[set row [mysqlnext $result]] != ""} {
584                set id [lindex $row 0]
585                set quote [lindex $row 3]
586
587                catch {
588                        if {$quote_shrinkspaces == 1} {
589                                regsub -all "  +" $quote " " quote
590                        }
591                        set quote [stripcodes bcruag $quote]
592                }
593
594                putloglev d * "RANDOM QUOTE: $quote ($id)"
595                puthelp "PRIVMSG $channel :\[\002$id\002\] $quote"
596                set quote_auto_last($channel) [clock seconds]
597        }
598        mysqlendquery $result
599
600}
601
602
603quote_connect
604putlog "QuoteEngine $quote_version loaded"
Note: See TracBrowser for help on using the repository browser.