libpurple/purple-url-handler

branch
release-2.x.y
changeset 37443
78408ad717bd
parent 31582
c561e11657b0
child 37444
8c851c1bc85b
equal deleted inserted replaced
37437:a5a43a84a61f 37443:78408ad717bd
1 #!/usr/bin/env python 1 #!/usr/bin/env python3
2 2
3 import dbus 3 import dbus
4 import re 4 import re
5 import sys 5 import sys
6 import time 6 import time
7 import urllib 7 import urllib.parse
8 8
9 bus = dbus.SessionBus() 9 bus = dbus.SessionBus()
10 obj = None 10 obj = None
11 try: 11 try:
12 obj = bus.get_object("im.pidgin.purple.PurpleService", 12 obj = bus.get_object("im.pidgin.purple.PurpleService",
13 "/im/pidgin/purple/PurpleObject") 13 "/im/pidgin/purple/PurpleObject")
14 except dbus.DBusException, e: 14 except dbus.DBusException as e:
15 if e._dbus_error_name == "org.freedesktop.DBus.Error.ServiceUnknown": 15 if e._dbus_error_name == "org.freedesktop.DBus.Error.ServiceUnknown":
16 print "Error: no libpurple-powered client is running. Try starting Pidgin or Finch." 16 print("Error: no libpurple-powered client is running. Try starting Pidgin or Finch.")
17 sys.exit(1) 17 sys.exit(1)
18 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface") 18 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
19 19
20 class CheckedObject: 20 class CheckedObject:
21 def __init__(self, obj): 21 def __init__(self, obj):
57 return int(value) 57 return int(value)
58 except: 58 except:
59 return value 59 return value
60 60
61 def account_not_found(): 61 def account_not_found():
62 print "No matching account found." 62 print("No matching account found.")
63 sys.exit(1) 63 sys.exit(1)
64 64
65 def bring_account_online(account): 65 def bring_account_online(account):
66 if not cpurple.PurpleAccountIsConnected(account): 66 if not cpurple.PurpleAccountIsConnected(account):
67 # The last argument is meant to be a GList * but the D-Bus binding 67 # The last argument is meant to be a GList * but the D-Bus binding
120 120
121 def aim(uri): 121 def aim(uri):
122 protocol = "prpl-aim" 122 protocol = "prpl-aim"
123 match = re.match(r"^aim:([^?]*)(\?(.*))", uri) 123 match = re.match(r"^aim:([^?]*)(\?(.*))", uri)
124 if not match: 124 if not match:
125 print "Invalid aim URI: %s" % uri 125 print("Invalid aim URI: %s" % uri)
126 return 126 return
127 127
128 command = urllib.unquote_plus(match.group(1)) 128 command = urllib.parse.unquote(match.group(1))
129 paramstring = match.group(3) 129 paramstring = match.group(3)
130 params = {} 130 params = {}
131 if paramstring: 131 if paramstring:
132 for param in paramstring.split("&"): 132 for param in paramstring.split("&"):
133 key, value = extendlist(param.split("=", 1), 2, "") 133 key, value = extendlist(param.split("=", 1), 2, "")
134 params[key] = urllib.unquote_plus(value) 134 params[key] = urllib.parse.unquote(value)
135 accountname = params.get("account", "") 135 accountname = params.get("account", "")
136 screenname = params.get("screenname", "") 136 screenname = params.get("screenname", "")
137 137
138 account = findaccount(protocol, accountname) 138 account = findaccount(protocol, accountname)
139 139
146 146
147 def gg(uri): 147 def gg(uri):
148 protocol = "prpl-gg" 148 protocol = "prpl-gg"
149 match = re.match(r"^gg:(.*)", uri) 149 match = re.match(r"^gg:(.*)", uri)
150 if not match: 150 if not match:
151 print "Invalid gg URI: %s" % uri 151 print("Invalid gg URI: %s" % uri)
152 return 152 return
153 153
154 screenname = urllib.unquote_plus(match.group(1)) 154 screenname = urllib.parse.unquote(match.group(1))
155 account = findaccount(protocol) 155 account = findaccount(protocol)
156 goim(account, screenname) 156 goim(account, screenname)
157 157
158 def icq(uri): 158 def icq(uri):
159 protocol = "prpl-icq" 159 protocol = "prpl-icq"
160 match = re.match(r"^icq:([^?]*)(\?(.*))", uri) 160 match = re.match(r"^icq:([^?]*)(\?(.*))", uri)
161 if not match: 161 if not match:
162 print "Invalid icq URI: %s" % uri 162 print("Invalid icq URI: %s" % uri)
163 return 163 return
164 164
165 command = urllib.unquote_plus(match.group(1)) 165 command = urllib.parse.unquote(match.group(1))
166 paramstring = match.group(3) 166 paramstring = match.group(3)
167 params = {} 167 params = {}
168 if paramstring: 168 if paramstring:
169 for param in paramstring.split("&"): 169 for param in paramstring.split("&"):
170 key, value = extendlist(param.split("=", 1), 2, "") 170 key, value = extendlist(param.split("=", 1), 2, "")
171 params[key] = urllib.unquote_plus(value) 171 params[key] = urllib.parse.unquote(value)
172 accountname = params.get("account", "") 172 accountname = params.get("account", "")
173 screenname = params.get("screenname", "") 173 screenname = params.get("screenname", "")
174 174
175 account = findaccount(protocol, accountname) 175 account = findaccount(protocol, accountname)
176 176
183 183
184 def irc(uri): 184 def irc(uri):
185 protocol = "prpl-irc" 185 protocol = "prpl-irc"
186 match = re.match(r"^irc:(//([^/]*))?/?([^?]*)(\?(.*))?", uri) 186 match = re.match(r"^irc:(//([^/]*))?/?([^?]*)(\?(.*))?", uri)
187 if not match: 187 if not match:
188 print "Invalid irc URI: %s" % uri 188 print("Invalid irc URI: %s" % uri)
189 return 189 return
190 190
191 server = urllib.unquote_plus(match.group(2) or "") 191 server = urllib.parse.unquote(match.group(2) or "")
192 target = match.group(3) or "" 192 target = match.group(3) or ""
193 query = match.group(5) or "" 193 query = match.group(5) or ""
194 194
195 modifiers = {} 195 modifiers = {}
196 if target: 196 if target:
202 paramstring = match.group(5) 202 paramstring = match.group(5)
203 params = {} 203 params = {}
204 if paramstring: 204 if paramstring:
205 for param in paramstring.split("&"): 205 for param in paramstring.split("&"):
206 key, value = extendlist(param.split("=", 1), 2, "") 206 key, value = extendlist(param.split("=", 1), 2, "")
207 params[key] = urllib.unquote_plus(value) 207 params[key] = urllib.parse.unquote(value)
208 208
209 def correct_server(account): 209 def correct_server(account):
210 username = cpurple.PurpleAccountGetUsername(account) 210 username = cpurple.PurpleAccountGetUsername(account)
211 return ((server == "") or ("@" in username) and (server == (username.split("@"))[1])) 211 return ((server == "") or ("@" in username) and (server == (username.split("@"))[1]))
212 212
213 account = findaccount(protocol, matcher=correct_server) 213 account = findaccount(protocol, matcher=correct_server)
214 214
215 if (target != ""): 215 if (target != ""):
216 if (isnick): 216 if (isnick):
217 goim(account, urllib.unquote_plus(target.split(",")[0]), params.get("msg")) 217 goim(account, urllib.parse.unquote(target.split(",")[0]), params.get("msg"))
218 else: 218 else:
219 channel = urllib.unquote_plus(target.split(",")[0]) 219 channel = urllib.parse.unquote(target.split(",")[0])
220 if channel[0] != "#": 220 if channel[0] != "#":
221 channel = "#" + channel 221 channel = "#" + channel
222 gochat(account, {"server": server, "channel": channel, "password": params.get("key", "")}, params.get("msg")) 222 gochat(account, {"server": server, "channel": channel, "password": params.get("key", "")}, params.get("msg"))
223 223
224 def msnim(uri): 224 def msnim(uri):
225 protocol = "prpl-msn" 225 protocol = "prpl-msn"
226 match = re.match(r"^msnim:([^?]*)(\?(.*))", uri) 226 match = re.match(r"^msnim:([^?]*)(\?(.*))", uri)
227 if not match: 227 if not match:
228 print "Invalid msnim URI: %s" % uri 228 print("Invalid msnim URI: %s" % uri)
229 return 229 return
230 230
231 command = urllib.unquote_plus(match.group(1)) 231 command = urllib.parse.unquote(match.group(1))
232 paramstring = match.group(3) 232 paramstring = match.group(3)
233 params = {} 233 params = {}
234 if paramstring: 234 if paramstring:
235 for param in paramstring.split("&"): 235 for param in paramstring.split("&"):
236 key, value = extendlist(param.split("=", 1), 2, "") 236 key, value = extendlist(param.split("=", 1), 2, "")
237 params[key] = urllib.unquote_plus(value) 237 params[key] = urllib.parse.unquote(value)
238 screenname = params.get("contact", "") 238 screenname = params.get("contact", "")
239 239
240 account = findaccount(protocol) 240 account = findaccount(protocol)
241 241
242 if command.lower() == "chat": 242 if command.lower() == "chat":
251 251
252 def sip(uri): 252 def sip(uri):
253 protocol = "prpl-simple" 253 protocol = "prpl-simple"
254 match = re.match(r"^sip:(.*)", uri) 254 match = re.match(r"^sip:(.*)", uri)
255 if not match: 255 if not match:
256 print "Invalid sip URI: %s" % uri 256 print("Invalid sip URI: %s" % uri)
257 return 257 return
258 258
259 screenname = urllib.unquote_plus(match.group(1)) 259 screenname = urllib.parse.unquote(match.group(1))
260 account = findaccount(protocol) 260 account = findaccount(protocol)
261 goim(account, screenname) 261 goim(account, screenname)
262 262
263 def xmpp(uri): 263 def xmpp(uri):
264 protocol = "prpl-jabber" 264 protocol = "prpl-jabber"
265 match = re.match(r"^xmpp:(//([^/?#]*)/?)?([^?#]*)(\?([^;#]*)(;([^#]*))?)?(#(.*))?", uri) 265 match = re.match(r"^xmpp:(//([^/?#]*)/?)?([^?#]*)(\?([^;#]*)(;([^#]*))?)?(#(.*))?", uri)
266 if not match: 266 if not match:
267 print "Invalid xmpp URI: %s" % uri 267 print("Invalid xmpp URI: %s" % uri)
268 return 268 return
269 269
270 tmp = match.group(2) 270 tmp = match.group(2)
271 if (tmp): 271 if (tmp):
272 accountname = urllib.unquote_plus(tmp) 272 accountname = urllib.parse.unquote(tmp)
273 else: 273 else:
274 accountname = "" 274 accountname = ""
275 275
276 screenname = urllib.unquote_plus(match.group(3)) 276 screenname = urllib.parse.unquote(match.group(3))
277 277
278 tmp = match.group(5) 278 tmp = match.group(5)
279 if (tmp): 279 if (tmp):
280 command = urllib.unquote_plus(tmp) 280 command = urllib.parse.unquote(tmp)
281 else: 281 else:
282 command = "" 282 command = ""
283 283
284 paramstring = match.group(7) 284 paramstring = match.group(7)
285 params = {} 285 params = {}
286 if paramstring: 286 if paramstring:
287 for param in paramstring.split(";"): 287 for param in paramstring.split(";"):
288 key, value = extendlist(param.split("=", 1), 2, "") 288 key, value = extendlist(param.split("=", 1), 2, "")
289 params[key] = urllib.unquote_plus(value) 289 params[key] = urllib.parse.unquote(value)
290 290
291 account = findaccount(protocol, accountname) 291 account = findaccount(protocol, accountname)
292 292
293 if command.lower() == "message": 293 if command.lower() == "message":
294 goim(account, screenname, params.get("body")) 294 goim(account, screenname, params.get("body"))
302 302
303 def gtalk(uri): 303 def gtalk(uri):
304 protocol = "prpl-jabber" 304 protocol = "prpl-jabber"
305 match = re.match(r"^gtalk:([^?]*)(\?(.*))", uri) 305 match = re.match(r"^gtalk:([^?]*)(\?(.*))", uri)
306 if not match: 306 if not match:
307 print "Invalid gtalk URI: %s" % uri 307 print("Invalid gtalk URI: %s" % uri)
308 return 308 return
309 309
310 command = urllib.unquote_plus(match.group(1)) 310 command = urllib.parse.unquote(match.group(1))
311 paramstring = match.group(3) 311 paramstring = match.group(3)
312 params = {} 312 params = {}
313 if paramstring: 313 if paramstring:
314 for param in paramstring.split("&"): 314 for param in paramstring.split("&"):
315 key, value = extendlist(param.split("=", 1), 2, "") 315 key, value = extendlist(param.split("=", 1), 2, "")
316 params[key] = urllib.unquote_plus(value) 316 params[key] = urllib.parse.unquote(value)
317 accountname = params.get("from_jid", "") 317 accountname = params.get("from_jid", "")
318 jid = params.get("jid", "") 318 jid = params.get("jid", "")
319 319
320 account = findaccount(protocol, accountname) 320 account = findaccount(protocol, accountname)
321 321
327 327
328 def ymsgr(uri): 328 def ymsgr(uri):
329 protocol = "prpl-yahoo" 329 protocol = "prpl-yahoo"
330 match = re.match(r"^ymsgr:([^?]*)(\?([^&]*)(&(.*))?)", uri) 330 match = re.match(r"^ymsgr:([^?]*)(\?([^&]*)(&(.*))?)", uri)
331 if not match: 331 if not match:
332 print "Invalid ymsgr URI: %s" % uri 332 print("Invalid ymsgr URI: %s" % uri)
333 return 333 return
334 334
335 command = urllib.unquote_plus(match.group(1)) 335 command = urllib.parse.unquote(match.group(1))
336 screenname = urllib.unquote_plus(match.group(3)) 336 screenname = urllib.parse.unquote(match.group(3))
337 paramstring = match.group(5) 337 paramstring = match.group(5)
338 params = {} 338 params = {}
339 if paramstring: 339 if paramstring:
340 for param in paramstring.split("&"): 340 for param in paramstring.split("&"):
341 key, value = extendlist(param.split("=", 1), 2, "") 341 key, value = extendlist(param.split("=", 1), 2, "")
342 params[key] = urllib.unquote_plus(value) 342 params[key] = urllib.parse.unquote(value)
343 343
344 account = findaccount(protocol) 344 account = findaccount(protocol)
345 345
346 if command.lower() == "sendim": 346 if command.lower() == "sendim":
347 goim(account, screenname, params.get("m")) 347 goim(account, screenname, params.get("m"))
351 addbuddy(account, screenname) 351 addbuddy(account, screenname)
352 352
353 353
354 def main(argv=sys.argv): 354 def main(argv=sys.argv):
355 if len(argv) != 2 or argv[1] == "--help" or argv[1] == "-h": 355 if len(argv) != 2 or argv[1] == "--help" or argv[1] == "-h":
356 print "Usage: %s URI" % argv[0] 356 print("Usage: %s URI" % argv[0])
357 print "Example: %s \"xmpp:romeo@montague.net?message\"" % argv[0] 357 print("Example: %s \"xmpp:romeo@montague.net?message\"" % argv[0])
358 358
359 if len(argv) != 2: 359 if len(argv) != 2:
360 sys.exit(1) 360 sys.exit(1)
361 else: 361 else:
362 return 0 362 return 0
384 elif type == "gtalk": 384 elif type == "gtalk":
385 gtalk(uri) 385 gtalk(uri)
386 elif type == "ymsgr": 386 elif type == "ymsgr":
387 ymsgr(uri) 387 ymsgr(uri)
388 else: 388 else:
389 print "Unknown protocol: %s" % type 389 print("Unknown protocol: %s" % type)
390 except dbus.DBusException, e: 390 except dbus.DBusException as e:
391 print "Error: %s" % (e.message) 391 print("Error: %s" % e.message)
392 sys.exit(1) 392 sys.exit(1)
393 393
394 if __name__ == "__main__": 394 if __name__ == "__main__":
395 main() 395 main()

mercurial