piphone.py 21.1 KB
Newer Older
1 2 3
#!/usr/bin/env python

import sqlite3
okhin's avatar
okhin committed
4 5 6
import datetime
import uuid
import json
7 8 9
import logging
import concurrent.futures
import asyncio
10
import re
11 12
import configparser
import argparse
13 14
import os.path
import sys
15
from operator import itemgetter
16 17

import jwt
18
import websockets
19
from bottle import request, abort, Bottle, JSONPlugin, template, static_file
20
from bottle_sqlite import SQLitePlugin
21 22

import ari
23

24
arg_parser = argparse.ArgumentParser(description='Manage the SIP Backend for the piphone')
25
config = configparser.ConfigParser()
26 27

arg_parser.add_argument('-c', '--config', help="Config file")
okhin's avatar
okhin committed
28
args = arg_parser.parse_args()
29
try:
30
    logging.debug("Let's use {} as a config file".format(args.config,))
okhin's avatar
okhin committed
31
    config.read(args.config)
32 33
except AttributeError as e:
    print(e)
34 35 36 37
    try:
        if os.path.isfile('config.ini'):
            logging.debug("Let's use config.ini as a config file")
            config.read('config.ini')
okhin's avatar
okhin committed
38
        elif os.path.isfile('/etc/piphone/sip_config.ini'):
39
            logging.debug("Let's use /etc/iphone/config.ini as a config file")
okhin's avatar
okhin committed
40
            config.read('/etc/piphone/sip_config.ini')
41
        else:
okhin's avatar
okhin committed
42
            raise Exception("No configuration file found (tried ./config.ini and /etc/piphone/sip_config.ini")
43 44 45 46
    except Exception as e:
        arg_parser.print_help()
        sys.exit(1)

47
application = app = Bottle(autojson=False)
48
app.install(SQLitePlugin(dbfile=config['piphone']['db']))
49 50
app.install(JSONPlugin(json_dumps=lambda s: json.dumps(s, cls=PiphoneJSONEncoder)))

51
threads = concurrent.futures.ThreadPoolExecutor(max_workers=5)
52
loop = asyncio.get_event_loop()
53

54 55 56 57
running = False
ws = None

# Loggers
58 59
handler = logging.FileHandler(config['piphone']['log'])
verbosity = getattr(logging, config['piphone']['verbosity'].upper()) or logging.DEBUG
60 61
phone_logger = logging.getLogger('piphone')
phone_logger.addHandler(handler)
62
phone_logger.setLevel(verbosity)
63 64
ws_logger = logging.getLogger('asterisk')
ws_logger.addHandler(handler)
65
ws_logger.setLevel(verbosity)
66 67
bottle_logger = logging.getLogger('bottle')
bottle_logger.addHandler(handler)
68
bottle_logger.setLevel(verbosity)
69 70 71 72 73 74 75 76 77 78

class PiphoneJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        """
        We need to implement this to be able to JSONEncode
        """
        if isinstance(obj, Call):
            return {  'caller': obj.caller
                , 'callee': obj.callee
                , 'callid': obj.id
okhin's avatar
okhin committed
79
                , 'url': obj.url
80 81 82
                , 'history': obj.history
                , 'owner': obj.owner }
        else:
83
            return json.JSONEncoder.default(self, obj)
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
def authenticated(f):
    '''
    We need a decorator to check if our query is authenticated.
    We will store an API key and SECRET in ur database, the client
    needs to have both of them.
    He must then send us a JWT token with an API claim in the payload.
    The JWT token must be encoded and signed with the SECRET. If the
    token is bad, we return a 403.
    '''
    def wrapped(db, *args, **kwargs):
        # Let's get the JWT token. It should be a params (from get or post or whatev')
        bottle_logger.debug("Authentication: {}".format([':'.join([key, request.params[key]]) for key in request.params],))
        if 'token' not in request.params:
            bottle_logger.error("No token found in the params")
            abort(401, "No token found in the query")
        # We want the api id in the params to.
        if 'api' not in request.params:
            bottle_logger.error("No api id found in the params")
            abort(401, "No api id found in the params")
        # Now, let's get the token on our side
        try:
            results = db.execute('SELECT token FROM users WHERE api = ?', (request.params['api'],)).fetchall()
            assert len(results) == 1
            token = results[0][0]
            auth_token = jwt.decode(request.params['token'], token)
            assert auth_token['api'] == request.params['api']
            for key in auth_token:
                request.params[key] = auth_token[key]
        except (jwt.exceptions.InvalidTokenError, AssertionError) as e:
            bottle_logger.error("Access refused")
            bottle_logger.exception(e)
            abort(403, e)
        except Exception as e:
