2004-01-17 Gernot Hillier <gernot@hillier.de>

* scripts/cs_helpers.pyin (escape,getAudio,uniqueName,sendMIMEMail,
          sendSimpleMail,sayNumber), scripts/idle.py (idle,sendfax,movejob),
          scripts/incoming.py (callIncoming,faxIncoming,voiceIncoming,
          remoteInquiry,newAnnouncement): use % instead of + operator for
          string concatenation (faster; thx to Hartmut Goebel for the hint);
          use os.path.join where appropriate


git-svn-id: https://svn.ibp.de/svn/capisuite/trunk/capisuite@209 4ebea2bb-67d4-0310-8558-a5799e421b66
This commit is contained in:
gernot 2004-01-17 23:11:08 +00:00
parent 68bc34c49e
commit 2327a0eb11
4 changed files with 161 additions and 146 deletions

View File

@ -1,3 +1,11 @@
2004-01-17 Gernot Hillier <gernot@hillier.de>
* scripts/cs_helpers.pyin (escape,getAudio,uniqueName,sendMIMEMail,
sendSimpleMail,sayNumber), scripts/idle.py (idle,sendfax,movejob),
scripts/incoming.py (callIncoming,faxIncoming,voiceIncoming,
remoteInquiry,newAnnouncement): use % instead of + operator for
string concatenation (faster; thx to Hartmut Goebel for the hint);
use os.path.join where appropriate
2004-01-11 Gernot Hillier <gernot@hillier.de>
* docs/manual[-de].docbook: many small improvements, typos, etc.
* docs/manual[-de].docbook (require_hard): mention Linux 2.6 & mISDN,

View File

