#Rewritten by Bass@Undernet.  bass@shell.lazerlink.net  7/98
#eventually will be located at ftp.tclslave.net/pub/eggdrop/tcl.bass/  
#(whenever I get around to uploading it)

# you may turn the script off by typing !verseoff in the channel or
# .verseoff in a dcc. you may turn it back on by typing !verseon in the
# channel or .verseon in a dcc.

set bible(mode) 1 	; #default on/off setting.  it will revert to this setting every rehash/restart
set bible(path) /opt/local/bin/bible ; #full path to bible executable
set bible(flood) 8:15	; # flood ratio: number of lines/number of secs
set bible(ignore) 0	; # ignore flooders? off=0 (don't reply), on=1 (don't reply *and* ignore) 
set bible(itime) 2	; # ignore time (minutes)

###### Don't edit below here ######

proc bible_flood_init {} {
  global bible bible_array
  if {![string match *:* $bible(flood)]} {putlog bible: var bible(flood) not set correctly. ; return 1}
  set bible(num) [lindex [split $bible(flood) :] 0]
  set bible(time) [lindex [split $bible(flood) :] 1]
  set i [expr $bible(num) - 1]
  while {$i >= 0} {
    set bible_array($i) 0
    incr i -1
  }
} ; bible_flood_init

proc bible_flood {nick uhost} {
  global bible bible_array
  if {$bible(num) == 0} {return 0}
  set i [expr $bible(num) - 1]
  while {$i >= 1} {
    set bible_array($i) $bible_array([expr $i - 1])
    incr i -1
  }
  set bible_array(0) [unixtime]
  if {[expr [unixtime] - $bible_array([expr $bible(num) - 1])] <= $bible(time)} {
    putlog "bible: Flood detected from $nick."
    if {$bible(ignore)} {newignore [join [maskhost *!*[string trimleft $uhost ~]]] bible flood $bible(itime)}
    return  1
  } {return 0}
}

bind pub - !kjv pub_lookupkjv
bind pub - !bible pub_lookupkjv
bind pub - !verse pub_lookupkjv
proc pub_lookupkjv {nick uhost hand chan arg} {
  global bible botnick
  if {$bible(mode) == 0} {puthelp "notice $nick :Verse display is currently off." ; return 0}
#gen 3 4 6
#gen 3:3-6
#gen3:3-6
#gen 3 4
#gen 3:3
#gen3:3
  set a0 [lindex $arg 0] 
  set a1 [lindex $arg 1] 
  set a2 [lindex $arg 2] 
  set a3 [lindex $arg 3] 
  if {$a3 != "" || [string match *-* $a0$a1]} {
    #it's a range
    if {$a0 == ""} {bible_flood $nick $uhost ; puthelp "privmsg $chan :Syntax is <book><chapter>:<verse>\[ -verse\]" ; return 1}
    if {$a3 != ""} {set v1 $a0$a1 ; set v2 $a2 ; set v3 $a3} elseif {$a1 != ""} {
      set v1 $a0[lindex [split $a1 :] 0] ; set v2 [lindex [split $a1 :-] 1] ; set v3 [lindex [split $a1 :-] 2]} {
      set v1 [lindex [split $a0 :] 0] ; set v2 [lindex [split $a0 :-] 1] ; set v3 [lindex [split $a0 :-] 2]
    } 
    #$v1 $v2-$v3
    if {$v3 > $v2} {
      if {[expr $v3 - $v2] > 4} {bible_flood $nick $uhost ; puthelp "privmsg $chan :Please limit the range to 5 or fewer verses." ; return 1}
      catch {exec $bible(path)/bible -f -p $bible(path) $v1:$v2-$v3 > /tmp/bible.$botnick}
    }
  } {
    #it's not a range
    if {$a2 != ""} {
      set verse $a0$a1:$a2} {
      if {$a1 != ""} {set verse $a0$a1} {set verse $a0} 
    }
    catch {exec $bible(path)/bible -f -p $bible(path) $verse > /tmp/bible.$botnick}
  }
  set fd [open /tmp/bible.$botnick r]  
  while {![eof $fd]} {
    set inp [gets $fd]
    if {[string trim $inp " "] == ""} {continue}
    if {[bible_flood $nick $uhost]} {break}
    puthelp "privmsg $chan :$inp"
  }
  close $fd
  catch {exec rm /tmp/bible.$botnick}
  return 1
}

