comparison libpurple/purple-url-handler @ 23855:e23b447aa5ca

propagate from branch 'im.pidgin.pidgin' (head e3ceb09b71185a9074ce73733f2a65b437c9a545) to branch 'im.pidgin.soc.2008.vv' (head a94b36f0065825b75de573746f680ae616b34f41)
author Mike Ruprecht <maiku@soc.pidgin.im>
date Wed, 16 Jul 2008 21:55:08 +0000
parents ab1f79d3ceeb
children c260fe3ac5c8
comparison
equal deleted inserted replaced
23854:517062503b48 23855:e23b447aa5ca
4 import re 4 import re
5 import sys 5 import sys
6 import time 6 import time
7 import urllib 7 import urllib
8 8
9 obj = dbus.SessionBus().get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject") 9 bus = dbus.SessionBus()
10 obj = None
11 try:
12 obj = bus.get_object("im.pidgin.purple.PurpleService",
13 "/im/pidgin/purple/PurpleObject")
14 except dbus.DBusException, e:
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."
17 sys.exit(1)
10 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface") 18 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
11 19
12 class CheckedObject: 20 class CheckedObject:
13 def __init__(self, obj): 21 def __init__(self, obj):
14 self.obj = obj 22 self.obj = obj
48 try: 56 try:
49 return int(value) 57 return int(value)
50 except: 58 except:
51 return value 59 return value
52 60
53 def findaccount(protocolname, accountname=""): 61 def account_not_found():
62 print "No matching account found."
63 sys.exit(1)
64
65 def bring_account_online(account):
66 if not cpurple.PurpleAccountIsConnected(account):
67 # The last argument is meant to be a GList * but the D-Bus binding
68 # generator thing just wants a UInt32, which is pretty failing.
69 # Happily, passing a 0 to mean an empty list turns out to work anyway.
70 purple.PurpleAccountSetStatusList(account, "online", 1, 0)
71 purple.PurpleAccountConnect(account)
72
73 def findaccount(protocolname, accountname="", matcher=None):
74 if matcher:
75 for account in cpurple.PurpleAccountsGetAll():
76 if accountname != "" and accountname != cpurple.PurpleAccountGetUsername(a):
77 continue
78 if matcher(account):
79 bring_account_online(account)
80 return account
81 account_not_found()
82
54 # prefer connected accounts 83 # prefer connected accounts
55 account = cpurple.PurpleAccountsFindConnected(accountname, protocolname) 84 account = cpurple.PurpleAccountsFindConnected(accountname, protocolname)
56 if (account != 0): 85 if (account != 0):
57 return account 86 return account
58 87
59 # try to get any account and connect it 88 # try to get any account and connect it
60 account = cpurple.PurpleAccountsFindAny(accountname, protocolname) 89 account = cpurple.PurpleAccountsFindAny(accountname, protocolname)
61 if (account == 0): 90 if (account == 0):
62 print "No matching account found." 91 account_not_found()
63 sys.exit(1) 92
64 93 bring_account_online(account)
65 purple.PurpleAccountSetStatusVargs(account, "online", 1)
66 purple.PurpleAccountConnect(account)
67 return account 94 return account
68 95
69 def goim(account, screenname, message=None): 96 def goim(account, screenname, message=None):
70 # XXX: 1 == PURPLE_CONV_TYPE_IM 97 # XXX: 1 == PURPLE_CONV_TYPE_IM
71 conversation = cpurple.PurpleConversationNew(1, account, screenname) 98 conversation = cpurple.PurpleConversationNew(1, account, screenname)
176 if paramstring: 203 if paramstring:
177 for param in paramstring.split("&"): 204 for param in paramstring.split("&"):
178 key, value = extendlist(param.split("=", 1), 2, "") 205 key, value = extendlist(param.split("=", 1), 2, "")
179 params[key] = urllib.unquote_plus(value) 206 params[key] = urllib.unquote_plus(value)
180 207
181 account = findaccount(protocol) 208 def correct_server(account):
209 username = cpurple.PurpleAccountGetUsername(account)
210 return (server == (username.split("@"))[1])
211
212 account = findaccount(protocol, matcher=correct_server)
182 213
183 if (target != ""): 214 if (target != ""):
184 if (isnick): 215 if (isnick):
185 goim(account, urllib.unquote_plus(target.split(",")[0]), params.get("msg")) 216 goim(account, urllib.unquote_plus(target.split(",")[0]), params.get("msg"))
186 else: 217 else:
187 channel = urllib.unquote_plus(target.split(",")[0]) 218 channel = urllib.unquote_plus(target.split(",")[0])
188 if channel[0] != "#": 219 if channel[0] != "#":
189 channel = "#" + channel 220 channel = "#" + channel
190 gochat(account, {"server": server, "channel": channel, "password": params.get("key", "")}, params.get("msg")) 221 gochat(account, {"server": server, "channel": channel, "password": params.get("key", "")}, params.get("msg"))
191 222
211 goim(account, screenname) 242 goim(account, screenname)
212 elif command.lower() == "add": 243 elif command.lower() == "add":
213 addbuddy(account, screenname) 244 addbuddy(account, screenname)
214 245
215 def myim(uri): 246 def myim(uri):
216 protocol = "prpl-myspace" 247 protocol = "prpl-myspace"
217 print "TODO: send uri: ", uri 248 print "TODO: send uri: ", uri
218 assert False, "Not implemented" 249 assert False, "Not implemented"
219 250
220 def sip(uri): 251 def sip(uri):
221 protocol = "prpl-simple" 252 protocol = "prpl-simple"
222 match = re.match(r"^sip:(.*)", uri) 253 match = re.match(r"^sip:(.*)", uri)
223 if not match: 254 if not match:
326 xmpp(uri) 357 xmpp(uri)
327 elif type == "ymsgr": 358 elif type == "ymsgr":
328 ymsgr(uri) 359 ymsgr(uri)
329 else: 360 else:
330 print "Unknown protocol: %s" % type 361 print "Unknown protocol: %s" % type
331 except dbus.dbus_bindings.DBusException: 362 except dbus.DBusException, e:
332 print "ERROR: Is there a libpurple-powered client (e.g. Pidgin or Finch) running?" 363 print "Error: %s" % (e.message)
333 364 sys.exit(1)
334 365
335 if __name__ == "__main__": 366 if __name__ == "__main__":
336 main() 367 main()