comparison libpurple/purple-url-handler @ 16143:598b1b15b199

Trac Ticket #149 from JensenDied This patch does a rename of our DBus stuff.
author Richard Laager <rlaager@wiktel.com>
date Sun, 15 Apr 2007 18:09:14 +0000
parents c6e563dfaa7a
children d06673964ff9
comparison
equal deleted inserted replaced
16142:74b2d576f458 16143:598b1b15b199
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("net.sf.gaim.GaimService", "/net/sf/gaim/GaimObject") 9 obj = dbus.SessionBus().get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
10 gaim = dbus.Interface(obj, "net.sf.gaim.GaimInterface") 10 purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")
11 11
12 class CheckedObject: 12 class CheckedObject:
13 def __init__(self, obj): 13 def __init__(self, obj):
14 self.obj = obj 14 self.obj = obj
15 15
25 result = self.cobj.obj.__getattr__(self.attr)(*args) 25 result = self.cobj.obj.__getattr__(self.attr)(*args)
26 if result == 0: 26 if result == 0:
27 raise "Error: " + self.attr + " " + str(args) + " returned " + str(result) 27 raise "Error: " + self.attr + " " + str(args) + " returned " + str(result)
28 return result 28 return result
29 29
30 cgaim = CheckedObject(gaim) 30 cpurple = CheckedObject(purple)
31 31
32 def extendlist(list, length, fill): 32 def extendlist(list, length, fill):
33 if len(list) < length: 33 if len(list) < length:
34 return list + [fill] * (length - len(list)) 34 return list + [fill] * (length - len(list))
35 else: 35 else:
42 return value 42 return value
43 43
44 def findaccount(protocolname, accountname=""): 44 def findaccount(protocolname, accountname=""):
45 try: 45 try:
46 # prefer connected accounts 46 # prefer connected accounts
47 account = cgaim.GaimAccountsFindConnected(accountname, protocolname) 47 account = cpurple.PurpleAccountsFindConnected(accountname, protocolname)
48 return account 48 return account
49 except: 49 except:
50 # try to get any account and connect it 50 # try to get any account and connect it
51 account = cgaim.GaimAccountsFindAny(accountname, protocolname) 51 account = cpurple.PurpleAccountsFindAny(accountname, protocolname)
52 gaim.GaimAccountSetStatusVargs(account, "online", 1) 52 purple.PurpleAccountSetStatusVargs(account, "online", 1)
53 gaim.GaimAccountConnect(account) 53 purple.PurpleAccountConnect(account)
54 return account 54 return account
55 55
56 def goim(account, screenname, message=None): 56 def goim(account, screenname, message=None):
57 # XXX: 1 == GAIM_CONV_TYPE_IM 57 # XXX: 1 == PURPLE_CONV_TYPE_IM
58 conversation = cgaim.GaimConversationNew(1, account, screenname) 58 conversation = cpurple.PurpleConversationNew(1, account, screenname)
59 if message: 59 if message:
60 gaim.GaimConvSendConfirm(conversation, message) 60 purple.PurpleConvSendConfirm(conversation, message)
61 61
62 def gochat(account, params, message=None): 62 def gochat(account, params, message=None):
63 connection = cgaim.GaimAccountGetConnection(account) 63 connection = cpurple.PurpleAccountGetConnection(account)
64 gaim.ServJoinChat(connection, params) 64 purple.ServJoinChat(connection, params)
65 65
66 if message != None: 66 if message != None:
67 for i in range(20): 67 for i in range(20):
68 # XXX: 2 == GAIM_CONV_TYPE_CHAT 68 # XXX: 2 == PURPLE_CONV_TYPE_CHAT
69 conversation = gaim.GaimFindConversationWithAccount(2, params.get("channel", params.get("room")), account) 69 conversation = purple.PurpleFindConversationWithAccount(2, params.get("channel", params.get("room")), account)
70 if conversation: 70 if conversation:
71 gaim.GaimConvSendConfirm(conversation, message) 71 purple.PurpleConvSendConfirm(conversation, message)
72 break 72 break
73 else: 73 else:
74 time.sleep(0.5) 74 time.sleep(0.5)
75 75
76 def addbuddy(account, screenname, group="", alias=""): 76 def addbuddy(account, screenname, group="", alias=""):
77 cgaim.GaimBlistRequestAddBuddy(account, screenname, group, alias) 77 cpurple.PurpleBlistRequestAddBuddy(account, screenname, group, alias)
78 78
79 79
80 def aim(uri): 80 def aim(uri):
81 protocol = "prpl-oscar" 81 protocol = "prpl-aim"
82 match = re.match(r"^(aim|icq):([^?]*)(\?(.*))", uri) 82 match = re.match(r"^(aim):([^?]*)(\?(.*))", uri)
83 if not match: 83 if not match:
84 print "Invalid aim URI: %s" % uri 84 print "Invalid aim URI: %s" % uri
85 return 85 return
86 86
87 command = urllib.unquote_plus(match.group(2)) 87 command = urllib.unquote_plus(match.group(2))
113 screenname = urllib.unquote_plus(match.group(1)) 113 screenname = urllib.unquote_plus(match.group(1))
114 account = findaccount(protocol) 114 account = findaccount(protocol)
115 goim(account, screenname) 115 goim(account, screenname)
116 116
117 def icq(uri): 117 def icq(uri):
118 aim(uri) 118 protocol = "prpl-icq"
119 match = re.match(r"^(icq):([^?]*)(\?(.*))", uri)
120 if not match:
121 print "Invalid aim URI: %s" % uri
122 return
123
124 command = urllib.unquote_plus(match.group(2))
125 paramstring = match.group(4)
126 params = {}
127 if paramstring:
128 for param in paramstring.split("&"):
129 key, value = extendlist(param.split("=", 1), 2, "")
130 params[key] = urllib.unquote_plus(value)
131 accountname = params.get("account", "")
132 screenname = params.get("screenname", "")
133
134 account = findaccount(protocol, accountname)
135
136 if command.lower() == "goim":
137 goim(account, screenname, params.get("message"))
138 elif command.lower() == "gochat":
139 gochat(account, params)
140 elif command.lower() == "addbuddy":
141 addbuddy(account, screenname, params.get("group", ""))
119 142
120 def irc(uri): 143 def irc(uri):
121 protocol = "prpl-irc" 144 protocol = "prpl-irc"
122 match = re.match(r"^irc:(//([^/]*)/)?([^?]*)(\?(.*))?", uri) 145 match = re.match(r"^irc:(//([^/]*)/)?([^?]*)(\?(.*))?", uri)
123 if not match: 146 if not match: