__module_name__,__module_version__=("BOT","0.3.0")
__module_description__="Module for a bible bot"
print("\0034"+__module_name__+" "+__module_version__+" loaded\003")
import string,re,time
import xchat
from collections import OrderedDict
###
# module variables
###
cmdc=";"
fn="./Apologia/resources/<n>.properties"
formats={"bold":"\002","colour":"\003","underline":"\010","italics":"\035"}
bold,underline=("\002","\010")
# get bible book names and then version names
bookName,versionName=(OrderedDict(),OrderedDict())
bn=fn.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=fn.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):
lr=decodeRef(ref)
lenlr=len(lr)
chapter=""
first,last=(0,0)
if lenlr==2:
chapter=lr[0]
first,last=(int(lr[1]),int(lr[1]))
elif lenlr==3:
chapter=lr[0]
first,last=(int(lr[1]),int(lr[2]))
if last<=first:
last=first
else:
xchat.command("SAY passage reference: "+bold+ref+bold+" is incorrect")
keylist=[]
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:
k=str(k)
ref=refExpander(k)
xchat.command("SAY "+bold+ref+bold+" "+verses[k])
except Exception,args:
xchat.command("SAY "+verses["error"])
def searchVersion(version,phrase):
sayString=""
rePhrase=r"\b"+phrase+r"\b"
verses=getVersion(version)
foundVerses=OrderedDict()
i=0
for k in verses:
verse=verses[k]
c=re.findall(rePhrase,removePunctuation(verse).lower())
if len(c)>0:
foundVerses[k]=verses[k]
i=i+1
if i>10: break
ref=refExpander(k)
if i==1:
sayString="SAY found in "+version+": "+ref
else:
sayString=sayString+", "+ref
if i>10: return xchat.command(sayString+" ... more than 10 found")
if i==0: return xchat.command("SAY I did not find any occurence of: "+
bold+phrase+bold+" in version: "+version)
if i<5:
for fk in foundVerses:
return xchat.command("SAY "+bold+refExpander(fk)+bold+" "+foundVerses[fk])
###
# utility functions
###
def decodeRef(ref):
s=".,:-_"
b=" "
table=string.maketrans(s,b)
r=string.translate(ref,table)
return r.split()
def getVersion(version):
global fn
bv=OrderedDict()
bn=fn.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 Exception,args:
print Exception,args
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]
r = bName+" "+chapter+":"+verse
return r
def removePunctuation(s):
table=string.maketrans("","")
return s.translate(table,string.punctuation)
###
# callback functions
###
def parseChannelMessage(word,word_eol,userdata):
sender=word[0]
message=word[1]
message=" ".join(message.split())
firstchar=message[0]
if firstchar==cmdc:
cmd=message[1:].split(" ")
firstpart=cmd[0]
if firstpart=="versions":
sayString=""
for k in versionName:
sayString=sayString+bold+k+bold+"("+versionName[k]+") "
return xchat.command("SAY "+sayString)
if firstpart[0].lower()=="s":
version=firstpart[1:].upper()
phrase=(" ".join(cmd[1:])).lower()
searchVersion(version,phrase)
else:
if len(cmd)<3:
return
#return xchat.command("SAY I cannot understand command: "
# +formats["bold"]+message+formats["bold"])
elif len(cmd)==3:
version=cmd[0].upper()
book=cmd[1].upper()
if book in ["JUDGES","JUDG"]:
book="JDG"
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 xchat.command("SAY I cannot understand command: "
+formats["bold"]+message+formats["bold"])
else:
return
###
# hooks
###
xchat.hook_print("Channel Message", parseChannelMessage)
xchat.hook_print("Private Message to Dialog", parseChannelMessage)