|
| 1 | +#!/usr/bin/env python |
| 2 | +## |
| 3 | +## This file is part of the OpenSIPS Python Package |
| 4 | +## (see https://github.com/OpenSIPS/python-opensips). |
| 5 | +## |
| 6 | +## This program is free software: you can redistribute it and/or modify |
| 7 | +## it under the terms of the GNU General Public License as published by |
| 8 | +## the Free Software Foundation, either version 3 of the License, or |
| 9 | +## (at your option) any later version. |
| 10 | +## |
| 11 | +## This program is distributed in the hope that it will be useful, |
| 12 | +## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +## GNU General Public License for more details. |
| 15 | +## |
| 16 | +## You should have received a copy of the GNU General Public License |
| 17 | +## along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +## |
| 19 | + |
| 20 | +import argparse |
| 21 | +from opensips.mi import OpenSIPSMI, OpenSIPSMIException |
| 22 | +from opensips.event import OpenSIPSEvent, OpenSIPSEventException |
| 23 | +import json |
| 24 | +import signal |
| 25 | + |
| 26 | +parser = argparse.ArgumentParser() |
| 27 | + |
| 28 | +communication = parser.add_argument_group('communication') |
| 29 | + |
| 30 | +communication.add_argument('-t', '--type', |
| 31 | + type=str, |
| 32 | + choices=['fifo', 'http', 'datagram'], |
| 33 | + help='OpenSIPS MI Communication Type') |
| 34 | +communication.add_argument('-i', '--ip', |
| 35 | + type=str, |
| 36 | + help='OpenSIPS MI IP Address', |
| 37 | + default='127.0.0.1') |
| 38 | +communication.add_argument('-p', '--port', |
| 39 | + type=int, |
| 40 | + help='OpenSIPS MI Port', |
| 41 | + default=8888) |
| 42 | +communication.add_argument('-f', '--file', |
| 43 | + metavar='FIFO_FILE', |
| 44 | + type=str, |
| 45 | + help='OpenSIPS MI FIFO File') |
| 46 | +communication.add_argument('-fb', '--fallback', |
| 47 | + metavar='FIFO_FALLBACK_FILE', |
| 48 | + type=str, |
| 49 | + help='OpenSIPS MI Fallback FIFO File') |
| 50 | +communication.add_argument('-fd', '--dir', |
| 51 | + metavar='FIFO_DIR', |
| 52 | + type=str, |
| 53 | + help='OpenSIPS MI FIFO Reply Directory') |
| 54 | + |
| 55 | +event = parser.add_argument_group('event') |
| 56 | + |
| 57 | +event.add_argument('event', |
| 58 | + type=str, |
| 59 | + help='OpenSIPS Event Name') |
| 60 | + |
| 61 | +event.add_argument('-T', '--transport', |
| 62 | + type=str, |
| 63 | + choices=['datagram', 'stream'], |
| 64 | + help='OpenSIPS Event Transport', |
| 65 | + required=True) |
| 66 | +event.add_argument('-li', '--listen-ip', |
| 67 | + type=str, |
| 68 | + help='OpenSIPS Event Listen IP Address', |
| 69 | + default='127.0.0.1') |
| 70 | +event.add_argument('-lp', '--listen-port', |
| 71 | + type=int, |
| 72 | + help='OpenSIPS Event Listen Port', |
| 73 | + required=True) |
| 74 | +event.add_argument('-e', '--expire', |
| 75 | + type=int, |
| 76 | + help='OpenSIPS Event Expire Time', |
| 77 | + default=3600) |
| 78 | + |
| 79 | +arguments = parser.parse_args() |
| 80 | + |
| 81 | +print(arguments) |
| 82 | + |
| 83 | +if not arguments.type or arguments.type == 'fifo': |
| 84 | + fifo_args = {} |
| 85 | + if arguments.file: |
| 86 | + fifo_args['file'] = arguments.file |
| 87 | + if arguments.fallback: |
| 88 | + fifo_args['fallback'] = arguments.fallback |
| 89 | + if arguments.dir: |
| 90 | + fifo_args['dir'] = arguments.dir |
| 91 | + mi = OpenSIPSMI('fifo', **fifo_args) |
| 92 | + |
| 93 | +if arguments.type == 'http': |
| 94 | + mi = OpenSIPSMI('http', url='http://{}:{}/mi'.format(arguments.ip, arguments.port)) |
| 95 | + |
| 96 | +if arguments.type == 'datagram': |
| 97 | + mi = OpenSIPSMI('datagram', datagram_ip=arguments.ip, datagram_port=arguments.port) |
| 98 | + |
| 99 | +event = OpenSIPSEvent(mi, arguments.transport, ip=arguments.listen_ip, port=arguments.listen_port) |
| 100 | + |
| 101 | +def event_handler(message): |
| 102 | + try: |
| 103 | + message_json = json.loads(message.decode('utf-8')) |
| 104 | + print(json.dumps(message_json, indent=4)) |
| 105 | + except json.JSONDecodeError as e: |
| 106 | + print(f"Failed to decode JSON: {e}") |
| 107 | + |
| 108 | +def timer(*args): |
| 109 | + event.unsubscribe(arguments.event) |
| 110 | + exit(1) |
| 111 | + |
| 112 | +signal.signal(signal.SIGALRM, timer) |
| 113 | +signal.alarm(arguments.expire) |
| 114 | + |
| 115 | +try: |
| 116 | + event.subscribe(arguments.event, event_handler, expire=arguments.expire) |
| 117 | +except OpenSIPSEventException as e: |
| 118 | + print(e) |
| 119 | + |
| 120 | +try: |
| 121 | + while True: |
| 122 | + pass |
| 123 | +except KeyboardInterrupt: |
| 124 | + event.unsubscribe(arguments.event) |
0 commit comments