bind pub - !skjv pub_seekversekjv
proc pub_seekversekjv {nick uhost hand chan arg} {
  global bible botnick
  if {[bible_flood $nick $uhost]} {return 0}
  if {$bible(mode) == 0} {puthelp "privmsg $chan :Verse display is currently off." ; return 0}

  set a0 [lindex $arg 0]
  set a1 [lindex $arg 1]
  if {$a0=="in"} {
    set invar "?in $a1 "
    set a2 [lindex $arg 2]
    set a3 [lindex $arg 3]
    set a4 [lindex $arg 4]
    set a5 [lindex $arg 5]
    set a6 [lindex $arg 6]
  } {
    set invar "?in all"
#    set invar "?in Gen1:1-Rev22:21"
    set a2 [lindex $arg 0]
    set a3 [lindex $arg 1]
    set a4 [lindex $arg 2]
    set a5 [lindex $arg 3]
    set a6 [lindex $arg 4]
  }
  if {$a6 != ""} {set match [exec $bible(path)/bible -f -l 250 -p $bible(path) $invar "??$a2" "?and $a3" "?and $a4" "?and $a5" "?and $a6" | grep -E ef|earch | tail -1]} {
    if {$a5 != ""} {set match [exec $bible(path)/bible -f -l 250 -p $bible(path) $invar "??$a2" "?and $a3" "?and $a4" "?and $a5" | grep -E ef|earch | tail -1]} {
      if {$a4 != ""} {set match [exec $bible(path)/bible -f -l 250 -p $bible(path) $invar "??$a2" "?and $a3" "?and $a4" | grep -E ef|earch | tail -1]} {
        if {$a3 != ""} {set match [exec $bible(path)/bible -f -l 250 -p $bible(path) $invar "??$a2" "?and $a3" | grep -E ef|earch | tail -1]} {
          if {$a2 != ""} {set match [exec $bible(path)/bible -f -l 250 -p $bible(path) $invar "??$a2" | grep -E ef|earch | tail -1]} {
            puthelp "privmsg $chan :You need to specify words to seek." ; return 1
          }
        }
      }
    }
  }
  if {[lindex $match 0] == "No"} {puthelp "privmsg $chan :No matches found for all terms." ; return 1}
  if {[string trimleft [lindex $match 0]] == "Searching"} {puthelp "privmsg $chan :No matches for the '[string trim [lindex $match 2] '.]' term." ; return 1}
  set count [string trim [lindex $match 1] "\[\]:"]
  set matches [lrange $match 2 e]
  if {$count > 24} {puthelp "privmsg $chan :$count matches found.  Please refine your query."} {
    if {$count > 1} {set temp matches} {set temp match}
    if {$count > 0} {puthelp "privmsg $chan :$count $temp found: $matches"} {puthelp "privmsg $chan :No matches found containing all terms."}
  }
  return 1
}

bind pub o|o !verseon pub_verseon
#bind msg - verseon pub_verseon
proc pub_verseon {nick uhost hand channel arg} {global bible ; set bible(mode) 1 ; putserv "notice $nick :Verse Display is now on!" ; return 1}

bind pub - !verseoff pub_verseoff
#bind msg - verseoff pub_verseoff
proc pub_verseoff {nick uhost hand channel arg} {global bible ; set bible(mode) 0 ; putserv "notice $nick :Verse Display is now off!" ; return 1}

bind dcc - verseoff dcc_verseoff
proc dcc_verseoff {hand idx arg} {global bible ; set bible(mode) 0 ; return 1}

bind dcc o verseon dcc_verseon
proc dcc_verseon {hand idx arg} {global bible ; set bible(mode) 1 ; return 1}