okhin's avatar
okhin committed
118
            bottle_logger.exception(e)
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
            abort(500, e)
        return f(db, *args, **kwargs)
    return wrapped

def connected(f):
    '''
    This is a decorator used to check if we are connected to a websocket or not. If we're not
    We're returning 500 error.
    '''
    def wrapped(db, *args, **kwargs):
        if isinstance(ws, websockets.client.WebSocketClientProtocol):
            if ws.open == True:
                return f(db, *args, **kwargs)
        ws_logger.error("Websocket connection is closed")
        abort(500, "Websocket isn't running")
    return wrapped

okhin's avatar
okhin committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149
def sanitize_phonenumber(number):
    """
    This function is used to sanitize a phone number.
    If it starts with a +, it will be removed and replaced by 00.
    Any chars who do notbelong to [0-9] will be stripped.
    If the number doesn't starts with 00, a TypeError will be raised
    """
    if number[0] == '+':
        number = '00' + number[1:]
    number = ''.join([c for c in number if c in '0123456789'])
    if not number.startswith('00'):
        raise TypeError('{} is not a valid international number, it should start with 00')
    return number

okhin's avatar
okhin committed
150 151
@asyncio.coroutine
def hold_bridge():
152 153 154 155 156
    '''
    We will create a bridge that will be used to park caller waiting for callee to join.
    It will play a song in a loop.
    '''
    try:
okhin's avatar
okhin committed
157
        bridge = ari.Bridge(config['asterisk'], config['moh']['name'], 'holding')
158 159
    except Exception as e:
        phone_logger.critical("No Music On Hold (moh) section in config file. Exiting")
okhin's avatar
okhin committed
160
        phone_logger.exception(e)
161 162 163
        raise e
    try:
        result = bridge.create()
okhin's avatar
okhin committed
164
        result = bridge.startMoh(config['moh']['class'])
165 166 167 168 169
    except Exception as e:
        phone_logger.error("Cannot start 'on hold' bridge")
        phone_logger.exception(e)
        raise e

okhin's avatar
okhin committed
170
@asyncio.coroutine
okhin's avatar
okhin committed
171
def listen(db):
172 173 174 175 176
    '''
    Start listening on the websocket
    '''
    global running
    global ws
177
    ws_logger.debug('Connecting to websocket: {}'.format(config['webservice']['base_url'] + '?app={}&api_key={}:{}'.format(config['asterisk']['app'], config['asterisk']['key'], config['asterisk']['password'])))
okhin's avatar
okhin committed
178
    ws = yield from websockets.connect(config['webservice']['base_url'] + '?app={}&api_key={}:{}'.format(
okhin's avatar
okhin committed
179
        config['asterisk']['app'], config['asterisk']['key'], config['asterisk']['password']))
180
    ws_logger.debug('Websocket connected: {}'.format(type(ws)))
okhin's avatar
okhin committed
181
    yield from hold_bridge()
182 183
    while running == True:
        try:
okhin's avatar
okhin committed
184
            event = yield from ws.recv()
185
            # Let's call the applications function
okhin's avatar
okhin committed
186
            yield from dispatch(json.loads(event), db)
187 188 189 190 191 192 193 194 195 196 197
        except websockets.exceptions.ConnectionClosed as e:
            ws_logger.warning("Connexion closed")
            ws_logger.exception(e)
            running = False
            ws.close()
            return
        except Exception as e:
            ws_logger.exception(e)
            continue
    ws.close()