@ -2,7 +2,7 @@
# -----------------------------------------------------------
# copyright : (C) 2002 by Gernot Hillier
# email : gernot@hillier.de
# version : $Revision: 1.16 $
# version : $Revision: 1.17 $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -44,7 +44,7 @@ def readConfig(file=""):
#
# @return the escaped filename
def escape(filename):
return "'"+filename.replace("'","'\\''")+"'"
return "'%s'" % filename.replace("'","'\\''")
# @brief get an option from the user or global section
#
@ -79,12 +79,12 @@ def getAudio(config,user,filename):
userdir=getOption(config,"","voice_user_dir")
if (userdir==None):
raise IOError("option voice_user_dir not found.")
userdir=os.path.join(userdir,user)+"/"
userdir=os.path.join(userdir,user)
if (int(getOption(config,"","user_audio_files","0"))
and os.access(userdir+filename,os.R_OK)):
return userdir+filename
and os.access(os.path.join(userdir,filename),os.R_OK)):
return os.path.join(userdir,filename)
else:
return systemdir+filename
return os.path.join(systemdir,filename)
# @brief thread-safe creation of a unique filename in a directory
#
@ -105,17 +105,17 @@ def getAudio(config,user,filename):
def uniqueName(directory,basename,suffix):
import fcntl,os,re
# acquire lock
lockfile=open(directory+"cs_lock","w")
lockfile=open(os.path.join(directory,"cs_lock"),"w")
fcntl.lockf(lockfile,fcntl.LOCK_EX)
try:
countfile=open(directory+basename+"-nextnr","r")
countfile=open("%s-nextnr" % os.path.join(directory,basename),"r")
nextnr=int(countfile.readline())
countfile.close()
except IOError:
# search for next free sequence number
files=os.listdir(directory)
files=filter (lambda s: re.match(re.escape(basename)+"-.*\."+re.escape(suffix),s),files)
files=filter (lambda s: re.match("%s-.*\.%s" % (re.escape(basename),re.escape(suffix)),s),files)
if (len(files)):
files=map(lambda s: int(s[len(basename)+1:-len(suffix)-1]),files)
nextnr=max(files)+1 # take nr of last file and increase it by one
@ -123,16 +123,16 @@ def uniqueName(directory,basename,suffix):
nextnr=0
files.sort()
newname=directory+basename+"-"+str(nextnr)+"."+suffix
newname="%s-%i.%s" % (os.path.join(directory,basename),nextnr,suffix)
countfile=open(directory+basename+"-nextnr","w")
countfile.write(str(nextnr+1)+'\n')
countfile=open("%s-nextnr" % os.path.join(directory,basename),"w")
countfile.write('%i\n' % (nextnr+1))
countfile.close()
# unlock
fcntl.lockf(lockfile,fcntl.LOCK_UN)
lockfile.close()
os.unlink(directory+"cs_lock")
os.unlink(os.path.join(directory,"cs_lock"))
return newname
# @brief send email with text and attachment of type sff or la converted to pdf/wav
@ -163,70 +163,70 @@ def sendMIMEMail(mail_from,mail_to,mail_subject,mail_type,text,attachment):
try:
if (mail_type=="sff"): # normal fax file
# sff -> tif
ret=os.spawnlp(os.P_WAIT,"sfftobmp","sfftobmp","-tif",attachment,basename+"tif")
if (ret or not os.access(basename+"tif",os.F_OK)):
ret=os.spawnlp(os.P_WAIT,"sfftobmp","sfftobmp","-tif",attachment,"%stif" % basename)
if (ret or not os.access("%stif" % basename,os.F_OK)):
raise "conv-error","Error while converting sff to tif. File damaged or sfftobmp not installed?"
# tif -> ps -> pdf
# the first pipe must be handled by the shell so that the output of
# of ps2pdf can be read immediately. Handling this shell in Python
# leads to an overflow of the ps2pdf output pipe...
command="tiff2ps -a "+escape(basename+"tif")+" | ps2pdf - -"
command="tiff2ps -a %s | ps2pdf - -" % escape("%stif" % basename)
tiff2pdf=popen2.Popen3(command)
if (tiff2pdf.poll()!=-1):
raise "conv-error","Error while calling tiff2ps or ps2pdf. Not installed?"
tiff2pdf.tochild.close() # we don't need the input pipe
# create attachment with pdf stream
filepart = email.MIMEBase.MIMEBase("application","pdf",name=os.path.basename(basename)+"pdf")
filepart.add_header('Content-Disposition','attachment',filename=os.path.basename(basename)+"pdf")
filepart = email.MIMEBase.MIMEBase("application","pdf",name="%spdf" % os.path.basename(basename))
filepart.add_header('Content-Disposition','attachment',filename="%spdf" % os.path.basename(basename))
filepart.set_payload(tiff2pdf.fromchild.read())
tiff2pdf.fromchild.close()
ret=tiff2pdf.wait()
if (ret!=0):
raise "conv-error","Error "+str(ret)+" occured during tiff2ps or ps2pdf"
os.unlink(basename+"tif")
raise "conv-error","Error %i occured during tiff2ps or ps2pdf" % ret
os.unlink("%stif" % basename)
email.Encoders.encode_base64(filepart)
elif (mail_type=="cff"): # color fax file
# cff -> ps
ret=os.spawnlp(os.P_WAIT,"jpeg2ps","jpeg2ps","-m",attachment,"-o",basename+"ps")
if (ret or not os.access(basename+"ps",os.F_OK)):
ret=os.spawnlp(os.P_WAIT,"jpeg2ps","jpeg2ps","-m",attachment,"-o","%sps" % basename)
if (ret or not os.access("%sps" % basename,os.F_OK)):
raise "conv-error","Can't convert cff to ps. File damaged or jpeg2ps not installed?"
# tif -> ps -> pdf
# the first pipe must be handled by the shell so that the output of
# of ps2pdf can be read immediately. Handling this shell in Python
# leads to an overflow of the ps2pdf output pipe...
command="ps2pdf "+escape(basename+"ps")+" -"
command="ps2pdf %s -" % escape("%sps" % basename)
ps2pdf=popen2.Popen3(command)
if (ps2pdf.poll()!=-1):
raise "conv-error","Error while calling ps2pdf. Not installed?"
ps2pdf.tochild.close() # we don't need the input pipe
# create attachment with pdf stream
filepart = email.MIMEBase.MIMEBase("application","pdf",name=os.path.basename(basename)+"pdf")
filepart.add_header('Content-Disposition','attachment',filename=os.path.basename(basename)+"pdf")
filepart = email.MIMEBase.MIMEBase("application","pdf",name="%spdf" % os.path.basename(basename))
filepart.add_header('Content-Disposition','attachment',filename="%spdf" % os.path.basename(basename))
filepart.set_payload(ps2pdf.fromchild.read())
ps2pdf.fromchild.close()
ret=ps2pdf.wait()
if (ret!=0):
raise "conv-error","Error "+str(ret)+" occured during ps2pdf"
os.unlink(basename+"ps")
raise "conv-error","Error %i occured during ps2pdf" % ret
os.unlink("%sps" % basename)
email.Encoders.encode_base64(filepart)
elif (mail_type=="la"): # voice file
# la -> wav
# don't use stdout as sox needs a file to be able to seek in it otherwise the header will be incomplete
ret = os.spawnlp(os.P_WAIT,"sox","sox",attachment,"-w ",basename+"wav")
if (ret or not os.access(basename+"wav",os.R_OK)):
ret = os.spawnlp(os.P_WAIT,"sox","sox",attachment,"-w ","%swav" % basename)
if (ret or not os.access("%swav" % basename,os.R_OK)):
raise "conv-error","Error while calling sox. File damaged or sox not installed?"
filepart = email.MIMEAudio.MIMEAudio(open(basename+"wav").read(),"x-wav",email.Encoders.encode_base64,name=os.path.basename(basename)+"wav")
filepart.add_header('Content-Disposition','attachment',filename=os.path.basename(basename)+"wav")
os.unlink(basename+"wav")
filepart = email.MIMEAudio.MIMEAudio(open("%swav" % basename).read(),"x-wav",email.Encoders.encode_base64,name="%swav" % os.path.basename(basename))
filepart.add_header('Content-Disposition','attachment',filename="%swav" % os.path.basename(basename))
os.unlink("%swav" % basename)
textpart = email.MIMEText.MIMEText(text)
msg.attach(textpart)
msg.attach(filepart)
except "conv-error",errormessage:
text+="\n\nERROR occured while converting file: "+errormessage+"\nPlease talk to your friendly administrator.\n"
text="%s\n\nERROR occured while converting file: %s\nPlease talk to your friendly administrator.\n" % (text,errormessage)
textpart = email.MIMEText.MIMEText(text)
msg.attach(textpart)
sendmail = popen2.Popen3("sendmail -t -f "+escape(mail_from))
sendmail = popen2.Popen3("sendmail -t -f %s" % escape(mail_from))
if (sendmail.poll()!=-1):
capisuite.error("Error while calling sendmail. Not installed?\n")
return
@ -235,7 +235,7 @@ def sendMIMEMail(mail_from,mail_to,mail_subject,mail_type,text,attachment):
sendmail.fromchild.close()
ret=sendmail.wait()
if (ret!=0):
capisuite.error("Error while calling sendmail, return code="+str(ret))
capisuite.error("Error while calling sendmail, return code=%i" % ret)
else:
capisuite.log("sendmail finished successful",3)
@ -257,7 +257,7 @@ def sendSimpleMail(mail_from,mail_to,mail_subject,text):
msg['From'] = mail_from
msg['To'] = mail_to
sendmail = popen2.Popen3("sendmail -t -f "+escape(mail_from))
sendmail = popen2.Popen3("sendmail -t -f %s" % escape(mail_from))
if (sendmail.poll()!=-1):
capisuite.error("Error while calling sendmail. Not installed?\n")
return
@ -266,7 +266,7 @@ def sendSimpleMail(mail_from,mail_to,mail_subject,text):
sendmail.fromchild.close()
ret=sendmail.wait()
if (ret!=0):
capisuite.error("Error while calling sendmail, return code="+str(ret))
capisuite.error("Error while calling sendmail, return code=%i" % ret)
else:
capisuite.log("sendmail finished successful",3)
@ -280,12 +280,12 @@ def sendSimpleMail(mail_from,mail_to,mail_subject,text):
# @param filename the data filename (with extension!)
# @param content the content as string
def writeDescription(filename,content):
descr=open(filename[:filename.rindex('.')+1]+"txt","w")
descr.write("# Description file for "+filename+"\n")
descr=open("%stxt" % filename[:filename.rindex('.')+1],"w")
descr.write("# Description file for %s\n" % filename)
descr.write("# This if for internal use of CapiSuite.\n")
descr.write("# Only change if you know what you do!!\n")
descr.write("[GLOBAL]\n")
descr.write("filename=\""+filename+"\"\n")
descr.write("filename=\"%s\"\n" % filename)
descr.write(content)
descr.close()
@ -327,18 +327,18 @@ def sayNumber(call,number,curr_user,config):
capisuite.audio_send(call,getAudio(config,curr_user,"19.la"),1)
else:
if (number[1]=="0"):
capisuite.audio_send(call,getAudio(config,curr_user,number+".la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,"%s.la" % number),1)
elif (number[1]=="1"):
capisuite.audio_send(call,getAudio(config,curr_user,"ein.la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,"und.la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,number[0]+"0.la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,"%s0.la" % number[0]),1)
else:
capisuite.audio_send(call,getAudio(config,curr_user,number[1]+".la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,"%s.la" % number[1]),1)
capisuite.audio_send(call,getAudio(config,curr_user,"und.la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,number[0]+"0.la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,"%s0.la" % number[0]),1)
else:
for i in number:
capisuite.audio_send(call,getAudio(config,curr_user,i+".la"),1)
capisuite.audio_send(call,getAudio(config,curr_user,"%s.la" % i),1)
# Old Log (for new changes see ChangeLog):
# Revision 1.14 2003/10/19 20:17:54 gernot

View File

@ -2,7 +2,7 @@
# ---------------------------------------------
# copyright : (C) 2002 by Gernot Hillier
# email : gernot@hillier.de
# version : $Revision: 1.12 $
# version : $Revision: 1.13 $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -21,8 +21,8 @@ def idle(capi):
capisuite.error("global option spool_dir not found.")
return
done=os.path.join(spool,"done")+"/"
failed=os.path.join(spool,"failed")+"/"
done=os.path.join(spool,"done")
failed=os.path.join(spool,"failed")
if (not os.access(done,os.W_OK) or not os.access(failed,os.W_OK)):
capisuite.error("Can't read/write to the necessary spool dirs")
@ -45,8 +45,8 @@ def idle(capi):
if (udir==None):
capisuite.error("global option fax_user_dir not found.")
return
udir=os.path.join(udir,user)+"/"
sendq=os.path.join(udir,"sendq")+"/"
udir=os.path.join(udir,user)
sendq=os.path.join(udir,"sendq")
if (not os.access(udir,os.F_OK)):
os.mkdir(udir,0700)
os.chown(udir,userdata[2],userdata[3])
@ -58,31 +58,31 @@ def idle(capi):
files=filter (lambda s: re.match("fax-.*\.txt",s),files)
for job in files:
job_fax=job[:-3]+"sff"
real_user_c=os.stat(sendq+job).st_uid
real_user_j=os.stat(sendq+job_fax).st_uid
job_fax="%ssff" % job[:-3]
real_user_c=os.stat(os.path.join(sendq,job)).st_uid
real_user_j=os.stat(os.path.join(sendq,job_fax)).st_uid
if (real_user_j!=pwd.getpwnam(user)[2] or real_user_c!=pwd.getpwnam(user)[2]):
capisuite.error("job "+sendq+job_fax+" seems to be manipulated (wrong uid)! Ignoring...")
capisuite.error("job %s seems to be manipulated (wrong uid)! Ignoring..." % os.path.join(sendq,job_fax))
continue
lockfile=open(sendq+job[:-3]+"lock","w")
lockfile=open(os.path.join(sendq,"%slock" % job[:-3]),"w")
# read directory contents
fcntl.lockf(lockfile,fcntl.LOCK_EX) # lock so that it isn't deleted while sending
if (not os.access(sendq+job,os.W_OK)): # perhaps it was cancelled?
if (not os.access(os.path.join(sendq,job),os.W_OK)): # perhaps it was cancelled?
fcntl.lockf(lockfile,fcntl.LOCK_UN)
lockfile.close()
os.unlink(sendq+job[:-3]+"lock")
os.unlink(os.path.join(sendq,"%slock" % job[:-3]))
continue
control=cs_helpers.readConfig(sendq+job)
control=cs_helpers.readConfig(os.path.join(sendq,job))
# set DST value to -1 (unknown), as strptime sets it wrong for some reason
starttime=(time.strptime(control.get("GLOBAL","starttime")))[0:8]+(-1,)
starttime=time.mktime(starttime)
if (starttime>time.time()):
fcntl.lockf(lockfile,fcntl.LOCK_UN)
lockfile.close()
os.unlink(sendq+job[:-3]+"lock")
os.unlink(os.path.join(sendq,"%slock" % job[:-3]))
continue
tries=control.getint("GLOBAL","tries")
@ -96,21 +96,23 @@ def idle(capi):
if (fromaddress==""):
fromaddress=user
capisuite.log("job "+job_fax+" from "+user+" to "+dialstring+" initiated",1)
result,resultB3 = sendfax(capi,sendq+job_fax,outgoing_nr,dialstring,user,config)
capisuite.log("job %s from %s to %s initiated" % (job_fax,user,dialstring),1)
result,resultB3 = sendfax(capi,os.path.join(sendq,job_fax),outgoing_nr,dialstring,user,config)
tries+=1
capisuite.log("job "+job_fax+": result was %x,%x" % (result,resultB3),1)
capisuite.log("job %s: result was %x,%x" % (job_fax,result,resultB3),1)
if (result in (0,0x3400,0x3480,0x3490,0x349f) and resultB3==0):
movejob(job_fax,sendq,done,user)
capisuite.log("job "+job_fax+": finished successfully",1)
mailtext="Your fax job to "+addressee+" ("+dialstring+") was sent successfully.\n\n" \
+"Subject: "+subject+"\nFilename: "+job_fax \
+"\nNeeded tries: "+str(tries) \
+("\nLast result: 0x%x/0x%x" % (result,resultB3)) \
+"\n\nIt was moved to file://"+done+user+"-"+job_fax
capisuite.log("job %s: finished successfully" % job_fax,1)
mailtext="Your fax job to %s (%s) was sent successfully.\n\n" \
"Subject: %s\nFilename: %s\nNeeded tries: %i\n" \
"Last result: 0x%x/0x%x\n\nIt was moved to " \
"file://%s on host \"%s\"" % (addressee,dialstring, \
subject,job_fax,tries,result,resultB3, \
os.path.join(done,"%s-%s" % (user,job_fax)), \
os.uname()[1])
cs_helpers.sendSimpleMail(fromaddress,mailaddress,
"Fax to "+addressee+" ("+dialstring+") sent successfully.",
"Fax to %s (%s) sent successfully." % (addressee,dialstring),
mailtext)
else:
max_tries=int(cs_helpers.getOption(config,"","send_tries","10"))
@ -121,25 +123,29 @@ def idle(capi):
else:
next_delay=delays[-1]
starttime=time.time()+next_delay
capisuite.log("job "+job_fax+": delayed for "+str(next_delay)+" seconds",2)
cs_helpers.writeDescription(sendq+job_fax,"dialstring=\""+dialstring+"\"\n"
+"starttime=\""+time.ctime(starttime)+"\"\ntries=\""+str(tries)+"\"\n"
+"user=\""+user+"\"\naddressee=\""+addressee+"\"\nsubject=\""+subject+"\"\n")
capisuite.log("job %s: delayed for %i seconds" % (job_fax,next_delay),2)
cs_helpers.writeDescription(os.path.join(sendq,job_fax), \
"dialstring=\"%s\"\nstarttime=\"%s\"\ntries=\"%i\"\n" \
"user=\"%s\"\naddressee=\"%s\"\nsubject=\"%s\"\n" \
% (dialstring,time.ctime(starttime),tries,user, \
addressee,subject))
if (tries>=max_tries):
movejob(job_fax,sendq,failed,user)
capisuite.log("job "+job_fax+": failed finally",1)
mailtext="I'm sorry, but your fax job to "+addressee+" ("+dialstring \
+") failed finally.\n\nSubject: "+subject \
+"\nFilename: "+job_fax+"\nTries: "+str(tries) \
+"\nLast result: 0x%x/0x%x" % (result,resultB3) \
+"\n\nIt was moved to file://"+failed+user+"-"+job_fax
capisuite.log("job %s: failed finally" % job_fax,1)
mailtext="I'm sorry, but your fax job to %s (%s) " \
"failed finally.\n\nSubject: %s\n" \
"Filename: %s\nTries: %i\n" \
"Last result: 0x%x/0x%x\n\n" \
"It was moved to file://%s-%s on host %s.\n\n" \
% (addressee,dialstring,subject,job_fax,tries,result, \
resultB3,os.path.join(failed,user),job_fax,os.uname()[1])
cs_helpers.sendSimpleMail(fromaddress,mailaddress,
"Fax to "+addressee+" ("+dialstring+") FAILED.",
"Fax to %s (%s) FAILED." % (addressee,dialstring),
mailtext)
fcntl.lockf(lockfile,fcntl.LOCK_UN)
lockfile.close()
os.unlink(sendq+job[:-3]+"lock")
os.unlink("%slock" % os.path.join(sendq,job[:-3]))
def sendfax(capi,job,outgoing_nr,dialstring,user,config):
try:
@ -147,7 +153,7 @@ def sendfax(capi,job,outgoing_nr,dialstring,user,config):
timeout=int(cs_helpers.getOption(config,user,"outgoing_timeout","60"))
stationID=cs_helpers.getOption(config,user,"fax_stationID")
if (stationID==None):
capisuite.error("Warning: fax_stationID for user "+user+" not set")
capisuite.error("Warning: fax_stationID for user %s not set" % user)
stationID=""
headline=cs_helpers.getOption(config,user,"fax_headline","")
(call,result)=capisuite.call_faxG3(capi,controller,outgoing_nr,dialstring,timeout,stationID,headline)
@ -159,8 +165,8 @@ def sendfax(capi,job,outgoing_nr,dialstring,user,config):
return(capisuite.disconnect(call))
def movejob(job,olddir,newdir,user):
os.rename(olddir+job,newdir+user+"-"+job)
os.rename(olddir+job[:-3]+"txt",newdir+user+"-"+job[:-3]+"txt")
os.rename(os.path.join(olddir,job),os.path.join(newdir,"%s-%s" % (user,job)))
os.rename(os.path.join(olddir,"%stxt" % job[:-3]),os.path.join(newdir, "%s-%stxt" % (user,job[:-3])))
#
# History:

View File

@ -2,7 +2,7 @@
# ----------------------------------------------------
# copyright : (C) 2002 by Gernot Hillier
# email : gernot@hillier.de
# version : $Revision: 1.15 $
# version : $Revision: 1.16 $
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -54,22 +54,22 @@ def callIncoming(call,service,call_from,call_to):
break
except IOError,e:
capisuite.error("Error occured during config file reading: "+e+" Disconnecting...")
capisuite.error("Error occured during config file reading: %s Disconnecting..." % e)
capisuite.reject(call,0x34A9)
return
# answer the call with the right service
if (curr_user==""):
capisuite.log("call from "+call_from+" to "+call_to+" ignoring",1,call)
capisuite.log("call from %s to %s ignoring" % (call_from,call_to),1,call)
capisuite.reject(call,1)
return
try:
if (curr_service==capisuite.SERVICE_VOICE):
delay=cs_helpers.getOption(config,curr_user,"voice_delay")
if (delay==None):
capisuite.error("voice_delay not found for user "+curr_user+"! -> rejecting call")
capisuite.error("voice_delay not found for user %s! -> rejecting call" % curr_user)
capisuite.reject(call,0x34A9)
return
capisuite.log("call from "+call_from+" to "+call_to+" for "+curr_user+" connecting with voice",1,call)
capisuite.log("call from %s to %s for %s connecting with voice" % (call_from,call_to,curr_user),1,call)
capisuite.connect_voice(call,int(delay))
voiceIncoming(call,call_from,call_to,curr_user,config)
elif (curr_service==capisuite.SERVICE_FAXG3):
@ -79,7 +79,7 @@ def callIncoming(call,service,call_from,call_to):
capisuite.log("connection lost with cause 0x%x,0x%x" % (cause,causeB3),1,call)
# @brief called by callIncoming when an incoming fax call is received
#
#
# @param call reference to the call. Needed by all capisuite functions
# @param call_from string containing the number of the calling party
# @param call_to string containing the number of the called party
@ -93,17 +93,17 @@ def faxIncoming(call,call_from,call_to,curr_user,config,already_connected):
capisuite.error("global option fax_user_dir not found! -> rejecting call")
capisuite.reject(call,0x34A9)
return
udir=os.path.join(udir,curr_user)+"/"
if (not os.access(udir,os.F_OK)):
udir=os.path.join(udir,curr_user)
if (not os.path.exists(udir)):
userdata=pwd.getpwnam(curr_user)
os.mkdir(udir,0700)
os.chown(udir,userdata[2],userdata[3])
if (not os.access(udir+"received/",os.F_OK)):
if (not os.path.exists(os.path.join(udir,"received"))):
userdata=pwd.getpwnam(curr_user)
os.mkdir(udir+"received/",0700)
os.chown(udir+"received/",userdata[2],userdata[3])
os.mkdir(os.path.join(udir,"received"),0700)
os.chown(os.path.join(udir,"received"),userdata[2],userdata[3])
except KeyError:
capisuite.error("user "+curr_user+" is not a valid system user. Disconnecting",call)
capisuite.error("user %s is not a valid system user. Disconnecting" % curr_user,call)
capisuite.reject(call,0x34A9)
return
filename="" # assure the variable is defined...
@ -111,10 +111,10 @@ def faxIncoming(call,call_from,call_to,curr_user,config,already_connected):
try:
stationID=cs_helpers.getOption(config,curr_user,"fax_stationID")
if (stationID==None):
capisuite.error("Warning: fax_stationID not found for user "+curr_user+" -> using empty string")
capisuite.error("Warning: fax_stationID not found for user %s -> using empty string" % curr_user)
stationID=""
headline=cs_helpers.getOption(config,curr_user,"fax_headline","") # empty string is no problem here
capisuite.log("call from "+call_from+" to "+call_to+" for "+curr_user+" connecting with fax",1,call)
capisuite.log("call from %s to %s for %s connecting with fax" % (call_from,call_to,curr_user),1,call)
if (already_connected):
faxInfo=capisuite.switch_to_faxG3(call,stationID,headline)
else:
@ -123,7 +123,7 @@ def faxIncoming(call,call_from,call_to,curr_user,config,already_connected):
faxFormat="cff" # color fax
else:
faxFormat="sff" # normal b&w fax
filename=cs_helpers.uniqueName(udir+"received/","fax",faxFormat)
filename=cs_helpers.uniqueName(os.path.join(udir,"received"),"fax",faxFormat)
faxInfo=capisuite.fax_receive(call,filename)
(cause,causeB3)=capisuite.disconnect(call)
capisuite.log("connection finished with cause 0x%x,0x%x" % (cause,causeB3),1,call)
@ -134,13 +134,13 @@ def faxIncoming(call,call_from,call_to,curr_user,config,already_connected):
if (os.access(filename,os.R_OK)):
cs_helpers.writeDescription(filename,
"call_from=\""+call_from+"\"\ncall_to=\""+call_to+"\"\ntime=\""
+time.ctime()+"\"\ncause=\"0x%x/0x%x\"\n" % (cause,causeB3))
"call_from=\"%s\"\ncall_to=\"%s\"\ntime=\"%s\"\n" \
"cause=\"0x%x/0x%x\"\n" % (call_from,call_to,time.ctime(),cause,causeB3))
userdata=pwd.getpwnam(curr_user)
os.chmod(filename,0600)
os.chown(filename,userdata[2],userdata[3])
os.chmod(filename[:-3]+"txt",0600)
os.chown(filename[:-3]+"txt",userdata[2],userdata[3])
os.chmod("%stxt" % filename[:-3],0600)
os.chown("%stxt" % filename[:-3],userdata[2],userdata[3])
fromaddress=cs_helpers.getOption(config,curr_user,"fax_email_from","")
if (fromaddress==""):
@ -150,18 +150,19 @@ def faxIncoming(call,call_from,call_to,curr_user,config,already_connected):
mailaddress=curr_user
action=cs_helpers.getOption(config,curr_user,"fax_action","").lower()
if (action not in ("mailandsave","saveonly")):
capisuite.error("Warning: No valid fax_action definition found for user "+curr_user+" -> assuming SaveOnly")
capisuite.error("Warning: No valid fax_action definition found for user %s -> assuming SaveOnly" % curr_user)
action="saveonly"
if (action=="mailandsave"):
mailText="You got a fax from "+call_from+" to "+call_to+"\nDate: "+time.ctime()
mailText+=str(faxInfo)
mailText="You got a fax from %s to %s\nDate: %s" % (call_from,call_to,time.ctime())
if (faxInfo!=None and len(faxInfo)>=5):
mailText+="Station ID: "+faxInfo[0]+"\nTransmission Details: bit rate "+str(faxInfo[1]) \
+(faxInfo[2] and "hiRes" or "loRes")+(faxInfo[3] and "color" or "")+"\nPages: " \
+str(faxInfo[4])
mailText+="\n\nSee attached file.\nThe original file was saved to file://"+filename \
+" on host \""+os.uname()[1]+"\"."
cs_helpers.sendMIMEMail(fromaddress, mailaddress, "Fax received from "+call_from+" to "+call_to,
mailText="%sStation ID: %s\nTransmission Details: bit rate %i " \
"%s %s\nPages: %i\n\nSee attached file.\n" \
"The original file was saved to file://%s " \
"on host \"%s\"." % (mailText,faxInfo[0], \
faxInfo[1],(faxInfo[2] and "hiRes" or "loRes"), \
(faxInfo[3] and "color" or ""),faxInfo[4], \
fileName,os.uname()[1])
cs_helpers.sendMIMEMail(fromaddress, mailaddress, "Fax received from %s to %s" % (call_from,call_to),
faxFormat, mailText, filename)
# @brief called by callIncoming when an incoming voice call is received
@ -178,27 +179,27 @@ def voiceIncoming(call,call_from,call_to,curr_user,config):
capisuite.error("global option voice_user_dir not found! -> rejecting call")
capisuite.reject(call,0x34A9)
return
udir=os.path.join(udir,curr_user)+"/"
if (not os.access(udir,os.F_OK)):
udir=os.path.join(udir,curr_user)
if (not os.path.exists(udir)):
userdata=pwd.getpwnam(curr_user)
os.mkdir(udir,0700)
os.chown(udir,userdata[2],userdata[3])
if (not os.access(udir+"received/",os.F_OK)):
if (not os.path.exists(os.path.join(udir,"received"))):
userdata=pwd.getpwnam(curr_user)
os.mkdir(udir+"received/",0700)
os.chown(udir+"received/",userdata[2],userdata[3])
os.mkdir(os.path.join(udir,"received"),0700)
os.chown(os.path.join(udir,"received"),userdata[2],userdata[3])
except KeyError:
capisuite.error("user "+curr_user+" is not a valid system user. Disconnecting",call)
capisuite.error("user %s is not a valid system user. Disconnecting" % curr_user,call)
capisuite.reject(call,0x34A9)
return
filename=cs_helpers.uniqueName(udir+"received/","voice","la")
filename=cs_helpers.uniqueName(os.path.join(udir,"received"),"voice","la")
action=cs_helpers.getOption(config,curr_user,"voice_action","").lower()
if (action not in ("mailandsave","saveonly","none")):
capisuite.error("Warning: No valid voice_action definition found for user "+curr_user+" -> assuming SaveOnly")
capisuite.error("Warning: No valid voice_action definition found for user %s -> assuming SaveOnly" % curr_user)
action="saveonly"
try:
capisuite.enable_DTMF(call)
userannouncement=udir+cs_helpers.getOption(config,curr_user,"announcement","announcement.la")
userannouncement=os.path.join(udir,cs_helpers.getOption(config,curr_user,"announcement","announcement.la"))
pin=cs_helpers.getOption(config,curr_user,"pin","")
if (os.access(userannouncement,os.R_OK)):
capisuite.audio_send(call,userannouncement,1)
@ -220,7 +221,7 @@ def voiceIncoming(call,call_from,call_to,curr_user,config):
os.unlink(filename)
faxIncoming(call,call_from,call_to,curr_user,config,1)
elif (dtmf_list!="" and pin!=""):
dtmf_list+=capisuite.read_DTMF(call,3) # wait 5 seconds for input
dtmf_list="%s%s" % (dtmf_list,capisuite.read_DTMF(call,3)) # wait 5 seconds for input
count=1
while (count<3 and pin!=dtmf_list): # try again if input was wrong
capisuite.log("wrong PIN entered...",1,call)
@ -242,13 +243,13 @@ def voiceIncoming(call,call_from,call_to,curr_user,config):
if (os.access(filename,os.R_OK)):
cs_helpers.writeDescription(filename,
"call_from=\""+call_from+"\"\ncall_to=\""+call_to+"\"\ntime=\""
+time.ctime()+"\"\ncause=\"0x%x/0x%x\"\n" % (cause,causeB3))
"call_from=\"%s\"\ncall_to=\"%s\"\ntime=\"%s\"\n" \
"cause=\"0x%x/0x%x\"\n" % (call_from,call_to,time.ctime(),cause,causeB3))
userdata=pwd.getpwnam(curr_user)
os.chmod(filename,0600)
os.chown(filename,userdata[2],userdata[3])
os.chmod(filename[:-2]+"txt",0600)
os.chown(filename[:-2]+"txt",userdata[2],userdata[3])
os.chmod("%stxt" % filename[:-2],0600)
os.chown("%stxt" % filename[:-2],userdata[2],userdata[3])
fromaddress=cs_helpers.getOption(config,curr_user,"voice_email_from","")
if (fromaddress==""):
@ -257,12 +258,12 @@ def voiceIncoming(call,call_from,call_to,curr_user,config):
if (mailaddress==""):
mailaddress=curr_user
if (action=="mailandsave"):
mailText="You got a voice call from "+call_from+" to " \
+call_to+"\nDate: "+time.ctime()+"\nLength: " \
+str(msg_length)+" s\n\nSee attached file.\n" \
+"The original file was saved to file://"+filename \
+" on host \""+os.uname()[1]+"\".\n\n"
subject="Voice call received from "+call_from+" to "+call_to
mailText="You got a voice call from %s to %s\n" \
"Date: %s\nLength: %i s\n\nSee attached file.\n" \
"The original file was saved to file://%s on" \
"host \"%s\".\n\n" % (call_from,call_to,time.ctime(), \
msg_length,filename,os.uname()[1])
subject="Voice call received from %s to %s" % (call_from,call_to)
cs_helpers.sendMIMEMail(fromaddress,mailaddress,subject,"la",mailText,filename)
# @brief remote inquiry function (uses german wave snippets!)
@ -280,7 +281,7 @@ def voiceIncoming(call,call_from,call_to,curr_user,config):
def remoteInquiry(call,userdir,curr_user,config):
import time,fcntl,errno,os
# acquire lock
lockfile=open(userdir+"received/inquiry_lock","w")
lockfile=open(os.path.join(userdir,"received/inquiry_lock"),"w")
try:
fcntl.lockf(lockfile,fcntl.LOCK_EX | fcntl.LOCK_NB) # only one inquiry at a time!
@ -292,15 +293,15 @@ def remoteInquiry(call,userdir,curr_user,config):
try:
# read directory contents
messages=os.listdir(userdir+"received/")
messages=os.listdir(os.path.join(userdir,"received"))
messages=filter (lambda s: re.match("voice-.*\.la",s),messages) # only use voice-* files
messages=map(lambda s: int(re.match("voice-([0-9]+)\.la",s).group(1)),messages) # filter out numbers
messages.sort()
# read the number of the message heard last at the last inquiry
lastinquiry=-1
if (os.access(userdir+"received/last_inquiry",os.W_OK)):
lastfile=open(userdir+"received/last_inquiry","r")
if (os.access(os.path.join(userdir,"received/last_inquiry"),os.W_OK)):
lastfile=open(os.path.join(userdir,"received/last_inquiry"),"r")
lastinquiry=int(lastfile.readline())
lastfile.close()
@ -347,8 +348,8 @@ def remoteInquiry(call,userdir,curr_user,config):
i=0
while (i<len(curr_msgs)):
filename=userdir+"received/voice-"+str(curr_msgs[i])+".la"
descr=cs_helpers.readConfig(filename[:-2]+"txt")
filename=os.path.join(userdir,"received/voice-%i.la" % curr_msgs[i])
descr=cs_helpers.readConfig("%stxt" % filename[:-2])
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"nachricht.la"),1)
cs_helpers.sayNumber(call,str(i+1),curr_user,config)
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"von.la"),1)
@ -372,14 +373,14 @@ def remoteInquiry(call,userdir,curr_user,config):
cmd=capisuite.read_DTMF(call,0,1)
if (cmd=="1"):
os.remove(filename)
os.remove(filename[:-2]+"txt")
os.remove("%stxt" % filename[:-2])
del curr_msgs[i]
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"nachricht-geloescht.la"))
elif (cmd=="4"):
if (curr_msgs[i]>lastinquiry):
lastinquiry=curr_msgs[i]
lastfile=open(userdir+"received/last_inquiry","w")
lastfile.write(str(curr_msgs[i])+"\n")
lastfile=open(os.path.join(userdir,"received/last_inquiry"),"w")
lastfile.write("%i\n" % curr_msgs[i])
lastfile.close()
i+=1
elif (cmd=="5"):
@ -390,7 +391,7 @@ def remoteInquiry(call,userdir,curr_user,config):
# unlock
fcntl.lockf(lockfile,fcntl.LOCK_UN)
lockfile.close()
os.unlink(userdir+"received/inquiry_lock")
os.unlink(os.path.join(userdir,"received/inquiry_lock"))
# @brief remote inquiry: record new announcement (uses german wave snippets!)
#
@ -403,16 +404,16 @@ def newAnnouncement(call,userdir,curr_user,config):
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"beep.la"))
cmd=""
while (cmd!="1"):
capisuite.audio_receive(call,userdir+"announcement-tmp.la",60,3)
capisuite.audio_receive(call,os.path.join(userdir,"announcement-tmp.la"),60,3)
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"neue-ansage-lautet.la"))
capisuite.audio_send(call,userdir+"announcement-tmp.la")
capisuite.audio_send(call,os.path.join(userdir,"announcement-tmp.la"))
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"wenn-einverstanden-1.la"))
cmd=capisuite.read_DTMF(call,0,1)
if (cmd!="1"):
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"bitte-neue-ansage-kurz.la"))
capisuite.audio_send(call,cs_helpers.getAudio(config,curr_user,"beep.la"))
userannouncement=userdir+cs_helpers.getOption(config,curr_user,"announcement","announcement.la")
os.rename(userdir+"announcement-tmp.la",userannouncement)
userannouncement=os.path.join(userdir,cs_helpers.getOption(config,curr_user,"announcement","announcement.la"))
os.rename(os.path.join(userdir,"announcement-tmp.la"),userannouncement)
userdata=pwd.getpwnam(curr_user)
os.chown(userannouncement,userdata[2],userdata[3])