new versions of freeswitch stuff

git-svn-id: http://wush.net/svn/range/software/public/openbts/trunk@2435 19bc5d8c-e614-43d4-8b26-e1612bc8e597
This commit is contained in:
Kurtis Heimerl
2011-11-04 05:52:11 +00:00
parent 079259b451
commit 7167ffd198
6 changed files with 100 additions and 88 deletions

View File

@@ -33,26 +33,40 @@ or implied, of Kurtis Heimerl.
<!-- set all the openbts variables --> <!-- set all the openbts variables -->
<extension name="openbts" continue="true"> <extension name="openbts" continue="true">
<condition field="to_user" expression="^smsc$"> <condition field="to_user" expression="^smsc$">
<!-- first, parse SMS -->
<action inline="true" application="python" data="OpenBTS_Parse_SMS"/> <action inline="true" application="python" data="OpenBTS_Parse_SMS"/>
<!-- second, look up sender -->
<!-- freeswitch eats 's, switch them up here -->
<action inline="true" application="python" data='OpenBTS_DB SELECT callerid FROM sip_buddies WHERE name="${from_user}"'/>
<!-- result in _openbts_ret -->
<action inline="true" application="set" data="openbts_callerid=${_openbts_ret}"/>
</condition> </condition>
</extension> </extension>
<!-- openbts_db_loc set in vars.xml --> <!-- register a user in the subscriber registry -->
<extension name="registration"> <extension name="registration">
<condition field="openbts_tp_dest_address" expression="^101$"/> <condition field="openbts_tp_dest_address" expression="^101$"/>
<!-- is it a number? --> <!-- is it a number? -->
<condition field="openbts_text" expression="^\d{7,10}$"> <condition field="openbts_text" expression="^\d{7,10}$">
<!-- if so, try to register user -->
<action application="set" data="caller_number=${python(OpenBTS_DB -d ${openbts_db_loc} -c ${from_user})}" />
<!-- return value in _openbts_ret -->
<action application="python" data="OpenBTS_New_User"/> <action application="python" data="OpenBTS_New_User"/>
<action application="set" data="response_text=${_openbts_ret}"/>
<!-- lookup new number -->
<action application="python" data='OpenBTS_DB SELECT callerid FROM sip_buddies WHERE name="${from_user}"'/>
<!-- text back the return value --> <!-- text back the return value -->
<action application="python" data="OpenBTS_Send_SMS ${caller_number}|101|${_openbts_ret}"/> <action application="python" data="OpenBTS_Send_SMS ${_openbts_ret}|101|${response_text}"/>
<!-- if not, look up user and respond with instructions --> <!-- if not, reply -->
<anti-action application="set" data="caller_number=${python(OpenBTS_DB -d ${openbts_db_loc} -c ${from_user})}" /> <!-- this doesn't work yet, missing MC->MS encoder
<anti-action application="python" data="OpenBTS_Send_SMS ${caller_number}|101|Please send just the number you want!"/> <anti-action application="python" data="OpenBTS_Reply_SMS 101|101|Please send a 7-10 digit number!"/>
-->
</condition>
</extension>
<extension name="echo">
<condition field="openbts_tp_dest_address" expression="^9189$">
<action application="python" data="OpenBTS_Send_SMS ${openbts_callerid}|9189|${openbts_text}"/>
</condition> </condition>
</extension> </extension>

View File

@@ -29,12 +29,11 @@ or implied, of Kurtis Heimerl.
<extension name="local_call"> <extension name="local_call">
<!-- openbts_db_loc set in vars.xml --> <!-- openbts_db_loc set in vars.xml -->
<condition field="destination_number" expression="^\d{7}$"/> <condition field='${python(OpenBTS_DB SELECT name FROM sip_buddies WHERE callerid="${destination_number}")}' expression="IMSI\d{15}"/>
<condition field="${python(OpenBTS_DB -d ${openbts_db_loc} -t ${destination_number})}" expression="IMSI\d{15}"/> <condition field='${python(OpenBTS_DB SELECT callerid FROM sip_buddies WHERE name="${username}"' expression="\d{7,10}">
<condition field="${python(OpenBTS_DB -d ${openbts_db_loc} -c ${username})}" expression="\d{7}"> <action application="set" data='target=${python(OpenBTS_DB SELECT name FROM sip_buddies WHERE callerid="${destination_number}")}' />
<action application="set" data="target=${python(OpenBTS_DB -d ${openbts_db_loc} -t ${destination_number})}" /> <action application="set" data='effective_caller_id_number=${python(OpenBTS_DB SELECT callerid FROM sip_buddies WHERE name="${username}"'/>
<action application="set" data="effective_caller_id_number=${python(OpenBTS_DB -d ${openbts_db_loc} -c ${username})}" /> <action application="bridge" data="sofia/internal/${target}@${domain}:${sip_received_port}"/>
<action application="bridge" data="sofia/internal/${target}@${domain}:${sip_received_port}"/>
</condition> </condition>
</extension> </extension>

