annotate src/gaim-remote.py @ 11187:744c0708d11f

[gaim-migrate @ 13303] gaim-remote.py implements the functionality of standard gaim-remote, but using DBus. It can also call all gaim functions exported via DBus. dbus-analize-function.py can now produce dbus bindings for GHashTable arguments. committer: Tailor Script <tailor@pidgin.im>
author Piotr Zielinski <zielaj>
date Wed, 03 Aug 2005 23:54:37 +0000
parents
children 66f872f30e40
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11187
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
1 #!/usr/bin/python
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
2
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
3 import dbus
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
4 import re
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
5 import urllib
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
6 import sys
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
7
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
8 import xml.dom.minidom
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
9
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
10 xml.dom.minidom.Element.all = xml.dom.minidom.Element.getElementsByTagName
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
11
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
12 obj = dbus.SessionBus().get_object("org.gaim.GaimService", "/org/gaim/GaimObject")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
13 gaim = dbus.Interface(obj, "org.gaim.GaimInterface")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
14
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
15 class CheckedObject:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
16 def __init__(self, obj):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
17 self.obj = obj
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
18
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
19 def __getattr__(self, attr):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
20 return CheckedAttribute(self, attr)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
21
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
22 class CheckedAttribute:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
23 def __init__(self, cobj, attr):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
24 self.cobj = cobj
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
25 self.attr = attr
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
26
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
27 def __call__(self, *args):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
28 result = self.cobj.obj.__getattr__(self.attr)(*args)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
29 if result == 0:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
30 raise "Error: " + self.attr + " " + str(args) + " returned " + str(result)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
31 return result
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
32
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
33 cgaim = CheckedObject(gaim)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
34
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
35 urlregexp = r"^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?"
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
36
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
37 def extendlist(list, length, fill):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
38 if len(list) < length:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
39 return list + [fill] * (length - len(list))
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
40 else:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
41 return list
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
42
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
43 def convert(value):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
44 try:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
45 return int(value)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
46 except:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
47 return value
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
48
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
49 def execute(uri):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
50 match = re.match(urlregexp, uri)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
51 protocol = match.group(2)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
52 if protocol is not None:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
53 protocol = "prpl-" + protocol
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
54 command = match.group(5)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
55 paramstring = match.group(7)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
56 params = {}
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
57 if paramstring is not None:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
58 for param in paramstring.split("&"):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
59 key, value = extendlist(param.split("=",1), 2, "")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
60 params[key] = urllib.unquote(value)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
61
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
62 accountname = params.get("account", "")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
63
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
64 if command == "goim":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
65 account = cgaim.GaimAccountsFindConnected(accountname, protocol)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
66 conversation = cgaim.GaimConversationNew(1, account, params["screenname"])
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
67 if "message" in params:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
68 im = cgaim.GaimConversationGetImData(conversation)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
69 gaim.GaimConvImSend(im, params["message"])
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
70 return None
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
71
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
72 elif command == "gochat":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
73 account = cgaim.GaimAccountsFindConnected(accountname, protocol)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
74 connection = cgaim.GaimAccountGetConnection(account)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
75 return gaim.ServJoinChat(connection, params)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
76
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
77 elif command == "addbuddy":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
78 account = cgaim.GaimAccountsFindConnected(accountname, protocol)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
79 return cgaim.GaimBlistRequestAddBuddy(account, params["screenname"],
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
80 params.get("group", ""), "")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
81
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
82 elif command == "setstatus":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
83 if "account" in params:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
84 accounts = [cgaim.GaimAccountsFindConnected(accountname, protocol)]
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
85 else:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
86 accounts = gaim.GaimAccountsGetAllActive()
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
87
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
88 for account in accounts:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
89 status = gaim.GaimAccountGetStatus(account, params["status"])
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
90 for key, value in params.items():
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
91 if key not in ["state", "account"]:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
92 gaim.GaimStatusSetAttrString(status, key, value)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
93 gaim.GaimAccountSetStatusVargs(account, params["status"], 1)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
94 return None
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
95
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
96 elif command == "quit":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
97 return gaim.GaimCoreQuit()
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
98
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
99 elif command == "uri":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
100 return None
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
101
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
102 else:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
103 match = re.match(r"(\w+)\s*\(([^)]*)\)", command)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
104 if match is not None:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
105 name = match.group(1)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
106 argstr = match.group(2)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
107 if argstr == "":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
108 args = []
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
109 else:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
110 args = argstr.split(",")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
111 fargs = []
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
112 for arg in args:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
113 fargs.append(convert(arg.strip()))
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
114 return gaim.__getattr__(name)(*fargs)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
115 else:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
116 # introspect the object to get parameter names and types
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
117 # this is slow because the entire introspection info must be downloaded
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
118 data = dbus.Interface(obj, "org.freedesktop.DBus.Introspectable").\
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
119 Introspect()
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
120 introspect = xml.dom.minidom.parseString(data).documentElement
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
121 for method in introspect.all("method"):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
122 if command == method.getAttribute("name"):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
123 methodparams = []
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
124 for arg in method.all("arg"):
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
125 if arg.getAttribute("direction") == "in":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
126 value = params[arg.getAttribute("name")]
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
127 type = arg.getAttribute("type")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
128 if type == "s":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
129 methodparams.append(value)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
130 elif type == "i":
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
131 methodparams.append(int(value))
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
132 else:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
133 raise "Don't know how to handle type \"%s\"" % type
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
134 return gaim.__getattr__(command)(*methodparams)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
135 raise "Unknown command: %s" % command
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
136
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
137 def example_code_do_not_call():
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
138 execute("jabber:addbuddy?screenname=friend")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
139 execute("setstatus?status=away&message=don't disturb")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
140
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
141 account = execute("GaimAccountsFindConnected?name=&protocol=")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
142 execute("GaimConversationNew?type=1&account=%i&name=testone@localhost" % account)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
143
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
144 execute("jabber:addbuddy?screenname=friend")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
145 execute("jabber:goim?screenname=testone@localhost&message=hi")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
146
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
147 execute("jabber:gochat?room=TestRoom&server=conference.localhost")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
148 execute("jabber:goim?screenname=testone@localhost&message=hi")
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
149
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
150
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
151
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
152
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
153
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
154 for arg in sys.argv[1:]:
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
155 print execute(arg)
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
156
744c0708d11f [gaim-migrate @ 13303]
Piotr Zielinski <zielaj>
parents:
diff changeset
157