From 18bd9cd8477cf5a11c7e1382db80ed164ce0364f Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Mon, 7 May 2012 02:43:08 +0200 Subject: [PATCH] [IMP] Better AGI script : - cleaner code - should avoid these Asterisk errors : utils.c ast_carefulwrite: write() returned error: Broken pipe --- asterisk_click2dial/scripts/get_cid_name.py | 25 ++++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/asterisk_click2dial/scripts/get_cid_name.py b/asterisk_click2dial/scripts/get_cid_name.py index 6b24383..e9fbd57 100755 --- a/asterisk_click2dial/scripts/get_cid_name.py +++ b/asterisk_click2dial/scripts/get_cid_name.py @@ -78,11 +78,17 @@ def stdout_write(string): '''Wrapper on sys.stdout.write''' sys.stdout.write(string.encode(sys.stdout.encoding or 'utf-8', 'replace')) sys.stdout.flush() + # When we output a command, we get an answer "200 result=1" on stdin + # Purge stdin to avoid these Asterisk error messages : + # utils.c ast_carefulwrite: write() returned error: Broken pipe + input_line = sys.stdin.readline() + return True def stderr_write(string): '''Wrapper on sys.stderr.write''' sys.stderr.write(string.encode(sys.stdout.encoding or 'utf-8', 'replace')) sys.stdout.flush() + return True def reformat_phone_number_before_query_openerp(number): @@ -111,25 +117,31 @@ def main(options, arguments): # AGI passes parameters to the script on standard input stdinput = {} while 1: - input_line = sys.stdin.readline().strip() - if input_line == '': + input_line = sys.stdin.readline() + if not input_line: break - variable, value = input_line.split(':') # TODO à protéger ! + line = input_line.strip() + try: + variable, value = line.split(':') + except: + break if variable[:4] != 'agi_': # All AGI parameters start with 'agi_' stderr_write("bad stdin variable : %s\n" % variable) continue variable = variable.strip() value = value.strip() - if variable != '': + if variable and value: stdinput[variable] = value stderr_write("full AGI environnement :\n") + for variable in stdinput.keys(): - stderr_write("%s = %s\n" % (variable, stdinput[variable])) + stderr_write("%s = %s\n" % (variable, stdinput.get(variable))) - input_cid_number = stdinput.get('agi_callerid', False) + input_cid_number = stdinput.get('agi_callerid') stderr_write('stdout encoding = %s\n' % sys.stdout.encoding or 'utf-8') if not isinstance(input_cid_number, str): + stdout_write('VERBOSE "CallerID number is empty"\n') exit(0) # Match for particular cases and anonymous phone calls # To test anonymous call in France, dial 3651 + number @@ -171,6 +183,7 @@ def main(options, arguments): stdout_write('VERBOSE "CallerID Name = %s"\n' % res) stdout_write('SET CALLERID "%s"<%s>\n' % (res, input_cid_number)) + return True if __name__ == '__main__': parser = OptionParser()