1 # -*- coding: UTF-8 -*-
2 # vim: expandtab sw=4 ts=4 sts=4:
5 Module for writing SMS to Email.
7 __author__
= 'Michal Čihař'
8 __email__
= 'michal@cihar.com'
10 Copyright (c) 2003 - 2007 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
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
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
39 if Wammu
.gammu_error
== None:
42 HEADER_FORMAT
= 'X-Wammu-%s'
43 CID_FORMAT
= '%d*sms@wammu.sms'
58 Convert XPM data to PNG image.
60 @todo: I'd like to avoid creating temporary file, but even PIL doesn't
61 seem to provide such functionality.
63 handle
, name
= tempfile
.mkstemp()
65 bitmap
= wx
.BitmapFromXPMData(image
)
66 bitmap
.SaveFile(name
, wx
.BITMAP_TYPE_PNG
)
73 def RingtoneToMIDI(data
):
75 Converts ringtone to MIDI data.
77 @todo: I'd like to avoid creating temporary file, but Gammmu doesn't
78 provide such functionality.
80 handle
, name
= tempfile
.mkstemp()
81 f
= os
.fdopen(handle
, 'w+')
82 gammu
.SaveRingtone(f
, data
, 'mid')
89 def DateToString(date
):
91 Convert date to RFC 2822 format.
93 return email
.Utils
.formatdate(time
.mktime(date
.timetuple()), True)
96 def SMSToMail(cfg
, sms
, lookuplist
= None, mailbox
= False):
98 Converts SMS to formated mail. It will contain all images and sounds from
99 message and predefined animations. Also text formatting is preserved in
102 msg
= MIMEMultipart('related', None, None, type='text/html')
105 if lookuplist
!= None:
106 i
= SearchNumber(lookuplist
, sms
['Number'])
108 msg
.add_header(HEADER_FORMAT
% 'ContactID', str(i
))
109 name
= '%s ' % lookuplist
[i
]['Name']
111 for header
in PARTS_TO_HEADER
:
112 msg
.add_header(HEADER_FORMAT
% header
, unicode(sms
['SMS'][0][header
]))
113 msg
.add_header(HEADER_FORMAT
% 'SMSC', sms
['SMS'][0]['SMSC']['Number'])
114 if sms
['SMS'][0]['SMSCDateTime'] is not None:
115 msg
.add_header(HEADER_FORMAT
% 'SMSCDate',
116 DateToString(sms
['SMS'][0]['SMSCDateTime']))
118 remote
= '%s<%s@wammu.sms>' % (name
, sms
['Number'])
119 local
= cfg
.Read('/MesageExport/From')
120 if sms
['SMS'][0]['Type'] == 'Submit':
126 prepend
= ('Received: from %s via GSM\n' %
127 unicode(sms
['SMS'][0]['SMSC']['Number'])) + prepend
129 if len(sms
['Name']) > 0 :
130 msg
['Subject'] = SmsTextFormat(cfg
, sms
['Name'], False)
132 msg
['Subject'] = SmsTextFormat(cfg
, sms
['Text'], False)[:50] + '...'
134 if sms
['DateTime'] is not None:
135 msg
['Date'] = DateToString(sms
['DateTime'])
137 if sms
.has_key('SMSInfo'):
140 for i
in sms
['SMSInfo']['Entries']:
141 if i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedAnimation']:
142 if i
['Number'] > len(Wammu
.Data
.PredefinedAnimations
):
143 sub
= MIMEImage(XPMToPNG(Wammu
.Data
.UnknownPredefined
))
145 img
= Wammu
.Data
.PredefinedAnimations
[i
['Number']][1]
148 sub
.add_header('Content-ID', '<%s>' % CID_FORMAT
% cid
)
149 sub
.add_header('Content-Disposition', 'inline')
151 text
= text
+ '<img src="cid:%s">' % (CID_FORMAT
% cid
)
155 if 0 and i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedSound']:
156 if i
['Number'] >= len(Wammu
.Data
.PredefinedSounds
):
160 sub
.add_header('Content-Disposition', 'attachment')
163 if i
['ID'] in Wammu
.Data
.SMSIDs
['Sound']:
164 sub
= MIMEAudio(RingtoneToMIDI(i
['Ringtone']), 'midi')
165 sub
.add_header('Content-Disposition', 'attachment')
168 if i
['ID'] in Wammu
.Data
.SMSIDs
['Text']:
170 for format_data
in Wammu
.Data
.TextFormats
:
171 for name
, dummy
, style
in format_data
[1:]:
172 if i
.has_key(name
) and i
[name
]:
174 text
= text
+ (fmt
% SmsTextFormat(cfg
, i
['Buffer']))
176 if i
['ID'] in Wammu
.Data
.SMSIDs
['Bitmap']:
177 for bitmap
in i
['Bitmap']:
178 sub
= MIMEImage(XPMToPNG(bitmap
['XPM']))
179 sub
.add_header('Content-ID', '<%s>' % CID_FORMAT
% cid
)
180 sub
.add_header('Content-Disposition', 'inline')
182 text
= text
+ '<img src="cid:%s">' % (CID_FORMAT
% cid
)
185 if i
['ID'] in Wammu
.Data
.SMSIDs
['Animation']:
186 for bitmap
in i
['Bitmap']:
187 sub
= MIMEImage(XPMToPNG(bitmap
['XPM']))
188 sub
.add_header('Content-ID', '<%s>' % CID_FORMAT
% cid
)
189 sub
.add_header('Content-Disposition', 'inline')
191 text
= text
+ '<img src="cid:%s">' % (CID_FORMAT
% cid
)
195 text
= SmsTextFormat(cfg
, sms
['Text'])
197 html
= '<html><head></head><body>%s</body></html>'
198 sub
= MIMEText(html
% text
.encode('utf-8'), 'html', 'utf-8')
201 if sms
['DateTime'] is not None:
202 filename
= '%s-%s-%s.eml' % (
203 sms
['SMS'][0]['Type'],
204 sms
['DateTime'].strftime("%Y%m%d%H%M%S"),
205 md5
.new(sms
['Text'].encode('utf-8')).hexdigest())
207 filename
= '%s-%s.eml' % (
208 sms
['SMS'][0]['Type'],
209 md5
.new(sms
['Text'].encode('utf-8')).hexdigest())
212 msg
.add_header('Message-ID', '<%s@%s>' % (filename
[:-4], sms
['Number']))
215 if sms
['DateTime'] is None:
216 timestamp
= email
.Utils
.formatdate(None, True)
218 timestamp
= DateToString(sms
['DateTime'])
219 prepend
= ('From wammu@wammu.sms %s\n' % timestamp
) + prepend
221 return filename
, prepend
+ msg
.as_string()