def SMSToXML(cfg, sms, contact=None): ''' Convert a sms to XML ''' text = SmsTextFormat(cfg, sms['Text'], doxml=True) smsxml = " <message>\n" if sms['DateTime'] is not None: smsxml += " <date>" smsxml += sms['DateTime'].strftime("%d.%m.%Y %H:%M:%S") smsxml += "</date>\n" smsxml += " <dateenc>" smsxml += sms['DateTime'].strftime("%Y%m%d%H%M%S") smsxml += "</dateenc>\n" smsxml += " <text>" smsxml += escape(text.encode('utf-8')) smsxml += "</text>\n" smsxml += " <telephone>" smsxml += escape(sms['Number'].encode('utf-8')) smsxml += "</telephone>\n" smsxml += " <contact>" smsxml += escape(contact.encode('utf-8')) smsxml += "</contact>\n" smsxml += " <folder>" smsxml += str(sms['Folder']) smsxml += "</folder>\n" smsxml += " <stat>" smsxml += sms['State'] smsxml += "</stat>\n" smsxml += " </message>\n" return smsxml
def SMSToMail(cfg, sms, lookuplist = None, mailbox = False): ''' Converts SMS to formated mail. It will contain all images and sounds from message and predefined animations. Also text formatting is preserved in HTML. ''' msg = MIMEMultipart('related', None, None, type='text/html') prepend = '' name = '' if lookuplist != None: i = SearchNumber(lookuplist, sms['Number']) if i != -1: msg.add_header(HEADER_FORMAT % 'ContactID', str(i)) name = '%s ' % lookuplist[i]['Name'] for header in PARTS_TO_HEADER: msg.add_header(HEADER_FORMAT % header, unicode(sms['SMS'][0][header])) msg.add_header(HEADER_FORMAT % 'SMSC', sms['SMS'][0]['SMSC']['Number']) if sms['SMS'][0]['SMSCDateTime'] is not None: msg.add_header(HEADER_FORMAT % 'SMSCDate', DateToString(sms['SMS'][0]['SMSCDateTime'])) remote = '%s<*****@*****.**>' % (name, sms['Number'].replace(' ', '_')) local = cfg.Read('/MessageExport/From') if sms['SMS'][0]['Type'] == 'Submit': msg['To'] = remote msg['From'] = local else: msg['To'] = local msg['From'] = remote prepend = ('Received: from %s via GSM\n' % unicode(sms['SMS'][0]['SMSC']['Number'])) + prepend if len(sms['Name']) > 0 : msg['Subject'] = SmsTextFormat(cfg, sms['Name'], False) else: msg['Subject'] = SmsTextFormat(cfg, sms['Text'], False)[:50] + '...' if sms['DateTime'] is not None: msg['Date'] = DateToString(sms['DateTime']) if sms.has_key('SMSInfo'): text = '' cid = 0 for i in sms['SMSInfo']['Entries']: if i['ID'] in Wammu.Data.SMSIDs['PredefinedAnimation']: if i['Number'] > len(Wammu.Data.PredefinedAnimations): sub = MIMEImage(XPMToPNG(Wammu.Data.UnknownPredefined)) else: img = Wammu.Data.PredefinedAnimations[i['Number']][1] xpm = XPMToPNG(img) sub = MIMEImage(xpm) sub.add_header('Content-ID', '<%s>' % CID_FORMAT % cid) sub.add_header('Content-Disposition', 'inline') msg.attach(sub) text = text + '<img src="cid:%s">' % (CID_FORMAT % cid) cid = cid + 1 # FIXME: need sounds if 0 and i['ID'] in Wammu.Data.SMSIDs['PredefinedSound']: if i['Number'] >= len(Wammu.Data.PredefinedSounds): sub = '' else: sub = '' sub.add_header('Content-Disposition', 'attachment') msg.attach(sub) if i['ID'] in Wammu.Data.SMSIDs['Sound']: sub = MIMEAudio(RingtoneToMIDI(i['Ringtone']), 'midi') sub.add_header('Content-Disposition', 'attachment') msg.attach(sub) if i['ID'] in Wammu.Data.SMSIDs['Text']: fmt = '%s' for format_data in Wammu.Data.TextFormats: for name, dummy, style in format_data[1:]: if i.has_key(name) and i[name]: fmt = style % fmt text = text + (fmt % SmsTextFormat(cfg, i['Buffer'])) if i['ID'] in Wammu.Data.SMSIDs['Bitmap']: for bitmap in i['Bitmap']: sub = MIMEImage(XPMToPNG(bitmap['XPM'])) sub.add_header('Content-ID', '<%s>' % CID_FORMAT % cid) sub.add_header('Content-Disposition', 'inline') msg.attach(sub) text = text + '<img src="cid:%s">' % (CID_FORMAT % cid) cid = cid + 1 if i['ID'] in Wammu.Data.SMSIDs['Animation']: for bitmap in i['Bitmap']: sub = MIMEImage(XPMToPNG(bitmap['XPM'])) sub.add_header('Content-ID', '<%s>' % CID_FORMAT % cid) sub.add_header('Content-Disposition', 'inline') msg.attach(sub) text = text + '<img src="cid:%s">' % (CID_FORMAT % cid) cid = cid + 1 else: text = SmsTextFormat(cfg, sms['Text']) html = '<html><head></head><body>%s</body></html>' sub = MIMEText(html % text.encode('utf-8'), 'html', 'utf-8') msg.attach(sub) if sms['DateTime'] is not None: filename = '%s-%s-%s.eml' % ( sms['SMS'][0]['Type'], sms['DateTime'].strftime("%Y%m%d%H%M%S"), md5(sms['Text'].encode('utf-8')).hexdigest()) else: filename = '%s-%s.eml' % ( sms['SMS'][0]['Type'], md5(sms['Text'].encode('utf-8')).hexdigest()) # Add message ID msgid = '<%s@%s>' % (filename[:-4], sms['Number'].replace(' ', '_')) msgid = msgid.encode('ascii', 'xmlcharrefreplace') msg.add_header('Message-ID', msgid) if mailbox: if sms['DateTime'] is None: timestamp = time.asctime(time.localtime(None)) else: timestamp = time.asctime(sms['DateTime'].timetuple()) prepend = ('From [email protected] %s\n' % timestamp) + prepend return filename, prepend + msg.as_string(), msgid