View File

@@ -26,66 +26,47 @@
import csv import csv
import sqlite3 import sqlite3
import getopt import sys
from freeswitch import * from freeswitch import *
def get_target(cur, destination): def execute_cmd(db_loc, cmd):
cur.execute('select name from sip_buddies where callerid=?', (destination,))
res = cur.fetchone()
if (res):
return res[0]
else:
return res
def get_caller_id(cur, caller):
cur.execute('select callerid from sip_buddies where name=?', (caller,))
res = cur.fetchone()
if (res):
return res[0]
else:
return res
def usage(stream, code):
stream.write("ERROR: %d" % code)
exit(1)
def fsapi(session, stream, env, args):
db_loc = None
caller = None
destination = None
opts, args = getopt.getopt(args.split(" "), "d:c:t:", ["db=", "caller=", "target="])
for o,a in opts:
if o in ("-d", "--db="):
db_loc = a
elif o in ("-c", "--caller="):
caller = a
elif o in ("-t", "--target="):
destination = a
else:
usage(stream,0)
if (not db_loc):
usage(stream,1)
if (caller and destination):
usage(stream,2)
conn = sqlite3.connect(db_loc) conn = sqlite3.connect(db_loc)
cur = conn.cursor() cur = conn.cursor()
if (caller): cur.execute(cmd)
stream.write(str(get_caller_id(cur, caller))) res = cur.fetchone()
else:
stream.write(str(get_target(cur, destination)))
conn.close() conn.close()
return res
#not using this now def err(msg):
def handler(session,args): consoleLog(msg)
db_loc = session.getVariable("db_loc") exit(1)
caller = session.getVariable("username")
destination = session.getVariable("destination_number")
if (db_loc and caller and destination):
res = get_vars(db_loc, caller, destination)
def parse_and_op(args):
sys.stderr.write(args + "\n")
args = args.split('|')
cmd = args[0]
db_loc = getGlobalVariable('openbts_db_loc')
if (len(args) > 1):
db_loc = args[1]
if not(db_loc):
err('Missing DB. Is openbts_db_loc defined?\n')
try:
res = execute_cmd(db_loc, cmd)
return str(res[0])
except Exception as err:
consoleLog('err', str(err) + "\n")
exit(1)
def chat(message, args):
res = parse_and_op(args)
consoleLog('info', "Returned: " + res)
message.chat_execute('set', '_openbts_ret=%s' % res)
def fsapi(session, stream, env, args):
res = parse_and_op(args)
consoleLog('info', "Returned: " + res)
if (res):
stream.write(str(res))

View File

@@ -86,21 +86,29 @@ def chat(message, args):
def fsapi(session, stream, env, args): def fsapi(session, stream, env, args):
args = args.split('|') args = args.split('|')
db_loc = args[0] if (len(args) < 3):
caller = args[1] err('Missing Args\n')
target = args[2] caller = args[0]
port = args[3] target = args[1]
port = args[2]
db_loc = None
if (len(args) == 4):
db_loc = args[3]
#if they don't all exist #if they don't all exist
if (not db_loc or db_loc == ''): if (not db_loc or db_loc == ''):
db_loc = getGlobalVariable("openbts_db_loc") db_loc = getGlobalVariable("openbts_db_loc")
if not (db_loc and caller and target and port): if (not db_loc):
err("Missing/Malformed Args \n") err("Missing DB. Is openbts_db_loc defined?\n")
if not (caller and target and port):
err("Malformed Args \n")
if (caller == '' or if (caller == '' or
target == '' or target == '' or
port == ''): port == ''):
err("Missing/Malformed Args \n") err("Malformed Args \n")
stream.write(str(create_user(db_loc, caller, target, port))) stream.write(str(create_user(db_loc, caller, target, port)))

View File

