commit 4ac934837cf0043f9801867dc248265112fa7cee
Author: parazyd <parazyd@dyne.org>
Date: Fri, 17 Nov 2017 22:12:45 +0100
import freepbx-glue
Diffstat:
5 files changed, 138 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE
@@ -0,0 +1,16 @@
+ISC License
+
+Copyright 2017 Ivan J. <parazyd@dyne.org>
+Copyright 2017 <awake@0324am.net>
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/freepbx-glue/README.md b/freepbx-glue/README.md
@@ -0,0 +1,33 @@
+freepbx-glue
+============
+
+freepbx-glue is a simple python daemon designed to parse logs output by
+Asterisk/FreePBX.
+
+It currently handles three states:
+
+* The receiver is ringing
+* The receiver has answered
+* The receiver has hung up
+
+Whenever a daemon enters one of these states, it will issue a callback
+to an HTTP API running on the calling station.
+
+
+Dependencies
+------------
+
+```
+python3-requests
+```
+
+
+Deploying
+---------
+
+On the FreePBX machine, this daemon can be executed by running the
+following command as root:
+
+```
+# tail -f /var/log/asterisk/full | ./freepbx_glue.py
+```
diff --git a/freepbx-glue/config.py b/freepbx-glue/config.py
@@ -0,0 +1,13 @@
+# See LICENSE file for copyright and license details.
+"""
+Configuration file for the FreePBX log parser
+"""
+
+# SIP ID of the caller station
+CALLER_ID = 'SIP/4005'
+
+# SIP ID of the telephone
+RECEIVER_ID = 'SIP/4004'
+
+# API url of the caller station listener
+STATION_API = 'http://10.20.30.31:8000'
diff --git a/freepbx-glue/freepbx_glue.py b/freepbx-glue/freepbx_glue.py
@@ -0,0 +1,69 @@
+#!/usr/bin/env python3
+# See LICENSE file for copyright and license details.
+"""
+FreePBX/Asterisk log parser and handler
+"""
+
+import sys
+import requests
+
+import globalvars
+from config import (CALLER_ID, RECEIVER_ID, STATION_API)
+
+
+def handle_line(logline):
+ """
+ Parses a single log line to make an according call
+ """
+ if not logline:
+ return
+
+ parsed = logline.split()
+
+ # We wait for a ring.
+ if parsed[4].split('-')[0] == RECEIVER_ID:
+ # stackno = parsed[4].split('-')[1]
+ if parsed[6] == 'ringing':
+ globalvars.weareringing = True
+ print('We are ringing')
+ return
+
+ # When a call is answered, the receiver stackno is +1 in hex than
+ # the caller's.
+
+ # The phone is ringing.
+ if globalvars.weareringing:
+ if parsed[3] == 'app_dial:' and parsed[5] == 'answered':
+ globalvars.weareringing = False
+ globalvars.wehaveanswered = True
+ print('We have answered')
+ resp = requests.get(STATION_API+'/callanswered')
+ if resp.status_code != 200:
+ print('Something went wrong with the API call.')
+ return
+
+ # The phone has been picked up.
+ if globalvars.wehaveanswered:
+ if parsed[3] == 'pbx.c:':
+ if parsed[6].startswith('Hangup("' + CALLER_ID):
+ globalvars.wehaveanswered = False
+ print('We hung up.')
+ print('---')
+ resp = requests.get(STATION_API+'/callended')
+ if resp.status_code != 200:
+ print('Something went wrong with the API call.')
+ return
+
+
+def main():
+ """
+ Main routine.
+ Reads standard input line by line.
+ """
+ while True:
+ line = sys.stdin.readline()
+ handle_line(line)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/freepbx-glue/globalvars.py b/freepbx-glue/globalvars.py
@@ -0,0 +1,7 @@
+# See LICENSE file for copyright and license details.
+"""
+Global variables used to hold state of the log handler
+"""
+
+weareringing = False
+wehaveanswered = False