okhin's avatar
okhin committed
198
@asyncio.coroutine
okhin's avatar
okhin committed
199
def dispatch(event, db):
200 201 202 203 204 205 206 207 208
    """
    Let's work on our events. Parse them and do request on the ARI API. Event is
    a dict loaded from JSON.
    """
    ws_logger.debug('Event received: {}'.format(event,))
    # Let's get the call ID, the call id isthe channel id minus the last -part.
    if 'channel' not in event:
        return
    call_id = re.sub('-\d+$', '', event['channel']['id'])
okhin's avatar
okhin committed
209
    call = Call.load(call_id, db)
210 211
    call.event_handler(event)

212
class Call(object):
okhin's avatar
okhin committed
213
    """
214
    This Class is used to manage operations on a call, to print it and dump it.
okhin's avatar
okhin committed
215
    """
216
    history = []
217
    actions = {'Created': 'call_caller'
218
        , 'ChannelStateChange': 'change'
219
        , 'ChannelDtmfReceived': 'dtmf'
220
        , 'ChannelDestroyed': 'hangup'
221
        , 'ChannelHangupRequest': 'hangup'}
222

223
    def __init__(self, caller, callee, owner, callid=None, db=None):
okhin's avatar
okhin committed
224 225 226 227
        try:
            self.caller = caller
            self.callee = callee
            self.owner = owner
228
            if callid == None:
229
                self.id = str(uuid.uuid4())
230 231
            else:
                self.id = callid
okhin's avatar
okhin committed
232
            self.db = db
okhin's avatar
okhin committed
233
        except Exception as e:
234
            phone_logger.exception(e)
okhin's avatar
okhin committed
235 236
            raise e

okhin's avatar
okhin committed
237
    @property
okhin's avatar
okhin committed
238 239 240
    def url(self):
        return ''.join(['/calls/', self.id])

okhin's avatar
okhin committed
241
    @property
242 243 244 245
    def state(self):
        sort = sorted(self.history, reverse=True, key=itemgetter(1))
        return sort[0][0]

okhin's avatar
okhin committed
246 247
    @classmethod
    def load(cls, callid, db):
okhin's avatar
okhin committed
248
        phone_logger.debug("Loading call {} from db {}".format(callid, db,))
okhin's avatar
okhin committed
249
        try:
250
            results = db.execute('SELECT caller, callee, owner, callid, history FROM calls WHERE callid = ?;', (callid,))
okhin's avatar
okhin committed
251
            result = results.fetchone()
252
            assert len(result) == 5
okhin's avatar
okhin committed
253
            object = cls(result[0], result[1], result[2], result[3], db=db)
okhin's avatar
okhin committed
254 255
            object.history = json.loads(result[4])
            return object
256
        except Exception as e:
257
            phone_logger.exception(e)
258
            raise e
okhin's avatar
okhin committed
259

260
    def update(self, new_state):
261
        '''
262
        Let's update the state of the call. new_state is a tuple in the form (newstate, timestamp,)
263
        '''
264
        phone_logger.debug("Got a new state: {}".format(new_state,))
265 266
        self.history.append(new_state)
        self.save()
267 268 269 270 271 272

    def event_handler(self, event):
        '''
        There's a new event related to our call
        '''
        state = event['type']
273
        if state in self.actions:
274
            getattr(self, self.actions[state])(event=event)
275

276 277 278 279 280 281 282 283
    def hangup(self, event):
        '''
        There's a call which has lost connection. Probably someone who hanged up the call.
        We need to check if the other side is still up, which can be done by checking
        the bridge item (it is in the channels part of the bridge object) and see if there's
        more than one channel.
        If there's more than one, then we need to send a hangup to the other side and then delete
        our channel, if not we need to delete ourselves and then delete the bridge.
284
        We might also be in a case where no channel has been created … or we're still on moh.
285 286
        '''
        bridge_id = '-'.join(event['channel']['id'].split('-')[:-1])
