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