| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser |
| 7 |
import os |
| 8 |
from pprint import pformat |
| 9 |
import sys |
| 10 |
import dbus |
| 11 |
import dbus.mainloop.glib |
| 12 |
try: |
| 13 |
from gi.repository import GObject |
| 14 |
except ImportError: |
| 15 |
import gobject as GObject |
| 16 |
|
| 17 |
BUS_NAME='org.bluez.obex' |
| 18 |
PATH = '/org/bluez/obex' |
| 19 |
CLIENT_INTERFACE = 'org.bluez.obex.Client1' |
| 20 |
SESSION_INTERFACE = 'org.bluez.obex.Session1' |
| 21 |
MESSAGE_ACCESS_INTERFACE = 'org.bluez.obex.MessageAccess1' |
| 22 |
MESSAGE_INTERFACE = 'org.bluez.obex.Message1' |
| 23 |
TRANSFER_INTERFACE = 'org.bluez.obex.Transfer1' |
| 24 |
|
| 25 |
def unwrap(x): |
| 26 |
"""Hack to unwrap D-Bus values, so that they're easier to read when |
| 27 |
printed. Taken from d-feet """ |
| 28 |
|
| 29 |
if isinstance(x, list): |
| 30 |
return map(unwrap, x) |
| 31 |
|
| 32 |
if isinstance(x, tuple): |
| 33 |
return tuple(map(unwrap, x)) |
| 34 |
|
| 35 |
if isinstance(x, dict): |
| 36 |
return dict([(unwrap(k), unwrap(v)) for k, v in x.items()]) |
| 37 |
|
| 38 |
for t in [unicode, str, long, int, float, bool]: |
| 39 |
if isinstance(x, t): |
| 40 |
return t(x) |
| 41 |
|
| 42 |
return x |
| 43 |
|
| 44 |
def parse_options(): |
| 45 |
parser.add_option("-d", "--device", dest="device", |
| 46 |
help="Device to connect", metavar="DEVICE") |
| 47 |
parser.add_option("-c", "--chdir", dest="new_dir", |
| 48 |
help="Change current directory to DIR", metavar="DIR") |
| 49 |
parser.add_option("-l", "--lsdir", action="store_true", dest="ls_dir", |
| 50 |
help="List folders in current directory") |
| 51 |
parser.add_option("-v", "--verbose", action="store_true", dest="verbose") |
| 52 |
parser.add_option("-L", "--lsmsg", action="store", dest="ls_msg", |
| 53 |
help="List messages in supplied CWD subdir") |
| 54 |
parser.add_option("-g", "--get", action="store", dest="get_msg", |
| 55 |
help="Get message contents") |
| 56 |
parser.add_option("-p", "--push", action="store", dest="push_msg", |
| 57 |
help="Push message") |
| 58 |
parser.add_option("--get-properties", action="store", dest="get_msg_properties", |
| 59 |
help="Get message properties") |
| 60 |
parser.add_option("--mark-read", action="store", dest="mark_msg_read", |
| 61 |
help="Marks the messages as read") |
| 62 |
parser.add_option("--mark-unread", action="store", dest="mark_msg_unread", |
| 63 |
help="Marks the messages as unread") |
| 64 |
parser.add_option("--mark-deleted", action="store", dest="mark_msg_deleted", |
| 65 |
help="Deletes the message from the folder") |
| 66 |
parser.add_option("--mark-undeleted", action="store", dest="mark_msg_undeleted", |
| 67 |
help="Undeletes the message") |
| 68 |
parser.add_option("-u", "--update-inbox", action="store_true", dest="update_inbox", |
| 69 |
help="Checks for new mails") |
| 70 |
|
| 71 |
return parser.parse_args() |
| 72 |
|
| 73 |
def set_folder(session, new_dir): |
| 74 |
session.SetFolder(new_dir) |
| 75 |
|
| 76 |
class MapClient: |
| 77 |
def __init__(self, session_path, verbose=False): |
| 78 |
self.progress = 0 |
| 79 |
self.transfer_path = None |
| 80 |
self.props = dict() |
| 81 |
self.verbose = verbose |
| 82 |
self.path = session_path |
| 83 |
bus = dbus.SessionBus() |
| 84 |
obj = bus.get_object(BUS_NAME, session_path) |
| 85 |
self.session = dbus.Interface(obj, SESSION_INTERFACE) |
| 86 |
self.map = dbus.Interface(obj, MESSAGE_ACCESS_INTERFACE) |
| 87 |
bus.add_signal_receiver(self.properties_changed, |
| 88 |
dbus_interface="org.freedesktop.DBus.Properties", |
| 89 |
signal_name="PropertiesChanged", |
| 90 |
path_keyword="path") |
| 91 |
|
| 92 |
def create_transfer_reply(self, path, properties): |
| 93 |
self.transfer_path = path |
| 94 |
self.props[path] = properties |
| 95 |
if self.verbose: |
| 96 |
print("Transfer created: %s (file %s)" % (path, |
| 97 |
properties["Filename"])) |
| 98 |
|
| 99 |
def generic_reply(self): |
| 100 |
if self.verbose: |
| 101 |
print("Operation succeeded") |
| 102 |
|
| 103 |
def error(self, err): |
| 104 |
print(err) |
| 105 |
mainloop.quit() |
| 106 |
|
| 107 |
def transfer_complete(self, path): |
| 108 |
if self.verbose: |
| 109 |
print("Transfer finished") |
| 110 |
properties = self.props.get(path) |
| 111 |
if properties == None: |
| 112 |
return |
| 113 |
f = open(properties["Filename"], "r") |
| 114 |
os.remove(properties["Filename"]) |
| 115 |
print(f.readlines()) |
| 116 |
|
| 117 |
def transfer_error(self, path): |
| 118 |
print("Transfer %s error" % path) |
| 119 |
mainloop.quit() |
| 120 |
|
| 121 |
def properties_changed(self, interface, properties, invalidated, path): |
| 122 |
req = self.props.get(path) |
| 123 |
if req == None: |
| 124 |
return |
| 125 |
|
| 126 |
if properties['Status'] == 'complete': |
| 127 |
self.transfer_complete(path) |
| 128 |
return |
| 129 |
|
| 130 |
if properties['Status'] == 'error': |
| 131 |
self.transfer_error(path) |
| 132 |
return |
| 133 |
|
| 134 |
def set_folder(self, new_dir): |
| 135 |
self.map.SetFolder(new_dir) |
| 136 |
|
| 137 |
def list_folders(self): |
| 138 |
for i in self.map.ListFolders(dict()): |
| 139 |
print("%s/" % (i["Name"])) |
| 140 |
|
| 141 |
def list_messages(self, folder): |
| 142 |
ret = self.map.ListMessages(folder, dict()) |
| 143 |
print(pformat(unwrap(ret))) |
| 144 |
|
| 145 |
def get_message(self, handle): |
| 146 |
self.map.ListMessages("", dict()) |
| 147 |
path = self.path + "/message" + handle |
| 148 |
obj = bus.get_object(BUS_NAME, path) |
| 149 |
msg = dbus.Interface(obj, MESSAGE_INTERFACE) |
| 150 |
msg.Get("", True, reply_handler=self.create_transfer_reply, |
| 151 |
error_handler=self.error) |
| 152 |
|
| 153 |
def push_message(self, filename): |
| 154 |
self.map.PushMessage(filename, "telecom/msg/outbox", dict(), |
| 155 |
reply_handler=self.create_transfer_reply, |
| 156 |
error_handler=self.error) |
| 157 |
|
| 158 |
def get_message_properties(self, handle): |
| 159 |
self.map.ListMessages("", dict()) |
| 160 |
path = self.path + "/message" + handle |
| 161 |
obj = bus.get_object(BUS_NAME, path) |
| 162 |
msg = dbus.Interface(obj, "org.freedesktop.DBus.Properties") |
| 163 |
ret = msg.GetAll(MESSAGE_INTERFACE) |
| 164 |
print(pformat(unwrap(ret))) |
| 165 |
|
| 166 |
def set_message_property(self, handle, prop, flag): |
| 167 |
self.map.ListMessages("", dict()) |
| 168 |
path = self.path + "/message" + handle |
| 169 |
obj = bus.get_object(BUS_NAME, path) |
| 170 |
msg = dbus.Interface(obj, MESSAGE_INTERFACE) |
| 171 |
msg.SetProperty (prop, flag); |
| 172 |
|
| 173 |
def update_inbox(self): |
| 174 |
self.map.UpdateInbox() |
| 175 |
|
| 176 |
|
| 177 |
if __name__ == '__main__': |
| 178 |
|
| 179 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 180 |
|
| 181 |
parser = OptionParser() |
| 182 |
|
| 183 |
(options, args) = parse_options() |
| 184 |
|
| 185 |
if not options.device: |
| 186 |
parser.print_help() |
| 187 |
exit(0) |
| 188 |
|
| 189 |
bus = dbus.SessionBus() |
| 190 |
mainloop = GObject.MainLoop() |
| 191 |
|
| 192 |
client = dbus.Interface(bus.get_object(BUS_NAME, PATH), |
| 193 |
CLIENT_INTERFACE) |
| 194 |
|
| 195 |
print("Creating Session") |
| 196 |
path = client.CreateSession(options.device, { "Target": "map" }) |
| 197 |
|
| 198 |
map_client = MapClient(path, options.verbose) |
| 199 |
|
| 200 |
if options.new_dir: |
| 201 |
map_client.set_folder(options.new_dir) |
| 202 |
|
| 203 |
if options.ls_dir: |
| 204 |
map_client.list_folders() |
| 205 |
|
| 206 |
if options.ls_msg is not None: |
| 207 |
map_client.list_messages(options.ls_msg) |
| 208 |
|
| 209 |
if options.get_msg is not None: |
| 210 |
map_client.get_message(options.get_msg) |
| 211 |
|
| 212 |
if options.push_msg is not None: |
| 213 |
map_client.push_message(options.push_msg) |
| 214 |
|
| 215 |
if options.get_msg_properties is not None: |
| 216 |
map_client.get_message_properties(options.get_msg_properties) |
| 217 |
|
| 218 |
if options.mark_msg_read is not None: |
| 219 |
map_client.set_message_property(options.mark_msg_read, "Read", True) |
| 220 |
|
| 221 |
if options.mark_msg_unread is not None: |
| 222 |
map_client.set_message_property(options.mark_msg_unread, "Read", False) |
| 223 |
|
| 224 |
if options.mark_msg_deleted is not None: |
| 225 |
map_client.set_message_property(options.mark_msg_deleted, "Deleted", True) |
| 226 |
|
| 227 |
if options.mark_msg_undeleted is not None: |
| 228 |
map_client.set_message_property(options.mark_msg_undeleted, "Deleted", False) |
| 229 |
|
| 230 |
if options.update_inbox: |
| 231 |
map_client.update_inbox() |
| 232 |
|
| 233 |
mainloop.run() |