287 288 289 290 291 292 293
        try:
            bridge = ari.Bridge(config['asterisk'], bridge_id, 'mixed')
        except:
            # Not in a bridge yet. Our channel has been destroyed.
            # That or we're in moh.
            phone_logger.info('Channel destroyed {}'.format(event['channel']['id']))
            self.update((':'.join([event['channel']['state']
okhin's avatar
okhin committed
294
                , event['channel']['id'].split('-')[-1]])
295 296 297
                , event['timestamp']))
            return

298 299 300 301 302 303 304 305 306 307 308 309
        results = json.loads(bridge.status())
        if len(results['channels']) == 0:
            # We were the last channel standing
            phone_logger.info('Deleting bridge {}'.format(bridge_id))
            bridge.delete()
        else:
            # There's at least one channel standing
            for channel in results['channels']:
                chan = ari.Channel(config['asterisk'], channel)
                phone_logger.info('Hanging up channel {}'.format(channel))
                chan.delete()

310 311 312 313 314
    def dtmf(self, event):
        '''
        We received a DTMF sequence
        '''
        try:
okhin's avatar
okhin committed
315
            assert self.state.startswith('Up')
316
            # The only thing we want to do is to call the callee if we press 1
317 318 319 320
            if event['digit'] != '1':
                return
            # Now, we're connectig the other side
            # But first we should stop the playback
okhin's avatar
okhin committed
321
            phone_logger.debug('Stopping the playback currently running')
322
            # We're stoping the playback, with the same ID as the channel, to keep track of it
okhin's avatar
okhin committed
323
            playback = ari.Playback(config['asterisk'], event['channel']['id'], 'sound:mario')
324
            playback.stop()
okhin's avatar
okhin committed
325
            # Now we're moving the channel to the MOH Bridge
okhin's avatar
okhin committed
326
            phone_logger.debug('Moving call {} to the garage'.format(event['channel']['id']))
okhin's avatar
okhin committed
327
            moh_bridge = ari.Bridge(config['asterisk'], config['moh']['name'])
328
            moh_bridge.addChannel(event['channel']['id'])
329
            # Next we need to originate a call to the other side
330
            phone_logger.info('Will now connect {} to {}'.format(self.caller, self.callee,))
331
            endpoint = 'SIP/' + sanitize_phonenumber(self.callee) + '@' + config['asterisk']['sip-context']
okhin's avatar
okhin committed
332
            channel = ari.Channel(config['asterisk'], self.id + '-' + sanitize_phonenumber(self.callee))
333
            channel.originate(endpoint)
334
        except AssertionError as e:
okhin's avatar
okhin committed
335
            phone_logger.error("Received a DTMF sequence out le being in  a '{}' state, ignoring: {}".format(self.state, event['digit']))
336

337 338 339 340
    def change(self, event):
        '''
        Let's change the state of the call
        '''
okhin's avatar
okhin committed
341
        # First we need to check if it's really a change ie, if the new state is not the previous one
342
        self.update((':'.join([event['channel']['state']
okhin's avatar
okhin committed
343
            , event['channel']['id'].split('-')[-1]])
344
            , event['timestamp'],))
345
        phone_logger.info("New state for call {}: {}".format(event['channel']['id'], event['channel']['state']))
346 347
        # We now need to take action according to our new state
        if event['channel']['state'] == 'Up':
348
            # Are we the caller or the callee?
349
            if event['channel']['id'].endswith(self.callee):
okhin's avatar
okhin committed
350
                # Step 1 create a bridge
okhin's avatar
okhin committed
351
                bridge = ari.Bridge(config['asterisk'], self.id, 'mixing')
352
                phone_logger.debug("Creating a bridges to connect {} to {}".format(self.caller, self.callee,))
353
                bridge.create()
okhin's avatar
okhin committed
354
                # Step 2, moving channels
355
                channels = ",".join([self.id + '-' + self.caller, self.id + '-' + self.callee])
356
                phone_logger.debug("Moving channels to the created bridge: {}".format(bridge.name,))
357
                bridge.addChannel(channels)
358
                phone_logger.info("Call now fully connected: {} <-> {}".format(self.caller, self.callee))
359
                return
360 361
            # Call is being picked up, we want to play a song
            try:
