1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00
profanity/src/xmpp/presence.c

792 lines
26 KiB
C
Raw Normal View History

2013-01-28 17:24:47 -05:00
/*
2013-02-02 14:47:41 -05:00
* presence.c
2013-01-28 17:24:47 -05:00
*
2014-03-08 20:18:19 -05:00
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
2013-01-28 17:24:47 -05:00
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
2013-01-28 17:24:47 -05:00
*/
2013-02-10 18:29:37 -05:00
#include <assert.h>
2013-01-28 17:24:47 -05:00
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
2013-01-28 17:24:47 -05:00
2013-01-28 17:55:26 -05:00
#include "common.h"
2013-02-02 16:59:29 -05:00
#include "config/preferences.h"
2013-01-28 17:24:47 -05:00
#include "log.h"
#include "muc.h"
#include "profanity.h"
#include "server_events.h"
2013-02-02 15:55:58 -05:00
#include "xmpp/capabilities.h"
#include "xmpp/connection.h"
2013-02-02 15:55:58 -05:00
#include "xmpp/stanza.h"
#include "xmpp/xmpp.h"
2013-01-28 17:24:47 -05:00
static Autocomplete sub_requests_ac;
2013-01-28 19:04:49 -05:00
2013-02-10 18:29:37 -05:00
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, \
STANZA_NAME_PRESENCE, type, ctx)
2013-01-28 19:04:49 -05:00
static int _unavailable_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
static int _subscribe_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
static int _subscribed_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
static int _unsubscribed_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
static int _available_handler(xmpp_conn_t * const conn,
2013-01-28 19:04:49 -05:00
xmpp_stanza_t * const stanza, void * const userdata);
static int _muc_user_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
static int _presence_error_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata);
2013-01-28 17:24:47 -05:00
2013-03-17 19:04:36 -04:00
void _send_caps_request(char *node, char *caps_key, char *id, char *from);
2014-09-20 19:22:03 -04:00
static void _send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence);
2013-02-10 18:29:37 -05:00
2013-01-28 17:24:47 -05:00
void
presence_sub_requests_init(void)
2013-01-28 17:24:47 -05:00
{
sub_requests_ac = autocomplete_new();
2013-01-28 17:24:47 -05:00
}
2013-01-28 19:04:49 -05:00
void
presence_add_handlers(void)
{
xmpp_conn_t * const conn = connection_get_conn();
xmpp_ctx_t * const ctx = connection_get_ctx();
HANDLE(NULL, STANZA_TYPE_ERROR, _presence_error_handler);
HANDLE(STANZA_NS_MUC_USER, NULL, _muc_user_handler);
2013-02-10 18:29:37 -05:00
HANDLE(NULL, STANZA_TYPE_UNAVAILABLE, _unavailable_handler);
HANDLE(NULL, STANZA_TYPE_SUBSCRIBE, _subscribe_handler);
HANDLE(NULL, STANZA_TYPE_SUBSCRIBED, _subscribed_handler);
HANDLE(NULL, STANZA_TYPE_UNSUBSCRIBED, _unsubscribed_handler);
HANDLE(NULL, NULL, _available_handler);
2013-01-28 19:04:49 -05:00
}
2013-12-18 15:16:55 -05:00
static void
_presence_subscription(const char * const jid, const jabber_subscr_t action)
2013-01-28 17:24:47 -05:00
{
2013-02-10 18:29:37 -05:00
assert(jid != NULL);
xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_conn_t * const conn = connection_get_conn();
const char *type = NULL;
Jid *jidp = jid_create(jid);
autocomplete_remove(sub_requests_ac, jidp->barejid);
2013-02-10 18:29:37 -05:00
switch (action)
{
case PRESENCE_SUBSCRIBE:
log_debug("Sending presence subscribe: %s", jid);
type = STANZA_TYPE_SUBSCRIBE;
break;
case PRESENCE_SUBSCRIBED:
log_debug("Sending presence subscribed: %s", jid);
type = STANZA_TYPE_SUBSCRIBED;
break;
case PRESENCE_UNSUBSCRIBED:
log_debug("Sending presence usubscribed: %s", jid);
type = STANZA_TYPE_UNSUBSCRIBED;
break;
default:
log_warning("Attempt to send unknown subscription action: %s", jid);
break;
2013-01-28 17:24:47 -05:00
}
2013-02-10 18:29:37 -05:00
xmpp_stanza_t *presence = xmpp_stanza_new(ctx);
char *id = create_unique_id("sub");
xmpp_stanza_set_id(presence, id);
2013-01-28 17:24:47 -05:00
xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE);
xmpp_stanza_set_type(presence, type);
2013-02-10 18:29:37 -05:00
xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, jidp->barejid);
2013-01-28 17:24:47 -05:00
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
2013-02-10 18:29:37 -05:00
jid_destroy(jidp);
free(id);
2013-01-28 17:24:47 -05:00
}
2013-12-18 15:16:55 -05:00
static GSList *
_presence_get_subscription_requests(void)
2013-01-28 17:24:47 -05:00
{
return autocomplete_create_list(sub_requests_ac);
2013-01-28 17:24:47 -05:00
}
2013-12-18 15:16:55 -05:00
static gint
_presence_sub_request_count(void)
{
return autocomplete_length(sub_requests_ac);
}
void
presence_clear_sub_requests(void)
{
autocomplete_clear(sub_requests_ac);
}
2013-12-18 15:16:55 -05:00
static char *
_presence_sub_request_find(char * search_str)
{
return autocomplete_complete(sub_requests_ac, search_str, TRUE);
}
2013-12-18 15:16:55 -05:00
static gboolean
_presence_sub_request_exists(const char * const bare_jid)
{
gboolean result = FALSE;
GSList *requests_p = autocomplete_create_list(sub_requests_ac);
GSList *requests = requests_p;
while (requests != NULL) {
if (strcmp(requests->data, bare_jid) == 0) {
result = TRUE;
break;
}
requests = g_slist_next(requests);
}
if (requests_p != NULL) {
g_slist_free_full(requests_p, free);
}
return result;
}
2013-12-18 15:16:55 -05:00
static void
_presence_reset_sub_request_search(void)
{
autocomplete_reset(sub_requests_ac);
}
2013-12-18 15:16:55 -05:00
static void
_presence_update(const resource_presence_t presence_type, const char * const msg,
2013-02-10 18:29:37 -05:00
const int idle)
2013-01-28 17:55:26 -05:00
{
2013-02-10 18:29:37 -05:00
if (jabber_get_connection_status() != JABBER_CONNECTED) {
log_warning("Error setting presence, not connected.");
2013-01-28 17:55:26 -05:00
return;
2013-02-10 18:29:37 -05:00
}
if (msg != NULL) {
log_debug("Updating presence: %s, \"%s\"",
string_from_resource_presence(presence_type), msg);
} else {
log_debug("Updating presence: %s",
string_from_resource_presence(presence_type));
}
xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_conn_t * const conn = connection_get_conn();
const int pri =
accounts_get_priority_for_presence_type(jabber_get_account_name(),
presence_type);
const char *show = stanza_get_presence_string_from_type(presence_type);
2013-01-28 17:55:26 -05:00
connection_set_presence_message(msg);
connection_set_priority(pri);
2013-01-28 17:55:26 -05:00
xmpp_stanza_t *presence = stanza_create_presence(ctx);
char *id = create_unique_id("presence");
xmpp_stanza_set_id(presence, id);
stanza_attach_show(ctx, presence, show);
stanza_attach_status(ctx, presence, msg);
stanza_attach_priority(ctx, presence, pri);
stanza_attach_last_activity(ctx, presence, idle);
stanza_attach_caps(ctx, presence);
2013-01-28 17:55:26 -05:00
xmpp_send(conn, presence);
2013-02-10 18:29:37 -05:00
_send_room_presence(conn, presence);
xmpp_stanza_release(presence);
2013-01-28 17:55:26 -05:00
2013-02-10 18:29:37 -05:00
// set last presence for account
const char *last = show;
if (last == NULL) {
last = STANZA_TEXT_ONLINE;
}
accounts_set_last_presence(jabber_get_account_name(), last);
free(id);
2013-02-10 18:29:37 -05:00
}
static void
_send_room_presence(xmpp_conn_t *conn, xmpp_stanza_t *presence)
{
2014-09-28 17:09:20 -04:00
GList *rooms_p = muc_rooms();
GList *rooms = rooms_p;
2013-01-28 17:55:26 -05:00
while (rooms != NULL) {
2013-02-10 18:29:37 -05:00
const char *room = rooms->data;
2014-09-28 17:09:20 -04:00
const char *nick = muc_nick(room);
2013-01-28 17:55:26 -05:00
if (nick != NULL) {
char *full_room_jid = create_fulljid(room, nick);
xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid);
log_debug("Sending presence to room: %s", full_room_jid);
xmpp_send(conn, presence);
free(full_room_jid);
}
2013-01-28 17:55:26 -05:00
rooms = g_list_next(rooms);
}
if (rooms_p != NULL) {
g_list_free(rooms_p);
}
2013-01-28 17:55:26 -05:00
}
2013-12-18 15:16:55 -05:00
static void
2014-03-08 16:10:23 -05:00
_presence_join_room(char *room, char *nick, char * passwd)
{
2014-03-08 16:10:23 -05:00
Jid *jid = jid_create_from_bare_and_resource(room, nick);
2013-02-10 18:29:37 -05:00
log_debug("Sending room join presence to: %s", jid->fulljid);
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_conn_t *conn = connection_get_conn();
resource_presence_t presence_type =
2013-02-10 18:29:37 -05:00
accounts_get_last_presence(jabber_get_account_name());
const char *show = stanza_get_presence_string_from_type(presence_type);
2013-02-03 18:46:20 -05:00
char *status = jabber_get_presence_message();
int pri = accounts_get_priority_for_presence_type(jabber_get_account_name(),
presence_type);
xmpp_stanza_t *presence = stanza_create_room_join_presence(ctx, jid->fulljid, passwd);
stanza_attach_show(ctx, presence, show);
stanza_attach_status(ctx, presence, status);
stanza_attach_priority(ctx, presence, pri);
stanza_attach_caps(ctx, presence);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
2014-03-08 16:10:23 -05:00
jid_destroy(jid);
}
2013-12-18 15:16:55 -05:00
static void
_presence_change_room_nick(const char * const room, const char * const nick)
{
2013-02-10 18:29:37 -05:00
assert(room != NULL);
assert(nick != NULL);
log_debug("Sending room nickname change to: %s, nick: %s", room, nick);
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_conn_t *conn = connection_get_conn();
resource_presence_t presence_type =
2013-02-10 18:29:37 -05:00
accounts_get_last_presence(jabber_get_account_name());
const char *show = stanza_get_presence_string_from_type(presence_type);
char *status = jabber_get_presence_message();
int pri = accounts_get_priority_for_presence_type(jabber_get_account_name(),
presence_type);
char *full_room_jid = create_fulljid(room, nick);
2013-02-10 18:29:37 -05:00
xmpp_stanza_t *presence =
stanza_create_room_newnick_presence(ctx, full_room_jid);
stanza_attach_show(ctx, presence, show);
stanza_attach_status(ctx, presence, status);
stanza_attach_priority(ctx, presence, pri);
stanza_attach_caps(ctx, presence);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
free(full_room_jid);
}
2013-12-18 15:16:55 -05:00
static void
_presence_leave_chat_room(const char * const room_jid)
{
2013-02-10 18:29:37 -05:00
assert(room_jid != NULL);
log_debug("Sending room leave presence to: %s", room_jid);
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_conn_t *conn = connection_get_conn();
2014-09-28 17:09:20 -04:00
char *nick = muc_nick(room_jid);
if (nick != NULL) {
xmpp_stanza_t *presence = stanza_create_room_leave_presence(ctx, room_jid,
nick);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
}
}
static int
_presence_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
2014-01-27 17:35:04 -05:00
char *id = xmpp_stanza_get_id(stanza);
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR);
xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_X);
char *xmlns = NULL;
if (x != NULL) {
xmlns = xmpp_stanza_get_ns(x);
}
2014-01-27 17:35:04 -05:00
char *type = NULL;
if (error_stanza != NULL) {
type = xmpp_stanza_get_attribute(error_stanza, STANZA_ATTR_TYPE);
}
// handle MUC join errors
if (g_strcmp0(xmlns, STANZA_NS_MUC) == 0) {
Jid *fulljid = jid_create(from);
char *error_cond = NULL;
xmpp_stanza_t *reason_st = xmpp_stanza_get_child_by_ns(error_stanza, STANZA_NS_STANZAS);
if (reason_st != NULL) {
error_cond = xmpp_stanza_get_name(reason_st);
}
if (error_cond == NULL) {
error_cond = "unknown";
}
log_info("Error joining room: %s, reason: %s", fulljid->barejid, error_cond);
handle_room_join_error(fulljid->barejid, error_cond);
2014-10-31 21:48:36 -04:00
jid_destroy(fulljid);
return 1;
}
2014-01-27 17:35:04 -05:00
// stanza_get_error never returns NULL
char *err_msg = stanza_get_error_message(stanza);
2014-01-27 17:35:04 -05:00
GString *log_msg = g_string_new("presence stanza error received");
if (id != NULL) {
g_string_append(log_msg, " id=");
g_string_append(log_msg, id);
}
if (from != NULL) {
g_string_append(log_msg, " from=");
g_string_append(log_msg, from);
}
if (type != NULL) {
g_string_append(log_msg, " type=");
g_string_append(log_msg, type);
}
g_string_append(log_msg, " error=");
g_string_append(log_msg, err_msg);
2014-01-27 17:35:04 -05:00
log_info(log_msg->str);
2014-01-27 17:35:04 -05:00
g_string_free(log_msg, TRUE);
handle_presence_error(from, type, err_msg);
2014-01-27 17:35:04 -05:00
free(err_msg);
return 1;
}
static int
_unsubscribed_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
Jid *from_jid = jid_create(from);
2013-02-10 18:29:37 -05:00
log_debug("Unsubscribed presence handler fired for %s", from);
handle_subscription(from_jid->barejid, PRESENCE_UNSUBSCRIBED);
autocomplete_remove(sub_requests_ac, from_jid->barejid);
jid_destroy(from_jid);
return 1;
}
static int
_subscribed_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
Jid *from_jid = jid_create(from);
2013-02-10 18:29:37 -05:00
log_debug("Subscribed presence handler fired for %s", from);
handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBED);
autocomplete_remove(sub_requests_ac, from_jid->barejid);
jid_destroy(from_jid);
return 1;
}
static int
_subscribe_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
2013-02-10 18:29:37 -05:00
log_debug("Subscribe presence handler fired for %s", from);
Jid *from_jid = jid_create(from);
if (from_jid == NULL) {
return 1;
}
handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBE);
2013-08-25 20:29:50 -04:00
autocomplete_add(sub_requests_ac, from_jid->barejid);
jid_destroy(from_jid);
return 1;
}
2013-01-28 17:55:26 -05:00
2013-01-28 19:04:49 -05:00
static int
_unavailable_handler(xmpp_conn_t * const conn,
2013-01-28 17:24:47 -05:00
xmpp_stanza_t * const stanza, void * const userdata)
{
const char *jid = xmpp_conn_get_jid(conn);
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
2013-02-10 18:29:37 -05:00
log_debug("Unavailable presence handler fired for %s", from);
2013-01-28 17:24:47 -05:00
Jid *my_jid = jid_create(jid);
Jid *from_jid = jid_create(from);
if (my_jid == NULL || from_jid == NULL) {
jid_destroy(my_jid);
jid_destroy(from_jid);
return 1;
}
2013-01-28 17:24:47 -05:00
char *status_str = stanza_get_status(stanza, NULL);
if (strcmp(my_jid->barejid, from_jid->barejid) !=0) {
if (from_jid->resourcepart != NULL) {
handle_contact_offline(from_jid->barejid, from_jid->resourcepart, status_str);
// hack for servers that do not send full jid with unavailable presence
} else {
handle_contact_offline(from_jid->barejid, "__prof_default", status_str);
}
} else {
if (from_jid->resourcepart != NULL) {
connection_remove_available_resource(from_jid->resourcepart);
}
2013-01-28 17:24:47 -05:00
}
free(status_str);
jid_destroy(my_jid);
jid_destroy(from_jid);
2013-01-28 17:24:47 -05:00
return 1;
}
2013-01-28 17:24:47 -05:00
static void
_handle_caps(xmpp_stanza_t *const stanza)
{
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
if (from) {
char *hash = stanza_caps_get_hash(stanza);
// hash supported xep-0115
if (g_strcmp0(hash, "sha-1") == 0) {
log_info("Hash %s supported");
char *ver = stanza_get_caps_ver(stanza);
if (ver) {
if (caps_contains(ver)) {
log_info("Capabilities cached: %s", ver);
caps_map(from, ver);
} else {
log_info("Capabilities not cached: %s, sending service discovery request", ver);
char *node = stanza_caps_get_node(stanza);
char *id = create_unique_id("caps");
iq_send_caps_request(from, id, node, ver);
2014-09-23 19:48:28 -04:00
free(id);
}
}
// no hash, or not supported
} else {
if (hash) {
log_info("Hash %s not supported, not sending service discovery request");
// send service discovery request, cache against from full jid
} else {
log_info("No hash specified, not sending service discovery request");
// do legacy
}
}
}
}
static int
_available_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata)
{
// handler still fires if error
if (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_ERROR) == 0) {
return 1;
}
2013-01-28 17:24:47 -05:00
// handler still fires if other types
if ((g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_UNAVAILABLE) == 0) ||
(g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_SUBSCRIBE) == 0) ||
(g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_SUBSCRIBED) == 0) ||
(g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_UNSUBSCRIBED) == 0)) {
return 1;
}
2013-01-28 17:24:47 -05:00
2013-02-10 18:29:37 -05:00
// handler still fires for muc presence
if (stanza_is_muc_presence(stanza)) {
return 1;
}
2014-11-26 16:07:46 -05:00
int err = 0;
XMPPPresence *xmpp_presence = stanza_parse_presence(stanza, &err);
if (!xmpp_presence) {
char *from = NULL;
switch(err) {
case STANZA_PARSE_ERROR_NO_FROM:
log_warning("Available presence handler fired with no from attribute.");
break;
case STANZA_PARSE_ERROR_INVALID_FROM:
from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
log_warning("Available presence handler fired with invalid from attribute: %s", from);
break;
default:
log_warning("Available presence handler fired, could not parse stanza.");
break;
}
2014-09-18 16:36:43 -04:00
return 1;
2014-11-26 16:07:46 -05:00
} else if (xmpp_presence->jid->fulljid) {
log_debug("Presence available handler fired for: %s", xmpp_presence->jid->fulljid);
} else {
log_debug("Presence available handler fired for: %s", xmpp_presence->jid->barejid);
2014-09-18 16:36:43 -04:00
}
2013-01-28 17:24:47 -05:00
2014-09-18 16:36:43 -04:00
const char *my_jid_str = xmpp_conn_get_jid(conn);
Jid *my_jid = jid_create(my_jid_str);
2014-11-26 16:07:46 -05:00
if ((g_strcmp0(my_jid->fulljid, xmpp_presence->jid->fulljid) != 0) && (stanza_contains_caps(stanza))) {
2014-09-18 19:49:48 -04:00
log_info("Presence contains capabilities.");
_handle_caps(stanza);
2014-09-18 16:36:43 -04:00
}
2014-11-26 16:07:46 -05:00
Resource *resource = stanza_resource_from_presence(xmpp_presence);
2014-11-26 16:07:46 -05:00
if (g_strcmp0(xmpp_presence->jid->barejid, my_jid->barejid) == 0) {
connection_add_available_resource(resource);
} else {
2014-11-26 16:07:46 -05:00
handle_contact_online(xmpp_presence->jid->barejid, resource, xmpp_presence->last_activity);
2013-01-28 17:24:47 -05:00
}
2014-09-18 16:36:43 -04:00
jid_destroy(my_jid);
2014-11-26 16:07:46 -05:00
stanza_free_presence(xmpp_presence);
2014-09-18 16:36:43 -04:00
2013-01-28 17:24:47 -05:00
return 1;
}
2013-03-17 19:04:36 -04:00
void
_send_caps_request(char *node, char *caps_key, char *id, char *from)
{
xmpp_ctx_t *ctx = connection_get_ctx();
xmpp_conn_t *conn = connection_get_conn();
if (node != NULL) {
log_debug("Node string: %s.", node);
if (!caps_contains(caps_key)) {
log_debug("Capabilities not cached for '%s', sending discovery IQ.", from);
xmpp_stanza_t *iq = stanza_create_disco_info_iq(ctx, id, from, node);
xmpp_send(conn, iq);
xmpp_stanza_release(iq);
} else {
log_debug("Capabilities already cached, for %s", caps_key);
}
} else {
log_debug("No node string, not sending discovery IQ.");
}
}
2013-01-28 17:24:47 -05:00
static int
2014-10-06 19:22:39 -04:00
_muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
2013-01-28 17:24:47 -05:00
{
2014-10-06 19:22:39 -04:00
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
// handler still fires if error
2014-10-06 19:22:39 -04:00
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
2013-01-28 17:24:47 -05:00
return 1;
}
2014-10-06 19:22:39 -04:00
// invalid from attribute
Jid *from_jid = jid_create(from);
if (from_jid == NULL || from_jid->resourcepart == NULL) {
return 1;
}
2014-10-06 19:22:39 -04:00
char *room = from_jid->barejid;
char *nick = from_jid->resourcepart;
2014-10-06 20:16:46 -04:00
char *show_str = stanza_get_show(stanza, "online");
char *status_str = stanza_get_status(stanza, NULL);
2014-10-06 19:46:08 -04:00
char *jid = NULL;
char *role = NULL;
char *affiliation = NULL;
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
if (x) {
xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(x, STANZA_NAME_ITEM);
if (item) {
jid = xmpp_stanza_get_attribute(item, "jid");
role = xmpp_stanza_get_attribute(item, "role");
affiliation = xmpp_stanza_get_attribute(item, "affiliation");
}
}
2013-01-28 17:24:47 -05:00
// handle self presence
2013-05-21 17:00:42 -04:00
if (stanza_is_muc_self_presence(stanza, jabber_get_fulljid())) {
2014-10-06 19:22:39 -04:00
log_debug("Room self presence received from %s", from_jid->fulljid);
2013-01-28 17:24:47 -05:00
2014-09-27 20:55:24 -04:00
// self unavailable
2014-10-06 19:22:39 -04:00
if (g_strcmp0(type, STANZA_TYPE_UNAVAILABLE) == 0) {
2013-01-28 17:24:47 -05:00
2014-10-06 19:22:39 -04:00
// handle nickname change
char *new_nick = stanza_get_new_nick(stanza);
if (new_nick) {
muc_nick_change_start(room, new_nick);
2013-01-28 17:24:47 -05:00
} else {
2014-10-05 15:52:34 -04:00
GSList *status_codes = stanza_get_status_codes_by_ns(stanza, STANZA_NS_MUC_USER);
// room destroyed
if (stanza_room_destroyed(stanza)) {
char *new_jid = stanza_get_muc_destroy_alternative_room(stanza);
char *password = stanza_get_muc_destroy_alternative_password(stanza);
char *reason = stanza_get_muc_destroy_reason(stanza);
2014-10-06 19:22:39 -04:00
handle_room_destroyed(room, new_jid, password, reason);
2014-10-05 15:52:34 -04:00
free(password);
free(reason);
// kicked from room
} else if (g_slist_find_custom(status_codes, "307", (GCompareFunc)g_strcmp0) != NULL) {
char *actor = stanza_get_actor(stanza);
char *reason = stanza_get_reason(stanza);
2014-10-06 19:22:39 -04:00
handle_room_kicked(room, actor, reason);
2014-10-05 15:52:34 -04:00
free(reason);
// banned from room
} else if (g_slist_find_custom(status_codes, "301", (GCompareFunc)g_strcmp0) != NULL) {
char *actor = stanza_get_actor(stanza);
char *reason = stanza_get_reason(stanza);
2014-10-06 19:22:39 -04:00
handle_room_banned(room, actor, reason);
free(reason);
2014-10-05 15:52:34 -04:00
// normal exit
} else {
2014-10-06 19:22:39 -04:00
handle_leave_room(room);
2014-10-05 15:52:34 -04:00
}
2014-10-31 21:48:36 -04:00
g_slist_free_full(status_codes, free);
2013-01-28 17:24:47 -05:00
}
2014-09-27 20:55:24 -04:00
// self online
} else {
2014-10-06 19:22:39 -04:00
gboolean config_required = stanza_muc_requires_config(stanza);
char *actor = stanza_get_actor(stanza);
char *reason = stanza_get_reason(stanza);
handle_muc_self_online(room, nick, config_required, role, affiliation, actor, reason, jid, show_str, status_str);
2013-01-28 17:24:47 -05:00
}
// handle presence from room members
} else {
log_debug("Room presence received from %s", from_jid->fulljid);
2014-10-06 19:22:39 -04:00
if (g_strcmp0(type, STANZA_TYPE_UNAVAILABLE) == 0) {
2013-01-28 17:24:47 -05:00
// handle nickname change
2014-10-06 19:22:39 -04:00
char *new_nick = stanza_get_new_nick(stanza);
if (new_nick) {
2014-10-06 19:46:08 -04:00
muc_occupant_nick_change_start(room, new_nick, nick);
2014-10-06 19:22:39 -04:00
// handle left room
2013-01-28 17:24:47 -05:00
} else {
2014-10-05 15:52:34 -04:00
GSList *status_codes = stanza_get_status_codes_by_ns(stanza, STANZA_NS_MUC_USER);
// kicked from room
if (g_slist_find_custom(status_codes, "307", (GCompareFunc)g_strcmp0) != NULL) {
char *actor = stanza_get_actor(stanza);
char *reason = stanza_get_reason(stanza);
2014-10-06 19:22:39 -04:00
handle_room_occupent_kicked(room, nick, actor, reason);
2014-10-05 15:52:34 -04:00
free(reason);
// banned from room
} else if (g_slist_find_custom(status_codes, "301", (GCompareFunc)g_strcmp0) != NULL) {
char *actor = stanza_get_actor(stanza);
char *reason = stanza_get_reason(stanza);
2014-10-06 19:22:39 -04:00
handle_room_occupent_banned(room, nick, actor, reason);
free(reason);
2014-10-05 15:52:34 -04:00
// normal exit
} else {
2014-10-06 19:46:08 -04:00
handle_room_occupant_offline(room, nick, "offline", status_str);
2014-10-05 15:52:34 -04:00
}
2014-10-31 21:48:36 -04:00
g_slist_free_full(status_codes, free);
2013-01-28 17:24:47 -05:00
}
2014-10-06 19:22:39 -04:00
// room occupant online
2013-01-28 17:24:47 -05:00
} else {
2014-09-21 11:51:35 -04:00
// send disco info for capabilities, if not cached
if (stanza_contains_caps(stanza)) {
log_info("Presence contains capabilities.");
_handle_caps(stanza);
}
char *actor = stanza_get_actor(stanza);
char *reason = stanza_get_reason(stanza);
handle_muc_occupant_online(room, nick, jid, role, affiliation, actor, reason, show_str, status_str);
2013-01-28 17:24:47 -05:00
}
}
2014-10-06 20:16:46 -04:00
free(show_str);
free(status_str);
jid_destroy(from_jid);
2013-01-28 17:24:47 -05:00
return 1;
}
2013-12-22 18:31:49 -05:00
void
presence_init_module(void)
{
presence_subscription = _presence_subscription;
presence_get_subscription_requests = _presence_get_subscription_requests;
presence_sub_request_count = _presence_sub_request_count;
presence_sub_request_find = _presence_sub_request_find;
presence_sub_request_exists = _presence_sub_request_exists;
presence_reset_sub_request_search = _presence_reset_sub_request_search;
presence_update = _presence_update;
presence_join_room = _presence_join_room;
presence_change_room_nick = _presence_change_room_nick;
presence_leave_chat_room = _presence_leave_chat_room;
}