purple: update purple-chat for 3.x.x API
[siplcs.git] / src / purple / purple-chat.c
blobb1b2e3ba1f211a4323ee72d8dba4627253b47a0f
1 /**
2 * @file purple-chat.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2009 pier11 <pier11@operamail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <time.h>
31 #include <glib.h>
33 #include "conversation.h"
34 #include "server.h"
35 /* for ENOTCONN */
36 #ifdef _WIN32
37 #include "win32/win32dep.h"
38 #else
39 #include <errno.h>
40 #endif
42 #include "version.h"
43 #if PURPLE_VERSION_CHECK(3,0,0)
44 #include "buddylist.h"
45 #include "conversations.h"
46 #define BACKEND_SESSION_TO_PURPLE_CONV_CHAT(s) ((PurpleChatConversation *) s)
47 #define PURPLE_CONV_CHAT(c) c
48 #define PURPLE_CONV_TO_SIPE_CORE_PUBLIC ((struct sipe_core_public *) purple_connection_get_protocol_data(purple_conversation_get_connection(conv)))
49 #else
50 #include "blist.h"
51 #define purple_chat_conversation_add_user(c, n, m, f, b) purple_conv_chat_add_user(c, n, m, f, b)
52 #define purple_chat_conversation_clear_users(c) purple_conv_chat_clear_users(c)
53 #define purple_chat_conversation_get_id(c) purple_conv_chat_get_id(c)
54 #define purple_chat_conversation_remove_user(c, n, s) purple_conv_chat_remove_user(c, n, s)
55 #define purple_chat_conversation_set_nick(c, n) purple_conv_chat_set_nick(c, n)
56 #define purple_chat_conversation_set_topic(c, n, s) purple_conv_chat_set_topic(c, n, s)
57 #define purple_chat_get_components(chat) chat->components
58 #define purple_conversations_find_chat(g, n) purple_find_chat(g, n)
59 #define purple_conversations_get_chats purple_get_chats
60 #define purple_conversation_get_connection(c) purple_conversation_get_gc(c)
61 #define purple_serv_got_chat_in(c, i, w, f, m, t) serv_got_chat_in(c, i, w, f, m, t)
62 #define purple_serv_got_joined_chat(c, i, n) serv_got_joined_chat(c, i, n)
63 #define BACKEND_SESSION_TO_PURPLE_CONV_CHAT(s) (PURPLE_CONV_CHAT(((PurpleConversation *)s)))
64 #define PURPLE_CHAT_USER_NONE PURPLE_CBFLAGS_NONE
65 #define PURPLE_CONV_TO_SIPE_CORE_PUBLIC ((struct sipe_core_public *) conv->account->gc->proto_data)
66 #define PURPLE_CONVERSATION_UPDATE_TOPIC PURPLE_CONV_UPDATE_TOPIC
67 #endif
69 #include "sipe-common.h"
70 #include "sipe-backend.h"
71 #include "sipe-core.h"
72 #include "sipe-nls.h"
74 #define _PurpleMessageFlags PurpleMessageFlags
75 #include "purple-private.h"
77 /**
78 * Mapping between chat sessions in SIPE core and libpurple backend
80 * PurpleAccount
81 * This data structure is created when the user creates the account or at
82 * startup. It lives as long as the account exists, i.e. until the user
83 * deletes it or shutdown.
85 * Value does not change when connection is dropped & re-created.
86 * HAS: gc (PurpleConnection *)
88 * PurpleConversation / PurpleConvChat (sub-type)
89 * This data structure is created by serv_got_join_chat(). It lives as long
90 * as the user doesn't leave the chat or until shutdown.
92 * Value does not change when connection is dropped & re-created.
93 * HAS: account (PurpleAccount *)
94 * HAS: chat ID (int), must be unique
95 * HAS: name (char *), must be unique
96 * HAS: data (GHashTable *)
98 * PurpleConnection
99 * This data structure is created when the connection to the service is
100 * set up. It lives as long as the connection stays open, the user disables
101 * the account or until shutdown.
103 * Value *DOES NOT* survive a connection drop & re-creation.
104 * ASSOCIATED TO: account
106 * SIPE -> libpurple API
107 * add user: purple_conv_chat_add_user(conv, ...)
108 * create: serv_got_joined_chat(gc, chat ID, name)
109 * find user: purple_conv_chat_find_user(conv, ...)
110 * message: serv_got_chat_in(gc, chat ID, ...)
111 * remove user: purple_conv_chat_remove_user(conv, ...)
112 * topic: purple_conv_chat_set_topic(conv, ...)
114 * libpurple -> SIPE API
115 * join_chat(gc, params (GHashTable *))
116 * request to join a channel (again) [only Group Chat]
117 * SIPE must call serv_got_joined_chat() on join response
119 * reject_chat(gc, params (GHashTable *)) NOT IMPLEMENTED
120 * get_chat_name(params (GHashTable *)) NOT IMPLEMENTED
122 * chat_invite(gc, chat ID,...)
123 * invite a user to a join a chat
125 * chat_leave(gc, chat ID)
126 * request to leave a channel, also called on conversation destroy
127 * SIPE must call serv_got_chat_left() immediately!
129 * chat_whisper(gc, chat ID, ...) NOT IMPLEMENTED
131 * chat_send(gc, chat ID, ...)
132 * send a message to the channel
134 * set_chat_topic(gc, chat ID, ...) NOT IMPLEMENTED
135 * set channel topic [@TODO: for Group Chat]
138 * struct sipe_chat_session
139 * Same life span as PurpleConversation
140 * Pointer stored under key "sipe" in PurpleConversation->data
141 * Contains information private to core to identify chat session on server
143 * If connection is closed and THEN the conversation, then libpurple will
144 * not call chat_leave() and this will be a dangling data structure! Core
145 * must take care to release them at unload.
147 * HAS: backend_session (gpointer) -> PurpleConversation
149 * struct sipe_backend_private
151 * HAS: rejoin_chats (GList *)
152 * created on login() for existing chats
153 * initiate re-join calls to core (sipe_backend_chat_rejoin_all)
156 #define SIPE_PURPLE_KEY_CHAT_SESSION "sipe"
158 static struct sipe_chat_session *sipe_purple_chat_get_session(PurpleConversation *conv)
160 return(
161 #if PURPLE_VERSION_CHECK(3,0,0)
162 g_object_get_data(G_OBJECT(conv),
163 #else
164 purple_conversation_get_data(conv,
165 #endif
166 SIPE_PURPLE_KEY_CHAT_SESSION));
169 static struct sipe_chat_session *sipe_purple_chat_find(PurpleConnection *gc,
170 int id)
172 PurpleConversation *conv = (PurpleConversation *) purple_conversations_find_chat(gc, id);
174 if (!conv) {
175 SIPE_DEBUG_ERROR("sipe_purple_chat_find: can't find chat with ID %d?!?",
176 id);
177 return NULL;
180 return sipe_purple_chat_get_session(conv);
183 void sipe_purple_chat_setup_rejoin(struct sipe_backend_private *purple_private)
185 GList *entry = purple_conversations_get_chats();
187 while (entry) {
188 PurpleConversation *conv = entry->data;
189 if (purple_conversation_get_connection(conv) == purple_private->gc)
190 purple_private->rejoin_chats = g_list_prepend(purple_private->rejoin_chats,
191 sipe_purple_chat_get_session(conv));
192 entry = entry->next;
196 void sipe_purple_chat_destroy_rejoin(struct sipe_backend_private *purple_private)
198 g_list_free(purple_private->rejoin_chats);
199 purple_private->rejoin_chats = NULL;
202 void sipe_purple_chat_invite(PurpleConnection *gc, int id,
203 SIPE_UNUSED_PARAMETER const char *message,
204 const char *name)
206 struct sipe_chat_session *session = sipe_purple_chat_find(gc, id);
207 if (!session) return;
209 sipe_core_chat_invite(PURPLE_GC_TO_SIPE_CORE_PUBLIC, session, name);
212 void sipe_purple_chat_leave(PurpleConnection *gc, int id)
214 struct sipe_chat_session *session = sipe_purple_chat_find(gc, id);
215 if (!session) return;
217 sipe_core_chat_leave(PURPLE_GC_TO_SIPE_CORE_PUBLIC, session);
220 int sipe_purple_chat_send(PurpleConnection *gc,
221 int id,
222 const char *what,
223 SIPE_UNUSED_PARAMETER PurpleMessageFlags flags)
225 struct sipe_chat_session *session = sipe_purple_chat_find(gc, id);
226 if (!session) return -ENOTCONN;
227 sipe_core_chat_send(PURPLE_GC_TO_SIPE_CORE_PUBLIC, session, what);
228 return 1;
231 static void sipe_purple_chat_menu_unlock_cb(SIPE_UNUSED_PARAMETER PurpleChat *chat,
232 PurpleConversation *conv)
234 struct sipe_core_public *sipe_public = PURPLE_CONV_TO_SIPE_CORE_PUBLIC;
235 struct sipe_chat_session *chat_session = sipe_purple_chat_get_session(conv);
236 SIPE_DEBUG_INFO("sipe_purple_chat_menu_lock_cb: %p %p", conv, chat_session);
237 sipe_core_chat_modify_lock(sipe_public, chat_session, FALSE);
240 static void sipe_purple_chat_menu_lock_cb(SIPE_UNUSED_PARAMETER PurpleChat *chat,
241 PurpleConversation *conv)
243 struct sipe_core_public *sipe_public = PURPLE_CONV_TO_SIPE_CORE_PUBLIC;
244 struct sipe_chat_session *chat_session = sipe_purple_chat_get_session(conv);
245 SIPE_DEBUG_INFO("sipe_purple_chat_menu_lock_cb: %p %p", conv, chat_session);
246 sipe_core_chat_modify_lock(sipe_public, chat_session, TRUE);
249 #ifdef HAVE_VV
251 static void sipe_purple_chat_menu_join_call_cb(SIPE_UNUSED_PARAMETER PurpleChat *chat,
252 PurpleConversation *conv)
254 struct sipe_core_public *sipe_public = PURPLE_CONV_TO_SIPE_CORE_PUBLIC;
255 struct sipe_chat_session *chat_session = sipe_purple_chat_get_session(conv);
256 SIPE_DEBUG_INFO("sipe_purple_chat_join_call_cb: %p %p", conv, chat_session);
257 sipe_core_media_connect_conference(sipe_public, chat_session);
260 #endif
262 GList *
263 sipe_purple_chat_menu(PurpleChat *chat)
265 PurpleConversation *conv = g_hash_table_lookup(purple_chat_get_components(chat),
266 SIPE_PURPLE_COMPONENT_KEY_CONVERSATION);
267 GList *menu = NULL;
269 if (conv) {
270 PurpleMenuAction *act = NULL;
272 SIPE_DEBUG_INFO("sipe_purple_chat_menu: %p", conv);
274 switch (sipe_core_chat_lock_status(PURPLE_CONV_TO_SIPE_CORE_PUBLIC,
275 sipe_purple_chat_get_session(conv))) {
276 case SIPE_CHAT_LOCK_STATUS_UNLOCKED:
277 act = purple_menu_action_new(_("Lock"),
278 PURPLE_CALLBACK(sipe_purple_chat_menu_lock_cb),
279 conv, NULL);
280 break;
281 case SIPE_CHAT_LOCK_STATUS_LOCKED:
282 act = purple_menu_action_new(_("Unlock"),
283 PURPLE_CALLBACK(sipe_purple_chat_menu_unlock_cb),
284 conv, NULL);
285 break;
286 default:
287 /* Not allowed */
288 break;
291 if (act)
292 menu = g_list_prepend(menu, act);
293 #ifdef HAVE_VV
294 if (!sipe_core_media_in_call(PURPLE_CONV_TO_SIPE_CORE_PUBLIC)) {
295 act = NULL;
296 act = purple_menu_action_new(_("Join conference call"),
297 PURPLE_CALLBACK(sipe_purple_chat_menu_join_call_cb),
298 conv, NULL);
299 if (act)
300 menu = g_list_prepend(menu, act);
302 #endif
305 return menu;
308 void sipe_backend_chat_session_destroy(SIPE_UNUSED_PARAMETER struct sipe_backend_chat_session *session)
310 /* Nothing to do here */
313 void sipe_backend_chat_add(struct sipe_backend_chat_session *backend_session,
314 const gchar *uri,
315 gboolean is_new)
317 purple_chat_conversation_add_user(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
318 uri,
319 NULL,
320 PURPLE_CHAT_USER_NONE,
321 is_new);
324 void sipe_backend_chat_close(struct sipe_backend_chat_session *backend_session)
326 purple_chat_conversation_clear_users(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session));
329 static int sipe_purple_chat_id(PurpleConnection *gc)
332 * A non-volatile ID counter.
333 * Should survive connection drop & reconnect.
335 static int chat_id = 0;
337 /* Find next free ID */
338 do {
339 if (++chat_id < 0) chat_id = 0;
340 } while (purple_conversations_find_chat(gc, chat_id) != NULL)
342 return chat_id;
345 struct sipe_backend_chat_session *sipe_backend_chat_create(struct sipe_core_public *sipe_public,
346 struct sipe_chat_session *session,
347 const gchar *title,
348 const gchar *nick)
350 struct sipe_backend_private *purple_private = sipe_public->backend_private;
351 #if PURPLE_VERSION_CHECK(3,0,0)
352 PurpleChatConversation *conv =
353 #else
354 PurpleConversation *conv =
355 #endif
356 purple_serv_got_joined_chat(purple_private->gc,
357 sipe_purple_chat_id(purple_private->gc),
358 title);
359 #if PURPLE_VERSION_CHECK(3,0,0)
360 g_object_set_data(G_OBJECT(conv),
361 #else
362 purple_conversation_set_data(conv,
363 #endif
364 SIPE_PURPLE_KEY_CHAT_SESSION,
365 session);
366 purple_chat_conversation_set_nick(PURPLE_CONV_CHAT(conv), nick);
367 return((struct sipe_backend_chat_session *) conv);
370 gboolean sipe_backend_chat_find(struct sipe_backend_chat_session *backend_session,
371 const gchar *uri)
373 #if PURPLE_VERSION_CHECK(3,0,0)
374 return(purple_chat_conversation_find_user(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
375 uri) != NULL);
376 #else
377 return purple_conv_chat_find_user(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
378 uri);
379 #endif
382 gboolean sipe_backend_chat_is_operator(struct sipe_backend_chat_session *backend_session,
383 const gchar *uri)
385 #if PURPLE_VERSION_CHECK(3,0,0)
386 return((purple_chat_user_get_flags(
387 purple_chat_conversation_find_user(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
388 uri))
389 & PURPLE_CHAT_USER_OP)
390 == PURPLE_CHAT_USER_OP);
391 #else
392 return (purple_conv_chat_user_get_flags(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
393 uri) & PURPLE_CBFLAGS_OP)
394 == PURPLE_CBFLAGS_OP;
395 #endif
398 void sipe_backend_chat_message(struct sipe_core_public *sipe_public,
399 struct sipe_backend_chat_session *backend_session,
400 const gchar *from,
401 time_t when,
402 const gchar *html)
404 struct sipe_backend_private *purple_private = sipe_public->backend_private;
405 purple_serv_got_chat_in(purple_private->gc,
406 purple_chat_conversation_get_id(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session)),
407 from,
408 PURPLE_MESSAGE_RECV,
409 html,
410 when ? when : time(NULL));
413 void sipe_backend_chat_operator(struct sipe_backend_chat_session *backend_session,
414 const gchar *uri)
416 #if PURPLE_VERSION_CHECK(3,0,0)
417 purple_chat_user_set_flags(
418 purple_chat_conversation_find_user(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
419 uri),
420 PURPLE_CHAT_USER_NONE | PURPLE_CHAT_USER_OP);
421 #else
422 purple_conv_chat_user_set_flags(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
423 uri,
424 PURPLE_CBFLAGS_NONE | PURPLE_CBFLAGS_OP);
425 #endif
428 void sipe_backend_chat_rejoin(struct sipe_core_public *sipe_public,
429 struct sipe_backend_chat_session *backend_session,
430 const gchar *nick,
431 const gchar *title)
433 struct sipe_backend_private *purple_private = sipe_public->backend_private;
434 #if PURPLE_VERSION_CHECK(3,0,0)
435 PurpleChatConversation *chat = BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session);
436 PurpleChatConversation *new;
437 #else
438 PurpleConvChat *chat = BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session);
439 PurpleConversation *new;
440 #endif
443 * As the chat is marked as "left", serv_got_joined_chat() will
444 * do a "rejoin cleanup" and return the same conversation.
446 new = purple_serv_got_joined_chat(purple_private->gc,
447 purple_chat_conversation_get_id(chat),
448 title);
449 SIPE_DEBUG_INFO("sipe_backend_chat_rejoin: old %p (%p) == new %p (%p)",
450 backend_session, chat,
451 new, PURPLE_CONV_CHAT(new));
452 purple_chat_conversation_set_nick(chat, nick);
456 * Connection re-established: tell core what chats need to be rejoined
458 void sipe_backend_chat_rejoin_all(struct sipe_core_public *sipe_public)
460 struct sipe_backend_private *purple_private = sipe_public->backend_private;
461 GList *entry = purple_private->rejoin_chats;
463 while (entry) {
464 sipe_core_chat_rejoin(sipe_public, entry->data);
465 entry = entry->next;
467 sipe_purple_chat_destroy_rejoin(purple_private);
470 void sipe_backend_chat_remove(struct sipe_backend_chat_session *backend_session,
471 const gchar *uri)
473 purple_chat_conversation_remove_user(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
474 uri,
475 NULL /* reason */);
478 void sipe_backend_chat_show(struct sipe_backend_chat_session *backend_session)
480 /* Bring existing purple chat to the front */
481 /* @TODO: This seems to the trick, but is it the correct way? */
482 purple_conversation_update((PurpleConversation *) backend_session,
483 PURPLE_CONVERSATION_UPDATE_TOPIC);
486 void sipe_backend_chat_topic(struct sipe_backend_chat_session *backend_session,
487 const gchar *topic)
489 purple_chat_conversation_set_topic(BACKEND_SESSION_TO_PURPLE_CONV_CHAT(backend_session),
490 NULL,
491 topic);
495 Local Variables:
496 mode: c
497 c-file-style: "bsd"
498 indent-tabs-mode: t
499 tab-width: 8
500 End: