e69a9665850c9ec5d64fab6d49634ec77408eedf
[python-purple] / purple.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
6 #  python-purple is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  python-purple is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 cimport purple
21
22 cdef extern from "c_purple.h":
23     glib.guint glib_input_add(glib.gint fd, eventloop.PurpleInputCondition condition, eventloop.PurpleInputFunction function, glib.gpointer data)
24
25 import ecore
26
27 __DEFAULT_PATH__ = "/tmp"
28 __APP_NAME__ = "carman-purple-python"
29 __APP_VERSION__ = "0.1"
30
31 cdef account.PurpleAccountUiOps c_account_ui_ops
32 cdef blist.PurpleBlistUiOps c_blist_ui_ops
33 cdef connection.PurpleConnectionUiOps c_conn_ui_ops
34 cdef conversation.PurpleConversationUiOps c_conv_ui_ops
35 cdef core.PurpleCoreUiOps c_core_ui_ops
36 cdef eventloop.PurpleEventLoopUiOps c_eventloop_ui_ops
37 #cdef ft.PurpleXferUiOps c_ft_ui_ops
38 cdef notify.PurpleNotifyUiOps c_notify_ui_ops
39 #cdef privacy.PurplePrivacyUiOps c_privacy_ui_ops
40 cdef request.PurpleRequestUiOps c_request_ui_ops
41 #cdef roomlist.PurpleRoomlistUiOps c_rlist_ui_ops
42
43 cdef glib.GHashTable *c_ui_info
44
45 c_ui_info = NULL
46
47 include "account_cbs.pxd"
48 include "blist_cbs.pxd"
49 include "connection_cbs.pxd"
50 include "conversation_cbs.pxd"
51 #include "xfer_cbs.pxd"
52 include "notify_cbs.pxd"
53 #include "privacy_cbs.pxd"
54 include "request_cbs.pxd"
55 #include "roomlist_cbs.pxd"
56 include "signal_cbs.pxd"
57
58 include "util.pxd"
59
60 cdef class Purple:
61     """ Purple class.
62
63     @parm debug_enabled: Toggle debug messages.
64     @parm app_name: Set application name.
65     @parm default_path: Full path for libpurple user files.
66     """
67
68     cdef object __accounts
69
70     def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
71         self.__accounts = {}
72         if app_name is not __APP_NAME__:
73             __APP_NAME__ = app_name
74
75         if default_path is not __DEFAULT_PATH__:
76             __DEFAULT_PATH__ = default_path
77
78         debug.c_purple_debug_set_enabled(debug_enabled)
79         util.c_purple_util_set_user_dir(default_path)
80         plugin.c_purple_plugins_add_search_path(default_path)
81
82         # adds glib iteration inside ecore main loop
83         ecore.timer_add(0.001, self.__glib_iteration_when_idle)
84
85     def destroy(self):
86         core.c_purple_core_quit()
87
88     cdef void __core_ui_ops_ui_prefs_init(self):
89         debug.c_purple_debug_info("core_ui_ops", "%s", "ui_prefs_init\n")
90         prefs.c_purple_prefs_load()
91
92         prefs.c_purple_prefs_add_none("/carman")
93
94     cdef void __core_ui_ops_debug_init(self):
95         debug.c_purple_debug_info("core_ui_ops", "%s", "debug_ui_init\n")
96         pass
97
98     cdef void __core_ui_ops_ui_init(self):
99         debug.c_purple_debug_info("core_ui_ops", "%s", "ui_init\n")
100
101         account.c_purple_accounts_set_ui_ops(&c_account_ui_ops)
102         connection.c_purple_connections_set_ui_ops(&c_conn_ui_ops)
103         blist.c_purple_blist_set_ui_ops(&c_blist_ui_ops)
104         conversation.c_purple_conversations_set_ui_ops(&c_conv_ui_ops)
105         notify.c_purple_notify_set_ui_ops(&c_notify_ui_ops)
106         #privacy.c_purple_privacy_set_ui_ops(&c_privacy_ui_ops)
107         request.c_purple_request_set_ui_ops(&c_request_ui_ops)
108         #ft.c_purple_xfers_set_ui_ops(&c_ft_ui_ops)
109         #roomlist.c_purple_roomlist_set_ui_ops(&c_rlist_ui_ops)
110
111     cdef void __core_ui_ops_quit(self):
112         debug.c_purple_debug_info("core_ui_ops", "%s", "quit\n")
113
114         global c_ui_info
115
116         account.c_purple_accounts_set_ui_ops(NULL)
117         connection.c_purple_connections_set_ui_ops(NULL)
118         blist.c_purple_blist_set_ui_ops(NULL)
119         conversation.c_purple_conversations_set_ui_ops(NULL)
120         notify.c_purple_notify_set_ui_ops(NULL)
121         #privacy.c_purple_privacy_set_ui_ops(NULL)
122         request.c_purple_request_set_ui_ops(NULL)
123         #ft.c_purple_xfers_set_ui_ops(NULL)
124         #roomlist.c_purple_roomlist_set_ui_ops(NULL)
125
126         if c_ui_info:
127             glib.g_hash_table_destroy(c_ui_info)
128
129     cdef glib.GHashTable *__core_ui_ops_get_ui_info(self):
130         global c_ui_info
131
132         if c_ui_info == NULL:
133             c_ui_info = glib.g_hash_table_new(glib.g_str_hash, glib.g_str_equal)
134
135             glib.g_hash_table_insert(c_ui_info, "name", <glib.gpointer> __APP_NAME__)
136             glib.g_hash_table_insert(c_ui_info, "version", <glib.gpointer> __APP_VERSION__)
137         return c_ui_info
138
139     def __glib_iteration_when_idle(self):
140         glib.g_main_context_iteration(NULL, False)
141         return True
142
143     def purple_init(self):
144         """ Initializes libpurple """
145
146         c_account_ui_ops.notify_added = notify_added
147         c_account_ui_ops.status_changed = status_changed
148         c_account_ui_ops.request_add = request_add
149         c_account_ui_ops.request_authorize = request_authorize
150         c_account_ui_ops.close_account_request = close_account_request
151
152         c_blist_ui_ops.new_list = new_list
153         c_blist_ui_ops.new_node = new_node
154         c_blist_ui_ops.show = show
155         c_blist_ui_ops.update = update
156         c_blist_ui_ops.remove = remove
157         c_blist_ui_ops.destroy = destroy
158         c_blist_ui_ops.set_visible = set_visible
159         c_blist_ui_ops.request_add_buddy = request_add_buddy
160         c_blist_ui_ops.request_add_chat = request_add_chat
161         c_blist_ui_ops.request_add_group = request_add_group
162
163         c_conn_ui_ops.connect_progress = connect_progress
164         c_conn_ui_ops.connected = connected
165         c_conn_ui_ops.disconnected = disconnected
166         c_conn_ui_ops.notice = notice
167         c_conn_ui_ops.report_disconnect = report_disconnect
168         c_conn_ui_ops.network_connected = network_connected
169         c_conn_ui_ops.network_disconnected = network_disconnected
170         c_conn_ui_ops.report_disconnect_reason = report_disconnect_reason
171
172         c_conv_ui_ops.create_conversation = create_conversation
173         c_conv_ui_ops.destroy_conversation = destroy_conversation
174         c_conv_ui_ops.write_chat = write_chat
175         c_conv_ui_ops.write_im = write_im
176         c_conv_ui_ops.write_conv = write_conv
177         c_conv_ui_ops.chat_add_users = chat_add_users
178         c_conv_ui_ops.chat_rename_user = chat_rename_user
179         c_conv_ui_ops.chat_remove_users = chat_remove_users
180         c_conv_ui_ops.chat_update_user = chat_update_user
181         c_conv_ui_ops.present = present
182         c_conv_ui_ops.has_focus = has_focus
183         c_conv_ui_ops.custom_smiley_add = custom_smiley_add
184         c_conv_ui_ops.custom_smiley_write = custom_smiley_write
185         c_conv_ui_ops.custom_smiley_close = custom_smiley_close
186         c_conv_ui_ops.send_confirm = send_confirm
187
188         c_notify_ui_ops.notify_message = notify_message
189         c_notify_ui_ops.notify_email = notify_email
190         c_notify_ui_ops.notify_emails = notify_emails
191         c_notify_ui_ops.notify_formatted = notify_formatted
192         c_notify_ui_ops.notify_searchresults = notify_searchresults
193         c_notify_ui_ops.notify_searchresults_new_rows = notify_searchresults_new_rows
194         c_notify_ui_ops.notify_userinfo = notify_userinfo
195         c_notify_ui_ops.notify_uri = notify_uri
196         c_notify_ui_ops.close_notify = close_notify
197
198         c_request_ui_ops.request_input = request_input
199         c_request_ui_ops.request_choice = request_choice
200         c_request_ui_ops.request_action = request_action
201         c_request_ui_ops.request_fields = request_fields
202         c_request_ui_ops.request_file = request_file
203         c_request_ui_ops.close_request = close_request
204         c_request_ui_ops.request_folder = request_folder
205
206         c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
207         c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
208         c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
209         c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
210         c_core_ui_ops.get_ui_info = <glib.GHashTable* (*)()> self.__core_ui_ops_get_ui_info
211
212         c_eventloop_ui_ops.timeout_add = glib.g_timeout_add
213         c_eventloop_ui_ops.timeout_remove = glib.g_source_remove
214         c_eventloop_ui_ops.input_add = glib_input_add
215         c_eventloop_ui_ops.input_remove = glib.g_source_remove
216         c_eventloop_ui_ops.input_get_error = NULL
217         c_eventloop_ui_ops.timeout_add_seconds = NULL
218
219         core.c_purple_core_set_ui_ops(&c_core_ui_ops)
220         eventloop.c_purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
221
222         # initialize purple core
223         ret = core.c_purple_core_init(__APP_NAME__)
224         if ret is False:
225             debug.c_purple_debug_fatal("main", "%s", "libpurple " \
226                                        "initialization failed.\n")
227             return False
228
229         # check if there is another instance of libpurple running
230         if core.c_purple_core_ensure_single_instance() == False:
231             debug.c_purple_debug_fatal("main", "%s", "Another instance of " \
232                                       "libpurple is already running.\n")
233             core.c_purple_core_quit()
234             return False
235
236         # create and load the buddy list
237         blist.c_purple_set_blist(blist.c_purple_blist_new())
238         blist.c_purple_blist_load()
239
240         # load pounces
241         pounce.c_purple_pounces_load()
242
243         # initialize accounts
244         self.load_accounts()
245
246         return ret
247
248     def add_callback(self, type, name, callback):
249         """
250         Adds a callback with given name inside callback's type.
251         Example: add_callback("account", "notify-added", notify_added_cb)
252         """
253         global account_cbs
254         global blist_cbs
255         global connection_cbs
256         global conversation_cbs
257         global notify_cbs
258         global request_cbs
259
260         { "account": account_cbs,
261           "blist": blist_cbs,
262           "connection": connection_cbs,
263           "conversation": conversation_cbs,
264           "notify": notify_cbs,
265           "request": request_cbs }[type][name] = callback
266
267     def signal_connect(self, name=None, cb=None):
268         cdef int handle
269         cdef plugin.PurplePlugin *jabber
270
271         if name is None:
272             return
273
274         jabber = prpl.c_purple_find_prpl("prpl-jabber")
275         if jabber == NULL:
276             return
277
278         global signal_cbs
279         signal_cbs[name] = cb
280
281         if name == "signed-on":
282             signals.c_purple_signal_connect(
283                     connection.c_purple_connections_get_handle(),
284                     "signed-on", &handle,
285                     <signals.PurpleCallback> signal_signed_on_cb, NULL)
286         elif name == "signed-off":
287             signals.c_purple_signal_connect(
288                     connection.c_purple_connections_get_handle(),
289                     "signed-off", &handle,
290                     <signals.PurpleCallback> signal_signed_off_cb, NULL)
291         elif name == "connection-error":
292             signals.c_purple_signal_connect(
293                     connection.c_purple_connections_get_handle(),
294                     "connection-error", &handle,
295                     <signals.PurpleCallback> signal_connection_error_cb, NULL)
296         elif name == "buddy-signed-on":
297             signals.c_purple_signal_connect(
298                     blist.c_purple_blist_get_handle(),
299                     "buddy-signed-on", &handle,
300                     <signals.PurpleCallback> signal_buddy_signed_on_cb, NULL)
301         elif name == "buddy-signed-off":
302             signals.c_purple_signal_connect(
303                     blist.c_purple_blist_get_handle(),
304                     "buddy-signed-off", &handle,
305                     <signals.PurpleCallback> signal_buddy_signed_off_cb, NULL)
306         elif name == "receiving-im-msg":
307             signals.c_purple_signal_connect(
308                     conversation.c_purple_conversations_get_handle(),
309                     "receiving-im-msg", &handle,
310                     <signals.PurpleCallback> signal_receiving_im_msg_cb, NULL)
311         elif name == "jabber-receiving-xmlnode":
312             signals.c_purple_signal_connect(
313                     jabber, "jabber-receiving-xmlnode", &handle,
314                     <signals.PurpleCallback> jabber_receiving_xmlnode_cb, NULL)
315
316     def __get_accounts(self):
317         return self.__accounts
318     accounts = property(__get_accounts)
319
320     def new_account(self, username, protocol_id):
321         acc = Account(username, protocol_id)
322         return acc
323
324     def load_accounts(self):
325         cdef glib.GList *iter
326         cdef account.PurpleAccount *acc
327         iter = account.c_purple_accounts_get_all()
328         while iter:
329             acc = <account.PurpleAccount *> iter.data
330             if <account.PurpleAccount *>acc:
331                 username = account.c_purple_account_get_username(acc)
332                 protocol_id = account.c_purple_account_get_protocol_id(acc)
333                 self.account_add(username.split("/")[0], protocol_id, "172.18.216.211", 8080)
334             iter = iter.next
335
336     def account_add(self, username, protocol_id, host, port):
337         if not self.account_verify(username):
338             acc = purple.Account(username, protocol_id)
339             self.__accounts[username] = acc
340             if not account.c_purple_accounts_find(username, protocol_id):
341                 acc.proxy.set_type(purple.ProxyInfoType().HTTP)
342                 acc.proxy.set_host(host)
343                 acc.proxy.set_port(port)
344                 acc.save_into_xml()
345
346     def account_verify(self, acc_username):
347         if self.__accounts:
348             for username in self.__accounts.keys():
349                 if acc_username == username:
350                     return self.__accounts[username]
351         else:
352             return None
353
354 include "plugin.pyx"
355 include "proxy.pyx"
356 include "account.pyx"
357 include "buddy.pyx"
358 #include "connection.pyx"
359 include "conversation.pyx"