Adding support to new class of protocols
[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["new_node"] = blist_callback
31 blist_cbs["show"] = blist_callback
32 blist_cbs["update"] = blist_callback
33 blist_cbs["remove"] = blist_callback
34 blist_cbs["destroy"] = blist_callback
35 blist_cbs["set_visible"] = blist_callback
36 blist_cbs["request_add_buddy"] = blist_callback
37 blist_cbs["request_add_chat"] = blist_callback
38 blist_cbs["request_add_group"] = blist_callback
39
40 cbs["blist"] = blist_cbs
41
42 def conn_callback(name):
43     print "---- connection callback example: %s" % name
44
45 conn_cbs["notice"] = conn_callback
46 conn_cbs["network_connected"] = conn_callback
47 conn_cbs["network_disconnected"] = conn_callback
48
49 def connect_progress_cb(text, step, step_count):
50     print "---- connection status: %s [%s/%s]" % (text, step, step_count)
51
52 def connected_cb():
53     print "---- connection status: Connected"
54
55 def disconnected_cb():
56     print "---- connection status: Disconnected"
57
58 def report_disconnect_cb(text):
59     print "---- %s" % text
60
61 def report_disconnect_reason_cb(reason, text):
62     print "---- %s (%s)" % (text, reason)
63
64 conn_cbs["connect_progress"] = connect_progress_cb
65 conn_cbs["connected"] = connected_cb
66 conn_cbs["disconnected"] = disconnected_cb
67 conn_cbs["report_disconnect"] = report_disconnect_cb
68 conn_cbs["report_disconnect_reason"] = report_disconnect_reason_cb
69
70 cbs["connection"] = conn_cbs
71
72 def conv_callback(name):
73     print "---- conversation callback example: %s" % name
74
75 conv_cbs["create_conversation"] = conv_callback
76 conv_cbs["destroy_conversation"] = conv_callback
77 conv_cbs["write_chat"] = conv_callback
78 conv_cbs["write_im"] = conv_callback
79 conv_cbs["write_conv"] = conv_callback
80 conv_cbs["chat_add_users"] = conv_callback
81 conv_cbs["chat_rename_user"] = conv_callback
82 conv_cbs["chat_remove_users"] = conv_callback
83 conv_cbs["chat_update_user"] = conv_callback
84 conv_cbs["present"] = conv_callback
85 conv_cbs["has_focus"] = conv_callback
86 conv_cbs["custom_smiley_add"] = conv_callback
87 conv_cbs["custom_smiley_write"] = conv_callback
88 conv_cbs["custom_smiley_close"] = conv_callback
89 conv_cbs["send_confirm"] = conv_callback
90
91 cbs["conversation"] = conv_cbs
92
93 def notify_callback(name):
94     print "----  notify callback example: %s" % name
95
96 notify_cbs["notify_message"] = notify_callback
97 notify_cbs["notify_email"] = notify_callback
98 notify_cbs["notify_emails"] = notify_callback
99 notify_cbs["notify_formatted"] = notify_callback
100 notify_cbs["notify_searchresults"] = notify_callback
101 notify_cbs["notify_searchresults_new_rows"] = notify_callback
102 notify_cbs["notify_userinfo"] = notify_callback
103 notify_cbs["notify_uri"] = notify_callback
104 notify_cbs["close_notify"] = notify_callback
105
106 cbs["notify"] = notify_cbs
107
108 def request_callback(name):
109     print "---- request callback example: %s" % name
110
111 request_cbs["request_input"] = request_callback
112 request_cbs["request_choice"] = request_callback
113 request_cbs["request_action"] = request_callback
114 request_cbs["request_fields"] = request_callback
115 request_cbs["request_file"] = request_callback
116 request_cbs["close_request"] = request_callback
117 request_cbs["request_folder"] = request_callback
118
119 cbs["request"] = request_cbs
120
121 def buddy_signed_off_cb(name):
122     print "---- sign off from buddy %s" % name
123
124 def receiving_im_msg_cb(sender, name, message):
125     print "---- receiving IM message from %s: %s" % (name, message)
126     return False
127
128 signal_cbs["buddy_signed_off"] = buddy_signed_off_cb
129 signal_cbs["receiving_im_msg"] = receiving_im_msg_cb
130
131 class NullClient:
132     def __init__(self):
133         self.p = purple.Purple(debug_enabled=False)
134         self.account = None
135
136     def execute(self):
137         global cbs
138         self.p.purple_init(cbs)
139
140     def set_protocol(self, protocol):
141         for i in self.p.get_protocols():
142             if i[1] == protocol:
143                 print "-- NULLCLIENT --: Choosing %s as protocol" % protocol
144                 self.protocol = i[0]
145                 print "-- NULLCLIENT --: Protocol successfully chosen: %s" % i[0]
146                 return
147
148     def new_account(self, username, protocol, password):
149         self.account = purple.Account(username, protocol)
150         self.account.set_password(password)
151         self.account.set_enabled("carman-purple-python", True)
152     def get_buddies(self):
153         buddies = self.account.get_buddies_online()
154         print buddies
155
156 def getuser():
157     sys.stdout.write("GTalk account: ")
158     username = sys.stdin.readline()
159     return username[:-1]
160
161 def getpassword():
162     return getpass.getpass()
163
164 if __name__ == '__main__':
165
166     client = NullClient()
167     client.execute()
168     client.set_protocol("XMPP")
169     username = getuser()
170     password = getpassword()
171     client.new_account(username, client.protocol, password)
172
173     client.p.connect()
174     client.p.attach_signals(signal_cbs)
175     ecore.timer_add(20, client.get_buddies)
176     ecore.main_loop_begin()