Added initial support for new_node, update and remove callbacks from PurpleBlistUiOps.
[python-purple] / nullclient.py
1 import purple
2 import ecore
3 import getpass
4 import sys
5
6 cbs = {}
7 acc_cbs = {}
8 blist_cbs = {}
9 conn_cbs = {}
10 conv_cbs = {}
11 notify_cbs = {}
12 request_cbs = {}
13 signal_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 conv_cbs["create_conversation"] = conv_callback
127 conv_cbs["destroy_conversation"] = conv_callback
128 conv_cbs["write_chat"] = conv_callback
129 conv_cbs["write_im"] = conv_callback
130 conv_cbs["write_conv"] = conv_callback
131 conv_cbs["chat_add_users"] = conv_callback
132 conv_cbs["chat_rename_user"] = conv_callback
133 conv_cbs["chat_remove_users"] = conv_callback
134 conv_cbs["chat_update_user"] = conv_callback
135 conv_cbs["present"] = conv_callback
136 conv_cbs["has_focus"] = conv_callback
137 conv_cbs["custom_smiley_add"] = conv_callback
138 conv_cbs["custom_smiley_write"] = conv_callback
139 conv_cbs["custom_smiley_close"] = conv_callback
140 conv_cbs["send_confirm"] = conv_callback
141
142 cbs["conversation"] = conv_cbs
143
144 def notify_callback(name):
145     print "----  notify callback example: %s" % name
146
147 notify_cbs["notify_message"] = notify_callback
148 notify_cbs["notify_email"] = notify_callback
149 notify_cbs["notify_emails"] = notify_callback
150 notify_cbs["notify_formatted"] = notify_callback
151 notify_cbs["notify_searchresults"] = notify_callback
152 notify_cbs["notify_searchresults_new_rows"] = notify_callback
153 notify_cbs["notify_userinfo"] = notify_callback
154 notify_cbs["notify_uri"] = notify_callback
155 notify_cbs["close_notify"] = notify_callback
156
157 cbs["notify"] = notify_cbs
158
159 def request_callback(name):
160     print "---- request callback example: %s" % name
161
162 request_cbs["request_input"] = request_callback
163 request_cbs["request_choice"] = request_callback
164 request_cbs["request_action"] = request_callback
165 request_cbs["request_fields"] = request_callback
166 request_cbs["request_file"] = request_callback
167 request_cbs["close_request"] = request_callback
168 request_cbs["request_folder"] = request_callback
169
170 cbs["request"] = request_cbs
171
172 def buddy_signed_off_cb(name):
173     print "---- sign off from buddy %s" % name
174
175 def receiving_im_msg_cb(sender, name, message):
176     print "---- receiving IM message from %s: %s" % (name, message)
177     return False
178
179 signal_cbs["buddy_signed_off"] = buddy_signed_off_cb
180 signal_cbs["receiving_im_msg"] = receiving_im_msg_cb
181
182 class NullClient:
183     def __init__(self):
184         self.p = purple.Purple(debug_enabled=False)
185         self.account = None
186
187     def execute(self):
188         global cbs
189         self.p.purple_init(cbs)
190
191     def set_protocol(self, protocol):
192         for p in self.p.get_protocols():
193             if p.get_name() == protocol:
194                 print "-- NULLCLIENT --: Choosing %s as protocol" % protocol
195                 self.protocol = p
196                 print "-- NULLCLIENT --: Protocol successfully chosen: %s" % p.get_id()
197                 return
198
199     def new_account(self, username, protocol, password):
200         self.account = purple.Account(username, protocol.get_id())
201         self.account.set_password(password)
202
203         self.account.proxy.set_type(purple.ProxyInfoType().HTTP)
204         self.account.proxy.set_host("172.18.216.211")
205         self.account.proxy.set_port(8080)
206
207         self.account.get_protocol_options()
208
209         self.account.set_enabled("carman-purple-python", True)
210     def get_buddies(self):
211         buddies = self.account.get_buddies_online()
212         print buddies
213
214 def getuser():
215     sys.stdout.write("GTalk account: ")
216     username = sys.stdin.readline()
217     return username[:-1]
218
219 def getpassword():
220     return getpass.getpass()
221
222 if __name__ == '__main__':
223
224     client = NullClient()
225     client.execute()
226     client.set_protocol("XMPP")
227     username = getuser()
228     password = getpassword()
229     client.new_account(username, client.protocol, password)
230
231     client.p.connect()
232     client.p.attach_signals(signal_cbs)
233     ecore.timer_add(20, client.get_buddies)
234     ecore.main_loop_begin()