Translated using Weblate.
[wammu.git] / Wammu / MailWriter.py
blobde30581c1e6dfb18e224a0034b3c6f92c057a2ef
1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
3 '''
4 Wammu - Phone manager
5 Module for writing SMS to Email.
6 '''
7 __author__ = 'Michal Čihař'
8 __email__ = 'michal@cihar.com'
9 __license__ = '''
10 Copyright © 2003 - 2010 Michal Čihař
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License version 2 as published by
14 the Free Software Foundation.
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 more details.
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 '''
26 from Wammu.Utils import SearchNumber
27 from Wammu.MessageDisplay import SmsTextFormat
28 from email.MIMEAudio import MIMEAudio
29 from email.MIMEImage import MIMEImage
30 from email.MIMEText import MIMEText
31 from email.MIMEMultipart import MIMEMultipart
32 import email.Utils
33 from hashlib import md5
34 try:
35 from hashlib import md5
36 except ImportError:
37 from md5 import new as md5
38 import time
39 import tempfile
40 import os
41 import wx
42 import Wammu.Data
43 if Wammu.gammu_error == None:
44 import gammu
46 HEADER_FORMAT = 'X-Wammu-%s'
47 CID_FORMAT = '%d*sms@wammu.sms'
48 PARTS_TO_HEADER = [
49 'Folder',
50 'Memory',
51 'Location',
52 'Name',
53 'Type',
54 'State',
55 'Class',
56 'MessageReference',
60 def XPMToPNG(image):
61 '''
62 Convert XPM data to PNG image.
64 @todo: I'd like to avoid creating temporary file, but even PIL doesn't
65 seem to provide such functionality.
66 '''
67 handle, name = tempfile.mkstemp()
68 os.close(handle)
69 bitmap = wx.BitmapFromXPMData(image)
70 bitmap.SaveFile(name, wx.BITMAP_TYPE_PNG)
71 f = file(name)
72 data = f.read()
73 f.close()
74 os.unlink(name)
75 return data
77 def RingtoneToMIDI(data):
78 '''
79 Converts ringtone to MIDI data.
81 @todo: I'd like to avoid creating temporary file, but Gammmu doesn't
82 provide such functionality.
83 '''
84 handle, name = tempfile.mkstemp()
85 f = os.fdopen(handle, 'w+')
86 gammu.SaveRingtone(f, data, 'mid')
87 f.seek(0)
88 data = f.read()
89 f.close()
90 os.unlink(name)
91 return data
93 def DateToString(date):
94 '''
95 Convert date to RFC 2822 format.
96 '''
97 return email.Utils.formatdate(time.mktime(date.timetuple()), True)
99 def SMSToMail(cfg, sms, lookuplist = None, mailbox = False):
101 Converts SMS to formated mail. It will contain all images and sounds from
102 message and predefined animations. Also text formatting is preserved in
103 HTML.
105 msg = MIMEMultipart('related', None, None, type='text/html')
106 prepend = ''
107 name = ''
108 if lookuplist != None:
109 i = SearchNumber(lookuplist, sms['Number'])
110 if i != -1:
111 msg.add_header(HEADER_FORMAT % 'ContactID', str(i))
112 name = '%s ' % lookuplist[i]['Name']
114 for header in PARTS_TO_HEADER:
115 msg.add_header(HEADER_FORMAT % header, unicode(sms['SMS'][0][header]))
116 msg.add_header(HEADER_FORMAT % 'SMSC', sms['SMS'][0]['SMSC']['Number'])
117 if sms['SMS'][0]['SMSCDateTime'] is not None:
118 msg.add_header(HEADER_FORMAT % 'SMSCDate',
119 DateToString(sms['SMS'][0]['SMSCDateTime']))
121 remote = '%s<%s@wammu.sms>' % (name, sms['Number'].replace(' ', '_'))
122 local = cfg.Read('/MessageExport/From')
124 if sms['SMS'][0]['Type'] == 'Submit':
125 msg['To'] = remote
126 msg['From'] = local
127 else:
128 msg['To'] = local
129 msg['From'] = remote
130 prepend = ('Received: from %s via GSM\n' %
131 unicode(sms['SMS'][0]['SMSC']['Number'])) + prepend
133 if len(sms['Name']) > 0 :
134 msg['Subject'] = SmsTextFormat(cfg, sms['Name'], False)
135 else:
136 msg['Subject'] = SmsTextFormat(cfg, sms['Text'], False)[:50] + '...'
138 if sms['DateTime'] is not None:
139 msg['Date'] = DateToString(sms['DateTime'])
141 if sms.has_key('SMSInfo'):
142 text = ''
143 cid = 0
144 for i in sms['SMSInfo']['Entries']:
145 if i['ID'] in Wammu.Data.SMSIDs['PredefinedAnimation']:
146 if i['Number'] > len(Wammu.Data.PredefinedAnimations):
147 sub = MIMEImage(XPMToPNG(Wammu.Data.UnknownPredefined))
148 else:
149 img = Wammu.Data.PredefinedAnimations[i['Number']][1]
150 xpm = XPMToPNG(img)
151 sub = MIMEImage(xpm)
152 sub.add_header('Content-ID', '<%s>' % CID_FORMAT % cid)
153 sub.add_header('Content-Disposition', 'inline')
154 msg.attach(sub)
155 text = text + '<img src="cid:%s">' % (CID_FORMAT % cid)
156 cid = cid + 1
158 # FIXME: need sounds
159 if 0 and i['ID'] in Wammu.Data.SMSIDs['PredefinedSound']:
160 if i['Number'] >= len(Wammu.Data.PredefinedSounds):
161 sub = ''
162 else:
163 sub = ''
164 sub.add_header('Content-Disposition', 'attachment')
165 msg.attach(sub)
167 if i['ID'] in Wammu.Data.SMSIDs['Sound']:
168 sub = MIMEAudio(RingtoneToMIDI(i['Ringtone']), 'midi')
169 sub.add_header('Content-Disposition', 'attachment')
170 msg.attach(sub)
172 if i['ID'] in Wammu.Data.SMSIDs['Text']:
173 fmt = '%s'
174 for format_data in Wammu.Data.TextFormats:
175 for name, dummy, style in format_data[1:]:
176 if i.has_key(name) and i[name]:
177 fmt = style % fmt
178 text = text + (fmt % SmsTextFormat(cfg, i['Buffer']))
180 if i['ID'] in Wammu.Data.SMSIDs['Bitmap']:
181 for bitmap in i['Bitmap']:
182 sub = MIMEImage(XPMToPNG(bitmap['XPM']))
183 sub.add_header('Content-ID', '<%s>' % CID_FORMAT % cid)
184 sub.add_header('Content-Disposition', 'inline')
185 msg.attach(sub)
186 text = text + '<img src="cid:%s">' % (CID_FORMAT % cid)
187 cid = cid + 1
189 if i['ID'] in Wammu.Data.SMSIDs['Animation']:
190 for bitmap in i['Bitmap']:
191 sub = MIMEImage(XPMToPNG(bitmap['XPM']))
192 sub.add_header('Content-ID', '<%s>' % CID_FORMAT % cid)
193 sub.add_header('Content-Disposition', 'inline')
194 msg.attach(sub)
195 text = text + '<img src="cid:%s">' % (CID_FORMAT % cid)
196 cid = cid + 1
198 else:
199 text = SmsTextFormat(cfg, sms['Text'])
201 html = '<html><head></head><body>%s</body></html>'
202 sub = MIMEText(html % text.encode('utf-8'), 'html', 'utf-8')
203 msg.attach(sub)
205 if sms['DateTime'] is not None:
206 filename = '%s-%s-%s.eml' % (
207 sms['SMS'][0]['Type'],
208 sms['DateTime'].strftime("%Y%m%d%H%M%S"),
209 md5(sms['Text'].encode('utf-8')).hexdigest())
210 else:
211 filename = '%s-%s.eml' % (
212 sms['SMS'][0]['Type'],
213 md5(sms['Text'].encode('utf-8')).hexdigest())
215 # Add message ID
216 msgid = '<%s@%s>' % (filename[:-4], sms['Number'].replace(' ', '_'))
217 msgid = msgid.encode('ascii', 'xmlcharrefreplace')
218 msg.add_header('Message-ID', msgid)
220 if mailbox:
221 if sms['DateTime'] is None:
222 timestamp = time.asctime(time.localtime(None))
223 else:
224 timestamp = time.asctime(sms['DateTime'].timetuple())
225 prepend = ('From wammu@wammu.sms %s\n' % timestamp) + prepend
227 return filename, prepend + msg.as_string(), msgid