1 #!/usr/bin/env python2.5
2 # -*- coding: utf-8 -*-
3 """ Useful functions that shouldn't be in the UI code
6 And, yes, I know this is not really a controller.
8 @author: Nick Leppänen Larsson <frals@frals.se>
15 from dbus.mainloop.glib import DBusGMainLoop
17 import fmms_config as fMMSconf
18 import dbhandler as DBHandler
19 from mms.message import MMSMessage
20 from mms import mms_pdu
28 class fMMS_controller():
31 self.config = fMMSconf.fMMS_config()
32 self._mmsdir = self.config.get_mmsdir()
33 self._pushdir = self.config.get_pushdir()
34 self._outdir = self.config.get_outdir()
35 self.store = DBHandler.DatabaseHandler()
38 def decode_mms_from_push(self, binarydata):
39 decoder = mms_pdu.MMSDecoder()
40 wsplist = decoder.decodeCustom(binarydata)
42 sndr, url, trans_id = None, None, None
43 bus = dbus.SystemBus()
44 proxy = bus.get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
45 interface = dbus.Interface(proxy,dbus_interface='org.freedesktop.Notifications')
48 url = wsplist["Content-Location"]
49 print "content-location:", url
50 trans_id = wsplist["Transaction-Id"]
51 trans_id = str(trans_id)
52 print "transid:", trans_id
54 print "no content-location/transid in push; aborting...", type(e), e
55 interface.SystemNoteInfoprint ("fMMS: Failed to parse SMS PUSH.")
58 sndr = wsplist["From"]
61 print "No sender value defined", type(e), e
62 sndr = "Unknown sender"
64 self.save_binary_push(binarydata, trans_id)
65 return (wsplist, sndr, url, trans_id)
68 def save_binary_push(self, binarydata, transaction):
69 data = array.array('B')
72 # TODO: move to config?
73 if not os.path.isdir(self._pushdir):
74 os.makedirs(self._pushdir)
76 fp = open(self._pushdir + transaction, 'wb')
78 print "saved binary push", fp
81 print "failed to save binary push:", type(e), e
84 def save_push_message(self, data):
85 """ Gets the decoded data as a list (preferably from decode_mms_from_push)
87 pushid = self.store.insert_push_message(data)
91 def get_push_list(self, types=None):
92 return self.store.get_push_list()
95 def is_fetched_push_by_transid(self, transactionid):
96 return self.store.is_mms_downloaded(transactionid)
99 def read_push_as_list(self, transactionid):
100 return self.store.get_push_message(transactionid)
103 def save_binary_mms(self, data, transaction):
104 dirname = self._mmsdir + transaction
105 if not os.path.isdir(dirname):
108 fp = open(dirname + "/message", 'wb')
110 print "saved binary mms", fp
114 def save_binary_outgoing_mms(self, data, transaction):
115 transaction = str(transaction)
116 dirname = self._outdir + transaction
117 if not os.path.isdir(dirname):
120 fp = open(dirname + "/message", 'wb')
122 print "saved binary mms", fp
126 def decode_binary_mms(self, path):
127 """ decodes and saves the binary mms"""
128 # Decode the specified file
129 # This also creates all the parts as files in path
130 print "decode_binary_mms running"
132 message = MMSMessage.fromFile(path + "/message")
136 print "returning message!"
140 def store_mms_message(self, pushid, message):
141 mmsid = self.store.insert_mms_message(pushid, message)
144 def store_outgoing_mms(self, message):
145 mmsid = self.store.insert_mms_message(0, message, DBHandler.MSG_DIRECTION_OUT)
148 def store_outgoing_push(self, wsplist):
149 pushid = self.store.insert_push_send(wsplist)
152 def link_push_mms(self, pushid, mmsid):
153 self.store.link_push_mms(pushid, mmsid)
155 def get_direction_mms(self, transid):
156 return self.store.get_direction_mms(transid)
158 def get_mms_from_push(self, transactionid):
159 plist = self.store.get_push_message(transactionid)
160 trans_id = plist['Transaction-Id']
161 pushid = plist['PUSHID']
162 url = plist['Content-Location']
164 from wappushhandler import PushHandler
166 path = p._get_mms_message(url, trans_id)
167 print "decoding mms..."
168 message = self.cont.decode_binary_mms(path)
169 print "storing mms..."
170 mmsid = self.cont.store_mms_message(pushid, message)
173 def get_mms_attachments(self, transactionid, allFiles=False):
174 return self.store.get_mms_attachments(transactionid, allFiles)
176 def get_mms_headers(self, transactionid):
177 return self.store.get_mms_headers(transactionid)
179 def delete_mms_message(self, fname):
180 fullpath = self._mmsdir + fname
182 if os.path.isdir(fullpath):
183 print "starting deletion of", fullpath
184 filelist = self.get_mms_attachments(fname, allFiles=True)
187 filelist.append("message")
190 fullfn = fullpath + "/" + fn
193 print "failed to remove", fullfn
195 print "trying to remove", fullpath
198 print "failed to remove dir:", type(e), e
200 self.store.delete_mms_message(fname)
202 def delete_push_message(self, fname):
203 fullpath = self._pushdir + fname
205 if os.path.isfile(fullpath):
206 print "removing", fullpath
211 self.store.delete_push_message(fname)
213 def wipe_message(self, transactionid):
214 self.delete_mms_message(transactionid)
215 self.delete_push_message(transactionid)
217 """ DEPRECATED AS OF 0.2.10
218 gets a mms from a previously received push """
219 """ this function requires the fname to be the fullpath """
220 # TODO: dont require fullpath
221 """def get_mms_from_push(self, fname):
223 plist = self.read_push_as_list(fname)
228 url = plist['Content-Location']
230 trans_id = plist['Transaction-Id']
233 from wappushhandler import PushHandler
235 path = push._get_mms_message(url, trans_id)
240 """ Old function relying on files... Deprecated as of 0.2.10
241 def is_fetched_push(self, filename):
242 this function takes the FILENAME, not the full path
243 path = self._mmsdir + filename
244 if os.path.isdir(path):
245 if os.path.isfile(self._mmsdir + filename + "/message"):
251 """def read_push_as_list(self, fname):
252 # reads a saved push message into a dict
253 fp = open(fname, 'r')
256 line = line.replace("\n", "")
257 lsplit = line.partition(" ")
258 pdict[lsplit[0]] = lsplit[2]
262 if __name__ == '__main__':
263 c = fMMS_controller()