okhin's avatar
okhin committed
362
                channel = ari.Channel(config['asterisk'], event['channel']['id'])
363
                phone_logger.debug('Preparing to create a playback')
364
                # We're starting a playback, with the same ID as the channel, to keep track of it
okhin's avatar
okhin committed
365
                channel.playback(channel.name, config['asterisk']['playback'])
366
            except Exception as e:
367
                phone_logger.exception(e)
368
                raise e
369 370

    def call_caller(self, event):
371
        '''
okhin's avatar
okhin committed
372 373
        Let's call the caller. It's a simple originate. We will also check if the MoH bridge is ready,
        because it will be used to store people in it waiting for the callee to pick up the phone.
374

okhin's avatar
okhin committed
375
        The bridge needed to connect the calls together will be created later.
376
        '''
377
        self.update((':'.join([event['type']
okhin's avatar
okhin committed
378
            , event['channel']['id'].split('-')[-1]])
379
            , event['timestamp'],))
okhin's avatar
okhin committed
380
        # We want to check if the moh bridge is opened
okhin's avatar
okhin committed
381
        moh_bridge = ari.Bridge(config['asterisk'], config['moh']['name'], 'holding')
382
        try:
383
            moh_bridge.status()
okhin's avatar
okhin committed
384 385
        except HTTPError as e:
            # The bridge dos not exist
386
            phone_logger.error("The Hold bridges does not exists")
387 388 389
            raise e

        # Now, let's create the channel
390
        try:
okhin's avatar
okhin committed
391 392
            endpoint = 'SIP/' + sanitize_phonenumber(self.caller) + '@' + config['asterisk']['sip-context']
            channel = ari.Channel(config['asterisk'], self.id + '-' + sanitize_phonenumber(self.caller))
okhin's avatar
okhin committed
393
            channel.originate(endpoint)
394
        except Exception as e:
395
            phone_logger.exception(e)
396
            raise e
397 398

    def save(self):
399 400 401
        '''
        Save the Call to database.
        '''
402
        phone_logger.debug("Saving call {}: {}".format(self.id, json.dumps(self, cls=PiphoneJSONEncoder)))
403 404 405 406 407 408
        try:
            self.db.execute('''INSERT OR REPLACE INTO calls (caller, callee, owner, callid, history)
                VALUES (?, ?, ?, ?, ?) '''
                , (self.caller, self.callee, self.owner, self.id, json.dumps(self.history)))
            self.db.commit()
        except Exception as e:
okhin's avatar
okhin committed
409
            bottle_logger.exception(e)
410
            phone_logger.exception(e)
411
            raise e
412

okhin's avatar
okhin committed
413
def start(db):
414 415
    global running
    running = True
416
    threads.submit(app.run, server='paste')
okhin's avatar
okhin committed
417
    loop.run_until_complete(listen(db))
418

419 420
def stop():
    global running
421
    running = False
422 423 424
    ws.close()
    loop.close()
    threads.shutdown(wait=False)
425
    sys.exit(0)
okhin's avatar
okhin committed
426

427
@app.get('/calls/<callid>')
428
@app.get('/calls/')
429
@authenticated
430
@connected
431
def calls(db, callid=None):
okhin's avatar
okhin committed
432 433 434 435
    """
    Return the list of calls associated to the connected user.
    The call has a status, caller, callee and history (status change+timestamp)
    """
436
    bottle_logger.debug("GET {}".format(request.fullpath))
437 438 439 440 441 442
    if callid == None:
        try:
            results = db.execute('SELECT callid FROM calls WHERE owner = ?;', (request.params['api'],))
            calls = []
            for row in results.fetchall():
                call = Call.load(row[0], db)
443
                bottle_logger.debug("Call fetched: {}".format(json.dumps(call, cls=PiphoneJSONEncoder)))
444 445 446 447
                calls.append(call)
            head = {'call': request.fullpath, 'user': request.params['api'], 'hits': len(calls)}
            return {'head': head, 'data': calls}
        except Exception as e:
448
            bottle_logger.exception(e)
