Mercurial > pidgin
annotate src/gaim-remote.py @ 12964:c0f68043854a
[gaim-migrate @ 15317]
A GG status fix from Bartosz Oler. This should fix SF Bug #1410147.
committer: Tailor Script <tailor@pidgin.im>
author | Richard Laager <rlaager@wiktel.com> |
---|---|
date | Fri, 20 Jan 2006 01:55:50 +0000 |
parents | 2e4e02b72e43 |
children | ac5bc9a7b603 |
rev | line source |
---|---|
11187 | 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 | |
11241 | 49 def findaccount(accountname, protocolname): |
50 try: | |
51 # prefer connected accounts | |
52 account = cgaim.GaimAccountsFindConnected(accountname, protocolname) | |
53 return account | |
54 except: | |
55 # try to get any account and connect it | |
56 account = cgaim.GaimAccountsFindAny(accountname, protocolname) | |
57 print gaim.GaimAccountGetUsername(account) | |
58 gaim.GaimAccountSetStatusVargs(account, "online", 1) | |
59 gaim.GaimAccountConnect(account) | |
60 return account | |
61 | |
62 | |
11187 | 63 def execute(uri): |
64 match = re.match(urlregexp, uri) | |
65 protocol = match.group(2) | |
12684
2e4e02b72e43
[gaim-migrate @ 15027]
Richard Laager <rlaager@wiktel.com>
parents:
11331
diff
changeset
|
66 if protocol == "aim" or protocol == "icq": |
2e4e02b72e43
[gaim-migrate @ 15027]
Richard Laager <rlaager@wiktel.com>
parents:
11331
diff
changeset
|
67 protocol = "oscar" |
11187 | 68 if protocol is not None: |
69 protocol = "prpl-" + protocol | |
70 command = match.group(5) | |
71 paramstring = match.group(7) | |
72 params = {} | |
73 if paramstring is not None: | |
74 for param in paramstring.split("&"): | |
75 key, value = extendlist(param.split("=",1), 2, "") | |
76 params[key] = urllib.unquote(value) | |
77 | |
78 accountname = params.get("account", "") | |
79 | |
80 if command == "goim": | |
11241 | 81 print params |
82 account = findaccount(accountname, protocol) | |
11187 | 83 conversation = cgaim.GaimConversationNew(1, account, params["screenname"]) |
84 if "message" in params: | |
85 im = cgaim.GaimConversationGetImData(conversation) | |
86 gaim.GaimConvImSend(im, params["message"]) | |
87 return None | |
88 | |
89 elif command == "gochat": | |
11241 | 90 account = findaccount(accountname, protocol) |
11187 | 91 connection = cgaim.GaimAccountGetConnection(account) |
92 return gaim.ServJoinChat(connection, params) | |
93 | |
94 elif command == "addbuddy": | |
11241 | 95 account = findaccount(accountname, protocol) |
11187 | 96 return cgaim.GaimBlistRequestAddBuddy(account, params["screenname"], |
97 params.get("group", ""), "") | |
98 | |
99 elif command == "setstatus": | |
100 if "account" in params: | |
11241 | 101 accounts = [cgaim.GaimAccountsFindAny(accountname, protocol)] |
11187 | 102 else: |
103 accounts = gaim.GaimAccountsGetAllActive() | |
104 | |
105 for account in accounts: | |
11241 | 106 status = cgaim.GaimAccountGetStatus(account, params["status"]) |
11187 | 107 for key, value in params.items(): |
11241 | 108 if key not in ["status", "account"]: |
11187 | 109 gaim.GaimStatusSetAttrString(status, key, value) |
110 gaim.GaimAccountSetStatusVargs(account, params["status"], 1) | |
111 return None | |
112 | |
11241 | 113 elif command == "getinfo": |
114 account = findaccount(accountname, protocol) | |
115 connection = cgaim.GaimAccountGetConnection(account) | |
11331 | 116 return gaim.ServGetInfo(connection, params["screenname"]) |
11241 | 117 |
11187 | 118 elif command == "quit": |
119 return gaim.GaimCoreQuit() | |
120 | |
121 elif command == "uri": | |
122 return None | |
123 | |
124 else: | |
125 match = re.match(r"(\w+)\s*\(([^)]*)\)", command) | |
126 if match is not None: | |
127 name = match.group(1) | |
128 argstr = match.group(2) | |
129 if argstr == "": | |
130 args = [] | |
131 else: | |
132 args = argstr.split(",") | |
133 fargs = [] | |
134 for arg in args: | |
135 fargs.append(convert(arg.strip())) | |
136 return gaim.__getattr__(name)(*fargs) | |
137 else: | |
138 # introspect the object to get parameter names and types | |
139 # this is slow because the entire introspection info must be downloaded | |
140 data = dbus.Interface(obj, "org.freedesktop.DBus.Introspectable").\ | |
141 Introspect() | |
142 introspect = xml.dom.minidom.parseString(data).documentElement | |
143 for method in introspect.all("method"): | |
144 if command == method.getAttribute("name"): | |
145 methodparams = [] | |
146 for arg in method.all("arg"): | |
147 if arg.getAttribute("direction") == "in": | |
148 value = params[arg.getAttribute("name")] | |
149 type = arg.getAttribute("type") | |
150 if type == "s": | |
151 methodparams.append(value) | |
152 elif type == "i": | |
153 methodparams.append(int(value)) | |
154 else: | |
155 raise "Don't know how to handle type \"%s\"" % type | |
11331 | 156 return gaim.__getattr__(command)(*methodparams) |
11187 | 157 raise "Unknown command: %s" % command |
158 | |
11331 | 159 |
160 if len(sys.argv) == 1: | |
161 print """This program uses DBus to communicate with gaim. | |
162 | |
163 Usage: | |
11187 | 164 |
11331 | 165 %s "command1" "command2" ... |
166 | |
167 Each command is of one of the three types: | |
168 | |
169 [protocol:]commandname?param1=value1¶m2=value2&... | |
170 FunctionName?param1=value1¶m2=value2&... | |
171 FunctionName(value1,value2,...) | |
11187 | 172 |
11331 | 173 The second and third form are provided for completeness but their use |
174 is not recommended; use gaim-send or gaim-send-async instead. The | |
175 second form uses introspection to find out the parameter names and | |
176 their types, therefore it is rather slow. | |
177 | |
178 Examples of commands: | |
11187 | 179 |
11331 | 180 jabber:goim?screenname=testone@localhost&message=hi |
181 jabber:gochat?room=TestRoom&server=conference.localhost | |
182 jabber:getinfo?screenname=testone@localhost | |
183 jabber:addbuddy?screenname=my friend | |
11187 | 184 |
11331 | 185 setstatus?status=away&message=don't disturb |
186 quit | |
11187 | 187 |
11331 | 188 GaimAccountsFindConnected?name=&protocol=prpl-jabber |
189 GaimAccountFindConnected(,prpl-jabber) | |
190 """ % sys.argv[0] | |
11187 | 191 |
192 for arg in sys.argv[1:]: | |
193 print execute(arg) | |
194 | |
195 |