示例#1
0
def smsSubmit(args):

    from messaging.sms import SmsSubmit
    from datetime import datetime

    text = ''.join(map(unichr, args["text_as_char_code_arr"]))

    sms = SmsSubmit(args["number"], text)

    if "csca" in args:
        sms.csca = args["csca"]

    if "validity" in args:
        sms.validity = datetime.strptime(args["validity"],
                                         "%a, %d %b %Y %H:%M:%S %Z")

    if "klass" in args:
        sms.klass = args["klass"]

    if "request_status" in args:
        sms.request_status = args["request_status"]

    out = []

    for pdu in sms.to_pdu():
        out.append(pdu.__dict__)

    return out
示例#2
0
文件: sms.py 项目: achiang/wader
    def to_pdu(self, store=False):
        """Returns the PDU representation of this message"""
        sms = SmsSubmit(self.number, self.text)

        sms.csca = self.csca
        sms.request_status = self.status_request

        if store:
            sms.validity = None

        return sms.to_pdu()
示例#3
0
    def test_encoding_8bit_message(self):
        number = "01000000000"
        csca = "+44000000000"
        text = "Hi there..."
        expected = "07914400000000F001000B811000000000F000040B48692074686572652E2E2E"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.csca = csca
        sms.fmt = 0x04  # 8 bits

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
示例#4
0
    def test_encoding_8bit_message(self):
        number = "01000000000"
        csca = "+44000000000"
        text = "Hi there..."
        expected = "07914400000000F001000B811000000000F000040B48692074686572652E2E2E"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.csca = csca
        sms.fmt = 0x04  # 8 bits

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
示例#5
0
    def test_encoding_csca(self):
        number = '2b3334363136353835313139'.decode('hex')
        text = "hola"
        csca = "+34646456456"
        expected = "07914346466554F601000B914316565811F9000004E8373B0C"
        expected_len = 17

        sms = SmsSubmit(number, text)
        sms.csca = csca
        sms.ref = 0x0

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
        self.assertEqual(pdu.length, expected_len)
        self.assertEqual(pdu.cnt, 1)
        self.assertEqual(pdu.seq, 1)
示例#6
0
    def test_encoding_csca(self):
        number = "+34616585119"
        text = "hola"
        csca = "+34646456456"
        expected = "07914346466554F601000B914316565811F9000004E8373B0C"
        expected_len = 17

        sms = SmsSubmit(number, text)
        sms.csca = csca
        sms.ref = 0x0

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
        self.assertEqual(pdu.length, expected_len)
        self.assertEqual(pdu.cnt, 1)
        self.assertEqual(pdu.seq, 1)
示例#7
0
    def test_encoding_csca(self):
        number = codecs.decode(b'2b3334363136353835313139', 'hex').decode()
        text = "hola"
        csca = "+34646456456"
        expected = "07914346466554F601000B914316565811F9000004E8373B0C"
        expected_len = 17

        sms = SmsSubmit(number, text)
        sms.csca = csca
        sms.ref = 0x0

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
        self.assertEqual(pdu.length, expected_len)
        self.assertEqual(pdu.cnt, 1)
        self.assertEqual(pdu.seq, 1)
