comparison libpurple/purple-url-handler @ 17282:5059a0a071a2

Restore the protocol specific URL handlers in purple-url-handler for those protocols that don't have their own uri-handler signal handler in libpurple yet. These can be phased out as protocols get their own uri-handler support, after reading other C uri parser implementations I didn't want to tackle that just yet.
author Stu Tomlinson <stu@nosnilmot.com>
date Fri, 25 May 2007 00:46:16 +0000
parents cc9242ce1435
children 8a7238fb7905
comparison
equal deleted inserted replaced
17281:0018ab54be9c 17282:5059a0a071a2
36 36
37 return result 37 return result
38 38
39 cpurple = CheckedObject(purple) 39 cpurple = CheckedObject(purple)
40 40
41 def extendlist(list, length, fill):
42 if len(list) < length:
43 return list + [fill] * (length - len(list))
44 else:
45 return list
46
47 def findaccount(protocolname, accountname=""):
48 # prefer connected accounts
49 account = cpurple.PurpleAccountsFindConnected(accountname, protocolname)
50 if (account != 0):
51 return account
52
53 # try to get any account and connect it
54 account = cpurple.PurpleAccountsFindAny(accountname, protocolname)
55 if (account == 0):
56 print "No matching account found."
57 sys.exit(1)
58
59 purple.PurpleAccountSetStatusVargs(account, "online", 1)
60 purple.PurpleAccountConnect(account)
61 return account
62
63 def goim(account, screenname, message=None):
64 # XXX: 1 == PURPLE_CONV_TYPE_IM
65 conversation = cpurple.PurpleConversationNew(1, account, screenname)
66 if message:
67 purple.PurpleConvSendConfirm(conversation, message)
68
69 def gochat(account, params, message=None):
70 connection = cpurple.PurpleAccountGetConnection(account)
71 purple.ServJoinChat(connection, params)
72
73 if message != None:
74 for i in range(20):
75 # XXX: 2 == PURPLE_CONV_TYPE_CHAT
76 conversation = purple.PurpleFindConversationWithAccount(2, params.get("channel", params.get("room")), account)
77 if conversation:
78 purple.PurpleConvSendConfirm(conversation, message)
79 break
80 else:
81 time.sleep(0.5)
82
83 def addbuddy(account, screenname, group="", alias=""):
84 cpurple.PurpleBlistRequestAddBuddy(account, screenname, group, alias)
85
86
87 def gg(uri):
88 protocol = "prpl-gg"
89 match = re.match(r"^gg:(.*)", uri)
90 if not match:
91 print "Invalid gg URI: %s" % uri
92 return
93
94 screenname = urllib.unquote_plus(match.group(1))
95 account = findaccount(protocol)
96 goim(account, screenname)
97
98 def irc(uri):
99 protocol = "prpl-irc"
100 match = re.match(r"^irc:(//([^/]*)/)?([^?]*)(\?(.*))?", uri)
101 if not match:
102 print "Invalid irc URI: %s" % uri
103 return
104
105 server = urllib.unquote_plus(match.group(2)) or ""
106 target = match.group(3) or ""
107 query = match.group(5) or ""
108
109 modifiers = {}
110 if target:
111 for modifier in target.split(",")[1:]:
112 modifiers[modifier] = True
113
114 isnick = modifiers.has_key("isnick")
115
116 paramstring = match.group(5)
117 params = {}
118 if paramstring:
119 for param in paramstring.split("&"):
120 key, value = extendlist(param.split("=", 1), 2, "")
121 params[key] = urllib.unquote_plus(value)
122
123 account = findaccount(protocol)
124
125 if (target != ""):
126 if (isnick):
127 goim(account, urllib.unquote_plus(target.split(",")[0]), params.get("msg"))
128 else:
129 channel = urllib.unquote_plus(target.split(",")[0])
130 if channel[0] != "#":
131 channel = "#" + channel
132 gochat(account, {"server": server, "channel": channel, "password": params.get("key", "")}, params.get("msg"))
133
134 def sip(uri):
135 protocol = "prpl-simple"
136 match = re.match(r"^sip:(.*)", uri)
137 if not match:
138 print "Invalid sip URI: %s" % uri
139 return
140
141 screenname = urllib.unquote_plus(match.group(1))
142 account = findaccount(protocol)
143 goim(account, screenname)
144
145 def xmpp(uri):
146 protocol = "prpl-jabber"
147 match = re.match(r"^xmpp:(//([^/?#]*)/?)?([^?#]*)(\?([^;#]*)(;([^#]*))?)?(#(.*))?", uri)
148 if not match:
149 print "Invalid xmpp URI: %s" % uri
150 return
151
152 tmp = match.group(2)
153 if (tmp):
154 accountname = urllib.unquote_plus(tmp)
155 else:
156 accountname = ""
157
158 screenname = urllib.unquote_plus(match.group(3))
159
160 tmp = match.group(5)
161 if (tmp):
162 command = urllib.unquote_plus(tmp)
163 else:
164 command = ""
165
166 paramstring = match.group(7)
167 params = {}
168 if paramstring:
169 for param in paramstring.split(";"):
170 key, value = extendlist(param.split("=", 1), 2, "")
171 params[key] = urllib.unquote_plus(value)
172
173 account = findaccount(protocol, accountname)
174
175 if command.lower() == "message":
176 goim(account, screenname, params.get("body"))
177 elif command.lower() == "join":
178 room, server = screenname.split("@")
179 gochat(account, {"room": room, "server": server})
180 elif command.lower() == "roster":
181 addbuddy(account, screenname, params.get("group", ""), params.get("name", ""))
182 else:
183 goim(account, screenname)
184
41 def main(argv=sys.argv): 185 def main(argv=sys.argv):
42 if len(argv) != 2: 186 if len(argv) != 2:
43 print "Usage: %s URI" % argv[0] 187 print "Usage: %s URI" % argv[0]
44 print "Example: %s \"xmpp:romeo@montague.net?message\"" % argv[0] 188 print "Example: %s \"xmpp:romeo@montague.net?message\"" % argv[0]
45 return 189 return
46 190
47 uri = argv[1] 191 uri = argv[1]
48 192 type = uri.split(":")[0]
49 print uri 193
50 try: 194 try:
51 cpurple.PurpleGotProtocolHandlerUri(uri) 195 if type == "gg":
196 gg(uri)
197 elif type == "irc":
198 irc(uri)
199 elif type == "sip":
200 sip(uri)
201 elif type == "xmpp":
202 xmpp(uri)
203 else:
204 cpurple.PurpleGotProtocolHandlerUri(uri)
52 except dbus.dbus_bindings.DBusException: 205 except dbus.dbus_bindings.DBusException:
53 print "ERROR: Is there a libpurple-powered client (e.g. Pidgin or Finch) running?" 206 print "ERROR: Is there a libpurple-powered client (e.g. Pidgin or Finch) running?"
54 207
55 208
56 if __name__ == "__main__": 209 if __name__ == "__main__":