__module_name__,__module_version__=("BOT","0.9.0")
__module_description__="Bible bot for displaying and searching bible textx"
print("\0034"+__module_name__+" "+__module_version__+" loaded\003")
import string,re,time
import xchat
from collections import OrderedDict
# module variables
cmdc=";"
fileName="./Apologia/resources/<n>.properties"
bold,underline,red=("\002","\010","\0034")
# get bible book names and then version names
bookName,versionName=(OrderedDict(),OrderedDict())
bn=fileName.replace("<n>","Biblebooks")
with open(bn,"r") as booksFile:
for line in booksFile:
k,n=line.split("=")
n=(n.replace("\r","")).replace("\n","")
bookName[k]=n
vn=fileName.replace("<n>","BibleVersions")
with open(vn,"r") as versionsFile:
for line in versionsFile:
k,n=line.split("=")
n=(n.replace("\r","")).replace("\n","")
versionName[k]=n
# helper functions
def displayPassage(version,book,ref):
reflist=decodeRef(ref)
chapter=reflist[0]
first,last=(0,0)
if len(reflist)==2: first,last=(int(reflist[1]),int(reflist[1]))
elif len(reflist)==3:
first,last=(int(reflist[1]),int(reflist[2]))
if last<=first: last=first
else:
say("passage reference: "+bold+ref+bold+" is incorrect")
return
keylist=[]
if book.lower()=="ccc": last=first # allow only one CCC verse
if (last-first)>3: last=first+3
for i in range(first,last+1):
s=book+chapter+"_"+str(i)
keylist.append(s)
verses=getVersion(version)
for k in keylist:
try:
ref=refExpander(k)
say(bold+ref+bold+" "+verses[k])
except:
say("I could not find "+bold+refExpander(k)+bold)
def searchVersion(version,phrase):
ss=""
rePhrase=r"\b"+phrase+r"\b"
verses=getVersion(version)
foundVerses=OrderedDict()
i=0
for k in verses:
verse=verses[k]
foundlist=re.findall(rePhrase,removePunctuation(verse).lower())
if len(foundlist)>0:
foundVerses[k]=verses[k]
i=i+1
if i>10: break
ref=refExpander(k)
if i==1:
ss="found in "+version+": "+ref
else:
ss=ss+", "+ref
if i>10:
say(ss+" ... more than 10 found")
return
if i==0:
say("I did not find any occurence of: "+
bold+phrase+bold+" in version: "+version)
return
say(ss)
if i<5:
for fk in foundVerses:
say(bold+refExpander(fk)+bold+" "+foundVerses[fk])
if version.lower()=="ccc":
say(red+"because CCC sections are large only one is displayed")
break
# utility functions
def decodeRef(ref):
s=".,:-_"
b=" "
table=string.maketrans(s,b)
r=string.translate(ref,table)
return r.split()
def getVersion(version):
bv=OrderedDict()
bn=fileName.replace("<n>",version.strip())
try:
properties=open(bn,"r")
for line in properties:
v=line.split("=")
k=v[0].strip()
t=v[1].replace("\r","")
t=t.replace("\n","")
bv[k]=t.strip()
properties.close()
return bv
except:
bv["error"]="I could not find version "+bold+version+bold
return bv
def refExpander(ref):
book=ref[:3]
bName=bookName[book]
chapter=(ref[3:]).split("_")[0]+":"
verse=(ref[4:]).split("_")[1]
if bName=="CCC": chapter=""
r = bName+" "+chapter+verse
return r
def removePunctuation(s):
table=string.maketrans("","")
return s.translate(table,string.punctuation)
def say(ss):
if len(ss)<400: xchat.command("SAY "+ss)
else:
while len(ss)>350:
pos=ss.find(" ",350,)
if pos!=-1:
ssp=ss[:pos]
ss=ss[pos+1:]
else:
ssp=ss[:350]
ss=ss[350:]
xchat.command("SAY "+ssp)
xchat.command("SAY "+ss)
# callback functions
def parseChannelMessage(word,word_eol,userdata):
sender,message=(word[0]," ".join(word[1].split()))
firstchar=message[0]
if firstchar==cmdc:
cmd=message[1:].split(" ")
firstpart=cmd[0].lower()
if firstpart=="versions":
ss=""
for k in versionName:
ss=ss+bold+k+bold+"("+versionName[k]+") "
say("SAY "+ss)
return
if firstpart=="help":
ss=("SAY to display a passage type "+bold+";rsv John 3:16"+bold
+" all bible book names are abbreviated to their first 3"
+" letters, for example 1Kings is 1ki, John is joh, and so forth."
+" There are exceptions: Judges is jdg,"
+" Judith is jdt, and Philemon is phm")
say(ss)
ss=("SAY searching is done by putting a 's' in front of the"
+" version name like this "+bold+";skjv God so loved"+bold
+" that will find all the verses in the KJV containing"
+" 'God so loved'")
say(ss)
ss=("SAY to display versions type "+bold+";versions"+bold)
say(ss)
return
if firstpart[0]=="s":
version=firstpart[1:].upper()
if version not in versionName: return
phrase=(" ".join(cmd[1:])).lower()
searchVersion(version,phrase)
else:
if len(cmd)<3:
if len(cmd)==2:
if cmd[0].upper() in ["CCC","CATECHISM"]:
version=book=cmd[0].upper()
ref="1_"+removePunctuation(cmd[1])
displayPassage(version,book,ref)
elif len(cmd)==3:
version=cmd[0].upper()
if version not in versionName: return
book=cmd[1].upper()
if book in ["JUDGES","JUDG"]: book="JDG"
elif book in ["JUDITH", "JUDI","JDT"]: book="JDT"
elif book in ["PHILEMON","PHL"]: book="PHM"
elif book in ["RUTH","RUT"]: book="RTH"
bref=book[:3]
ref=cmd[2]
displayPassage(version,bref,ref)
else:
return
else:
return
# hooks
xchat.hook_print("Channel Message", parseChannelMessage)
xchat.hook_print("Private Message to Dialog", parseChannelMessage)