示例#8
0
    def test_encoding_ucs2_message(self):
        number = '2b3334363136353835313139'.decode('hex')
        text = u'あ叶葉'
        csca = '+34646456456'
        expected = "07914346466554F601000B914316565811F9000806304253F68449"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.csca = csca

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)

        text = u"Русский"
        number = '363535333435363738'.decode('hex')
        expected = "001100098156355476F80008AA0E0420044304410441043A04380439"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.validity = timedelta(days=4)

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
示例#9
0
    def test_encoding_ucs2_message(self):
        number = "+34616585119"
        text = u'あ叶葉'
        csca = '+34646456456'
        expected = "07914346466554F601000B914316565811F9000806304253F68449"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.csca = csca

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)

        text = u"Русский"
        number = "655345678"
        expected = "001100098156355476F80008AA0E0420044304410441043A04380439"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.validity = timedelta(days=4)

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
示例#10
0
    def test_encoding_ucs2_message(self):
        number = codecs.decode(b'2b3334363136353835313139', 'hex').decode()
        text = u'あ叶葉'
        csca = '+34646456456'
        expected = "07914346466554F601000B914316565811F9000806304253F68449"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.csca = csca

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)

        text = u"Русский"
        number = codecs.decode(b'363535333435363738', 'hex').decode()
        expected = "001100098156355476F80008AA0E0420044304410441043A04380439"

        sms = SmsSubmit(number, text)
        sms.ref = 0x0
        sms.validity = timedelta(days=4)

        pdu = sms.to_pdu()[0]
        self.assertEqual(pdu.pdu, expected)
	def createSMS(self,num,msg,smsc,shahash):
		num=str(strip(num))
		msg=str(strip(msg))
		smsc=str(strip(smsc))
		### Log('createSMS new msg (0): NUM: '+num+", MSG: "+msg+", SMSC: "+smsc+", LEN:"+str(len(msg))+", HASH: "+shahash)
		if smsc[0] != "+" and smsc[0] != "0":
			smsc = "+"+smsc
		if num[0] != "0" and num[0] != "+":
			num = "+"+num
		Log('createSMS new msg (1): NUM: '+num+", MSG: "+msg+", SMSC: "+smsc+", LEN:"+str(len(msg))+", HASH: "+shahash)
		
		#reaplace unusual characters and white spaces
		msg=msg.replace("*","")
		msg=msg.replace("[","")
		msg=msg.replace("]","")
		msg=msg.replace("/","")
		msg=msg.replace("\\"," ")
		msg=msg.replace("\t"," ")
		msg=msg.replace("\n"," ")
		while True:
			if "  " in msg:
				msg=msg.replace("  "," ")
			else:
				break
		msg=strip(msg)
		if len(msg) > SMSLENGTH:
			Log('createSMS cutted msg: '+msg)
			msg=msg[0:SMSLENGTH]
		Log('	: NUM: '+num+", MSG:"+msg+", SMSC: "+smsc+", MSGLEN:"+str(len(msg))+", ID: "+shahash)
			
		if not USE_GSM_MODEM:
			
			#create sms format pdu
			y=strftime("%Y", gmtime())
			sms = SmsSubmit(num, msg)
			
			#validity= end of this year
			sms.validity = datetime(int(y)+1, 12, 31, 23, 59, 59)
			sms.csca = smsc
			pdu = sms.to_pdu()[0]
			
			s=pdu.pdu
			#print s
			
			##calculate checksum
			l=0
			sum=0
			for i in xrange(len(s)):
				if (i == 0 or i%2 == 0):
					j=i+2
					h= s[i:j]
					#print "hex:",h
					ih=int(h,16)
					#print "int:",str(ih)
					sum=(sum+ih) % 256
				l+=1
			fulllength=str((l/2)-8)
			chsum=str(hex(sum))[2:]
			#print "length: ",fulllength
			#print "sum: ",chsum

			tosend="AT^SM=32,"+fulllength+","+s+","+chsum
			#print "pdu:", tosend
			
			#try to send sms 5 times:
			Log('createSMS: SMS SENDING, BODY: '+str(tosend))
			msgsent=self.CommandSender(tosend,False,False,"smsout",20)
		else:
			if not len(self.GSMModules):
				Log("ERROR! No GSM module found! Message not sent.")
				return
			r=randint(0,(len(self.GSMModules)-1))
			usemod=str(self.GSMModules[r])
			
			self.initTelnetConnection()
			self.sendTelnetCommand("AT",False,False,'OK')
			self.sendTelnetCommand("AT!G=55",False,False,'OK')
			self.sendTelnetCommand("AT!G=A6",False,False,'OK')
			self.sendTelnetCommand("AT&G0"+usemod+"=AT+CMGF=1",False,False,'OK')
			#1
			tosend='AT&G0'+usemod+'=AT+CMGS="%s"' % num
			result=self.sendTelnetCommand(tosend,False,False,"++g00")
			Log("USE_GSM_MODEM, step1: ",result)
			#2
			tosend=msg+'\x1A'
			msgsent=self.sendTelnetCommand(tosend,False,False,"OK")
			Log("USE_GSM_MODEM, step2: ",msgsent)
			self.sendTelnetCommand("AT!G=55",False,False,'OK')
		
		#if not sent, put back to the queue:
		if not msgsent:
			Log('createSMS ERROR: cannot send SMS. Put back to INQ:'+str(msgsent))
			nowdate=str(strftime('%Y-%m-%d %H:%M:%S', localtime()))
			sqlobj=initSQLite(True)
			qry="UPDATE sms SET status = 'senderror', lastdate='"+nowdate+"'  WHERE hash = '"+shahash+"'"
			SQLiteExec(qry,sqlobj)
			SQLiteClose(sqlobj)
			###INQ.put_nowait([num,msg,smsc,shahash])
			sleep(1)
			return False
		
		#if sent: change status:
		else:
			nowdate=str(strftime('%Y-%m-%d %H:%M:%S', localtime()))
			sqlobj=initSQLite(True)
			qry="UPDATE sms SET status = 'sent', lastdate='"+nowdate+"'  WHERE hash = '"+shahash+"'"
			SQLiteExec(qry,sqlobj)
			SQLiteClose(sqlobj)
			Log('createSMS: SMS sent OK:'+str(msgsent))
			return True
	def createSMS(self,num,msg,smsc,shahash):
		num=str(strip(num))
		msg=str(strip(msg))
		smsc=str(strip(smsc))
		
		### Log('createSMS new msg (0): NUM: '+num+", MSG: "+msg+", SMSC: "+smsc+", LEN:"+str(len(msg))+", HASH: "+shahash)
		if smsc[0] != "+" and smsc[0] != "0":
			smsc = "+"+smsc
		if num[0] != "0" and num[0] != "+":
			num = "+"+num
		### Log('createSMS new msg (1): NUM: '+num+", MSG: "+msg+", SMSC: "+smsc+", LEN:"+str(len(msg))+", HASH: "+shahash)
		
		#reaplace unusual characters and white spaces
		msg=msg.replace("*","")
		msg=msg.replace("\t"," ")
		msg=msg.replace("\n"," ")
		while True:
			if "  " in msg:
				msg=msg.replace("  "," ")
			else:
				break
		msg=strip(msg)
		if len(msg) > SMSLENGTH:
			Log('createSMS cutted msg: '+msg)
			msg=msg[0:SMSLENGTH]
		Log('createSMS new msg: NUM: '+num+", MSG:"+msg+", SMSC: "+smsc+", MSGLEN:"+str(len(msg))+", ID: "+shahash)
		
		#create sms format pdu
		y=strftime("%Y", gmtime())
		sms = SmsSubmit(num, msg)
		
		#validity= end of this year
		sms.validity = datetime(int(y)+1, 12, 31, 23, 59, 59)
		sms.csca = smsc
		pdu = sms.to_pdu()[0]
		
		s=pdu.pdu
		#print s
		
		##calculate checksum
		l=0
		sum=0
		for i in xrange(len(s)):
			if (i == 0 or i%2 == 0):
				j=i+2
				h= s[i:j]
				#print "hex:",h
				ih=int(h,16)
				#print "int:",str(ih)
				sum=(sum+ih) % 256
			l+=1
		fulllength=str((l/2)-8)
		chsum=str(hex(sum))[2:]
		#print "length: ",fulllength
		#print "sum: ",chsum

		tosend="AT^SM=32,"+fulllength+","+s+","+chsum
		#print "pdu:", tosend
		
		#try to send sms 5 times:
		Log('createSMS: SMS SENDING, BODY: '+str(tosend))
		msgsent=self.CommandSender(tosend,False,False,"smsout",20)
		
		#if not sent, put back to the queue:
		if not msgsent:
			Log('createSMS ERROR: cannot send SMS. Put back to INQ:'+str(msgsent))
			nowdate=str(strftime('%Y-%m-%d %H:%M:%S', localtime()))
			sqlobj=initSQLite(True)
			qry="UPDATE sms SET status = 'senderror', lastdate='"+nowdate+"'  WHERE hash = '"+shahash+"'"
			SQLiteExec(qry,sqlobj)
			SQLiteClose(sqlobj)
			###INQ.put_nowait([num,msg,smsc,shahash])
			sleep(1)
			return False
		
		#if sent: change status:
		else:
			nowdate=str(strftime('%Y-%m-%d %H:%M:%S', localtime()))
			sqlobj=initSQLite(True)
			qry="UPDATE sms SET status = 'sent', lastdate='"+nowdate+"'  WHERE hash = '"+shahash+"'"
			SQLiteExec(qry,sqlobj)
			SQLiteClose(sqlobj)
			Log('createSMS: SMS sent OK:'+str(msgsent))
			return True