def gotConnection(conn, username, password,src,dest,text): yield conn.authenticate(username, password) #print 'Connected to RabbitMQ' chan = yield conn.channel(1) yield chan.channel_open() DeliverSmPDU = DeliverSM( source_addr = src, source_addr_ton = AddrTon.NATIONAL, source_addr_npi = AddrNpi.ISDN, destination_addr = dest, dest_addr_ton = AddrTon.NATIONAL, dest_addr_npi = AddrNpi.ISDN, esm_class=EsmClass(EsmClassMode.DEFAULT, EsmClassType.DEFAULT), protocol_id=0, priority_flag=PriorityFlag.LEVEL_0, registered_delivery=RegisteredDelivery(RegisteredDeliveryReceipt.NO_SMSC_DELIVERY_RECEIPT_REQUESTED), replace_if_present_flag=ReplaceIfPresentFlag.DO_NOT_REPLACE, short_message = text, seqNum = 1,) content = DeliverSmContent(DeliverSmPDU, 'SIMULATOR' ) #print DeliverSmPDU yield chan.basic_publish(exchange='messaging', routing_key='deliver.sm.SIMULATOR', content=content) # A clean way to tear down and stop yield chan.channel_close() chan0 = yield conn.channel(0) yield chan0.connection_close()
def getReceipt(self, msgid, source_addr, destination_addr, message_status): "Will build a DataSm containing a receipt data" # Build pdu pdu = DataSM( source_addr = source_addr, destination_addr = destination_addr, esm_class = EsmClass(EsmClassMode.DEFAULT, EsmClassType.SMSC_DELIVERY_RECEIPT), receipted_message_id = msgid, ) # Set pdu.message_state if message_status[:5] == 'ESME_': # It is a receipt catched from a submit_sm_resp if message_status == 'ESME_ROK': pdu.params['message_state'] = MessageState.ACCEPTED else: pdu.params['message_state'] = MessageState.UNDELIVERABLE elif message_status == 'UNDELIV': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.UNDELIVERABLE elif message_status == 'REJECTD': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.REJECTED elif message_status == 'DELIVRD': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.DELIVERED elif message_status == 'EXPIRED': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.EXPIRED elif message_status == 'DELETED': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.DELETED elif message_status == 'ACCEPTD': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.ACCEPTED elif message_status == 'UNKNOWN': # It is a receipt catched from a deliver_sm pdu.params['message_state'] = MessageState.UNKNOWN else: raise UnknownMessageStatusError('Unknow message_status: %s' % message_status) return pdu
def __init__(self, **kwargs): ##################### # Generick configuration block # cid validation if kwargs.get('id', None) == None: raise ConfigUndefinedIdError('SMPPConfig must have an id') idcheck = re.compile(r'^[A-Za-z0-9_-]{3,25}$') if idcheck.match(str(kwargs.get('id'))) == None: raise ConfigInvalidIdError('SMPPConfig id syntax is invalid') self.id = str(kwargs.get('id')) self.port = kwargs.get('port', 2775) if not isinstance(self.port, int): raise TypeMismatch('port must be an integer') # Logging configuration self.log_file = kwargs.get('log_file', '/var/log/jasmin/default-%s.log' % self.id) self.log_rotate = kwargs.get('log_rotate', 'midnight') self.log_level = kwargs.get('log_level', logging.INFO) self.log_format = kwargs.get( 'log_format', '%(asctime)s %(levelname)-8s %(process)d %(message)s') self.log_date_format = kwargs.get('log_dateformat', '%Y-%m-%d %H:%M:%S') # Timeout for response to bind request self.sessionInitTimerSecs = kwargs.get('sessionInitTimerSecs', 30) if not isinstance(self.sessionInitTimerSecs, int) and not isinstance( self.sessionInitTimerSecs, float): raise TypeMismatch( 'sessionInitTimerSecs must be an integer or float') # Enquire link interval self.enquireLinkTimerSecs = kwargs.get('enquireLinkTimerSecs', 30) if not isinstance(self.enquireLinkTimerSecs, int) and not isinstance( self.enquireLinkTimerSecs, float): raise TypeMismatch( 'enquireLinkTimerSecs must be an integer or float') # Maximum time lapse allowed between transactions, after which, the connection is considered as inactive # and will reconnect self.inactivityTimerSecs = kwargs.get('inactivityTimerSecs', 300) if not isinstance(self.inactivityTimerSecs, int) and not isinstance( self.inactivityTimerSecs, float): raise TypeMismatch( 'inactivityTimerSecs must be an integer or float') # Timeout for responses to any request PDU self.responseTimerSecs = kwargs.get('responseTimerSecs', 60) if not isinstance(self.responseTimerSecs, int) and not isinstance( self.responseTimerSecs, float): raise TypeMismatch('responseTimerSecs must be an integer or float') # Timeout for reading a single PDU, this is the maximum lapse of time between # receiving PDU's header and its complete read, if the PDU reading timed out, # the connection is considered as 'corrupt' and will reconnect self.pduReadTimerSecs = kwargs.get('pduReadTimerSecs', 10) if not isinstance(self.pduReadTimerSecs, int) and not isinstance( self.pduReadTimerSecs, float): raise TypeMismatch('pduReadTimerSecs must be an integer or float') # DLR # How much time a message is kept in redis waiting for receipt self.dlr_expiry = kwargs.get('dlr_expiry', 86400) if not isinstance(self.dlr_expiry, int) and not isinstance( self.dlr_expiry, float): raise TypeMismatch('dlr_expiry must be an integer or float') ##################### # SMPPClient Specific configuration block self.host = kwargs.get('host', '127.0.0.1') if not isinstance(self.host, str): raise TypeMismatch('host must be a string') self.username = kwargs.get('username', 'smppclient') self.password = kwargs.get('password', 'password') self.systemType = kwargs.get('systemType', '') # Reconnection self.reconnectOnConnectionLoss = kwargs.get( 'reconnectOnConnectionLoss', True) if not isinstance(self.reconnectOnConnectionLoss, bool): raise TypeMismatch('reconnectOnConnectionLoss must be a boolean') self.reconnectOnConnectionFailure = kwargs.get( 'reconnectOnConnectionFailure', True) if not isinstance(self.reconnectOnConnectionFailure, bool): raise TypeMismatch( 'reconnectOnConnectionFailure must be a boolean') self.reconnectOnConnectionLossDelay = kwargs.get( 'reconnectOnConnectionLossDelay', 10) if not isinstance(self.reconnectOnConnectionLossDelay, int) and not isinstance( self.reconnectOnConnectionLossDelay, float): raise TypeMismatch( 'reconnectOnConnectionLossDelay must be an integer or float') self.reconnectOnConnectionFailureDelay = kwargs.get( 'reconnectOnConnectionFailureDelay', 10) if not isinstance(self.reconnectOnConnectionFailureDelay, int) and not isinstance( self.reconnectOnConnectionFailureDelay, float): raise TypeMismatch( 'reconnectOnConnectionFailureDelay must be an integer or float' ) self.useSSL = kwargs.get('useSSL', False) self.SSLCertificateFile = kwargs.get('SSLCertificateFile', None) # Type of bind operation, can be one of these: # - transceiver # - transmitter # - receiver self.bindOperation = kwargs.get('bindOperation', 'transceiver') if self.bindOperation not in [ 'transceiver', 'transmitter', 'receiver' ]: raise UnknownValue('Invalid bindOperation: %s' % self.bindOperation) # These are default parameters, c.f. _setConfigParamsInPDU method in SMPPOperationFactory self.service_type = kwargs.get('service_type', None) self.addressTon = kwargs.get('addressTon', AddrTon.UNKNOWN) self.addressNpi = kwargs.get('addressNpi', AddrNpi.UNKNOWN) self.source_addr_ton = kwargs.get('source_addr_ton', AddrTon.NATIONAL) self.source_addr_npi = kwargs.get('source_addr_npi', AddrNpi.ISDN) self.dest_addr_ton = kwargs.get('dest_addr_ton', AddrTon.INTERNATIONAL) self.dest_addr_npi = kwargs.get('dest_addr_npi', AddrNpi.ISDN) self.addressRange = kwargs.get('addressRange', None) self.source_addr = kwargs.get('source_addr', None) self.esm_class = kwargs.get( 'esm_class', EsmClass(EsmClassMode.STORE_AND_FORWARD, EsmClassType.DEFAULT)) self.protocol_id = kwargs.get('protocol_id', None) self.priority_flag = kwargs.get('priority_flag', PriorityFlag.LEVEL_0) self.schedule_delivery_time = kwargs.get('schedule_delivery_time', None) self.validity_period = kwargs.get('validity_period', None) self.registered_delivery = kwargs.get( 'registered_delivery', RegisteredDelivery( RegisteredDeliveryReceipt.NO_SMSC_DELIVERY_RECEIPT_REQUESTED)) self.replace_if_present_flag = kwargs.get( 'replace_if_present_flag', ReplaceIfPresentFlag.DO_NOT_REPLACE) self.sm_default_msg_id = kwargs.get('sm_default_msg_id', 0) # 5.2.19 data_coding / c. There is no default setting for the data_coding parameter. # Possible values: # SMSC_DEFAULT_ALPHABET: 0x00 / 0 # IA5_ASCII: 0x01 / 1 # OCTET_UNSPECIFIED: 0x02 / 2 # LATIN_1: 0x03 / 3 # OCTET_UNSPECIFIED_COMMON: 0x04 / 4 # JIS: 0x05 / 5 # CYRILLIC: 0x06 / 6 # ISO_8859_8: 0x07 / 7 # UCS2: 0x08 / 8 # PICTOGRAM: 0x09 / 9 # ISO_2022_JP: 0x0a / 10 # EXTENDED_KANJI_JIS: 0x0d / 13 # KS_C_5601: 0x0e / 14 self.data_coding = kwargs.get('data_coding', 0) if self.data_coding not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14]: raise UnknownValue('Invalid data_coding: %s' % self.data_coding) # QoS # Rejected messages are requeued with a fixed delay self.requeue_delay = kwargs.get('requeue_delay', 120) if not isinstance(self.requeue_delay, int) and not isinstance( self.requeue_delay, float): raise TypeMismatch('requeue_delay must be an integer or float') self.submit_sm_throughput = kwargs.get('submit_sm_throughput', 1) if not isinstance(self.submit_sm_throughput, int) and not isinstance( self.submit_sm_throughput, float): raise TypeMismatch( 'submit_sm_throughput must be an integer or float') # DLR Message id bases from submit_sm_resp to deliver_sm, possible values: # [0] (default) : submit_sm_resp and deliver_sm messages IDs are on the same base. # [1] : submit_sm_resp msg-id is in hexadecimal base, deliver_sm msg-id is in # decimal base. # [2] : submit_sm_resp msg-id is in decimal base, deliver_sm msg-id is in # hexadecimal base. self.dlr_msg_id_bases = kwargs.get('dlr_msg_id_bases', 0) if self.dlr_msg_id_bases not in [0, 1, 2]: raise UnknownValue('Invalid dlr_msg_id_bases: %s' % self.dlr_msg_id_bases)
def SubmitSM(self, short_message, data_coding=0, **kwargs): """Depending on the short_message length, this method will return a classical SubmitSM or a serie of linked SubmitSMs (parted message) """ kwargs['short_message'] = short_message kwargs['data_coding'] = data_coding # Possible data_coding values : 0,1,2,3,4,5,6,7,8,9,10,13,14 # Set the max short message length depending on the # coding (7, 8 or 16 bits) if kwargs['data_coding'] in [3, 6, 7, 10]: # 8 bit coding maxSmLength = 140 slicedMaxSmLength = maxSmLength - 6 elif kwargs['data_coding'] in [2, 4, 5, 8, 9, 13, 14]: # 16 bit coding maxSmLength = 70 slicedMaxSmLength = maxSmLength - 3 else: # 7 bit coding is the default # for data_coding in [0, 1] or any other invalid value maxSmLength = 160 slicedMaxSmLength = 153 longMessage = kwargs['short_message'] smLength = len(kwargs['short_message']) # if SM is longer than maxSmLength, build multiple SubmitSMs # and link them if smLength > maxSmLength: total_segments = int(math.ceil(smLength / float(slicedMaxSmLength))) # Obey to configured longContentMaxParts if total_segments > self.long_content_max_parts: total_segments = self.long_content_max_parts msg_ref_num = self.claimLongSmSeqNum() for i in range(total_segments): segment_seqnum = i + 1 # Keep in memory previous PDU in order to set nextPdu in it later try: tmpPdu previousPdu = tmpPdu except NameError: previousPdu = None kwargs['short_message'] = longMessage[slicedMaxSmLength * i:slicedMaxSmLength * (i + 1)] tmpPdu = self._setConfigParamsInPDU(SubmitSM(**kwargs), kwargs) if self.long_content_split == 'sar': # Slice short_message and create the PDU using SAR options tmpPdu.params['sar_total_segments'] = total_segments tmpPdu.params['sar_segment_seqnum'] = segment_seqnum tmpPdu.params['sar_msg_ref_num'] = msg_ref_num elif self.long_content_split == 'udh': # Slice short_message and create the PDU using UDH options tmpPdu.params['esm_class'] = EsmClass( EsmClassMode.DEFAULT, EsmClassType.DEFAULT, [EsmClassGsmFeatures.UDHI_INDICATOR_SET]) if segment_seqnum < total_segments: tmpPdu.params[ 'more_messages_to_send'] = MoreMessagesToSend.MORE_MESSAGES else: tmpPdu.params[ 'more_messages_to_send'] = MoreMessagesToSend.NO_MORE_MESSAGES # UDH composition: udh = [] udh.append(struct.pack('!B', 5)) # Length of User Data Header udh.append( struct.pack('!B', 0) ) # Information Element Identifier, equal to 00 (Concatenated short messages, 8-bit reference number) udh.append( struct.pack('!B', 3) ) # Length of the header, excluding the first two fields; equal to 03 udh.append(struct.pack('!B', msg_ref_num)) udh.append(struct.pack('!B', total_segments)) udh.append(struct.pack('!B', segment_seqnum)) tmpPdu.params['short_message'] = ''.join( udh) + kwargs['short_message'] # - The first PDU is the one we return back # - sar_msg_ref_num takes the seqnum of the initial submit_sm if i == 0: pdu = tmpPdu # PDU chaining if previousPdu is not None: previousPdu.nextPdu = tmpPdu else: pdu = self._setConfigParamsInPDU(SubmitSM(**kwargs), kwargs) return pdu
def getReceipt(self, dlr_pdu, msgid, source_addr, destination_addr, message_status, sub_date): "Will build a DataSm or a DeliverSm (depending on dlr_pdu) containing a receipt data" sm_message_stat = message_status # Prepare message_state if message_status[:5] == 'ESME_': if message_status == 'ESME_ROK': message_state = MessageState.ACCEPTED sm_message_stat = 'ACCEPTD' err = 0 else: message_state = MessageState.UNDELIVERABLE sm_message_stat = 'UNDELIV' err = 10 elif message_status == 'UNDELIV': message_state = MessageState.UNDELIVERABLE err = 10 elif message_status == 'REJECTD': message_state = MessageState.REJECTED err = 20 elif message_status == 'DELIVRD': err = 0 message_state = MessageState.DELIVERED elif message_status == 'EXPIRED': err = 30 message_state = MessageState.EXPIRED elif message_status == 'DELETED': err = 40 message_state = MessageState.DELETED elif message_status == 'ACCEPTD': err = 0 message_state = MessageState.ACCEPTED elif message_status == 'UNKNOWN': err = 50 message_state = MessageState.UNKNOWN else: raise UnknownMessageStatusError('Unknow message_status: %s' % message_status) # Build pdu if dlr_pdu == 'deliver_sm': short_message = r"id:%s submit date:%s done date:%s stat:%s err:%03d" % ( msgid, parser.parse(sub_date).strftime("%Y%m%d%H%M"), datetime.datetime.now().strftime("%Y%m%d%H%M"), sm_message_stat, err, ) # Build DeliverSM pdu pdu = DeliverSM( source_addr=destination_addr, destination_addr=source_addr, esm_class=EsmClass(EsmClassMode.DEFAULT, EsmClassType.SMSC_DELIVERY_RECEIPT), receipted_message_id=msgid, short_message=short_message, message_state=message_state, ) else: # Build DataSM pdu pdu = DataSM( source_addr=destination_addr, destination_addr=source_addr, esm_class=EsmClass(EsmClassMode.DEFAULT, EsmClassType.SMSC_DELIVERY_RECEIPT), receipted_message_id=msgid, message_state=message_state, ) return pdu