449 450 451 452 453 454
            abort(500, "Exception")
    # We first need to check if we can access the callid we asked for
    try:
        results = db.execute('SELECT callid FROM calls WHERE owner = ? AND callid = ? ;'
                , (request.params['api'], callid,))
        rows = results.fetchall()
455
        bottle_logger.debug("Found {} results: {}".format(len(rows), rows))
456 457
        assert len(rows) == 1
        call = Call.load(callid, db)
okhin's avatar
okhin committed
458
        head = {'call': call.url, 'user': request.params['api'], 'hits': 1}
459 460
        return {'head': head, 'data': call}
    except AssertionError as e:
461 462
        bottle_logger.debug("Not exactly one results found, this is an issue")
        bottle_logger.error("Unauthorized access to call {} from user {}".format(callid, request.params['api']))
463 464
        abort(403, "You do not have the authorization to get this call")
    except Exception as e:
465 466
        bottle_logger.debug("Exception catched: {}".format(e,))
        bottle_logger.error("Call not found {}".format(callid,))
467 468 469 470
        abort(404, "This call does not exist")

@app.post('/calls/')
@app.post('/calls/<callid>')
471
@authenticated
472
@connected
473
def originate(db, callid=None):
474
    bottle_logger.debug("POST {}".format(request.fullpath))
475 476
    try:
        if callid is not None:
okhin's avatar
okhin committed
477
            call = Call(request.params['caller'], request.params['callee'], request.params['api'], callid=callid, db=db)
478 479
        else:
            call = Call(request.params['caller'], request.params['callee'], request.params['api'], db=db)
480
        bottle_logger.debug("Originate a call: {}".format(json.dumps(call, cls=PiphoneJSONEncoder)))
481
        call.event_handler({'type': 'Created', 'timestamp': datetime.datetime.now().isoformat(), 'channel': {'id': 'Init'}})
okhin's avatar
okhin committed
482 483
        call.save()
        head = {'call': call.url
484 485
            , 'user': request.params['api']
            , 'hits': 1}
486 487
        return {'header': head, 'data': call}
    except Exception as e:
488 489
        bottle_logger.debug("Missing params : {}".format([p for p in request.params],))
        bottle_logger.exception(e)
490
        abort(400, "Missing or incorrect fields, the call cannot be processed")
491

492 493 494 495 496 497 498 499 500
@app.get('/static/<filepath:path>')
def static_files(filepath):
    """
    take care of static files.
    should use apache/nginx instead.
    """
    return static_file(filepath, root='./views')

@app.get('/admin')
501
@authenticated
502
def little_admin(db):
503
    # We need to check if we're admin
okhin's avatar
okhin committed
504
    admin = db.execute('SELECT admin FROM users WHERE api = ?', (request.params['api'],));
505
    admin = admin.fetchone()
okhin's avatar
okhin committed
506
    if admin[0] != 1:
507
        abort(403, "You need to have an admin access")
508
    users = db.execute('SELECT api, token, admin FROM users').fetchall()
509
    return template('index', users=users, token=request.params['token'])
510 511

@app.post('/admin')
512
@authenticated
513
def medium_admin(db):
514
    api = request.forms.get('api')
515
    token = request.forms.get('api_token')
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    admin = request.forms.get('admin')
    action = request.forms.get('action')

    if action == 'delete':
        db.execute("DELETE FROM users WHERE api = ?", (api, ))
        db.commit()
    elif action == 'add':
        db.execute("INSERT INTO users (api, token, admin) VALUES (?, ?, ?)", (api, token, admin))
        db.commit()
    elif action == 'update':
        db.execute("UPDATE users set token = ?, admin = ? where api = ?", (token, admin, api ))
        db.commit()

    users = db.execute('SELECT api, token, admin FROM users').fetchall()
    return template('index', users=users)


533
if __name__ == '__main__':
534 535
    db = sqlite3.connect(config['piphone']['db'])
    phone_logger.info("Starting the piphone SIP backend")
536
    try:
okhin's avatar
okhin committed
537
        start(db)
538
    except (KeyboardInterrupt, SystemExit):
539
        stop()