Implement purple.Buddy()
[python-purple] / nullclient.py
1 import purple
2 import ecore
3 import getpass
4 import sys
5 from xml.dom import minidom
6
7 cbs = {}
8 acc_cbs = {}
9 blist_cbs = {}
10 conn_cbs = {}
11 conv_cbs = {}
12 notify_cbs = {}
13 request_cbs = {}
14
15 def account_callback(name):
16     print "---- account callback example: %s" % name
17
18 acc_cbs["notify_added"] = account_callback
19 acc_cbs["status_changed"] = account_callback
20 acc_cbs["request_add"] = account_callback
21 acc_cbs["request_authorize"] = account_callback
22 acc_cbs["close_account_request"] = account_callback
23
24 cbs["account"] = acc_cbs
25
26 def blist_callback(name):
27     print "---- blist callback example: %s" % name
28
29 blist_cbs["new_list"] = blist_callback
30 blist_cbs["show"] = blist_callback
31 blist_cbs["destroy"] = blist_callback
32 blist_cbs["set_visible"] = blist_callback
33 blist_cbs["request_add_buddy"] = blist_callback
34 blist_cbs["request_add_chat"] = blist_callback
35 blist_cbs["request_add_group"] = blist_callback
36
37 def new_node_cb(type, name=None, totalsize=None, currentsize=None, online=None):
38     if type == 0:
39         print "---- blist callback: new node (group): %s (%s/%s) (%s online)" % \
40                 (name, totalsize, currentsize, online)
41     elif type == 1:
42         print "---- blist callback: new node (contact): %s (%s/%s) (%s online)" % \
43                 (name, totalsize, currentsize, online)
44     elif type == 2: # totalsize = alias
45         print "---- blist callback: new node (buddy): %s (%s)" % (name, totalsize)
46     elif type == 3:
47         print "---- blist callback: new node (chat): %s" % alias
48     elif type == 4:
49         print "---- blist callback: new node (other type)"
50     else:
51         print "---- blist callback: new node (unknown type %s)" % type
52
53 def update_cb(type, name=None, totalsize=None, currentsize=None, online=None):
54     if type == 0:
55         print "---- blist callback: update (group): %s (%s/%s) (%s online)" % \
56                 (name, totalsize, currentsize, online)
57     elif type == 1:
58         print "---- blist callback: update (contact): %s (%s/%s) (%s online)" % \
59                 (name, totalsize, currentsize, online)
60     elif type == 2: # totalsize = alias
61         print "---- blist callback: update (buddy): %s (%s)" % \
62                 (name, totalsize)
63     elif type == 3:
64         print "---- blist callback: update (chat): %s" % alias
65     elif type == 4:
66         print "---- blist callback: update (other type)"
67     else:
68         print "---- blist callback: update (unknown type %s)" % type
69
70 def remove_cb(type, name=None, totalsize=None, currentsize=None, online=None):
71     if type == 0:
72         print "---- blist callback: remove (group): %s (%s/%s) (%s online)" % \
73                 (name, totalsize, currentsize, online)
74     elif type == 1:
75         print "---- blist callback: remove (contact): %s (%s/%s) (%s online)" % \
76                 (name, totalsize, currentsize, online)
77     elif type == 2: # totalsize = alias
78         print "---- blist callback: remove (buddy): %s (%s)" % \
79                 (name, totalsize)
80     elif type == 3:
81         print "---- blist callback: remove (chat): %s" % alias
82     elif type == 4:
83         print "---- blist callback: remove (other type)"
84     else:
85         print "---- blist callback: remove (unknown type %s)" % type
86
87 blist_cbs["new_node"] = new_node_cb
88 blist_cbs["update"] = update_cb
89 blist_cbs["remove"] = remove_cb
90
91 cbs["blist"] = blist_cbs
92
93 def conn_callback(name):
94     print "---- connection callback example: %s" % name
95
96 conn_cbs["notice"] = conn_callback
97 conn_cbs["network_connected"] = conn_callback
98 conn_cbs["network_disconnected"] = conn_callback
99
100 def connect_progress_cb(text, step, step_count):
101     print "---- connection status: %s [%s/%s]" % (text, step, step_count)
102
103 def connected_cb():
104     print "---- connection status: Connected"
105
106 def disconnected_cb():
107     print "---- connection status: Disconnected"
108
109 def report_disconnect_cb(text):
110     print "---- %s" % text
111
112 def report_disconnect_reason_cb(reason, text):
113     print "---- %s (%s)" % (text, reason)
114
115 conn_cbs["connect_progress"] = connect_progress_cb
116 conn_cbs["connected"] = connected_cb
117 conn_cbs["disconnected"] = disconnected_cb
118 conn_cbs["report_disconnect"] = report_disconnect_cb
119 conn_cbs["report_disconnect_reason"] = report_disconnect_reason_cb
120
121 cbs["connection"] = conn_cbs
122
123 def conv_callback(name):
124     print "---- conversation callback example: %s" % name
125
126 def write_im_cb(name, message):
127     print "---- (conversation) write_im: %s %s" % (name, message)
128
129 conv_cbs["create_conversation"] = conv_callback
130 conv_cbs["destroy_conversation"] = conv_callback
131 conv_cbs["write_chat"] = conv_callback
132 conv_cbs["write_im"] = write_im_cb
133 conv_cbs["write_conv"] = conv_callback
134 conv_cbs["chat_add_users"] = conv_callback
135 conv_cbs["chat_rename_user"] = conv_callback
136 conv_cbs["chat_remove_users"] = conv_callback
137 conv_cbs["chat_update_user"] = conv_callback
138 conv_cbs["present"] = conv_callback
139 conv_cbs["has_focus"] = conv_callback
140 conv_cbs["custom_smiley_add"] = conv_callback
141 conv_cbs["custom_smiley_write"] = conv_callback
142 conv_cbs["custom_smiley_close"] = conv_callback
143 conv_cbs["send_confirm"] = conv_callback
144
145 cbs["conversation"] = conv_cbs
146
147 def notify_callback(name):
148     print "----  notify callback example: %s" % name
149
150 notify_cbs["notify_message"] = notify_callback
151 notify_cbs["notify_email"] = notify_callback
152 notify_cbs["notify_emails"] = notify_callback
153 notify_cbs["notify_formatted"] = notify_callback
154 notify_cbs["notify_searchresults"] = notify_callback
155 notify_cbs["notify_searchresults_new_rows"] = notify_callback
156 notify_cbs["notify_userinfo"] = notify_callback
157 notify_cbs["notify_uri"] = notify_callback
158 notify_cbs["close_notify"] = notify_callback
159
160 cbs["notify"] = notify_cbs
161
162 def request_callback(name):
163     print "---- request callback example: %s" % name
164
165 request_cbs["request_input"] = request_callback
166 request_cbs["request_choice"] = request_callback
167 request_cbs["request_action"] = request_callback
168 request_cbs["request_fields"] = request_callback
169 request_cbs["request_file"] = request_callback
170 request_cbs["close_request"] = request_callback
171 request_cbs["request_folder"] = request_callback
172
173 cbs["request"] = request_cbs
174
175 def buddy_signed_off_cb(name):
176     print "---- (signal) sign off from buddy %s" % name
177
178 def receiving_im_msg_cb(sender, name, message):
179     print "---- (signal) receiving IM message from %s: %s" % (name, message)
180     return False
181
182 def jabber_received_xmlnode_cb(message):
183     xml = minidom.parse(message)
184
185     for msg in xml.getElementsByTagName("message"):
186         who = msg.getAttribute("from")
187         for geoloc in msg.getElementsByTagNameNS("http://jabber.org/protocol/geoloc", "geoloc"):
188             lat = geoloc.getElementsByTagName("lat")[0].childNodes[0].nodeValue
189             lon = geoloc.getElementsByTagName("lon")[0].childNodes[0].nodeValue
190             print "who: %s lat: %s lon: %s" % (who, lat, lon)
191
192 class NullClient:
193     def __init__(self):
194         self.p = purple.Purple(debug_enabled=False)
195         self.account = None
196         self.protocol_id = "prpl-jabber"
197
198     def execute(self):
199         global cbs
200         self.p.purple_init(cbs)
201
202     def new_account(self, username, password):
203         self.account = purple.Account(username, self.protocol_id)
204         self.account.set_password(password)
205
206         self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
207         self.account.proxy.set_host("172.18.216.211")
208         self.account.proxy.set_port(8080)
209
210         self.account.get_protocol_options()
211
212         self.account.set_enabled("carman-purple-python", True)
213     def get_buddies(self):
214         buddies = self.account.get_buddies_online()
215         print buddies
216
217 def getuser():
218     sys.stdout.write("GTalk account: ")
219     username = sys.stdin.readline()
220     return username[:-1]
221
222 def getpassword():
223     return getpass.getpass()
224
225 if __name__ == '__main__':
226     client = NullClient()
227     client.execute()
228     client.set_protocol("XMPP")
229     client.p.signal_connect("buddy-signed-off", buddy_signed_off_cb)
230     client.p.signal_connect("receiving-im-msg", receiving_im_msg_cb)
231     client.p.signal_connect("jabber-receiving-xmlnode", jabber_received_xmlnode_cb)
232     username = getuser()
233     password = getpassword()
234     client.new_account(username, password)
235
236     client.p.connect()
237     ecore.timer_add(20, client.get_buddies)
238     ecore.main_loop_begin()