The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
biblebot-diatheke.tcl
Go to the documentation of this file.
1 # Diatheke/Tcl 5.0 by Chris Little <chrislit@crosswire.org>
2 # Based on code schema of <cking@acy.digex.net>
3 
4 # Copyright 1999-2009 CrossWire Bible Society (http://www.crosswire.org)
5 # CrossWire Bible Society
6 # P. O. Box 2528
7 # Tempe, AZ 85280-2528
8 #
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the
11 # Free Software Foundation version 2.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 
18 # modify this to reflect actual location of diatheke and dict binaries
19 set diatheke "/usr/bin/diatheke"
20 set dict "/usr/bin/dict"
21 
22 set diaver 5.0
23 
24 proc publookupverse {vlookup} {
25  global botnick chan bibver diatheke
26 
27  set arg "-b"
28  set n [string first "@" $vlookup]
29  if {$n > -1 && $n < 2} {
30  append arg n
31  }
32 
33  set n [string first "#" $vlookup]
34  if {$n > -1 && $n < 2} {
35  append arg f
36  }
37  set vlookup [string trimleft $vlookup "#"]
38  set vlookup [string trimleft $vlookup "@"]
39  set vlookup [string trimleft $vlookup "#"]
40 
41  regsub -all {[\[\]\{\}\#\%\\\$\'\"\/\s]} $vlookup { } vlookup
42  catch {exec $diatheke -f plaintext -o $arg -b $bibver -k "$vlookup" >& /tmp/fooout.$botnick}
43  catch {set foofile [open /tmp/fooout.$botnick]}
44  while {[gets $foofile fooverse] >= 0} {
45  set len [string length $fooverse]
46  set i 0
47  set j 255
48  while {$j < $len} {
49  if {[regexp ">" $fooverse]} {
50  while {[string index $fooverse $j] != ">" && [string index $fooverse $j] != "\n"} {set j [expr $j - 1]}
51  } else {
52  while {[string index $fooverse $j] != " " && [string index $fooverse $j] != "\n"} {set j [expr $j - 1]}
53  }
54  set foo2 [string range $fooverse $i $j]
55  set foo2 [string trim $foo2]
56  regsub -all -nocase {(<FI>|<CM>|<FB>)} $foo2 {} foo2
57  regsub -all {<RF>} $foo2 {(footnote: } foo2
58  regsub -all {<Rf>} $foo2 {)} foo2
59  putmsg $chan "$foo2"
60  set i [expr $j + 1]
61  set j [expr $j + 256]
62  if {$j > $len} {set j $len}
63  }
64  set foo2 [string range $fooverse $i end]
65  set foo2 [string trim $foo2]
66  regsub -all -nocase {(<FI>|<CM>|<FB>)} $foo2 {} foo2
67  regsub -all {<RF>} $foo2 {(footnote: } foo2
68  regsub -all {<Rf>} $foo2 {)} foo2
69  putmsg $chan "$foo2"
70  }
71 
72  catch {close $foofile}
73  exec rm /tmp/fooout.$botnick
74  return 1
75 }
76 
77 proc pub_lookup {nick uhost hand channel arg} {
78  global von chan bibver
79  set chan $channel
80  if {$von==0} {
81  putmsg $nick "Verse display is currently off."
82  return 0
83  }
84  if {($von==2) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
85  putmsg $nick "Only ops can display verses at this time."
86  return 0
87  }
88 
89  publookupverse $arg
90 }
91 
92 #----------------------------------------------------------------------
93 
94 proc pubsearchword {vlookup} {
95  global botnick chan bibver diatheke
96  regsub -all {[\[\]\{\}\#\%\\\$\'\"\/\s]} $vlookup { } vlookup
97  catch {exec $diatheke -s $bibver "$vlookup" >& /tmp/fooout.$botnick}
98  catch {set foofile [open /tmp/fooout.$botnick]}
99 
100  while {[gets $foofile fooverse] >= 0} {
101  set len [string length $fooverse]
102  set i 0
103  set j 72
104  while {$j < $len} {
105  while {[string index $fooverse $j] != ";" && [string index $fooverse $j] != ")" && [string index $fooverse $j] != "\n"} {set j [expr $j + 1]}
106  set foo2 [string range $fooverse $i $j]
107  set foo2 [string trim $foo2]
108  putmsg $chan "$foo2"
109  set i [expr $j + 1]
110  set j [expr $j + 73]
111  if {$j > $len} {set j $len}
112  }
113  set foo2 [string range $fooverse $i end]
114  set foo2 [string trim $foo2]
115  putmsg $chan "$foo2"
116  }
117  catch {close $foofile}
118 
119  putmsg $chan "$fooverse"
120  exec rm /tmp/fooout.$botnick
121  return 1
122 }
123 
124 proc pub_lookups {nick uhost hand channel arg} {
125  global von chan bibver
126  set chan $channel
127  if {$von==0} {
128  putmsg $nick "Verse display is currently off."
129  return 0
130  }
131  if {($von==3) && (![matchattr $hand 3]) && (![matchattr $hand o]) && (![isvoice $nick $chan])} {
132  putmsg $nick "Sorry, only ops and voiced users can do searches right now."
133  return 0
134  }
135  if {($von==2) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
136  putmsg $nick "Sorry, only ops can do searches right now."
137  return 0
138  }
139 
140  pubsearchword $arg
141 }
142 
143 #----------------------------------------------------------------------
144 
145 proc publookupdict {vlookup} {
146  global botnick chan bibver diatheke
147  regsub -all {[\[\]\{\}\#\%\\\$\'\"\/\s]} $vlookup { } vlookup
148  catch {exec $diatheke -f plaintext -b $bibver -k "$vlookup" >& /tmp/fooout.$botnick}
149  catch {set foofile [open /tmp/fooout.$botnick]}
150 
151  while {[gets $foofile fooverse] >= 0} {
152  putmsg $chan "$fooverse"
153  }
154  catch {close $foofile}
155  exec rm /tmp/fooout.$botnick
156  return 1
157 }
158 
159 proc pub_lookupd {nick uhost hand channel arg} {
160  global von chan bibver
161  set chan $channel
162  if {$von==0} {
163  putmsg $nick "Verse display is currently off."
164  return 0
165  }
166  if {($von==3) && (![matchattr $hand 3]) && (![matchattr $hand o]) && (![isvoice $nick $chan])} {
167  putmsg $nick "Sorry, only ops and voiced users can use dictionaries and indices right now."
168  return 0
169  }
170  if {($von==2) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
171  putmsg $nick "Sorry, only ops can use dictionaries and indices right now."
172  return 0
173  }
174 
175  publookupdict $arg
176 }
177 
178 # Saving this as an example of some interesting (but now lost) functionality
179 # whereby the argument is overridden by the current date for a daily
180 # devotional query.
181 
182 #bind pub - !losung setver_losung
183 #proc setver_losung {nick uhost hand channel arg} {
184 # global botnick chan bibver
185 # set bibver losung_en_99
186 # set arg [exec date "+%m.%d"]
187 # pub_lookupd $nick $uhost $hand $channel $arg
188 #}
189 
190 #----------------------------------------------------------------------
191 
192 proc publookupcomm {vlookup} {
193  global botnick chan bibver diatheke
194  regsub -all {[\[\]\{\}\#\%\\\$\'\"\/\s]} $vlookup { } vlookup
195  catch {exec $diatheke -c $bibver "$vlookup" >& /tmp/fooout.$botnick}
196  catch {set foofile [open /tmp/fooout.$botnick]}
197  while {[gets $foofile fooverse] >= 0} {
198  set len [string length $fooverse]
199  set i 0
200  set j 72
201  while {$j < $len} {
202  while {[string index $fooverse $j] != " " && [string index $fooverse $j] != "\n"} {set j [expr $j + 1]}
203  set foo2 [string range $fooverse $i $j]
204  set foo2 [string trim $foo2]
205  regsub -all -nocase {(<FI>|<CM>|<FB>)} $foo2 {} foo2
206  regsub -all {<RF>} $foo2 {(footnote: } foo2
207  regsub -all {<Rf>} $foo2 {)} foo2
208  putmsg $chan "$foo2"
209  set i [expr $j + 1]
210  set j [expr $j + 73]
211  if {$j > $len} {set j $len}
212  }
213  set foo2 [string range $fooverse $i end]
214  set foo2 [string trim $foo2]
215  regsub -all -nocase {(<FI>|<CM>|<FB>)} $foo2 {} foo2
216  regsub -all {<RF>} $foo2 {(footnote: } foo2
217  regsub -all {<Rf>} $foo2 {)} foo2
218  putmsg $chan "$foo2"
219  }
220  catch {close $foofile}
221  exec rm /tmp/fooout.$botnick
222  return 1
223 }
224 
225 proc pub_lookupc {nick uhost hand channel arg} {
226  global von chan bibver
227  set chan $channel
228  if {$von==0} {
229  putmsg $nick "Verse display is currently off."
230  return 0
231  }
232  if {($von==3) && (![matchattr $hand 3]) && (![matchattr $hand o]) && (![isvoice $nick $chan])} {
233  putmsg $nick "Sorry, only ops and voiced users can use commentaries right now."
234  return 0
235  }
236  if {($von==2) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
237  putmsg $nick "Sorry, only ops can use commentaries right now."
238  return 0
239  }
240 
241  publookupcomm $arg
242 }
243 
244 #----------------------------------------------------------------------
245 
246 bind pub - !dict dictlookup
247 
248 proc dictlookup {nick uhost hand channel arg} {
249  global botnick von dict
250 
251  if {$von==0} {
252  putmsg $nick "Verse display is currently off."
253  return 0
254  }
255  if {($von==3) && (![matchattr $hand 3]) && (![matchattr $hand o]) && (![isvoice $nick $channel])} {
256  putmsg $nick "Sorry, only ops and voiced users can use dictionaries and indices right now."
257  return 0
258  }
259  if {($von==2) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
260  putmsg $nick "Only ops can use dictionaries and indices right now."
261  return 0
262  }
263 
264  regsub -all {[\[\]\{\}\#\%\\\$\'\"\/\s]} $arg { } arg
265  catch {exec $dict "$arg" >& /tmp/fooout.$botnick}
266  catch {set foofile [open /tmp/fooout.$botnick]}
267  catch {set fooverse [gets $foofile]}
268  while {[gets $foofile fooverse] >= 0} {
269  set fooverse [string trim $fooverse]
270  putmsg $channel "$fooverse"
271  }
272  catch {close $foofile}
273  exec rm /tmp/fooout.$botnick
274  return 1
275 }
276 
277 #----------------------------------------------------------------------
278 
279 bind pub - !biblehelp pub_help
280 bind msg - biblehelp pub_help
281 
282 proc pub_help {nick uhost hand channel arg} {
283  global diaver
284  global von
285  putserv "NOTICE $nick :Diatheke/Tcl BibleBot version $diaver"
286 
287  if {(($von==0) || ($von==2)) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
288  putserv "NOTICE $nick :BibleBot displays are currently turned off."
289  return 1
290  }
291 
292  putserv "NOTICE $nick :Supported commands:"
293  putserv "NOTICE $nick :Help, using \"!biblehelp\""
294  putserv "NOTICE $nick :Book list, using \"!books\" (it's long)"
295  putserv "NOTICE $nick :Check display status, using \"!status\""
296  putserv "NOTICE $nick :Bible lookups, using \"!<bible version> <book> <chapter>:<verse>\""
297  putserv "NOTICE $nick :verse ranges can be specified by adding \"-<last verse>\" to this"
298  putserv "NOTICE $nick :To turn Strong's numbers and/or footnotes on, use @ and/or # respectively before the book name. For example \"!kjv @#Gen 1:4\" will retrieve Genesis 1:3 with the Strong's numbers and footnotes associated with it."
299 
300  if {($von==3) && (![matchattr $hand 3]) && (![matchattr $hand o]) && (![isvoice $nick $channel])} {
301  return 1
302  }
303 
304  putserv "NOTICE $nick :Commentary lookups, using \"!<commentary> <book> <chapter>:<verse>\""
305  putserv "NOTICE $nick :Dictionary/index lookups, using \"!<dictionary> <word or number>\""
306  putserv "NOTICE $nick :Bible searches, using \"!s<bible version> <word>\""
307  putserv "NOTICE $nick :Diatheke/Tcl defaults to PHRASE search mode. To use MULTI-WORD search mode, preface your search with an @. To use REGEX mode, preface your search with a #. For example: \"!skjv @Jesus love\" will print a list of all verses in the KJV containing the words Jesus and love."
308 
309  if {(![matchattr $hand 3]) && (![matchattr $hand o])} {
310  return 1
311  }
312 
313  putserv "NOTICE $nick :To turn verse display off, use \"!verseoff\""
314  putserv "NOTICE $nick :To turn all displays on for all users, use \"!verseon\""
315  putserv "NOTICE $nick :To turn all displays on for ops only, use \"!verseon o\""
316  putserv "NOTICE $nick :To turn verse diaplays on for regular users and all other displays on for ops and voiced users only, use \"!verseon v\" (default)"
317 }
318 
319 bind pub - !books pub_books
320 bind msg - books pub_books
321 
322 proc pub_books {nick uhost hand channel arg} {
323  global von
324 
325  if {(($von==0) || ($von==2)) && (![matchattr $hand 3]) && (![matchattr $hand o])} {
326  putserv "NOTICE $nick :BibleBot displays are currently turned off."
327  return 1
328  }
329 
330  printBibles $nick
331 
332  if {($von==3) && (![matchattr $hand 3]) && (![matchattr $hand o]) && (![isvoice $nick $channel])} {
333  return 1
334  }
335 
336  printComms $nick
337  printDicts $nick
338 }
339 
340 #----------------------------------------------------------------------
341 
342 bind pub - !status pub_status
343 bind msg - status pub_status
344 
345 proc pub_status {nick uhost hand channel arg} {
346  global von
347 
348  if {$von==0} {
349  putserv "NOTICE $nick :All BibleBot displays are currently off."
350  } elseif {$von==1} {
351  putserv "NOTICE $nick :All BibleBot displays are currently on."
352  } elseif {$von==2} {
353  putserv "NOTICE $nick :All BibleBot displays are currently on for ops only."
354  } else {
355  putserv "NOTICE $nick :Verse displays are currently on for all users, but other BibleBot displays are currently restricted to ops and voiced users."
356  }
357  return 1
358 }
359 
360 #---------------------------------------------------------------------
361 
362 proc pub_verseon {nick uhost hand channel arg} {
363  global von
364  if {![matchattr $hand 3] && ![matchattr $hand o]} {
365  return 0
366  } elseif {$arg=="v"} {
367  set von 3
368 # putserv "NOTICE $nick :Long Text Display is now on for voiced only!"
369  } elseif {$arg=="o"} {
370  set von 2
371 # putserv "NOTICE $nick :Verse Display is now on for ops only!"
372  } else {
373  set von 1
374 # putserv "NOTICE $nick :All Display is now on!"
375  }
376  pub_status $nick $uhost $hand $channel $arg
377  return 1
378 }
379 bind pub - !verseon pub_verseon
380 bind msg - verseon pub_verseon
381 
382 proc pub_verseoff {nick uhost hand channel arg} {
383  global von
384 
385  if {![matchattr $hand 3] && ![matchattr $hand o]} {
386  return 0
387  }
388  set von 0
389 # putserv "NOTICE $nick :Verse Display is now off!"
390  pub_status $nick $uhost $hand $channel $arg
391  return 1
392 }
393 bind pub - !verseoff pub_verseoff
394 bind msg - verseoff pub_verseoff
395 
396 proc dcc_verseoff {hand idx arg} {
397  global von
398  global whovoff
399  if {![matchattr $hand 3] && ![matchattr $hand o]} {
400  return 0
401  }
402  set von 0
403  set whovoff $hand
404  return 1
405 }
406 bind dcc - verseoff dcc_verseoff
407 
408 proc dcc_verseon {hand idx arg} {
409  global von
410  if {![matchattr $hand 3] && ![matchattr $hand o]} {
411  return 0
412  }
413  elseif {$arg=="v"} {
414  set von 3
415  }
416  elseif {$arg=="o"} {
417  set von 2
418  } else {
419  set von 1
420  }
421  return 1
422 }
423 bind dcc - verseon dcc_verseon
424 
425 #sets default von mode
426 set von 1