From 2af8af9a8a9c4eded3bd40021547e1fd5a0e85ec Mon Sep 17 00:00:00 2001 From: Roman Bazalevskiy Date: Fri, 3 Nov 2017 02:39:39 +0300 Subject: [PATCH] =?utf8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?utf8?q?=D1=8B=20=D1=81=D0=BA=D1=80=D0=B8=D0=BF=D1=82=D1=8B=20=D0=B4?= =?utf8?q?=D0=BB=D1=8F=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=20?= =?utf8?q?=D1=80=D0=B0=D0=B7=D0=B1=D0=B8=D1=82=D1=8B=D0=BC=D0=B8=20=D0=BD?= =?utf8?q?=D0=B0=20=D1=84=D1=80=D0=B0=D0=B3=D0=BC=D0=B5=D0=BD=D1=82=D1=8B?= =?utf8?q?=20=D0=A1=D0=9C=D0=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- mqtt-agi/clear-sms-spool | 49 +++++++++++++++ mqtt-agi/mqtt-sms | 127 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 mqtt-agi/clear-sms-spool create mode 100755 mqtt-agi/mqtt-sms diff --git a/mqtt-agi/clear-sms-spool b/mqtt-agi/clear-sms-spool new file mode 100644 index 0000000..b8814b4 --- /dev/null +++ b/mqtt-agi/clear-sms-spool @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +reload(sys) +sys.setdefaultencoding('utf-8') + +from ConfigParser import ConfigParser +import paho.mqtt.client as paho +from pymessaging.sms import SmsDeliver + +import sqlite3 +from sqlite3 import Error + +from tempfile import mkstemp +from shutil import move + +conffile = sys.argv[1:2] + +config = ConfigParser() +config.add_section('sms') +# set defaults for anonymous auth +config.set('sms', 'tmpfile', '/tmp/sms.db') +config.read(conffile) + +sms_db = config.get('sms','tmpfile') + +try: + conn = sqlite3.connect(sms_db) + + cur = conn.cursor() + cur.execute("select ref from sms_parts group by ref having max(datetime) is null or (julianday('now')-julianday(max(datetime)))>0.005;") + refs=cur.fetchall() + for ref in refs: + cur.execute("select text from sms_parts where ref=%s order by seq" % (ref)) + full_text=''.join(rec[0] for rec in cur.fetchall()).replace("\"","") + f,path=mkstemp(suffix=".call") + f=open(path,"w") + f.write("Application: Dial\n") + f.write("Channel: Local/smsraw@DID_mobile\n") + f.write("Context: smsraw\n") + f.write("Setvar: SMS_FULL_TEXT=%s" % full_text) + f.close() + move(path,'/var/spool/asterisk/outgoing') + cur.execute("delete from sms_parts where ref=%s" % (ref)) + conn.commit() + +except: + None \ No newline at end of file diff --git a/mqtt-agi/mqtt-sms b/mqtt-agi/mqtt-sms new file mode 100755 index 0000000..ba1cdd5 --- /dev/null +++ b/mqtt-agi/mqtt-sms @@ -0,0 +1,127 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# +# Usage: +# AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed) +# or, to override the extension: +# AGI(/etc/asterisk/agi/mqtt,/etc/asterisk/agi/mqtt.cfg,calls/missed,+43123456789) +# + +import sys +reload(sys) +sys.setdefaultencoding('utf-8') + +from ConfigParser import ConfigParser +import paho.mqtt.client as paho +from pymessaging.sms import SmsDeliver + +import json + +from os import getpid,uname +from pprint import pprint + +import sqlite3 +from sqlite3 import Error + +from os import remove,system + +client_name='agi-'+uname()[1]+'-'+str(getpid()) + +conffile, topic, base64 = sys.argv[1:4] + +config = ConfigParser() +config.add_section('mqtt') +config.add_section('sms') +# set defaults for anonymous auth +config.set('mqtt', 'username', '') +config.set('mqtt', 'password', '') +config.set('mqtt', 'port', '1883') +config.set('sms', 'tmpfile', '/tmp/sms.db') +config.read(conffile) + +mqtt_server = config.get('mqtt', 'server') +mqtt_port = config.getint('mqtt', 'port') +mqtt_username = config.get('mqtt', 'username') +mqtt_password = config.get('mqtt', 'password') +sms_db = config.get('sms','tmpfile') + +try: + conn = sqlite3.connect(sms_db) +except: + remove(sms_db) + conn = sqlite3.connect(sms_db) + +agi = [] +while 1: + line = sys.stdin.readline() + if not line or line == "\n": + break + agi.append(line) + +agi = dict([line.rstrip('\n').replace('agi_', '', 1).split(': ', 1) for line in agi]) + +agi['base64'] = base64 + +full_text="" +try: + sms=SmsDeliver(base64.decode('base64').split('\r\n')[1]) + if 'cnt' in sms.data: + sms_type='sms-multipart' + part_count=sms.data['cnt'] + part_number=sms.data['seq'] + ref=sms.data['ref'] + agi['part-count']=part_count + agi['part-number']=part_number + agi['ref']=ref + else: + sms_type='sms' + agi['sms-type']=sms_type + sms_text=sms.data['text'].encode('utf-8') + agi['sms_text']=sms_text + if sms_type=='sms': + full_text=sms_text + else: + cur = conn.cursor() + cur.execute("create table if not exists sms_parts (ref integer,cnt integer,seq integer,datetime timestamp default current_timestamp,text varchar(255))") + cur.execute("insert into sms_parts(ref,cnt,seq,text) values(%s,%s,%s,\'%s\')" % (ref,part_count,part_number,sms_text)) + cur.execute("select count(*),cnt from sms_parts where ref=%s" % (ref)) + received,cnt=cur.fetchone() + if received==cnt: + cur.execute("select text from sms_parts where ref=%s order by seq" % (ref)) + full_text=''.join(rec[0] for rec in cur.fetchall()) +# full_text=full_text.replace("\"","") + agi["full_text"]=full_text + cur.execute("delete from sms_parts where ref=%s" % (ref)) + conn.commit() +except: + raise + +if full_text: + print full_text + f1=open("/tmp/sms.log","w+") + f1.write(full_text.strip().encode('utf-8')) + f1.close() + +def agi_exit(rc, *args): + if rc != 0: + print "VERBOSE rc=%s %s" % (rc, args) + sys.exit(rc) + +def on_connect(mosq, rc, *args): + if rc != 0: + agi_exit(1, "Connection failed: %d" % rc) + +def on_publish(mosq, *args): + # done + agi_exit(0) + +client = paho.Client(client_name) +client.username_pw_set(mqtt_username, mqtt_password) +client.connect(mqtt_server, port=mqtt_port) +client.on_connect = on_connect +client.on_publish = on_publish + +client.publish(topic, payload=json.dumps(agi)) +client.loop() +agi_exit(1, "Message publish timed out") -- 2.34.1