purple: restore copying of username when login is empty
[siplcs.git] / src / purple / purple-plugin.c
blob29284e388dbd75f5988fad5f2aef59f304efa820
1 /**
2 * @file purple-plugin.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <string.h>
28 #include <time.h>
30 #include <glib.h>
32 #include "sipe-common.h"
34 /* Flag needed for correct version of PURPLE_INIT_PLUGIN() */
35 #ifndef PURPLE_PLUGINS
36 #define PURPLE_PLUGINS
37 #endif
39 /* for LOCALEDIR
40 * as it's determined on runtime, as Pidgin installation can be anywhere.
42 #ifdef _WIN32
43 #include "win32/win32dep.h"
44 #endif
46 #include "accountopt.h"
47 #include "blist.h"
48 #include "connection.h"
49 #include "core.h"
50 #include "dnssrv.h"
51 #ifdef HAVE_VV
52 #include "media.h"
53 #endif
54 #include "prpl.h"
55 #include "plugin.h"
56 #include "request.h"
57 #include "status.h"
59 * NOTE: Currently PURPLE_VERSION_CHECK(2,y,z) returns FALSE for libpurple >= 3.0.0.
60 * See also <http://developer.pidgin.im/ticket/14551>
62 * As a workaround an additional PURPLE_VERSION_CHECK(3,0,0) needs to be added.
64 #include "version.h"
66 #include "sipe-backend.h"
67 #include "sipe-core.h"
68 #include "sipe-nls.h"
70 #define _PurpleMessageFlags PurpleMessageFlags
71 #include "purple-private.h"
73 /* Backward compatibility when compiling against 2.4.x API */
74 #if !PURPLE_VERSION_CHECK(2,5,0) && !PURPLE_VERSION_CHECK(3,0,0)
75 #define PURPLE_CONNECTION_ALLOW_CUSTOM_SMILEY 0x0100
76 #endif
79 * NOTE: this flag means two things:
81 * - is Single Sign-On supported, and
82 * - is Kerberos supported
84 #if defined(HAVE_LIBKRB5) || defined(HAVE_SSPI)
85 #define PURPLE_SIPE_SSO_AND_KERBEROS 1
86 #else
87 #define PURPLE_SIPE_SSO_AND_KERBEROS 0
88 #endif
90 /* Sipe core activity <-> Purple status mapping */
91 static const gchar * const activity_to_purple_map[SIPE_ACTIVITY_NUM_TYPES] = {
92 /* SIPE_ACTIVITY_UNSET */ "unset", /* == purple_primitive_get_id_from_type(PURPLE_STATUS_UNSET) */
93 /* SIPE_ACTIVITY_AVAILABLE */ "available", /* == purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE) */
94 /* SIPE_ACTIVITY_ONLINE */ "online",
95 /* SIPE_ACTIVITY_INACTIVE */ "idle",
96 /* SIPE_ACTIVITY_BUSY */ "busy",
97 /* SIPE_ACTIVITY_BUSYIDLE */ "busyidle",
98 /* SIPE_ACTIVITY_DND */ "do-not-disturb",
99 /* SIPE_ACTIVITY_BRB */ "be-right-back",
100 /* SIPE_ACTIVITY_AWAY */ "away", /* == purple_primitive_get_id_from_type(PURPLE_STATUS_AWAY) */
101 /* SIPE_ACTIVITY_LUNCH */ "out-to-lunch",
102 /* SIPE_ACTIVITY_INVISIBLE */ "invisible", /* == purple_primitive_get_id_from_type(PURPLE_STATUS_INVISIBLE) */
103 /* SIPE_ACTIVITY_OFFLINE */ "offline", /* == purple_primitive_get_id_from_type(PURPLE_STATUS_OFFLINE) */
104 /* SIPE_ACTIVITY_ON_PHONE */ "on-the-phone",
105 /* SIPE_ACTIVITY_IN_CONF */ "in-a-conference",
106 /* SIPE_ACTIVITY_IN_MEETING */ "in-a-meeting",
107 /* SIPE_ACTIVITY_OOF */ "out-of-office",
108 /* SIPE_ACTIVITY_URGENT_ONLY */ "urgent-interruptions-only",
111 GHashTable *purple_token_map;
113 static void sipe_purple_activity_init(void)
115 guint index;
117 purple_token_map = g_hash_table_new(g_str_hash, g_str_equal);
118 for (index = SIPE_ACTIVITY_UNSET;
119 index < SIPE_ACTIVITY_NUM_TYPES;
120 index++) {
121 g_hash_table_insert(purple_token_map,
122 (gchar *) activity_to_purple_map[index],
123 GUINT_TO_POINTER(index));
127 static void sipe_purple_activity_shutdown(void)
129 g_hash_table_destroy(purple_token_map);
132 const gchar *sipe_purple_activity_to_token(guint type)
134 return(activity_to_purple_map[type]);
137 guint sipe_purple_token_to_activity(const gchar *token)
139 return(GPOINTER_TO_UINT(g_hash_table_lookup(purple_token_map, token)));
142 gchar *sipe_backend_version(void)
144 return(g_strdup_printf("Purple/%s", purple_core_get_version()));
147 /* PurplePluginProtocolInfo function calls & data structure */
148 static const char *sipe_list_icon(SIPE_UNUSED_PARAMETER PurpleAccount *a,
149 SIPE_UNUSED_PARAMETER PurpleBuddy *b)
151 return "sipe";
154 static gchar *sipe_purple_status_text(PurpleBuddy *buddy)
156 const PurpleStatus *status = purple_presence_get_active_status(purple_buddy_get_presence(buddy));
157 return sipe_core_buddy_status(PURPLE_BUDDY_TO_SIPE_CORE_PUBLIC,
158 buddy->name,
159 sipe_purple_token_to_activity(purple_status_get_id(status)),
160 purple_status_get_name(status));
163 static void sipe_purple_tooltip_text(PurpleBuddy *buddy,
164 PurpleNotifyUserInfo *user_info,
165 SIPE_UNUSED_PARAMETER gboolean full)
167 const PurplePresence *presence = purple_buddy_get_presence(buddy);
168 sipe_core_buddy_tooltip_info(PURPLE_BUDDY_TO_SIPE_CORE_PUBLIC,
169 buddy->name,
170 purple_status_get_name(purple_presence_get_active_status(presence)),
171 purple_presence_is_online(presence),
172 (struct sipe_backend_buddy_tooltip *) user_info);
175 static GList *sipe_purple_status_types(SIPE_UNUSED_PARAMETER PurpleAccount *acc)
177 PurpleStatusType *type;
178 GList *types = NULL;
180 /* Macros to reduce code repetition.
181 Translators: noun */
182 #define SIPE_ADD_STATUS(prim,id,name,user) type = purple_status_type_new_with_attrs( \
183 prim, id, name, \
184 TRUE, user, FALSE, \
185 SIPE_PURPLE_STATUS_ATTR_ID_MESSAGE, _("Message"), purple_value_new(PURPLE_TYPE_STRING), \
186 NULL); \
187 types = g_list_append(types, type);
189 /* Online */
190 SIPE_ADD_STATUS(PURPLE_STATUS_AVAILABLE,
191 NULL,
192 NULL,
193 TRUE);
195 /* Busy */
196 SIPE_ADD_STATUS(PURPLE_STATUS_UNAVAILABLE,
197 sipe_purple_activity_to_token(SIPE_ACTIVITY_BUSY),
198 sipe_core_activity_description(SIPE_ACTIVITY_BUSY),
199 TRUE);
201 /* Do Not Disturb */
202 SIPE_ADD_STATUS(PURPLE_STATUS_UNAVAILABLE,
203 sipe_purple_activity_to_token(SIPE_ACTIVITY_DND),
204 NULL,
205 TRUE);
207 /* In a call */
208 SIPE_ADD_STATUS(PURPLE_STATUS_UNAVAILABLE,
209 sipe_purple_activity_to_token(SIPE_ACTIVITY_ON_PHONE),
210 sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE),
211 FALSE);
213 /* In a conference call */
214 SIPE_ADD_STATUS(PURPLE_STATUS_UNAVAILABLE,
215 sipe_purple_activity_to_token(SIPE_ACTIVITY_IN_CONF),
216 sipe_core_activity_description(SIPE_ACTIVITY_IN_CONF),
217 FALSE);
219 /* Away */
220 /* Goes first in the list as
221 * purple picks the first status with the AWAY type
222 * for idle.
224 SIPE_ADD_STATUS(PURPLE_STATUS_AWAY,
225 NULL,
226 NULL,
227 TRUE);
229 /* Be Right Back */
230 SIPE_ADD_STATUS(PURPLE_STATUS_AWAY,
231 sipe_purple_activity_to_token(SIPE_ACTIVITY_BRB),
232 sipe_core_activity_description(SIPE_ACTIVITY_BRB),
233 TRUE);
235 /* Appear Offline */
236 SIPE_ADD_STATUS(PURPLE_STATUS_INVISIBLE,
237 NULL,
238 NULL,
239 TRUE);
241 /* Offline */
242 type = purple_status_type_new(PURPLE_STATUS_OFFLINE,
243 NULL,
244 NULL,
245 TRUE);
246 types = g_list_append(types, type);
248 return types;
251 static GList *sipe_purple_blist_node_menu(PurpleBlistNode *node)
253 if(PURPLE_BLIST_NODE_IS_BUDDY(node)) {
254 return sipe_purple_buddy_menu((PurpleBuddy *) node);
255 } else if(PURPLE_BLIST_NODE_IS_CHAT(node)) {
256 return sipe_purple_chat_menu((PurpleChat *)node);
257 } else {
258 return NULL;
262 static guint get_authentication_type(PurpleAccount *account)
264 const gchar *auth = purple_account_get_string(account, "authentication", "ntlm");
266 /* map option list to type - default is NTLM */
267 guint authentication_type = SIPE_AUTHENTICATION_TYPE_NTLM;
268 #if PURPLE_SIPE_SSO_AND_KERBEROS
269 if (sipe_strequal(auth, "krb5")) {
270 authentication_type = SIPE_AUTHENTICATION_TYPE_KERBEROS;
272 #endif
273 if (sipe_strequal(auth, "tls-dsk")) {
274 authentication_type = SIPE_AUTHENTICATION_TYPE_TLS_DSK;
277 return(authentication_type);
280 static gboolean get_sso_flag(PurpleAccount *account)
282 #if PURPLE_SIPE_SSO_AND_KERBEROS
284 * NOTE: the default must be *OFF*, i.e. it is up to the user to tell
285 * SIPE that it is OK to use Single Sign-On or not.
287 return(purple_account_get_bool(account, "sso", FALSE));
288 #else
289 (void) account; /* keep compiler happy */
290 return(FALSE);
291 #endif
294 static void connect_to_core(PurpleConnection *gc,
295 PurpleAccount *account,
296 const gchar *password)
298 const gchar *username = purple_account_get_username(account);
299 const gchar *email = purple_account_get_string(account, "email", NULL);
300 const gchar *email_url = purple_account_get_string(account, "email_url", NULL);
301 const gchar *transport = purple_account_get_string(account, "transport", "auto");
302 struct sipe_core_public *sipe_public;
303 gchar **username_split;
304 gchar *login_domain = NULL;
305 gchar *login_account = NULL;
306 const gchar *errmsg;
307 guint transport_type;
308 struct sipe_backend_private *purple_private;
309 gboolean sso = get_sso_flag(account);
311 /* username format: <username>,[<optional login>] */
312 SIPE_DEBUG_INFO("sipe_purple_login: username '%s'", username);
313 username_split = g_strsplit(username, ",", 2);
315 /* login name is ignored when SSO has been selected */
316 if (!sso) {
317 /* login name specified? */
318 if (username_split[1] && strlen(username_split[1])) {
319 /* Allowed domain-account separators are / or \ */
320 gchar **domain_user = g_strsplit_set(username_split[1], "/\\", 2);
321 gboolean has_domain = domain_user[1] != NULL;
322 SIPE_DEBUG_INFO("sipe_purple_login: login '%s'", username_split[1]);
323 login_domain = has_domain ? g_strdup(domain_user[0]) : NULL;
324 login_account = g_strdup(domain_user[has_domain ? 1 : 0]);
325 SIPE_DEBUG_INFO("sipe_purple_login: auth domain '%s' user '%s'",
326 login_domain ? login_domain : "",
327 login_account);
328 g_strfreev(domain_user);
329 } else {
330 /* No -> duplicate username */
331 login_account = g_strdup(username_split[0]);
335 sipe_public = sipe_core_allocate(username_split[0],
336 sso,
337 login_domain, login_account,
338 password,
339 email,
340 email_url,
341 &errmsg);
342 g_free(login_domain);
343 g_free(login_account);
344 g_strfreev(username_split);
346 if (!sipe_public) {
347 #if PURPLE_VERSION_CHECK(3,0,0)
348 purple_connection_error(
349 #else
350 purple_connection_error_reason(
351 #endif
353 PURPLE_CONNECTION_ERROR_INVALID_USERNAME,
354 errmsg);
355 return;
358 sipe_public->backend_private = purple_private = g_new0(struct sipe_backend_private, 1);
359 purple_private->public = sipe_public;
360 purple_private->gc = gc;
361 purple_private->account = account;
363 sipe_purple_chat_setup_rejoin(purple_private);
365 gc->proto_data = sipe_public;
366 gc->flags |= PURPLE_CONNECTION_HTML | PURPLE_CONNECTION_FORMATTING_WBFO | PURPLE_CONNECTION_NO_BGCOLOR |
367 PURPLE_CONNECTION_NO_FONTSIZE | PURPLE_CONNECTION_NO_URLDESC | PURPLE_CONNECTION_ALLOW_CUSTOM_SMILEY;
368 purple_connection_set_display_name(gc, sipe_public->sip_name);
369 purple_connection_update_progress(gc, _("Connecting"), 1, 2);
371 username_split = g_strsplit(purple_account_get_string(account, "server", ""), ":", 2);
372 if (sipe_strequal(transport, "auto")) {
373 transport_type = (username_split[0] == NULL) ?
374 SIPE_TRANSPORT_AUTO : SIPE_TRANSPORT_TLS;
375 } else if (sipe_strequal(transport, "tls")) {
376 transport_type = SIPE_TRANSPORT_TLS;
377 } else {
378 transport_type = SIPE_TRANSPORT_TCP;
380 sipe_core_transport_sip_connect(sipe_public,
381 transport_type,
382 get_authentication_type(account),
383 username_split[0],
384 username_split[0] ? username_split[1] : NULL);
385 g_strfreev(username_split);
388 static void password_required_cb(PurpleConnection *gc,
389 SIPE_UNUSED_PARAMETER PurpleRequestFields *fields)
391 if (!PURPLE_CONNECTION_IS_VALID(gc))
392 return;
394 #if PURPLE_VERSION_CHECK(3,0,0)
395 purple_connection_error(
396 #else
397 purple_connection_error_reason(
398 #endif
400 PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED,
401 _("Password required"));
404 static void password_ok_cb(PurpleConnection *gc,
405 PurpleRequestFields *fields)
407 const gchar *password;
409 if (!PURPLE_CONNECTION_IS_VALID(gc))
410 return;
412 password = purple_request_fields_get_string(fields, "password");
414 if (password && strlen(password)) {
415 PurpleAccount *account = purple_connection_get_account(gc);
417 if (purple_request_fields_get_bool(fields, "remember"))
418 purple_account_set_remember_password(account, TRUE);
419 purple_account_set_password(account, password);
421 /* Now we have a password and we can connect */
422 connect_to_core(gc, account, password);
424 } else
425 /* reject an empty password */
426 password_required_cb(gc, fields);
429 static void sipe_purple_login(PurpleAccount *account)
431 PurpleConnection *gc = purple_account_get_connection(account);
432 const gchar *password = purple_connection_get_password(gc);
434 /* Password required? */
435 if (sipe_core_transport_sip_requires_password(get_authentication_type(account),
436 get_sso_flag(account)) &&
437 (!password || !strlen(password)))
438 /* No password set - request one from user */
439 purple_account_request_password(account,
440 G_CALLBACK(password_ok_cb),
441 G_CALLBACK(password_required_cb),
442 gc);
443 else
444 /* No password required or saved password - connect now */
445 connect_to_core(gc, account, password);
449 static void sipe_purple_close(PurpleConnection *gc)
451 struct sipe_core_public *sipe_public = PURPLE_GC_TO_SIPE_CORE_PUBLIC;
453 if (sipe_public) {
454 struct sipe_backend_private *purple_private = sipe_public->backend_private;
456 sipe_core_deallocate(sipe_public);
458 if (purple_private->roomlist_map)
459 g_hash_table_destroy(purple_private->roomlist_map);
460 sipe_purple_chat_destroy_rejoin(purple_private);
461 g_free(purple_private);
462 gc->proto_data = NULL;
466 static int sipe_purple_send_im(PurpleConnection *gc,
467 const char *who,
468 const char *what,
469 SIPE_UNUSED_PARAMETER PurpleMessageFlags flags)
471 sipe_core_im_send(PURPLE_GC_TO_SIPE_CORE_PUBLIC, who, what);
472 return 1;
475 #define SIPE_TYPING_SEND_TIMEOUT 4
477 static unsigned int sipe_purple_send_typing(PurpleConnection *gc,
478 const char *who,
479 PurpleTypingState state)
481 if (state == PURPLE_NOT_TYPING)
482 return 0;
484 sipe_core_user_feedback_typing(PURPLE_GC_TO_SIPE_CORE_PUBLIC,
485 who);
487 return SIPE_TYPING_SEND_TIMEOUT;
490 static void sipe_purple_get_info(PurpleConnection *gc, const char *who)
492 sipe_core_buddy_get_info(PURPLE_GC_TO_SIPE_CORE_PUBLIC,
493 who);
496 static void sipe_purple_add_permit(PurpleConnection *gc, const char *name)
498 sipe_core_contact_allow_deny(PURPLE_GC_TO_SIPE_CORE_PUBLIC, name, TRUE);
501 static void sipe_purple_add_deny(PurpleConnection *gc, const char *name)
503 sipe_core_contact_allow_deny(PURPLE_GC_TO_SIPE_CORE_PUBLIC, name, FALSE);
506 static void sipe_purple_keep_alive(PurpleConnection *gc)
508 struct sipe_core_public *sipe_public = PURPLE_GC_TO_SIPE_CORE_PUBLIC;
509 struct sipe_backend_private *purple_private = sipe_public->backend_private;
510 time_t now = time(NULL);
512 if ((sipe_public->keepalive_timeout > 0) &&
513 ((guint) (now - purple_private->last_keepalive) >= sipe_public->keepalive_timeout) &&
514 ((guint) (now - gc->last_received) >= sipe_public->keepalive_timeout)
516 sipe_core_transport_sip_keepalive(sipe_public);
517 purple_private->last_keepalive = now;
521 static void sipe_purple_alias_buddy(PurpleConnection *gc, const char *name,
522 const char *alias)
524 sipe_core_group_set_alias(PURPLE_GC_TO_SIPE_CORE_PUBLIC, name, alias);
527 static void sipe_purple_group_rename(PurpleConnection *gc,
528 const char *old_name,
529 PurpleGroup *group,
530 SIPE_UNUSED_PARAMETER GList *moved_buddies)
532 sipe_core_group_rename(PURPLE_GC_TO_SIPE_CORE_PUBLIC, old_name, group->name);
535 static void sipe_purple_convo_closed(PurpleConnection *gc,
536 const char *who)
538 sipe_core_im_close(PURPLE_GC_TO_SIPE_CORE_PUBLIC, who);
541 static void sipe_purple_group_remove(PurpleConnection *gc, PurpleGroup *group)
543 sipe_core_group_remove(PURPLE_GC_TO_SIPE_CORE_PUBLIC, group->name);
546 #if PURPLE_VERSION_CHECK(2,5,0) || PURPLE_VERSION_CHECK(3,0,0)
547 static GHashTable *
548 sipe_purple_get_account_text_table(SIPE_UNUSED_PARAMETER PurpleAccount *account)
550 GHashTable *table;
551 table = g_hash_table_new(g_str_hash, g_str_equal);
552 g_hash_table_insert(table, "login_label", (gpointer)_("user@company.com"));
553 return table;
556 #if PURPLE_VERSION_CHECK(2,6,0) || PURPLE_VERSION_CHECK(3,0,0)
557 #ifdef HAVE_VV
559 static void
560 sipe_purple_sigusr1_handler(SIPE_UNUSED_PARAMETER int signum)
562 capture_pipeline("PURPLE_SIPE_PIPELINE");
565 static gboolean sipe_purple_initiate_media(PurpleAccount *account, const char *who,
566 SIPE_UNUSED_PARAMETER PurpleMediaSessionType type)
568 sipe_core_media_initiate_call(PURPLE_ACCOUNT_TO_SIPE_CORE_PUBLIC,
569 who,
570 (type & PURPLE_MEDIA_VIDEO));
571 return TRUE;
574 static PurpleMediaCaps sipe_purple_get_media_caps(SIPE_UNUSED_PARAMETER PurpleAccount *account,
575 SIPE_UNUSED_PARAMETER const char *who)
577 return PURPLE_MEDIA_CAPS_AUDIO
578 | PURPLE_MEDIA_CAPS_AUDIO_VIDEO
579 | PURPLE_MEDIA_CAPS_MODIFY_SESSION;
581 #endif
582 #endif
583 #endif
586 * Simplistic source upward compatibility path for newer libpurple APIs
588 * Usually we compile with -Werror=missing-field-initializers if GCC supports
589 * it. But that means that the compilation of this structure can fail if the
590 * newer API has added additional plugin callbacks. For the benefit of the
591 * user we downgrade it to a warning here.
593 * Diagnostic #pragma was added in GCC 4.2.0
594 * Diagnostic push/pop was added in GCC 4.6.0
596 #ifdef __GNUC__
597 #if (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 2)
598 #if __GNUC_MINOR__ >= 6
599 #pragma GCC diagnostic push
600 #endif
601 #pragma GCC diagnostic warning "-Wmissing-field-initializers"
602 #endif
603 #endif
604 static PurplePluginProtocolInfo sipe_prpl_info =
606 #if PURPLE_VERSION_CHECK(3,0,0)
607 sizeof(PurplePluginProtocolInfo), /* struct_size */
608 #endif
609 OPT_PROTO_CHAT_TOPIC |
610 OPT_PROTO_PASSWORD_OPTIONAL,
611 NULL, /* user_splits */
612 NULL, /* protocol_options */
613 NO_BUDDY_ICONS, /* icon_spec */
614 sipe_list_icon, /* list_icon */
615 NULL, /* list_emblems */
616 sipe_purple_status_text, /* status_text */
617 sipe_purple_tooltip_text, /* tooltip_text */ // add custom info to contact tooltip
618 sipe_purple_status_types, /* away_states */
619 sipe_purple_blist_node_menu, /* blist_node_menu */
620 sipe_purple_chat_info, /* chat_info */
621 sipe_purple_chat_info_defaults, /* chat_info_defaults */
622 sipe_purple_login, /* login */
623 sipe_purple_close, /* close */
624 sipe_purple_send_im, /* send_im */
625 NULL, /* set_info */ // TODO maybe
626 sipe_purple_send_typing, /* send_typing */
627 sipe_purple_get_info, /* get_info */
628 sipe_purple_set_status, /* set_status */
629 sipe_purple_set_idle, /* set_idle */
630 NULL, /* change_passwd */
631 sipe_purple_add_buddy, /* add_buddy */
632 NULL, /* add_buddies */
633 sipe_purple_remove_buddy, /* remove_buddy */
634 NULL, /* remove_buddies */
635 sipe_purple_add_permit, /* add_permit */
636 sipe_purple_add_deny, /* add_deny */
637 sipe_purple_add_deny, /* rem_permit */
638 sipe_purple_add_permit, /* rem_deny */
639 NULL, /* set_permit_deny */
640 sipe_purple_chat_join, /* join_chat */
641 NULL, /* reject_chat */
642 NULL, /* get_chat_name */
643 sipe_purple_chat_invite, /* chat_invite */
644 sipe_purple_chat_leave, /* chat_leave */
645 NULL, /* chat_whisper */
646 sipe_purple_chat_send, /* chat_send */
647 sipe_purple_keep_alive, /* keepalive */
648 NULL, /* register_user */
649 NULL, /* get_cb_info */ // deprecated
650 #if !PURPLE_VERSION_CHECK(3,0,0)
651 NULL, /* get_cb_away */ // deprecated
652 #endif
653 sipe_purple_alias_buddy, /* alias_buddy */
654 sipe_purple_group_buddy, /* group_buddy */
655 sipe_purple_group_rename, /* rename_group */
656 NULL, /* buddy_free */
657 sipe_purple_convo_closed, /* convo_closed */
658 purple_normalize_nocase, /* normalize */
659 NULL, /* set_buddy_icon */
660 sipe_purple_group_remove, /* remove_group */
661 NULL, /* get_cb_real_name */ // TODO?
662 NULL, /* set_chat_topic */
663 NULL, /* find_blist_chat */
664 sipe_purple_roomlist_get_list, /* roomlist_get_list */
665 sipe_purple_roomlist_cancel, /* roomlist_cancel */
666 NULL, /* roomlist_expand_category */
667 NULL, /* can_receive_file */
668 sipe_purple_ft_send_file, /* send_file */
669 sipe_purple_ft_new_xfer, /* new_xfer */
670 NULL, /* offline_message */
671 NULL, /* whiteboard_prpl_ops */
672 NULL, /* send_raw */
673 NULL, /* roomlist_room_serialize */
674 NULL, /* unregister_user */
675 NULL, /* send_attention */
676 NULL, /* get_attention_types */
677 #if !PURPLE_VERSION_CHECK(2,5,0) && !PURPLE_VERSION_CHECK(3,0,0)
678 /* Backward compatibility when compiling against 2.4.x API */
679 (void (*)(void)) /* _purple_reserved4 */
680 #endif
681 #if !PURPLE_VERSION_CHECK(3,0,0)
682 sizeof(PurplePluginProtocolInfo), /* struct_size */
683 #endif
684 #if PURPLE_VERSION_CHECK(2,5,0) || PURPLE_VERSION_CHECK(3,0,0)
685 sipe_purple_get_account_text_table, /* get_account_text_table */
686 #if PURPLE_VERSION_CHECK(2,6,0) || PURPLE_VERSION_CHECK(3,0,0)
687 #ifdef HAVE_VV
688 sipe_purple_initiate_media, /* initiate_media */
689 sipe_purple_get_media_caps, /* get_media_caps */
690 #else
691 NULL, /* initiate_media */
692 NULL, /* get_media_caps */
693 #endif
694 #if PURPLE_VERSION_CHECK(2,7,0) || PURPLE_VERSION_CHECK(3,0,0)
695 NULL, /* get_moods */
696 NULL, /* set_public_alias */
697 NULL, /* get_public_alias */
698 #if PURPLE_VERSION_CHECK(2,8,0)
699 NULL, /* add_buddy_with_invite */
700 NULL, /* add_buddies_with_invite */
701 #endif
702 #endif
703 #endif
704 #endif
706 #ifdef __GNUC__
707 #if (__GNUC__ >= 4) && (__GNUC_MINOR__ >= 6)
708 #pragma GCC diagnostic pop
709 #endif
710 #endif
711 /* Original GCC error checking restored from here on... (see above) */
713 /* PurplePluginInfo function calls & data structure */
714 static gboolean sipe_purple_plugin_load(SIPE_UNUSED_PARAMETER PurplePlugin *plugin)
716 #ifdef HAVE_VV
717 struct sigaction action;
718 memset(&action, 0, sizeof (action));
719 action.sa_handler = sipe_purple_sigusr1_handler;
720 sigaction(SIGUSR1, &action, NULL);
721 #endif
722 return TRUE;
725 static gboolean sipe_purple_plugin_unload(SIPE_UNUSED_PARAMETER PurplePlugin *plugin)
727 #ifdef HAVE_VV
728 struct sigaction action;
729 memset(&action, 0, sizeof (action));
730 action.sa_handler = SIG_DFL;
731 sigaction(SIGUSR1, &action, NULL);
732 #endif
733 return TRUE;
736 static void sipe_purple_plugin_destroy(SIPE_UNUSED_PARAMETER PurplePlugin *plugin)
738 GList *entry;
740 sipe_purple_activity_shutdown();
741 sipe_core_destroy();
743 entry = sipe_prpl_info.protocol_options;
744 while (entry) {
745 purple_account_option_destroy(entry->data);
746 entry = g_list_delete_link(entry, entry);
748 sipe_prpl_info.protocol_options = NULL;
750 entry = sipe_prpl_info.user_splits;
751 while (entry) {
752 purple_account_user_split_destroy(entry->data);
753 entry = g_list_delete_link(entry, entry);
755 sipe_prpl_info.user_splits = NULL;
758 static void sipe_purple_show_about_plugin(PurplePluginAction *action)
760 gchar *tmp = sipe_core_about();
761 purple_notify_formatted((PurpleConnection *) action->context,
762 NULL, " ", NULL, tmp, NULL, NULL);
763 g_free(tmp);
766 static void sipe_purple_find_contact_cb(PurpleConnection *gc,
767 PurpleRequestFields *fields)
769 GList *entries = purple_request_field_group_get_fields(purple_request_fields_get_groups(fields)->data);
770 const gchar *given_name = NULL;
771 const gchar *surname = NULL;
772 const gchar *email = NULL;
773 const gchar *company = NULL;
774 const gchar *country = NULL;
776 while (entries) {
777 PurpleRequestField *field = entries->data;
778 const char *id = purple_request_field_get_id(field);
779 const char *value = purple_request_field_string_get_value(field);
781 SIPE_DEBUG_INFO("sipe_purple_find_contact_cb: %s = '%s'", id, value ? value : "");
783 if (value) {
784 if (strcmp(id, "given") == 0) {
785 given_name = value;
786 } else if (strcmp(id, "surname") == 0) {
787 surname = value;
788 } else if (strcmp(id, "email") == 0) {
789 email = value;
790 } else if (strcmp(id, "company") == 0) {
791 company = value;
792 } else if (strcmp(id, "country") == 0) {
793 country = value;
797 entries = g_list_next(entries);
800 sipe_core_buddy_search(PURPLE_GC_TO_SIPE_CORE_PUBLIC,
801 NULL,
802 given_name,
803 surname,
804 email,
805 company,
806 country);
809 static void sipe_purple_show_find_contact(PurplePluginAction *action)
811 PurpleConnection *gc = (PurpleConnection *) action->context;
812 PurpleRequestFields *fields;
813 PurpleRequestFieldGroup *group;
814 PurpleRequestField *field;
816 fields = purple_request_fields_new();
817 group = purple_request_field_group_new(NULL);
818 purple_request_fields_add_group(fields, group);
820 field = purple_request_field_string_new("given", _("First name"), NULL, FALSE);
821 purple_request_field_group_add_field(group, field);
822 field = purple_request_field_string_new("surname", _("Last name"), NULL, FALSE);
823 purple_request_field_group_add_field(group, field);
824 field = purple_request_field_string_new("email", _("Email"), NULL, FALSE);
825 purple_request_field_group_add_field(group, field);
826 field = purple_request_field_string_new("company", _("Company"), NULL, FALSE);
827 purple_request_field_group_add_field(group, field);
828 field = purple_request_field_string_new("country", _("Country"), NULL, FALSE);
829 purple_request_field_group_add_field(group, field);
831 purple_request_fields(gc,
832 _("Search"),
833 _("Search for a contact"),
834 _("Enter the information for the person you wish to find. Empty fields will be ignored."),
835 fields,
836 _("_Search"), G_CALLBACK(sipe_purple_find_contact_cb),
837 _("_Cancel"), NULL,
838 purple_connection_get_account(gc), NULL, NULL, gc);
841 static void sipe_purple_join_conference_cb(PurpleConnection *gc,
842 PurpleRequestFields *fields)
844 GList *entries = purple_request_field_group_get_fields(purple_request_fields_get_groups(fields)->data);
846 if (entries) {
847 PurpleRequestField *field = entries->data;
848 const char *id = purple_request_field_get_id(field);
849 const char *value = purple_request_field_string_get_value(field);
851 if (!sipe_strequal(id, "meetingLocation"))
852 return;
854 sipe_core_conf_create(PURPLE_GC_TO_SIPE_CORE_PUBLIC, value);
858 #ifdef HAVE_VV
859 static void sipe_purple_test_call(PurplePluginAction *action)
861 PurpleConnection *gc = (PurpleConnection *) action->context;
862 sipe_core_media_test_call(PURPLE_GC_TO_SIPE_CORE_PUBLIC);
864 #endif
866 static void sipe_purple_show_join_conference(PurplePluginAction *action)
868 PurpleConnection *gc = (PurpleConnection *) action->context;
869 PurpleRequestFields *fields;
870 PurpleRequestFieldGroup *group;
871 PurpleRequestField *field;
873 fields = purple_request_fields_new();
874 group = purple_request_field_group_new(NULL);
875 purple_request_fields_add_group(fields, group);
877 field = purple_request_field_string_new("meetingLocation", _("Meeting location"), NULL, FALSE);
878 purple_request_field_group_add_field(group, field);
880 purple_request_fields(gc,
881 _("Join conference"),
882 _("Join scheduled conference"),
883 _("Enter meeting location string you received in the invitation.\n"
884 "\n"
885 "Valid location will be something like\n"
886 "meet:sip:someone@company.com;gruu;opaque=app:conf:focus:id:abcdef1234\n"
887 "conf:sip:someone@company.com;gruu;opaque=app:conf:focus:id:abcdef1234\n"
888 "or\n"
889 "https://meet.company.com/someone/abcdef1234"),
890 fields,
891 _("_Join"), G_CALLBACK(sipe_purple_join_conference_cb),
892 _("_Cancel"), NULL,
893 purple_connection_get_account(gc), NULL, NULL, gc);
896 static void sipe_purple_republish_calendar(PurplePluginAction *action)
898 PurpleConnection *gc = (PurpleConnection *) action->context;
899 sipe_core_update_calendar(PURPLE_GC_TO_SIPE_CORE_PUBLIC);
902 static void sipe_purple_reset_status(PurplePluginAction *action)
904 PurpleConnection *gc = (PurpleConnection *) action->context;
905 sipe_core_reset_status(PURPLE_GC_TO_SIPE_CORE_PUBLIC);
908 static GList *sipe_purple_actions(SIPE_UNUSED_PARAMETER PurplePlugin *plugin,
909 SIPE_UNUSED_PARAMETER gpointer context)
911 GList *menu = NULL;
912 PurplePluginAction *act;
914 act = purple_plugin_action_new(_("About SIPE plugin..."), sipe_purple_show_about_plugin);
915 menu = g_list_prepend(menu, act);
917 act = purple_plugin_action_new(_("Contact search..."), sipe_purple_show_find_contact);
918 menu = g_list_prepend(menu, act);
920 #ifdef HAVE_VV
921 act = purple_plugin_action_new(_("Test call"), sipe_purple_test_call);
922 menu = g_list_prepend(menu, act);
923 #endif
925 act = purple_plugin_action_new(_("Join scheduled conference..."), sipe_purple_show_join_conference);
926 menu = g_list_prepend(menu, act);
928 act = purple_plugin_action_new(_("Republish Calendar"), sipe_purple_republish_calendar);
929 menu = g_list_prepend(menu, act);
931 act = purple_plugin_action_new(_("Reset status"), sipe_purple_reset_status);
932 menu = g_list_prepend(menu, act);
934 return g_list_reverse(menu);
937 static PurplePluginInfo sipe_purple_info = {
938 PURPLE_PLUGIN_MAGIC,
939 PURPLE_MAJOR_VERSION,
940 PURPLE_MINOR_VERSION,
941 PURPLE_PLUGIN_PROTOCOL, /**< type */
942 NULL, /**< ui_requirement */
943 0, /**< flags */
944 NULL, /**< dependencies */
945 PURPLE_PRIORITY_DEFAULT, /**< priority */
946 "prpl-sipe", /**< id */
947 "Office Communicator", /**< name */
948 PACKAGE_VERSION, /**< version */
949 "Microsoft Office Communicator Protocol Plugin", /**< summary */
950 "A plugin for the extended SIP/SIMPLE protocol used by " /**< description */
951 "Microsoft Live/Office Communications/Lync Server (LCS2005/OCS2007+)", /**< description */
952 "Stefan Becker <chemobejk@gmail.com>, " /**< author */
953 "Jakub Adam <jakub.adam@tieto.com>, " /**< author */
954 "Anibal Avelar <avelar@gmail.com> (retired), " /**< author */
955 "pier11 <pier11@operamail.com> (retired), " /**< author */
956 "Gabriel Burt <gburt@novell.com> (retired)", /**< author */
957 PACKAGE_URL, /**< homepage */
958 sipe_purple_plugin_load, /**< load */
959 sipe_purple_plugin_unload, /**< unload */
960 sipe_purple_plugin_destroy, /**< destroy */
961 NULL, /**< ui_info */
962 &sipe_prpl_info, /**< extra_info */
963 NULL,
964 sipe_purple_actions,
965 NULL,
966 NULL,
967 NULL,
968 NULL
971 static void sipe_purple_init_plugin(PurplePlugin *plugin)
973 PurpleAccountUserSplit *split;
974 PurpleAccountOption *option;
976 /* This needs to be called first */
977 sipe_core_init(LOCALEDIR);
978 sipe_purple_activity_init();
980 purple_plugin_register(plugin);
983 * When adding new string settings please make sure to keep these
984 * in sync:
986 * api/sipe-backend.h
987 * purple-settings.c:setting_name[]
989 split = purple_account_user_split_new(_("Login\n user or DOMAIN\\user or\n user@company.com"), NULL, ',');
990 purple_account_user_split_set_reverse(split, FALSE);
991 sipe_prpl_info.user_splits = g_list_append(sipe_prpl_info.user_splits, split);
993 option = purple_account_option_string_new(_("Server[:Port]\n(leave empty for auto-discovery)"), "server", "");
994 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
996 option = purple_account_option_list_new(_("Connection type"), "transport", NULL);
997 purple_account_option_add_list_item(option, _("Auto"), "auto");
998 purple_account_option_add_list_item(option, _("SSL/TLS"), "tls");
999 purple_account_option_add_list_item(option, _("TCP"), "tcp");
1000 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1002 /*option = purple_account_option_bool_new(_("Publish status (note: everyone may watch you)"), "doservice", TRUE);
1003 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);*/
1005 option = purple_account_option_string_new(_("User Agent"), "useragent", "");
1006 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1008 option = purple_account_option_list_new(_("Authentication scheme"), "authentication", NULL);
1009 purple_account_option_add_list_item(option, _("NTLM"), "ntlm");
1010 #if PURPLE_SIPE_SSO_AND_KERBEROS
1011 purple_account_option_add_list_item(option, _("Kerberos"), "krb5");
1012 #endif
1013 purple_account_option_add_list_item(option, _("TLS-DSK"), "tls-dsk");
1014 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1016 #if PURPLE_SIPE_SSO_AND_KERBEROS
1018 * When the user selects Single Sign-On then SIPE will ignore the
1019 * settings for "login name" and "password". Instead it will use the
1020 * default credentials provided by the OS.
1022 * NOTE: the default must be *OFF*, i.e. it is up to the user to tell
1023 * SIPE that it is OK to use Single Sign-On or not.
1025 * Configurations that are known to support Single Sign-On:
1027 * - Windows, host joined to domain, SIPE with SSPI: NTLM
1028 * - Windows, host joined to domain, SIPE with SSPI: Kerberos
1029 * - SIPE with libkrb5, valid TGT in cache (kinit): Kerberos
1031 option = purple_account_option_bool_new(_("Use Single Sign-On"), "sso", FALSE);
1032 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1033 #endif
1035 /** Example (Exchange): https://server.company.com/EWS/Exchange.asmx
1036 * Example (Domino) : https://[domino_server]/[mail_database_name].nsf
1038 option = purple_account_option_string_new(_("Email services URL\n(leave empty for auto-discovery)"), "email_url", "");
1039 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1041 option = purple_account_option_string_new(_("Email address\n(if different from Username)"), "email", "");
1042 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1044 /** Example (Exchange): DOMAIN\user or user@company.com
1045 * Example (Domino) : email_address
1047 option = purple_account_option_string_new(_("Email login\n(if different from Login)"), "email_login", "");
1048 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1050 option = purple_account_option_string_new(_("Email password\n(if different from Password)"), "email_password", "");
1051 #if PURPLE_VERSION_CHECK(3,0,0)
1052 purple_account_option_string_set_masked(
1053 #else
1054 purple_account_option_set_masked(
1055 #endif
1056 option, TRUE);
1057 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1059 /** Example (federated domain): company.com (i.e. ocschat@company.com)
1060 * Example (non-default user): user@company.com
1062 option = purple_account_option_string_new(_("Group Chat Proxy\n company.com or user@company.com\n(leave empty to determine from Username)"), "groupchat_user", "");
1063 sipe_prpl_info.protocol_options = g_list_append(sipe_prpl_info.protocol_options, option);
1066 /* This macro makes the code a purple plugin */
1067 PURPLE_INIT_PLUGIN(sipe, sipe_purple_init_plugin, sipe_purple_info);
1070 Local Variables:
1071 mode: c
1072 c-file-style: "bsd"
1073 indent-tabs-mode: t
1074 tab-width: 8
1075 End: