2013-01-28 19:21:04 -05:00
|
|
|
/*
|
2013-02-02 14:47:41 -05:00
|
|
|
* message.c
|
2013-01-28 19:21:04 -05:00
|
|
|
*
|
2016-02-14 17:54:46 -05:00
|
|
|
* Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
|
2013-01-28 19:21:04 -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
|
2016-07-23 20:14:49 -04:00
|
|
|
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
|
2013-01-28 19:21:04 -05:00
|
|
|
*
|
2014-08-24 15:57:39 -04:00
|
|
|
* 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 19:21:04 -05:00
|
|
|
*/
|
|
|
|
|
2016-03-31 16:05:02 -04:00
|
|
|
#include "config.h"
|
2015-09-21 16:40:04 -04:00
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-03-31 16:05:02 -04:00
|
|
|
#ifdef HAVE_LIBMESODE
|
2015-09-21 16:40:04 -04:00
|
|
|
#include <mesode.h>
|
|
|
|
#endif
|
2016-07-24 10:43:51 -04:00
|
|
|
|
2016-03-31 16:05:02 -04:00
|
|
|
#ifdef HAVE_LIBSTROPHE
|
2013-01-28 19:21:04 -05:00
|
|
|
#include <strophe.h>
|
2015-09-21 16:40:04 -04:00
|
|
|
#endif
|
2013-01-28 19:21:04 -05:00
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
#include "profanity.h"
|
2016-07-24 10:43:51 -04:00
|
|
|
#include "log.h"
|
|
|
|
#include "config/preferences.h"
|
2015-04-19 11:54:16 -04:00
|
|
|
#include "event/server_events.h"
|
2016-07-24 10:43:51 -04:00
|
|
|
#include "pgp/gpg.h"
|
|
|
|
#include "plugins/plugins.h"
|
|
|
|
#include "ui/ui.h"
|
|
|
|
#include "xmpp/chat_session.h"
|
|
|
|
#include "xmpp/muc.h"
|
2016-05-05 17:10:10 -04:00
|
|
|
#include "xmpp/session.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "xmpp/message.h"
|
2013-05-19 18:35:02 -04:00
|
|
|
#include "xmpp/roster.h"
|
2016-07-24 10:43:51 -04:00
|
|
|
#include "xmpp/roster_list.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "xmpp/stanza.h"
|
2016-05-05 19:53:03 -04:00
|
|
|
#include "xmpp/connection.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "xmpp/xmpp.h"
|
2013-01-28 19:21:04 -05:00
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static int _message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
|
2013-01-28 19:45:03 -05:00
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void _handle_error(xmpp_stanza_t *const stanza);
|
|
|
|
static void _handle_groupchat(xmpp_stanza_t *const stanza);
|
|
|
|
static void _handel_muc_user(xmpp_stanza_t *const stanza);
|
|
|
|
static void _handle_conference(xmpp_stanza_t *const stanza);
|
|
|
|
static void _handle_captcha(xmpp_stanza_t *const stanza);
|
|
|
|
static void _handle_receipt_received(xmpp_stanza_t *const stanza);
|
|
|
|
static void _handle_chat(xmpp_stanza_t *const stanza);
|
2016-03-27 16:36:29 -04:00
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void _send_message_stanza(xmpp_stanza_t *const stanza);
|
|
|
|
|
|
|
|
static int
|
|
|
|
_message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
|
|
|
|
{
|
|
|
|
log_debug("Message stanza handler fired");
|
|
|
|
|
|
|
|
char *text;
|
|
|
|
size_t text_size;
|
|
|
|
xmpp_stanza_to_text(stanza, &text, &text_size);
|
|
|
|
gboolean cont = plugins_on_message_stanza_receive(text);
|
2016-05-01 19:40:16 -04:00
|
|
|
xmpp_free(connection_get_ctx(), text);
|
2016-03-27 20:25:48 -04:00
|
|
|
if (!cont) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *type = xmpp_stanza_get_type(stanza);
|
2016-03-27 20:25:48 -04:00
|
|
|
|
|
|
|
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
|
|
|
|
_handle_error(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_strcmp0(type, STANZA_TYPE_GROUPCHAT) == 0) {
|
|
|
|
_handle_groupchat(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_stanza_t *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
|
|
|
|
if (mucuser) {
|
|
|
|
_handel_muc_user(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_stanza_t *conference = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE);
|
|
|
|
if (conference) {
|
|
|
|
_handle_conference(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA);
|
|
|
|
if (captcha) {
|
|
|
|
_handle_captcha(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_stanza_t *receipts = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS);
|
|
|
|
if (receipts) {
|
|
|
|
_handle_receipt_received(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
_handle_chat(stanza);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2013-01-28 19:45:03 -05:00
|
|
|
void
|
2016-05-02 17:34:26 -04:00
|
|
|
message_handlers_init(void)
|
2013-01-28 19:45:03 -05:00
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2016-03-27 20:25:48 -04:00
|
|
|
xmpp_handler_add(conn, _message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx);
|
2013-01-28 19:45:03 -05:00
|
|
|
}
|
|
|
|
|
2015-10-25 20:14:23 -04:00
|
|
|
char*
|
2016-08-18 17:51:06 -04:00
|
|
|
message_send_chat(const char *const barejid, const char *const msg, const char *const oob_url,
|
|
|
|
gboolean request_receipt)
|
2015-06-21 12:11:23 -04:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
|
2016-08-18 17:51:06 -04:00
|
|
|
char *state = chat_session_get_state(barejid);
|
|
|
|
char *jid = chat_session_get_jid(barejid);
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("msg");
|
2015-06-20 20:48:25 -04:00
|
|
|
|
2016-08-20 13:51:19 -04:00
|
|
|
xmpp_stanza_t *message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
2015-06-20 20:48:25 -04:00
|
|
|
free(jid);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
stanza_attach_state(ctx, message, state);
|
|
|
|
}
|
2015-06-21 12:11:23 -04:00
|
|
|
|
2016-04-11 14:13:18 -04:00
|
|
|
if (oob_url) {
|
|
|
|
stanza_attach_x_oob_url(ctx, message, oob_url);
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:51:06 -04:00
|
|
|
if (request_receipt) {
|
2015-06-20 20:48:25 -04:00
|
|
|
stanza_attach_receipt_request(ctx, message);
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2015-06-20 20:48:25 -04:00
|
|
|
xmpp_stanza_release(message);
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-10-25 20:14:23 -04:00
|
|
|
char*
|
2016-08-18 17:51:06 -04:00
|
|
|
message_send_chat_pgp(const char *const barejid, const char *const msg, gboolean request_receipt)
|
2015-06-20 20:48:25 -04:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
|
2016-08-18 17:51:06 -04:00
|
|
|
char *state = chat_session_get_state(barejid);
|
|
|
|
char *jid = chat_session_get_jid(barejid);
|
2015-06-20 20:48:25 -04:00
|
|
|
char *id = create_unique_id("msg");
|
2015-03-24 19:34:24 -04:00
|
|
|
|
2015-06-21 12:11:23 -04:00
|
|
|
xmpp_stanza_t *message = NULL;
|
2016-03-31 16:05:02 -04:00
|
|
|
#ifdef HAVE_LIBGPGME
|
2016-05-05 19:53:03 -04:00
|
|
|
char *account_name = session_get_account_name();
|
2015-03-24 19:34:24 -04:00
|
|
|
ProfAccount *account = accounts_get_account(account_name);
|
|
|
|
if (account->pgp_keyid) {
|
|
|
|
Jid *jidp = jid_create(jid);
|
2016-03-29 16:24:37 -04:00
|
|
|
char *encrypted = p_gpg_encrypt(jidp->barejid, msg, account->pgp_keyid);
|
2015-03-24 19:34:24 -04:00
|
|
|
if (encrypted) {
|
2016-08-20 13:51:19 -04:00
|
|
|
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
|
|
|
xmpp_message_set_body(message, "This message is encrypted.");
|
2015-03-24 19:34:24 -04:00
|
|
|
xmpp_stanza_t *x = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(x, STANZA_NAME_X);
|
|
|
|
xmpp_stanza_set_ns(x, STANZA_NS_ENCRYPTED);
|
|
|
|
xmpp_stanza_t *enc_st = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_text(enc_st, encrypted);
|
|
|
|
xmpp_stanza_add_child(x, enc_st);
|
|
|
|
xmpp_stanza_release(enc_st);
|
|
|
|
xmpp_stanza_add_child(message, x);
|
|
|
|
xmpp_stanza_release(x);
|
|
|
|
free(encrypted);
|
|
|
|
} else {
|
2016-08-20 13:51:19 -04:00
|
|
|
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
2015-03-24 19:34:24 -04:00
|
|
|
}
|
2015-06-24 15:53:04 -04:00
|
|
|
jid_destroy(jidp);
|
2015-04-05 11:03:50 -04:00
|
|
|
} else {
|
2016-08-20 13:51:19 -04:00
|
|
|
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
2015-03-24 19:34:24 -04:00
|
|
|
}
|
2015-08-10 20:00:23 -04:00
|
|
|
account_free(account);
|
2015-03-24 19:34:24 -04:00
|
|
|
#else
|
2016-08-20 13:51:19 -04:00
|
|
|
message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
2015-03-24 19:34:24 -04:00
|
|
|
#endif
|
2015-03-11 19:18:28 -04:00
|
|
|
free(jid);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
stanza_attach_state(ctx, message, state);
|
|
|
|
}
|
2015-06-21 12:11:23 -04:00
|
|
|
|
2016-08-18 17:51:06 -04:00
|
|
|
if (request_receipt) {
|
2015-03-11 19:18:28 -04:00
|
|
|
stanza_attach_receipt_request(ctx, message);
|
2015-03-10 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2015-03-10 17:16:34 -04:00
|
|
|
xmpp_stanza_release(message);
|
2015-03-11 19:47:11 -04:00
|
|
|
|
|
|
|
return id;
|
2015-03-10 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
2015-10-25 20:14:23 -04:00
|
|
|
char*
|
2016-08-18 17:51:06 -04:00
|
|
|
message_send_chat_otr(const char *const barejid, const char *const msg, gboolean request_receipt)
|
2015-03-10 17:16:34 -04:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
|
2016-08-18 17:51:06 -04:00
|
|
|
char *state = chat_session_get_state(barejid);
|
|
|
|
char *jid = chat_session_get_jid(barejid);
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("msg");
|
2015-06-21 12:11:23 -04:00
|
|
|
|
2016-08-20 13:51:19 -04:00
|
|
|
xmpp_stanza_t *message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, barejid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
|
|
|
|
2015-03-11 19:18:28 -04:00
|
|
|
free(jid);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
stanza_attach_state(ctx, message, state);
|
|
|
|
}
|
2015-06-21 12:11:23 -04:00
|
|
|
|
2015-03-11 19:18:28 -04:00
|
|
|
stanza_attach_carbons_private(ctx, message);
|
2015-07-01 10:04:54 -04:00
|
|
|
stanza_attach_hints_no_copy(ctx, message);
|
|
|
|
stanza_attach_hints_no_store(ctx, message);
|
2015-06-21 12:11:23 -04:00
|
|
|
|
2016-08-18 17:51:06 -04:00
|
|
|
if (request_receipt) {
|
2015-03-11 19:18:28 -04:00
|
|
|
stanza_attach_receipt_request(ctx, message);
|
2013-01-28 19:21:04 -05:00
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2013-01-28 19:21:04 -05:00
|
|
|
xmpp_stanza_release(message);
|
2015-03-11 19:47:11 -04:00
|
|
|
|
|
|
|
return id;
|
2013-01-28 19:21:04 -05:00
|
|
|
}
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2016-04-11 14:13:18 -04:00
|
|
|
message_send_private(const char *const fulljid, const char *const msg, const char *const oob_url)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("prv");
|
2016-08-20 13:51:19 -04:00
|
|
|
|
|
|
|
xmpp_stanza_t *message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, fulljid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
|
|
|
|
2015-03-11 19:47:11 -04:00
|
|
|
free(id);
|
2013-05-04 21:31:04 -04:00
|
|
|
|
2016-04-11 14:13:18 -04:00
|
|
|
if (oob_url) {
|
|
|
|
stanza_attach_x_oob_url(ctx, message, oob_url);
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2013-05-04 21:31:04 -04:00
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2016-04-11 14:13:18 -04:00
|
|
|
message_send_groupchat(const char *const roomjid, const char *const msg, const char *const oob_url)
|
2014-10-04 22:05:46 -04:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("muc");
|
2016-08-20 13:51:19 -04:00
|
|
|
|
|
|
|
xmpp_stanza_t *message = xmpp_message_new(ctx, STANZA_TYPE_GROUPCHAT, roomjid, id);
|
|
|
|
xmpp_message_set_body(message, msg);
|
|
|
|
|
2015-03-11 19:47:11 -04:00
|
|
|
free(id);
|
2014-10-04 22:05:46 -04:00
|
|
|
|
2016-04-11 14:13:18 -04:00
|
|
|
if (oob_url) {
|
|
|
|
stanza_attach_x_oob_url(ctx, message, oob_url);
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2014-10-04 22:05:46 -04:00
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
message_send_groupchat_subject(const char *const roomjid, const char *const subject)
|
2014-12-19 19:52:35 -05:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
xmpp_stanza_t *message = stanza_create_room_subject_message(ctx, roomjid, subject);
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2014-12-19 19:52:35 -05:00
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
message_send_invite(const char *const roomjid, const char *const contact,
|
|
|
|
const char *const reason)
|
2013-04-20 15:18:13 -04:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-03-28 18:51:41 -04:00
|
|
|
xmpp_stanza_t *stanza;
|
|
|
|
|
|
|
|
muc_member_type_t member_type = muc_member_type(roomjid);
|
|
|
|
if (member_type == MUC_MEMBER_TYPE_PUBLIC) {
|
|
|
|
log_debug("Sending direct invite to %s, for %s", contact, roomjid);
|
2015-03-28 21:46:59 -04:00
|
|
|
char *password = muc_password(roomjid);
|
|
|
|
stanza = stanza_create_invite(ctx, roomjid, contact, reason, password);
|
2015-03-28 18:51:41 -04:00
|
|
|
} else {
|
|
|
|
log_debug("Sending mediated invite to %s, for %s", contact, roomjid);
|
|
|
|
stanza = stanza_create_mediated_invite(ctx, roomjid, contact, reason);
|
|
|
|
}
|
2013-04-20 15:18:13 -04:00
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(stanza);
|
2013-04-20 15:18:13 -04:00
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
message_send_composing(const char *const jid)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2015-01-11 15:20:17 -05:00
|
|
|
xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_COMPOSING);
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_stanza_release(stanza);
|
2015-01-11 15:20:17 -05:00
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
message_send_paused(const char *const jid)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-01-11 15:20:17 -05:00
|
|
|
xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_PAUSED);
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
message_send_inactive(const char *const jid)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-01-11 15:20:17 -05:00
|
|
|
xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_INACTIVE);
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
message_send_gone(const char *const jid)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-01-11 15:20:17 -05:00
|
|
|
xmpp_stanza_t *stanza = stanza_create_chat_state(ctx, jid, STANZA_NAME_GONE);
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handle_error(xmpp_stanza_t *const stanza)
|
2014-01-26 13:30:34 -05:00
|
|
|
{
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *id = xmpp_stanza_get_id(stanza);
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *jid = xmpp_stanza_get_from(stanza);
|
2014-01-27 17:35:04 -05:00
|
|
|
xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR);
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *type = NULL;
|
2015-05-02 18:40:14 -04:00
|
|
|
if (error_stanza) {
|
2016-08-20 13:16:51 -04:00
|
|
|
type = xmpp_stanza_get_type(error_stanza);
|
2014-01-27 17:35:04 -05:00
|
|
|
}
|
2014-01-27 17:09:16 -05:00
|
|
|
|
|
|
|
// stanza_get_error never returns NULL
|
2014-01-26 17:50:22 -05:00
|
|
|
char *err_msg = stanza_get_error_message(stanza);
|
|
|
|
|
2014-01-27 17:35:04 -05:00
|
|
|
GString *log_msg = g_string_new("message stanza error received");
|
2015-05-02 18:40:14 -04:00
|
|
|
if (id) {
|
2014-01-27 17:35:04 -05:00
|
|
|
g_string_append(log_msg, " id=");
|
2014-01-26 17:50:22 -05:00
|
|
|
g_string_append(log_msg, id);
|
|
|
|
}
|
2015-05-02 18:40:14 -04:00
|
|
|
if (jid) {
|
2014-01-27 17:35:04 -05:00
|
|
|
g_string_append(log_msg, " from=");
|
2014-12-28 20:17:59 -05:00
|
|
|
g_string_append(log_msg, jid);
|
2014-01-26 13:30:34 -05:00
|
|
|
}
|
2015-05-02 18:40:14 -04:00
|
|
|
if (type) {
|
2014-01-27 17:35:04 -05:00
|
|
|
g_string_append(log_msg, " type=");
|
|
|
|
g_string_append(log_msg, type);
|
|
|
|
}
|
|
|
|
g_string_append(log_msg, " error=");
|
2014-01-26 17:50:22 -05:00
|
|
|
g_string_append(log_msg, err_msg);
|
2014-01-27 17:09:16 -05:00
|
|
|
|
2014-01-26 17:50:22 -05:00
|
|
|
log_info(log_msg->str);
|
|
|
|
|
|
|
|
g_string_free(log_msg, TRUE);
|
2014-01-26 13:30:34 -05:00
|
|
|
|
2015-04-30 19:55:40 -04:00
|
|
|
if (!jid) {
|
|
|
|
ui_handle_error(err_msg);
|
|
|
|
} else if (type && (strcmp(type, "cancel") == 0)) {
|
|
|
|
log_info("Recipient %s not found: %s", jid, err_msg);
|
|
|
|
Jid *jidp = jid_create(jid);
|
|
|
|
chat_session_remove(jidp->barejid);
|
|
|
|
jid_destroy(jidp);
|
|
|
|
} else {
|
|
|
|
ui_handle_recipient_error(jid, err_msg);
|
|
|
|
}
|
2014-01-26 13:30:34 -05:00
|
|
|
|
2014-01-27 17:35:04 -05:00
|
|
|
free(err_msg);
|
2014-01-26 13:30:34 -05:00
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handel_muc_user(xmpp_stanza_t *const stanza)
|
2013-03-24 17:46:00 -04:00
|
|
|
{
|
2013-06-09 07:20:48 -04:00
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2014-01-25 15:47:32 -05:00
|
|
|
xmpp_stanza_t *xns_muc_user = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *room = xmpp_stanza_get_from(stanza);
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!room) {
|
2013-03-24 17:46:00 -04:00
|
|
|
log_warning("Message received with no from attribute, ignoring");
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2013-03-24 17:46:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// XEP-0045
|
2014-01-25 16:20:56 -05:00
|
|
|
xmpp_stanza_t *invite = xmpp_stanza_get_child_by_name(xns_muc_user, STANZA_NAME_INVITE);
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!invite) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 18:40:14 -04:00
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *invitor_jid = xmpp_stanza_get_from(invite);
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!invitor_jid) {
|
|
|
|
log_warning("Chat room invite received with no from attribute");
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 18:40:14 -04:00
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2015-05-02 18:40:14 -04:00
|
|
|
Jid *jidp = jid_create(invitor_jid);
|
|
|
|
if (!jidp) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 18:40:14 -04:00
|
|
|
}
|
|
|
|
char *invitor = jidp->barejid;
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2015-05-02 18:40:14 -04:00
|
|
|
char *reason = NULL;
|
|
|
|
xmpp_stanza_t *reason_st = xmpp_stanza_get_child_by_name(invite, STANZA_NAME_REASON);
|
|
|
|
if (reason_st) {
|
|
|
|
reason = xmpp_stanza_get_text(reason_st);
|
|
|
|
}
|
2015-03-28 22:16:41 -04:00
|
|
|
|
2015-05-02 18:40:14 -04:00
|
|
|
char *password = NULL;
|
|
|
|
xmpp_stanza_t *password_st = xmpp_stanza_get_child_by_name(xns_muc_user, STANZA_NAME_PASSWORD);
|
|
|
|
if (password_st) {
|
|
|
|
password = xmpp_stanza_get_text(password_st);
|
|
|
|
}
|
|
|
|
|
|
|
|
sv_ev_room_invite(INVITE_MEDIATED, invitor, room, reason, password);
|
|
|
|
jid_destroy(jidp);
|
|
|
|
if (reason) {
|
|
|
|
xmpp_free(ctx, reason);
|
|
|
|
}
|
|
|
|
if (password) {
|
|
|
|
xmpp_free(ctx, password);
|
2014-01-25 16:20:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handle_conference(xmpp_stanza_t *const stanza)
|
2014-01-25 16:20:56 -05:00
|
|
|
{
|
|
|
|
xmpp_stanza_t *xns_conference = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE);
|
|
|
|
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *from = xmpp_stanza_get_from(stanza);
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!from) {
|
2014-01-25 16:20:56 -05:00
|
|
|
log_warning("Message received with no from attribute, ignoring");
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2014-01-25 16:20:56 -05:00
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
Jid *jidp = jid_create(from);
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!jidp) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2014-01-25 16:20:56 -05:00
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2015-05-02 19:35:31 -04:00
|
|
|
// XEP-0249
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *room = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_JID);
|
2015-05-02 19:35:31 -04:00
|
|
|
if (!room) {
|
2015-06-24 15:53:04 -04:00
|
|
|
jid_destroy(jidp);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 19:35:31 -04:00
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *reason = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_REASON);
|
|
|
|
const char *password = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_PASSWORD);
|
2014-01-25 16:20:56 -05:00
|
|
|
|
2015-05-02 19:35:31 -04:00
|
|
|
sv_ev_room_invite(INVITE_DIRECT, jidp->barejid, room, reason, password);
|
2014-01-25 16:20:56 -05:00
|
|
|
jid_destroy(jidp);
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handle_captcha(xmpp_stanza_t *const stanza)
|
2014-01-25 16:20:56 -05:00
|
|
|
{
|
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *from = xmpp_stanza_get_from(stanza);
|
2014-01-25 16:20:56 -05:00
|
|
|
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!from) {
|
2014-01-25 16:20:56 -05:00
|
|
|
log_warning("Message received with no from attribute, ignoring");
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2014-01-25 16:20:56 -05:00
|
|
|
}
|
2013-06-09 09:05:37 -04:00
|
|
|
|
|
|
|
// XEP-0158
|
2016-08-20 14:23:32 -04:00
|
|
|
char *message = xmpp_message_get_body(stanza);
|
2015-05-02 19:35:31 -04:00
|
|
|
if (!message) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2013-03-24 17:46:00 -04:00
|
|
|
}
|
|
|
|
|
2015-05-02 19:35:31 -04:00
|
|
|
sv_ev_room_broadcast(from, message);
|
|
|
|
xmpp_free(ctx, message);
|
2013-03-24 17:46:00 -04:00
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handle_groupchat(xmpp_stanza_t *const stanza)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2013-06-09 07:20:48 -04:00
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2013-01-28 19:37:50 -05:00
|
|
|
char *message = NULL;
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *room_jid = xmpp_stanza_get_from(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
Jid *jid = jid_create(room_jid);
|
|
|
|
|
2014-04-12 22:11:46 -04:00
|
|
|
// handle room subject
|
|
|
|
xmpp_stanza_t *subject = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SUBJECT);
|
2015-05-02 18:40:14 -04:00
|
|
|
if (subject) {
|
2014-04-12 22:11:46 -04:00
|
|
|
message = xmpp_stanza_get_text(subject);
|
2015-04-28 18:38:56 -04:00
|
|
|
sv_ev_room_subject(jid->barejid, jid->resourcepart, message);
|
2014-10-04 22:05:46 -04:00
|
|
|
xmpp_free(ctx, message);
|
2014-04-12 22:11:46 -04:00
|
|
|
|
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2014-04-12 22:11:46 -04:00
|
|
|
}
|
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
// handle room broadcasts
|
2015-05-02 18:40:14 -04:00
|
|
|
if (!jid->resourcepart) {
|
2016-08-20 14:23:32 -04:00
|
|
|
message = xmpp_message_get_body(stanza);
|
2015-05-02 19:35:31 -04:00
|
|
|
if (!message) {
|
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 19:35:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sv_ev_room_broadcast(room_jid, message);
|
|
|
|
xmpp_free(ctx, message);
|
|
|
|
|
2014-04-12 22:11:46 -04:00
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2014-04-12 22:11:46 -04:00
|
|
|
}
|
2013-01-28 19:37:50 -05:00
|
|
|
|
|
|
|
if (!jid_is_valid_room_form(jid)) {
|
|
|
|
log_error("Invalid room JID: %s", jid->str);
|
2013-06-09 07:20:48 -04:00
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// room not active in profanity
|
2014-09-28 17:09:20 -04:00
|
|
|
if (!muc_active(jid->barejid)) {
|
2014-08-11 12:54:20 -04:00
|
|
|
log_error("Message received for inactive chat room: %s", jid->str);
|
2013-06-09 07:20:48 -04:00
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
|
2016-08-20 14:23:32 -04:00
|
|
|
message = xmpp_message_get_body(stanza);
|
2015-05-02 19:35:31 -04:00
|
|
|
if (!message) {
|
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 19:35:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// determine if the notifications happened whilst offline
|
2015-06-29 18:48:41 -04:00
|
|
|
GDateTime *timestamp = stanza_get_delay(stanza);
|
|
|
|
if (timestamp) {
|
|
|
|
sv_ev_room_history(jid->barejid, jid->resourcepart, timestamp, message);
|
2015-06-30 11:11:39 -04:00
|
|
|
g_date_time_unref(timestamp);
|
2015-05-02 19:35:31 -04:00
|
|
|
} else {
|
|
|
|
sv_ev_room_message(jid->barejid, jid->resourcepart, message);
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
|
2015-05-02 19:35:31 -04:00
|
|
|
xmpp_free(ctx, message);
|
2013-01-28 19:37:50 -05:00
|
|
|
jid_destroy(jid);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:28 -04:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
_message_send_receipt(const char *const fulljid, const char *const message_id)
|
2015-03-11 19:18:28 -04:00
|
|
|
{
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2016-08-20 14:08:13 -04:00
|
|
|
|
2015-08-09 18:08:36 -04:00
|
|
|
char *id = create_unique_id("receipt");
|
2016-08-20 14:08:13 -04:00
|
|
|
xmpp_stanza_t *message = xmpp_message_new(ctx, NULL, fulljid, id);
|
2015-08-09 18:08:36 -04:00
|
|
|
free(id);
|
2015-03-11 19:18:28 -04:00
|
|
|
|
|
|
|
xmpp_stanza_t *receipt = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(receipt, "received");
|
|
|
|
xmpp_stanza_set_ns(receipt, STANZA_NS_RECEIPTS);
|
2016-08-20 13:16:51 -04:00
|
|
|
xmpp_stanza_set_id(receipt, message_id);
|
2015-03-11 19:18:28 -04:00
|
|
|
|
|
|
|
xmpp_stanza_add_child(message, receipt);
|
|
|
|
xmpp_stanza_release(receipt);
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(message);
|
2015-03-11 19:18:28 -04:00
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handle_receipt_received(xmpp_stanza_t *const stanza)
|
2015-05-02 19:05:47 -04:00
|
|
|
{
|
|
|
|
xmpp_stanza_t *receipt = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS);
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *name = xmpp_stanza_get_name(receipt);
|
2015-05-02 19:05:47 -04:00
|
|
|
if (g_strcmp0(name, "received") != 0) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 19:05:47 -04:00
|
|
|
}
|
|
|
|
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *id = xmpp_stanza_get_id(receipt);
|
2015-05-02 19:05:47 -04:00
|
|
|
if (!id) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 19:05:47 -04:00
|
|
|
}
|
|
|
|
|
2016-08-20 13:16:51 -04:00
|
|
|
const char *fulljid = xmpp_stanza_get_from(stanza);
|
2015-05-02 19:05:47 -04:00
|
|
|
if (!fulljid) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 19:05:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Jid *jidp = jid_create(fulljid);
|
|
|
|
sv_ev_message_receipt(jidp->barejid, id);
|
|
|
|
jid_destroy(jidp);
|
|
|
|
}
|
|
|
|
|
2015-05-02 20:48:12 -04:00
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
_receipt_request_handler(xmpp_stanza_t *const stanza)
|
2015-05-02 20:48:12 -04:00
|
|
|
{
|
|
|
|
if (!prefs_get_boolean(PREF_RECEIPTS_SEND)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *id = xmpp_stanza_get_id(stanza);
|
2015-05-02 20:48:12 -04:00
|
|
|
if (!id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_stanza_t *receipts = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS);
|
|
|
|
if (!receipts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *receipts_name = xmpp_stanza_get_name(receipts);
|
2015-05-02 20:48:12 -04:00
|
|
|
if (g_strcmp0(receipts_name, "request") != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-20 13:16:51 -04:00
|
|
|
const gchar *from = xmpp_stanza_get_from(stanza);
|
2015-05-02 20:48:12 -04:00
|
|
|
Jid *jid = jid_create(from);
|
|
|
|
_message_send_receipt(jid->fulljid, id);
|
|
|
|
jid_destroy(jid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-25 20:14:23 -04:00
|
|
|
_private_chat_handler(xmpp_stanza_t *const stanza, const char *const fulljid)
|
2015-05-02 20:25:33 -04:00
|
|
|
{
|
2016-08-20 14:23:32 -04:00
|
|
|
char *message = xmpp_message_get_body(stanza);
|
2015-05-02 20:25:33 -04:00
|
|
|
if (!message) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-29 18:48:41 -04:00
|
|
|
GDateTime *timestamp = stanza_get_delay(stanza);
|
|
|
|
if (timestamp) {
|
|
|
|
sv_ev_delayed_private_message(fulljid, message, timestamp);
|
2015-06-30 11:11:39 -04:00
|
|
|
g_date_time_unref(timestamp);
|
2015-05-02 20:25:33 -04:00
|
|
|
} else {
|
|
|
|
sv_ev_incoming_private_message(fulljid, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
|
|
|
xmpp_free(ctx, message);
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:31:05 -04:00
|
|
|
static gboolean
|
2015-10-25 20:14:23 -04:00
|
|
|
_handle_carbons(xmpp_stanza_t *const stanza)
|
2015-05-04 16:31:05 -04:00
|
|
|
{
|
|
|
|
xmpp_stanza_t *carbons = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CARBONS);
|
|
|
|
if (!carbons) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *name = xmpp_stanza_get_name(carbons);
|
2016-08-20 16:37:20 -04:00
|
|
|
if (!name) {
|
|
|
|
log_error("Unable to retrieve stanza name for Carbon");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((g_strcmp0(name, "received") != 0) && (g_strcmp0(name, "sent") != 0)) {
|
|
|
|
log_warning("Carbon received with unrecognised stanza name: %s", name);
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-05-04 16:31:05 -04:00
|
|
|
|
2016-08-20 16:37:20 -04:00
|
|
|
xmpp_stanza_t *forwarded = xmpp_stanza_get_child_by_ns(carbons, STANZA_NS_FORWARD);
|
|
|
|
if (!forwarded) {
|
|
|
|
log_warning("Carbon received with no forwarded element");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_stanza_t *message = xmpp_stanza_get_child_by_name(forwarded, STANZA_NAME_MESSAGE);
|
|
|
|
if (!message) {
|
|
|
|
log_warning("Carbon received with no message element");
|
|
|
|
return TRUE;
|
|
|
|
}
|
2015-05-04 16:31:05 -04:00
|
|
|
|
2016-08-20 16:37:20 -04:00
|
|
|
char *message_txt = xmpp_message_get_body(message);
|
|
|
|
if (!message_txt) {
|
|
|
|
log_warning("Carbon received with no message.");
|
2015-05-04 16:31:05 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-20 16:37:20 -04:00
|
|
|
const gchar *to = xmpp_stanza_get_to(message);
|
|
|
|
const gchar *from = xmpp_stanza_get_from(message);
|
|
|
|
|
|
|
|
// happens when receive a carbon of a self sent message
|
|
|
|
if (!to) to = from;
|
|
|
|
|
|
|
|
Jid *jid_from = jid_create(from);
|
|
|
|
Jid *jid_to = jid_create(to);
|
|
|
|
Jid *my_jid = jid_create(connection_get_fulljid());
|
|
|
|
|
|
|
|
// check for pgp encrypted message
|
|
|
|
char *enc_message = NULL;
|
|
|
|
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(message, STANZA_NS_ENCRYPTED);
|
|
|
|
if (x) {
|
|
|
|
enc_message = xmpp_stanza_get_text(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we are the recipient, treat as standard incoming message
|
|
|
|
if (g_strcmp0(my_jid->barejid, jid_to->barejid) == 0) {
|
|
|
|
sv_ev_incoming_carbon(jid_from->barejid, jid_from->resourcepart, message_txt, enc_message);
|
|
|
|
|
|
|
|
// else treat as a sent message
|
|
|
|
} else {
|
|
|
|
sv_ev_outgoing_carbon(jid_to->barejid, message_txt, enc_message);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
|
|
|
xmpp_free(ctx, message_txt);
|
|
|
|
xmpp_free(ctx, enc_message);
|
|
|
|
|
|
|
|
jid_destroy(jid_from);
|
|
|
|
jid_destroy(jid_to);
|
|
|
|
jid_destroy(my_jid);
|
|
|
|
|
|
|
|
return TRUE;
|
2015-05-04 16:31:05 -04:00
|
|
|
}
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
static void
|
|
|
|
_handle_chat(xmpp_stanza_t *const stanza)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2015-05-04 16:18:53 -04:00
|
|
|
// ignore if type not chat or absent
|
2016-04-27 06:31:28 -04:00
|
|
|
const char *type = xmpp_stanza_get_type(stanza);
|
2015-05-04 16:18:53 -04:00
|
|
|
if (!(g_strcmp0(type, "chat") == 0 || type == NULL)) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-04 16:18:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if carbon message
|
2015-05-04 16:31:05 -04:00
|
|
|
gboolean res = _handle_carbons(stanza);
|
|
|
|
if (res) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-04 16:18:53 -04:00
|
|
|
}
|
|
|
|
|
2015-01-04 12:54:56 -05:00
|
|
|
// ignore handled namespaces
|
|
|
|
xmpp_stanza_t *conf = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE);
|
|
|
|
xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA);
|
2015-08-01 20:12:54 -04:00
|
|
|
if (conf || captcha) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-08-01 20:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// some clients send the mucuser namespace with private messages
|
|
|
|
// if the namespace exists, and the stanza contains a body element, assume its a private message
|
|
|
|
// otherwise exit the handler
|
|
|
|
xmpp_stanza_t *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
|
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
|
|
|
if (mucuser && body == NULL) {
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-01-04 12:54:56 -05:00
|
|
|
}
|
|
|
|
|
2016-08-20 13:16:51 -04:00
|
|
|
const gchar *from = xmpp_stanza_get_from(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
Jid *jid = jid_create(from);
|
|
|
|
|
|
|
|
// private message from chat room use full jid (room/nick)
|
2014-10-17 19:07:48 -04:00
|
|
|
if (muc_active(jid->barejid)) {
|
2015-05-02 20:25:33 -04:00
|
|
|
_private_chat_handler(stanza, jid->fulljid);
|
2015-06-24 15:53:04 -04:00
|
|
|
jid_destroy(jid);
|
2016-03-27 20:25:48 -04:00
|
|
|
return;
|
2015-05-02 20:25:33 -04:00
|
|
|
}
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2015-05-04 16:18:53 -04:00
|
|
|
// standard chat message, use jid without resource
|
2015-08-10 20:00:23 -04:00
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2015-06-29 18:48:41 -04:00
|
|
|
GDateTime *timestamp = stanza_get_delay(stanza);
|
2015-05-02 20:25:33 -04:00
|
|
|
if (body) {
|
|
|
|
char *message = xmpp_stanza_get_text(body);
|
|
|
|
if (message) {
|
2015-09-08 15:18:31 -04:00
|
|
|
char *enc_message = NULL;
|
|
|
|
xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_ENCRYPTED);
|
|
|
|
if (x) {
|
|
|
|
enc_message = xmpp_stanza_get_text(x);
|
2015-05-02 20:25:33 -04:00
|
|
|
}
|
2015-09-08 15:18:31 -04:00
|
|
|
sv_ev_incoming_message(jid->barejid, jid->resourcepart, message, enc_message, timestamp);
|
|
|
|
xmpp_free(ctx, enc_message);
|
2015-05-02 20:25:33 -04:00
|
|
|
|
2015-05-02 20:48:12 -04:00
|
|
|
_receipt_request_handler(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2015-05-02 20:25:33 -04:00
|
|
|
xmpp_free(ctx, message);
|
2015-01-11 15:20:17 -05:00
|
|
|
}
|
2015-05-02 20:25:33 -04:00
|
|
|
}
|
2015-01-11 15:20:17 -05:00
|
|
|
|
2015-05-02 20:25:33 -04:00
|
|
|
// handle chat sessions and states
|
2015-06-29 18:48:41 -04:00
|
|
|
if (!timestamp && jid->resourcepart) {
|
2015-05-02 20:25:33 -04:00
|
|
|
gboolean gone = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_GONE) != NULL;
|
|
|
|
gboolean typing = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_COMPOSING) != NULL;
|
|
|
|
gboolean paused = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PAUSED) != NULL;
|
|
|
|
gboolean inactive = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL;
|
|
|
|
if (gone) {
|
|
|
|
sv_ev_gone(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (typing) {
|
|
|
|
sv_ev_typing(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (paused) {
|
|
|
|
sv_ev_paused(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (inactive) {
|
|
|
|
sv_ev_inactive(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (stanza_contains_chat_state(stanza)) {
|
|
|
|
sv_ev_activity(jid->barejid, jid->resourcepart, TRUE);
|
|
|
|
} else {
|
|
|
|
sv_ev_activity(jid->barejid, jid->resourcepart, FALSE);
|
|
|
|
}
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
2015-05-02 20:25:33 -04:00
|
|
|
|
2015-06-30 11:11:39 -04:00
|
|
|
if (timestamp) g_date_time_unref(timestamp);
|
2015-05-02 20:25:33 -04:00
|
|
|
jid_destroy(jid);
|
2015-02-23 04:47:54 -05:00
|
|
|
}
|
2016-03-26 11:50:16 -04:00
|
|
|
|
|
|
|
static void
|
2016-03-27 20:25:48 -04:00
|
|
|
_send_message_stanza(xmpp_stanza_t *const stanza)
|
2016-03-26 11:50:16 -04:00
|
|
|
{
|
|
|
|
char *text;
|
|
|
|
size_t text_size;
|
|
|
|
xmpp_stanza_to_text(stanza, &text, &text_size);
|
|
|
|
|
2016-03-27 20:25:48 -04:00
|
|
|
xmpp_conn_t *conn = connection_get_conn();
|
2016-03-26 11:50:16 -04:00
|
|
|
char *plugin_text = plugins_on_message_stanza_send(text);
|
|
|
|
if (plugin_text) {
|
2016-03-26 12:44:02 -04:00
|
|
|
xmpp_send_raw_string(conn, "%s", plugin_text);
|
2016-05-01 18:41:57 -04:00
|
|
|
free(plugin_text);
|
2016-03-26 11:50:16 -04:00
|
|
|
} else {
|
2016-03-26 12:44:02 -04:00
|
|
|
xmpp_send_raw_string(conn, "%s", text);
|
2016-03-26 11:50:16 -04:00
|
|
|
}
|
2016-05-01 19:40:16 -04:00
|
|
|
xmpp_free(connection_get_ctx(), text);
|
2016-03-26 11:50:16 -04:00
|
|
|
}
|