def _get_new_msisdn(self, msisdn, name): try: newext = msisdn while True: # There was an Infinite loop here if msisdn + 1 does exist we never get out # Not ever sure what this is for. # if last ext available reset to 0 if newext == 99999: newext = 00000 # increment msisdn of one and check if exists newexti = int(newext) + 1 newext = str(newexti) api_log.debug('New Extension: %s' % newext) if not self._check_subscriber_exists(newext): try: self._authorize_subscriber_in_local_hlr( msisdn, config['internal_prefix'] + newext, name) except OsmoHlrError: raise SubscriberException( 'SQ_HLR error adding new extension %s to the db' % newext) return newext except SubscriberException: raise SubscriberException( 'Error in getting new msisdn for existing subscriber')
def get(self, request): api_log.info('%s - [GET] %s/', request.getHost().host, self.path) try: credit = Credit() data = json.dumps(credit.get_all_credit_allocated(), cls=PGEncoder) except CreditException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def search(self, request, search): api_log.info('%s - [GET] %s', request.getHost().host, self.path) try: sub = Subscriber() data = json.dumps(sub.get_msisdn_autocomplete(search)) except SubscriberException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def packages(self, request): api_log.debug('%s - [GET] %s/packages', request.getHost().host, self.path) try: config = Configuration() data = json.dumps(config.get_packages(), cls=PGEncoder) except ConfigurationException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def _check_subscriber_exists(self, msisdn): try: api_log.debug('Check exists: %s' % msisdn) full_msisdn = config['internal_prefix'] + msisdn entry = self._osmo_hlr.get_imsi_from_msisdn(full_msisdn) return entry is not None except NoDataException: return False except OsmoHlrError as e: raise SubscriberException('SQ_HLR error sub: %s' % e.args[0])
def records(self, request, year): api_log.info('%s - [GET] %s/records %s', request.getHost().host, self.path, year) try: credit = Credit() data = json.dumps(credit.get_credit_records(year), cls=PGEncoder) except CreditException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def get_sms_stat(self, request, year, month): api_log.info('%s - [GET] %s/calls/obm', request.getHost().host, self.path) try: stats = CallsStatistics() data = json.dumps(stats.get_sms_stat(year, month), cls=PGEncoder) except StatisticException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return str(data)
def _authorize_subscriber_in_local_hlr(self, msisdn, new_msisdn, name): try: api_log.debug('Auth Subscriber in Local HLR: %s, %s' % (msisdn, new_msisdn)) self._osmo_hlr.update_msisdn(msisdn, new_msisdn) self._osmo_hlr.enable_access_by_msisdn(new_msisdn) if use_nitb_osmo_stack: self._osmo_hlr.update_name(msisdn, unidecode(name)) except Exception as e: raise SubscriberException( 'SQ_HLR error provisioning the subscriber %s' % e)
def imei(self, request, partial_imei): api_log.info('%s - [GET] %s/%s', request.getHost().host, self.path, partial_imei) try: sub = Subscriber() data = json.dumps(sub.get_imei_autocomplete(partial_imei)) except SubscriberException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def month(self, request, year, month): api_log.info('%s - [POST] %s/month %s %s', request.getHost().host, self.path, year, month) try: credit = Credit() data = json.dumps(credit.get_month_credit(year, month), cls=PGEncoder) except CreditException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def extension(self, request, imsi): api_log.info('%s - [GET] %s/%s', request.getHost().host, self.path, imsi) if request.getHeader('Origin').find("8080") > -1: request.setHeader('Access-Control-Allow-Origin', '*') try: sub = Subscriber() data = json.dumps(sub.get_local_extension(imsi), cls=PGEncoder) except SubscriberException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data
def receive(self, request, source, destination, charset, coding, text): try: sms = SMS() import threading thread = threading.Thread(target=sms.receive, args=(source, destination, text, charset, coding)) thread.daemon = True api_log.info('Starting thread for chat message to %s via %s', destination, request.getClientIP()) thread.start() data = {'status': 'success', 'error': ''} api_log.debug(data) return data except Exception as e: api_log.error("Chat handler exception %s", e, exc_info=True)
def monitor_feed(self, request): api_log.info('%s - [GET] %s/feed', request.getHost().host, self.path) if request.getClientIP().find("10.23") > -1: request.setHeader('Access-Control-Allow-Origin', '*') else: return '' try: stats = LiveStatistics() data = json.dumps(stats.monitor_feed(), cls=PGEncoder) except StatisticException as e: data = {'status': 'failed', 'error': str(e)} except psycopg2.InterfaceError as e: if str(e) == "connection already closed": api_log.debug("Exiting due to DB Error: %s", e) os._exit(-1) api_log.debug(data) return data
def offline(self, request, msisdn='', local='no'): api_log.info('%s - [PUT] %s/offline Data: msisdn: "%s"', request.getClientIP(), self.path, msisdn) try: sub = Subscriber() sub.expire_lu(msisdn) data = {'status': 'success', 'error': ''} except SubscriberException as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data # Take advantage to point this subscriber to the new location in our hlr # so we (at least) don't have to wait for the next hlr sync run try: if local == 'yes': current_bts = config['local_ip'] else: current_bts = request.getClientIP() cur = db_conn.cursor() now = datetime.datetime.fromtimestamp(int(time.time())) cur.execute( 'UPDATE hlr SET current_bts=%(current_bts)s, updated=%(updated)s WHERE msisdn=%(msisdn)s', { 'msisdn': msisdn, 'current_bts': current_bts, 'updated': now }) db_conn.commit() except psycopg2.DatabaseError as e: data = {'status': 'failed', 'error': str(e)} api_log.debug(data) return data api_log.debug(data) return data
def get(self, request, msisdn): api_log.info('%s - [GET] %s/%s', request.getHost().host, self.path, msisdn) try: sub = Subscriber() if msisdn == 'all_connected': data = json.dumps(sub.get_all_connected(), cls=PGEncoder) elif msisdn == 'all_sip': data = json.dumps(sub.get_sip_connected()) elif msisdn == 'unpaid_subscription': data = json.dumps(sub.get_unpaid_subscription(), cls=PGEncoder) elif msisdn == 'paid_subscription': data = json.dumps(sub.get_paid_subscription(), cls=PGEncoder) elif msisdn == 'unauthorized': data = json.dumps(sub.get_unauthorized(), cls=PGEncoder) elif msisdn == 'online': data = json.dumps(sub.get_online(), cls=PGEncoder) elif msisdn == 'offline': data = json.dumps(sub.get_offline(), cls=PGEncoder) elif msisdn == 'all_roaming': data = json.dumps(sub.get_roaming(), cls=PGEncoder) elif msisdn == 'all_foreign': if request.getClientIP().find("10.23") > -1: request.setHeader('Access-Control-Allow-Origin', '*') data = json.dumps(sub.get_all_foreign(), cls=PGEncoder) else: data = json.dumps(sub.get(msisdn), cls=PGEncoder) except SubscriberException as e: data = {'status': 'failed', 'error': str(e)} if msisdn != 'all_connected': api_log.info(data) else: api_log.debug(data) return data
def site(self, request): api_log.debug('%s - [GET] %s/site', request.getHost().host, self.path) try: config = Configuration() data = json.dumps(config.get_site(), cls=PGEncoder) except ConfigurationException as e: data = {'status': 'failed', 'error': str(e)} except psycopg2.InterfaceError as e: if str(e) == "connection already closed": api_log.debug("Exiting due to DB Error: %s", e) os._exit(-1) api_log.debug(data) return data
def receive(self, request, source, destination, charset, coding, text, btext='', dr='', dcs=''): if btext == '': btext = text thex = binascii.hexlify(btext) api_log.info( '%s - [POST] %s Data: source:"%s" destination:"%s" charset:"%s"', request.getHost().host, self.path, source, destination, charset) api_log.debug( 'Data: source:"%s" destination:"%s" charset:"%s" coding: "%s" content: %s HexofBin: %s DR: %s DCS: %s', source, destination, charset, coding, text.decode(charset, 'replace'), thex, dr, dcs) sms = SMS() unicode_text = text.decode(charset, 'replace') if use_kannel == 'yes': # Kannel posts to: # post-url = "http://localhost:8085/sms?source=%p&destination=%P&charset=%C&coding=%c&text=%a&btext=%b&dr=%d&dcs=%O" # Kannel sends us GSM0338 but sets charset param to UTF-8 if coding == '0': try: unicode_text = sms.check_decode0338(btext) api_log.info('SMS Decoded from GSM 03.38') api_log.debug('Decoded text:"%s"', unicode_text) except Exception as ex: api_log.debug('Coding(%s), but: %s', coding, str(ex), exc_info=True) data = { 'status': 'failed', 'error': str(ex) + ' ' + str(sys.exc_info()[1]) } # It's probably utf-8 unicode_text = btext.decode('utf-8', 'replace') elif coding == '2' and charset == 'UTF-16BE': try: unicode_text = btext.decode('utf-16be') api_log.info('SMS decoded as UTF-16BE') api_log.debug('Decoded text: "%s"', text) except Exception as e: # Catch Everything, try to not actually LOSE messages! api_log.debug('Exception: %s', e, exc_info=True) # Some phones are sending multi part messages with different charsets. # Kannel concatenates and sends as UTF-16BE coding 2 try: api_log.info('Trying multi part trick') a = btext[:134] b = btext[134:] unicode_text = a.decode('utf-16be') + b.decode('utf8') except Exception as e: api_log.debug('Exception: %s', e, exc_info=True) unicode_text = btext.decode('utf-16be', 'replace') else: unicode_text = btext.decode('utf-8', 'replace') try: sms.receive(source, destination, unicode_text, charset, coding) data = {'status': 'success', 'error': ''} except SMSException as e: data = {'status': 'failed', 'error': str(e)} api_log.info(data) return data