Mercurial > pidgin
comparison src/gaim-remote.py @ 11187:744c0708d11f
[gaim-migrate @ 13303]
gaim-remote.py implements the functionality of standard gaim-remote,
but using DBus. It can also call all gaim functions exported via
DBus.
dbus-analize-function.py can now produce dbus bindings for GHashTable
arguments.
committer: Tailor Script <tailor@pidgin.im>
author | Piotr Zielinski <zielaj> |
---|---|
date | Wed, 03 Aug 2005 23:54:37 +0000 |
parents | |
children | 66f872f30e40 |
comparison
equal
deleted
inserted
replaced
11186:bbe84acea03a | 11187:744c0708d11f |
---|---|
1 #!/usr/bin/python | |
2 | |
3 import dbus | |
4 import re | |
5 import urllib | |
6 import sys | |
7 | |
8 import xml.dom.minidom | |
9 | |
10 xml.dom.minidom.Element.all = xml.dom.minidom.Element.getElementsByTagName | |
11 | |
12 obj = dbus.SessionBus().get_object("org.gaim.GaimService", "/org/gaim/GaimObject") | |
13 gaim = dbus.Interface(obj, "org.gaim.GaimInterface") | |
14 | |
15 class CheckedObject: | |
16 def __init__(self, obj): | |
17 self.obj = obj | |
18 | |
19 def __getattr__(self, attr): | |
20 return CheckedAttribute(self, attr) | |
21 | |
22 class CheckedAttribute: | |
23 def __init__(self, cobj, attr): | |
24 self.cobj = cobj | |
25 self.attr = attr | |
26 | |
27 def __call__(self, *args): | |
28 result = self.cobj.obj.__getattr__(self.attr)(*args) | |
29 if result == 0: | |
30 raise "Error: " + self.attr + " " + str(args) + " returned " + str(result) | |
31 return result | |
32 | |
33 cgaim = CheckedObject(gaim) | |
34 | |
35 urlregexp = r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?" | |
36 | |
37 def extendlist(list, length, fill): | |
38 if len(list) < length: | |
39 return list + [fill] * (length - len(list)) | |
40 else: | |
41 return list | |
42 | |
43 def convert(value): | |
44 try: | |
45 return int(value) | |
46 except: | |
47 return value | |
48 | |
49 def execute(uri): | |
50 match = re.match(urlregexp, uri) | |
51 protocol = match.group(2) | |
52 if protocol is not None: | |
53 protocol = "prpl-" + protocol | |
54 command = match.group(5) | |
55 paramstring = match.group(7) | |
56 params = {} | |
57 if paramstring is not None: | |
58 for param in paramstring.split("&"): | |
59 key, value = extendlist(param.split("=",1), 2, "") | |
60 params[key] = urllib.unquote(value) | |
61 | |
62 accountname = params.get("account", "") | |
63 | |
64 if command == "goim": | |
65 account = cgaim.GaimAccountsFindConnected(accountname, protocol) | |
66 conversation = cgaim.GaimConversationNew(1, account, params["screenname"]) | |
67 if "message" in params: | |
68 im = cgaim.GaimConversationGetImData(conversation) | |
69 gaim.GaimConvImSend(im, params["message"]) | |
70 return None | |
71 | |
72 elif command == "gochat": | |
73 account = cgaim.GaimAccountsFindConnected(accountname, protocol) | |
74 connection = cgaim.GaimAccountGetConnection(account) | |
75 return gaim.ServJoinChat(connection, params) | |
76 | |
77 elif command == "addbuddy": | |
78 account = cgaim.GaimAccountsFindConnected(accountname, protocol) | |
79 return cgaim.GaimBlistRequestAddBuddy(account, params["screenname"], | |
80 params.get("group", ""), "") | |
81 | |
82 elif command == "setstatus": | |
83 if "account" in params: | |
84 accounts = [cgaim.GaimAccountsFindConnected(accountname, protocol)] | |
85 else: | |
86 accounts = gaim.GaimAccountsGetAllActive() | |
87 | |
88 for account in accounts: | |
89 status = gaim.GaimAccountGetStatus(account, params["status"]) | |
90 for key, value in params.items(): | |
91 if key not in ["state", "account"]: | |
92 gaim.GaimStatusSetAttrString(status, key, value) | |
93 gaim.GaimAccountSetStatusVargs(account, params["status"], 1) | |
94 return None | |
95 | |
96 elif command == "quit": | |
97 return gaim.GaimCoreQuit() | |
98 | |
99 elif command == "uri": | |
100 return None | |
101 | |
102 else: | |
103 match = re.match(r"(\w+)\s*\(([^)]*)\)", command) | |
104 if match is not None: | |
105 name = match.group(1) | |
106 argstr = match.group(2) | |
107 if argstr == "": | |
108 args = [] | |
109 else: | |
110 args = argstr.split(",") | |
111 fargs = [] | |
112 for arg in args: | |
113 fargs.append(convert(arg.strip())) | |
114 return gaim.__getattr__(name)(*fargs) | |
115 else: | |
116 # introspect the object to get parameter names and types | |
117 # this is slow because the entire introspection info must be downloaded | |
118 data = dbus.Interface(obj, "org.freedesktop.DBus.Introspectable").\ | |
119 Introspect() | |
120 introspect = xml.dom.minidom.parseString(data).documentElement | |
121 for method in introspect.all("method"): | |
122 if command == method.getAttribute("name"): | |
123 methodparams = [] | |
124 for arg in method.all("arg"): | |
125 if arg.getAttribute("direction") == "in": | |
126 value = params[arg.getAttribute("name")] | |
127 type = arg.getAttribute("type") | |
128 if type == "s": | |
129 methodparams.append(value) | |
130 elif type == "i": | |
131 methodparams.append(int(value)) | |
132 else: | |
133 raise "Don't know how to handle type \"%s\"" % type | |
134 return gaim.__getattr__(command)(*methodparams) | |
135 raise "Unknown command: %s" % command | |
136 | |
137 def example_code_do_not_call(): | |
138 execute("jabber:addbuddy?screenname=friend") | |
139 execute("setstatus?status=away&message=don't disturb") | |
140 | |
141 account = execute("GaimAccountsFindConnected?name=&protocol=") | |
142 execute("GaimConversationNew?type=1&account=%i&name=testone@localhost" % account) | |
143 | |
144 execute("jabber:addbuddy?screenname=friend") | |
145 execute("jabber:goim?screenname=testone@localhost&message=hi") | |
146 | |
147 execute("jabber:gochat?room=TestRoom&server=conference.localhost") | |
148 execute("jabber:goim?screenname=testone@localhost&message=hi") | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 for arg in sys.argv[1:]: | |
155 print execute(arg) | |
156 | |
157 |