From: Michael Tremer Date: Thu, 12 Jul 2018 16:43:39 +0000 (+0100) Subject: We don't use Asterisk & Kamailio any more X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=22ddc7b619545b9932a6ab4212aa41e8be9ed391;p=ipfire.org.git We don't use Asterisk & Kamailio any more This does not remove all of the code, but at least let's the webapp start Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index e078f031..9c6a6294 100644 --- a/Makefile.am +++ b/Makefile.am @@ -50,7 +50,6 @@ backend_PYTHON = \ src/backend/__init__.py \ src/backend/accounts.py \ src/backend/ads.py \ - src/backend/asterisk.py \ src/backend/base.py \ src/backend/countries.py \ src/backend/database.py \ diff --git a/src/backend/asterisk.py b/src/backend/asterisk.py deleted file mode 100644 index a45539fd..00000000 --- a/src/backend/asterisk.py +++ /dev/null @@ -1,280 +0,0 @@ -#!/usr/bin/python - -import logging -import socket -import time - -log = logging.getLogger("asterisk") - -class AsteriskManager(object): - def __init__(self, address, port=5038, username=None, password=None, timeout=10): - self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.socket.settimeout(timeout) - self.socket.connect((address, port)) - - self.conn = self.socket.makefile("rw", 0) - - self._authenticate(username, password) - - def _authenticate(self, username, password): - banner = self.conn.readline() - - if not banner.startswith("Asterisk Call Manager"): - raise RuntimeError("Did not connect to an Asterisk here") - - self._send_action("Login", { - "Username" : username, - "Secret" : password, - "Events" : "off", - }) - - def _make_action_id(self): - return "%s" % time.time() - - def _send_action(self, action, parameters={}): - self._send("Action", action) - - action_id = self._make_action_id() - self._send("ActionID", action_id) - - for k, v in parameters.items(): - self._send(k, v) - - return self._submit() - - def _send(self, key, value): - line = "%s: %s" % (key, value) - log.debug("S: %s" % line) - - self.conn.write("%s\r\n" % line) - - def _recv(self): - line = self.conn.readline() - line = line.rstrip() - - log.debug("R: %s" % line) - - return line - - def _recv_response(self): - response = {} - - while True: - # Read one line - line = self._recv() - - # An empty line signals end of response - if not line: - break - - k, sep, v = line.partition(": ") - response[k] = v - - return response - - def _submit(self): - # End command - self.conn.write("\r\n") - - # Read response - res = self._recv_response() - - if res["Response"] == "Error": - raise Exception(res["Message"]) - - if res.get("EventList") == "start": - events = [] - - while True: - event = self._recv_response() - - # This is the end of the list - if event.get("EventList") == "Complete": - break - - events.append(event) - - # Return event list - return events - - return res - - def ping(self): - """ - Sends a ping to asterisk and expects pong - """ - res = self._send_action("Ping") - - return res["Ping"] == "Pong" - - def call(self, caller, callee, caller_id=None, timeout=30000): - res = self._send_action("Originate", { - "Channel" : "SIP/%s@kamailio" % caller, - "Exten" : callee, - "Context" : "from-cli", - "Priority" : 1, - "Timeout" : timeout, - "CallerID" : caller_id or caller, - }) - - return res - - def list_channels(self): - channels = [] - - for c in self._send_action("CoreShowChannels"): - channel = Channel(self, c.get("Channel")) - channels.append(channel) - - return sorted(channels) - - def _mailbox_status(self, mailbox): - return self._send_action("MailboxStatus", { "Mailbox" : "%s@default" % mailbox }) - - def messages_waiting(self, mailbox): - status = self._mailbox_status(mailbox) - - # Get messages waiting - waiting = status.get("Waiting", 0) - - return int(waiting) - - def list_peers(self): - peers = [] - - for p in self._send_action("SIPPeers"): - peer = Peer(self, p.get("ObjectName")) - peers.append(peer) - - return sorted(peers) - - def list_registry(self): - print self._send_action("SIPShowRegistry") - - -class Channel(object): - def __init__(self, manager, channel_id): - self.manager = manager - self.id = channel_id - - self.status = self.manager._send_action("Status", { "Channel" : self.id })[0] - - def __eq__(self, other): - return self.id == other.id - - def __lt__(self, other): - # Longest first - return not self.duration < other.duration - - def __repr__(self): - return "<%s %s>" % (self.__class__.__name__, self.id) - - def hangup(self): - res = self.manager._send_action("Hangup", { "Channel" : self.id }) - - return res["Response"] == "Success" - - @property - def type(self): - return self.status.get("Type") - - @property - def application(self): - return self.status.get("Application") - - @property - def duration(self): - seconds = self.status.get("Seconds", None) - - if seconds is None: - return - - return int(seconds) - - _states = { - "4" : "ringing", - "6" : "connected", - } - - @property - def state(self): - state = self.status.get("ChannelState") - - try: - return self._states[state] - except KeyError: - return - - @property - def format(self): - format = self.status.get("Nativeformats") - - return format.lstrip("(").rstrip(")") - - @staticmethod - def _format_number(number): - # Replace 00 by + - if number and number.startswith("00"): - return "+%s" % number[2:] - - return number - - @property - def caller(self): - num = self.status.get("CallerIDNum") - - return self._format_number(num) - - @property - def caller_name(self): - name = self.status.get("CallerIDName") - - if name in ("", ""): - return None - - return name - - @property - def callee(self): - if self.application == "ConfBridge": - return self.data # Will have the conference room number - - elif self.application == "Echo": - return None - - elif self.application in ("VoiceMail", "VoiceMailMain"): - try: - user, rest = self.data.split("@", 1) - except: - return self.data - - return user - - num = self.status.get("EffectiveConnectedLineNum") or self.status.get("DNID") - - return self._format_number(num) - - @property - def application(self): - return self.status.get("Application") - - @property - def data(self): - return self.status.get("Data") - - -class Peer(object): - def __init__(self, manager, peer_id): - self.manager = manager - self.id = peer_id - - self.data = self.manager._send_action("SIPShowPeer", { "Peer" : self.id }) - - def __repr__(self): - return "<%s %s>" % (self.__class__.__name__, self.id) - - def __eq__(self, other): - return self.id == other.id - - def __lt__(self, other): - return self.id < other.id diff --git a/src/backend/talk.py b/src/backend/talk.py index faf8d67b..fab3964b 100644 --- a/src/backend/talk.py +++ b/src/backend/talk.py @@ -2,33 +2,11 @@ import re -from . import asterisk from . import database from misc import Object class Talk(Object): - def init(self): - db_args = self.backend.db._db_args - db_args.update({ - "database" : "kamailio", - }) - - self._db = database.Connection(**db_args) - - @property - def db(self): - return self._db - - def connect_to_asterisk(self): - hostname = self.settings.get("asterisk_hostname") - username = self.settings.get("asterisk_username") - password = self.settings.get("asterisk_password") - - return asterisk.AsteriskManager( - hostname, username=username, password=password - ) - def get_phonebook(self, account=None): accounts = [] for a in self.accounts.list(): @@ -148,8 +126,6 @@ class Talk(Object): return self._process_cdr(res) def get_channels(self, account=None): - a = self.connect_to_asterisk() - channels = [] for c in a.list_channels(): if account and not account.sip_id in (c.caller, c.callee):