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 © 2003 - 2008 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)
95 def SMSToMail(cfg
, sms
, lookuplist
= None, mailbox
= False):
97 Converts SMS to formated mail. It will contain all images and sounds from
98 message and predefined animations. Also text formatting is preserved in
101 msg
= MIMEMultipart('related', None, None, type='text/html')
104 if lookuplist
!= None:
105 i
= SearchNumber(lookuplist
, sms
['Number'])
107 msg
.add_header(HEADER_FORMAT
% 'ContactID', str(i
))
108 name
= '%s ' % lookuplist
[i
]['Name']
110 for header
in PARTS_TO_HEADER
:
111 msg
.add_header(HEADER_FORMAT
% header
, unicode(sms
['SMS'][0][header
]))
112 msg
.add_header(HEADER_FORMAT
% 'SMSC', sms
['SMS'][0]['SMSC']['Number'])
113 if sms
['SMS'][0]['SMSCDateTime'] is not None:
114 msg
.add_header(HEADER_FORMAT
% 'SMSCDate',
115 DateToString(sms
['SMS'][0]['SMSCDateTime']))
117 remote
= '%s<%s@wammu.sms>' % (name
, sms
['Number'])
118 local
= cfg
.Read('/MesageExport/From')
119 if sms
['SMS'][0]['Type'] == 'Submit':
125 prepend
= ('Received: from %s via GSM\n' %
126 unicode(sms
['SMS'][0]['SMSC']['Number'])) + prepend
128 if len(sms
['Name']) > 0 :
129 msg
['Subject'] = SmsTextFormat(cfg
, sms
['Name'], False)
131 msg
['Subject'] = SmsTextFormat(cfg
, sms
['Text'], False)[:50] + '...'
133 if sms
['DateTime'] is not None:
134 msg
['Date'] = DateToString(sms
['DateTime'])
136 if sms
.has_key('SMSInfo'):
139 for i
in sms
['SMSInfo']['Entries']:
140 if i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedAnimation']:
141 if i
['Number'] > len(Wammu
.Data
.PredefinedAnimations
):
142 sub
= MIMEImage(XPMToPNG(Wammu
.Data
.UnknownPredefined
))
144 img
= Wammu
.Data
.PredefinedAnimations
[i
['Number']][1]
147 sub
.add_header('Content-ID', '<%s>' % CID_FORMAT
% cid
)
148 sub
.add_header('Content-Disposition', 'inline')
150 text
= text
+ '<img src="cid:%s">' % (CID_FORMAT
% cid
)
154 if 0 and i
['ID'] in Wammu
.Data
.SMSIDs
['PredefinedSound']:
155 if i
['Number'] >= len(Wammu
.Data
.PredefinedSounds
):
159 sub
.add_header('Content-Disposition', 'attachment')
162 if i
['ID'] in Wammu
.Data
.SMSIDs
['Sound']:
163 sub
= MIMEAudio(RingtoneToMIDI(i
['Ringtone']), 'midi')
164 sub
.add_header('Content-Disposition', 'attachment')
167 if i
['ID'] in Wammu
.Data
.SMSIDs
['Text']:
169 for format_data
in Wammu
.Data
.TextFormats
:
170 for name
, dummy
, style
in format_data
[1:]:
171 if i
.has_key(name
) and i
[name
]:
173 text
= text
+ (fmt
% SmsTextFormat(cfg
, i
['Buffer']))
175 if i
['ID'] in Wammu
.Data
.SMSIDs
['Bitmap']:
176 for bitmap
in i
['Bitmap']:
177 sub
= MIMEImage(XPMToPNG(bitmap
['XPM']))
178 sub
.add_header('Content-ID', '<%s>' % CID_FORMAT
% cid
)
179 sub
.add_header('Content-Disposition', 'inline')
181 text
= text
+ '<img src="cid:%s">' % (CID_FORMAT
% cid
)
184 if i
['ID'] in Wammu
.Data
.SMSIDs
['Animation']:
185 for bitmap
in i
['Bitmap']:
186 sub
= MIMEImage(XPMToPNG(bitmap
['XPM']))
187 sub
.add_header('Content-ID', '<%s>' % CID_FORMAT
% cid
)
188 sub
.add_header('Content-Disposition', 'inline')
190 text
= text
+ '<img src="cid:%s">' % (CID_FORMAT
% cid
)
194 text
= SmsTextFormat(cfg
, sms
['Text'])
196 html
= '<html><head></head><body>%s</body></html>'
197 sub
= MIMEText(html
% text
.encode('utf-8'), 'html', 'utf-8')
200 if sms
['DateTime'] is not None:
201 filename
= '%s-%s-%s.eml' % (
202 sms
['SMS'][0]['Type'],
203 sms
['DateTime'].strftime("%Y%m%d%H%M%S"),
204 md5
.new(sms
['Text'].encode('utf-8')).hexdigest())
206 filename
= '%s-%s.eml' % (
207 sms
['SMS'][0]['Type'],
208 md5
.new(sms
['Text'].encode('utf-8')).hexdigest())
211 msg
.add_header('Message-ID', '<%s@%s>' % (filename
[:-4], sms
['Number']))
214 if sms
['DateTime'] is None:
215 timestamp
= time
.asctime(time
.localtime(None))
217 timestamp
= time
.asctime(sms
['DateTime'].timetuple())
218 prepend
= ('From wammu@wammu.sms %s\n' % timestamp
) + prepend
220 return filename
, prepend
+ msg
.as_string()