@@ -114,15 +114,21 @@ def parse(rp_message):
(rp_dest_address_type, rp_dest_address, rp_message) = get_rp_destination_address(rp_message) (rp_dest_address_type, rp_dest_address, rp_message) = get_rp_destination_address(rp_message)
rp_user_data = get_rp_user_data(rp_message) rp_user_data = get_rp_user_data(rp_message)
#rp_message finished #rp_message finished
(tp_message_type, rp_user_data) = get_tp_message_type(rp_user_data) (tp_message_type, rp_user_data) = get_tp_message_type(rp_user_data)
(tp_message_reference, rp_user_data) = get_tp_message_reference(rp_user_data) (tp_message_reference, rp_user_data) = get_tp_message_reference(rp_user_data)
(tp_dest_address_type, tp_dest_address, rp_user_data) = get_tp_destination_address(rp_user_data) (tp_dest_address_type, tp_dest_address, rp_user_data) = get_tp_destination_address(rp_user_data)
(tp_protocol_id, rp_user_data) = get_tp_protocol_identifier(rp_user_data) (tp_protocol_id, rp_user_data) = get_tp_protocol_identifier(rp_user_data)
(tp_data_coding_scheme, rp_user_data) = get_tp_data_coding_scheme(rp_user_data) (tp_data_coding_scheme, rp_user_data) = get_tp_data_coding_scheme(rp_user_data)
(tp_validity_period, rp_user_data) = get_tp_validity_period(rp_user_data) #check to see if validity period field is there
if (int(tp_message_type, 16) & 0x10 == 0):
tp_validity_period = None
else:
(tp_validity_period, rp_user_data) = get_tp_validity_period(rp_user_data)
(tp_user_data, rp_user_data) = get_tp_user_data(rp_user_data) (tp_user_data, rp_user_data) = get_tp_user_data(rp_user_data)
sys.stderr.write(tp_user_data)
return {"openbts_rp_message_type" : rp_message_type, return {"openbts_rp_message_type" : rp_message_type,
"openbts_rp_message_reference" : rp_message_reference, "openbts_rp_message_reference" : rp_message_reference,
"openbts_rp_originator_address" : rp_originator_address, "openbts_rp_originator_address" : rp_originator_address,
@@ -136,14 +142,10 @@ def parse(rp_message):
"openbts_tp_data_coding_scheme" : tp_data_coding_scheme, "openbts_tp_data_coding_scheme" : tp_data_coding_scheme,
"openbts_tp_validity_period" : tp_validity_period, "openbts_tp_validity_period" : tp_validity_period,
"openbts_tp_user_data" : tp_user_data, "openbts_tp_user_data" : tp_user_data,
"openbts_text" : messaging.utils.unpack_msg(tp_user_data).encode('UTF8') "openbts_text" : messaging.utils.unpack_msg(tp_user_data).encode('UTF8').rstrip('\0')
} }
if (len(rp_user_data[0]) != rp_user_data[1]): #index equals total
raise Exception("EXTRA DATA AFTER TP-USER-DATA")
def chat(message, args): def chat(message, args):
#sys.stderr.write(str(message.serialize()))
try: try:
content = parse(message.getBody()) content = parse(message.getBody())
for key in content.keys(): for key in content.keys():
@@ -151,6 +153,11 @@ def chat(message, args):
except Exception as err: except Exception as err:
consoleLog('err', str(err)) consoleLog('err', str(err))
sys.stderr.write(str(err)) sys.stderr.write(str(err))
exit(1)
def fsapi(session, stream, env, args):
consoleLog('err', 'Cannot call Parse_SMS from the FS API\n')
exit(1)
if __name__ == '__main__': if __name__ == '__main__':
if (len(sys.argv) < 2): if (len(sys.argv) < 2):

View File

@@ -68,8 +68,8 @@ def gen_body(to, text):
tp_len = (len(tp_header) + len(tp_user_data))/2 #octets, not bytes tp_len = (len(tp_header) + len(tp_user_data))/2 #octets, not bytes
return rp_header + gen_hex(tp_len) + tp_header + tp_user_data return rp_header + gen_hex(tp_len) + tp_header + tp_user_data
def send_message(to, fromm, text): #forward the message to smqueue for store-and-forwarding
#fromm = 'IMSI641104278340235' def send_smqueue_message(to, fromm, text):
event = Event("CUSTOM", "SMS::SEND_MESSAGE") event = Event("CUSTOM", "SMS::SEND_MESSAGE")
event.addHeader("proto", "sip"); event.addHeader("proto", "sip");
event.addHeader("dest_proto", "sip"); event.addHeader("dest_proto", "sip");
@@ -82,18 +82,21 @@ def send_message(to, fromm, text):
event.addHeader("replying", "false"); event.addHeader("replying", "false");
event.addBody(gen_body(to, text)); event.addBody(gen_body(to, text));
#sys.stderr.write(event.serialize())
event.fire() event.fire()
def chat(message, args): def chat(message, args):
args = args.split('|') args = args.split('|')
if (len(args) < 3):
consoleLog('err', 'Missing Args\n')
exit(1)
to = args[0] to = args[0]
fromm = args[1] fromm = args[1]
text = args[2] text = args[2]
if ((not to or to == '') or if ((not to or to == '') or
(not fromm or fromm == '')): (not fromm or fromm == '')):
consoleLog('err', 'Missing Args\n') consoleLog('err', 'Malformed Args\n')
send_message(to, fromm, text) exit(1)
send_smqueue_message(to, fromm, text)
def fsapi(session, stream, env, args): def fsapi(session, stream, env, args):
#chat doesn't use message anyhow #chat doesn't use message anyhow