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
|
|
|
*
|
2015-02-10 18:16:09 -05:00
|
|
|
* Copyright (C) 2012 - 2015 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
|
|
|
|
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-01-28 19:21:04 -05:00
|
|
|
#include <strophe.h>
|
|
|
|
|
|
|
|
#include "chat_session.h"
|
2013-02-02 16:59:29 -05:00
|
|
|
#include "config/preferences.h"
|
2013-01-28 19:37:50 -05:00
|
|
|
#include "log.h"
|
|
|
|
#include "muc.h"
|
|
|
|
#include "profanity.h"
|
2014-01-05 16:33:12 -05:00
|
|
|
#include "server_events.h"
|
2013-02-03 17:40:54 -05:00
|
|
|
#include "xmpp/connection.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"
|
2013-12-14 10:34:17 -05:00
|
|
|
#include "roster_list.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "xmpp/stanza.h"
|
|
|
|
#include "xmpp/xmpp.h"
|
2015-03-24 19:34:24 -04:00
|
|
|
#include "pgp/gpg.h"
|
2013-01-28 19:21:04 -05:00
|
|
|
|
2013-01-28 19:45:03 -05:00
|
|
|
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_MESSAGE, type, ctx)
|
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
static int _groupchat_handler(xmpp_conn_t * const conn,
|
2013-02-10 14:39:19 -05:00
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
2014-01-25 16:20:56 -05:00
|
|
|
static int _chat_handler(xmpp_conn_t * const conn,
|
2013-01-28 19:45:03 -05:00
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
2014-01-25 16:20:56 -05:00
|
|
|
static int _muc_user_handler(xmpp_conn_t * const conn,
|
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
|
|
|
static int _conference_handler(xmpp_conn_t * const conn,
|
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
|
|
|
static int _captcha_handler(xmpp_conn_t * const conn,
|
2013-03-24 17:46:00 -04:00
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
2014-01-26 13:30:34 -05:00
|
|
|
static int _message_error_handler(xmpp_conn_t * const conn,
|
|
|
|
xmpp_stanza_t * const stanza, void * const userdata);
|
2013-01-28 19:37:50 -05:00
|
|
|
|
2013-01-28 19:45:03 -05:00
|
|
|
void
|
|
|
|
message_add_handlers(void)
|
|
|
|
{
|
2013-02-03 17:40:54 -05:00
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2013-02-10 14:39:19 -05:00
|
|
|
|
2014-01-26 13:30:34 -05:00
|
|
|
HANDLE(NULL, STANZA_TYPE_ERROR, _message_error_handler);
|
2014-01-25 16:20:56 -05:00
|
|
|
HANDLE(NULL, STANZA_TYPE_GROUPCHAT, _groupchat_handler);
|
2015-01-04 12:54:56 -05:00
|
|
|
HANDLE(NULL, NULL, _chat_handler);
|
2014-01-25 16:20:56 -05:00
|
|
|
HANDLE(STANZA_NS_MUC_USER, NULL, _muc_user_handler);
|
|
|
|
HANDLE(STANZA_NS_CONFERENCE, NULL, _conference_handler);
|
|
|
|
HANDLE(STANZA_NS_CAPTCHA, NULL, _captcha_handler);
|
2013-01-28 19:45:03 -05:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:47:11 -04:00
|
|
|
char *
|
2015-01-04 18:40:10 -05:00
|
|
|
message_send_chat(const char * const barejid, const char * const msg)
|
2013-01-28 19:21:04 -05:00
|
|
|
{
|
2013-06-20 12:41:44 -04:00
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2014-12-26 19:52:34 -05:00
|
|
|
|
2015-01-07 19:57:25 -05:00
|
|
|
ChatSession *session = chat_session_get(barejid);
|
2015-03-11 19:18:28 -04:00
|
|
|
char *state = NULL;
|
|
|
|
char *jid = NULL;
|
2015-01-07 19:57:25 -05:00
|
|
|
if (session) {
|
2015-01-10 17:03:40 -05:00
|
|
|
if (prefs_get_boolean(PREF_STATES) && session->send_states) {
|
|
|
|
state = STANZA_NAME_ACTIVE;
|
|
|
|
}
|
2015-01-07 19:57:25 -05:00
|
|
|
Jid *jidp = jid_create_from_bare_and_resource(session->barejid, session->resource);
|
2015-03-11 19:18:28 -04:00
|
|
|
jid = strdup(jidp->fulljid);
|
2015-01-07 19:57:25 -05:00
|
|
|
jid_destroy(jidp);
|
2015-03-11 19:18:28 -04:00
|
|
|
|
2013-01-28 19:21:04 -05:00
|
|
|
} else {
|
2015-01-10 17:03:40 -05:00
|
|
|
if (prefs_get_boolean(PREF_STATES)) {
|
|
|
|
state = STANZA_NAME_ACTIVE;
|
|
|
|
}
|
2015-03-11 19:18:28 -04:00
|
|
|
jid = strdup(barejid);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("msg");
|
2015-03-24 19:34:24 -04:00
|
|
|
xmpp_stanza_t *message = NULL;
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBGPGME
|
|
|
|
char *account_name = jabber_get_account_name();
|
|
|
|
ProfAccount *account = accounts_get_account(account_name);
|
|
|
|
if (account->pgp_keyid) {
|
|
|
|
Jid *jidp = jid_create(jid);
|
|
|
|
char *encrypted = p_gpg_encrypt(jidp->barejid, msg);
|
|
|
|
if (encrypted) {
|
|
|
|
message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, "This message is encrypted.");
|
|
|
|
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 {
|
|
|
|
message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, msg);
|
|
|
|
#endif
|
|
|
|
|
2015-03-11 19:18:28 -04:00
|
|
|
free(jid);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
stanza_attach_state(ctx, message, state);
|
|
|
|
}
|
2015-03-19 18:57:51 -04:00
|
|
|
if (prefs_get_boolean(PREF_RECEIPTS_REQUEST)) {
|
2015-03-11 19:18:28 -04:00
|
|
|
stanza_attach_receipt_request(ctx, message);
|
2015-03-10 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_send(conn, message);
|
|
|
|
xmpp_stanza_release(message);
|
2015-03-11 19:47:11 -04:00
|
|
|
|
|
|
|
return id;
|
2015-03-10 17:16:34 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:47:11 -04:00
|
|
|
char *
|
2015-03-10 17:16:34 -04:00
|
|
|
message_send_chat_encrypted(const char * const barejid, const char * const msg)
|
|
|
|
{
|
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
|
|
|
|
ChatSession *session = chat_session_get(barejid);
|
2015-03-11 19:18:28 -04:00
|
|
|
char *state = NULL;
|
|
|
|
char *jid = NULL;
|
2015-03-10 17:16:34 -04:00
|
|
|
if (session) {
|
|
|
|
if (prefs_get_boolean(PREF_STATES) && session->send_states) {
|
|
|
|
state = STANZA_NAME_ACTIVE;
|
|
|
|
}
|
|
|
|
Jid *jidp = jid_create_from_bare_and_resource(session->barejid, session->resource);
|
2015-03-11 19:18:28 -04:00
|
|
|
jid = strdup(jidp->fulljid);
|
2015-03-10 17:16:34 -04:00
|
|
|
jid_destroy(jidp);
|
|
|
|
} else {
|
|
|
|
if (prefs_get_boolean(PREF_STATES)) {
|
|
|
|
state = STANZA_NAME_ACTIVE;
|
|
|
|
}
|
2015-03-11 19:18:28 -04:00
|
|
|
jid = strdup(barejid);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("msg");
|
|
|
|
xmpp_stanza_t *message = stanza_create_message(ctx, id, barejid, STANZA_TYPE_CHAT, msg);
|
2015-03-11 19:18:28 -04:00
|
|
|
free(jid);
|
|
|
|
|
|
|
|
if (state) {
|
|
|
|
stanza_attach_state(ctx, message, state);
|
|
|
|
}
|
|
|
|
stanza_attach_carbons_private(ctx, message);
|
2015-03-19 18:57:51 -04:00
|
|
|
if (prefs_get_boolean(PREF_RECEIPTS_REQUEST)) {
|
2015-03-11 19:18:28 -04:00
|
|
|
stanza_attach_receipt_request(ctx, message);
|
2013-01-28 19:21:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
xmpp_send(conn, message);
|
|
|
|
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
|
|
|
|
message_send_private(const char * const fulljid, const char * const msg)
|
2013-01-28 19:37:50 -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();
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("prv");
|
|
|
|
xmpp_stanza_t *message = stanza_create_message(ctx, id, fulljid, STANZA_TYPE_CHAT, msg);
|
|
|
|
free(id);
|
2013-05-04 21:31:04 -04:00
|
|
|
|
|
|
|
xmpp_send(conn, message);
|
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
|
|
|
message_send_groupchat(const char * const roomjid, const char * const msg)
|
2014-10-04 22:05:46 -04:00
|
|
|
{
|
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2015-03-11 19:47:11 -04:00
|
|
|
char *id = create_unique_id("muc");
|
|
|
|
xmpp_stanza_t *message = stanza_create_message(ctx, id, roomjid, STANZA_TYPE_GROUPCHAT, msg);
|
|
|
|
free(id);
|
2014-10-04 22:05:46 -04:00
|
|
|
|
|
|
|
xmpp_send(conn, message);
|
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
|
|
|
message_send_groupchat_subject(const char * const roomjid, const char * const subject)
|
2014-12-19 19:52:35 -05:00
|
|
|
{
|
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
xmpp_stanza_t *message = stanza_create_room_subject_message(ctx, roomjid, subject);
|
|
|
|
|
|
|
|
xmpp_send(conn, message);
|
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
|
|
|
message_send_invite(const char * const roomjid, const char * const contact,
|
2013-04-20 15:18:13 -04:00
|
|
|
const char * const reason)
|
|
|
|
{
|
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
2014-12-19 19:52:35 -05:00
|
|
|
xmpp_stanza_t *stanza = stanza_create_invite(ctx, roomjid, contact, reason);
|
2013-04-20 15:18:13 -04:00
|
|
|
|
|
|
|
xmpp_send(conn, stanza);
|
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-01-04 18:40:10 -05: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_conn_t * const conn = connection_get_conn();
|
|
|
|
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);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_send(conn, stanza);
|
|
|
|
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-01-04 18:40:10 -05: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_conn_t * const conn = connection_get_conn();
|
|
|
|
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);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_send(conn, stanza);
|
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-01-04 18:40:10 -05: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_conn_t * const conn = connection_get_conn();
|
|
|
|
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
|
|
|
|
|
|
|
xmpp_send(conn, stanza);
|
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-22 17:13:42 -05:00
|
|
|
void
|
2015-01-04 18:40:10 -05: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_conn_t * const conn = connection_get_conn();
|
|
|
|
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);
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_send(conn, stanza);
|
|
|
|
xmpp_stanza_release(stanza);
|
|
|
|
}
|
|
|
|
|
2014-01-26 13:30:34 -05:00
|
|
|
static int
|
|
|
|
_message_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
|
|
|
void * const userdata)
|
|
|
|
{
|
|
|
|
char *id = xmpp_stanza_get_id(stanza);
|
2014-12-28 20:17:59 -05:00
|
|
|
char *jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
2014-01-27 17:35:04 -05:00
|
|
|
xmpp_stanza_t *error_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR);
|
|
|
|
char *type = NULL;
|
|
|
|
if (error_stanza != NULL) {
|
|
|
|
type = xmpp_stanza_get_attribute(error_stanza, STANZA_ATTR_TYPE);
|
|
|
|
}
|
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");
|
2014-01-26 13:30:34 -05:00
|
|
|
if (id != NULL) {
|
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);
|
|
|
|
}
|
2014-12-28 20:17:59 -05:00
|
|
|
if (jid != NULL) {
|
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
|
|
|
}
|
2014-01-27 17:35:04 -05:00
|
|
|
if (type != NULL) {
|
|
|
|
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
|
|
|
|
2014-12-28 20:17:59 -05:00
|
|
|
handle_message_error(jid, type, 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
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-03-24 17:46:00 -04:00
|
|
|
static int
|
2014-01-25 16:20:56 -05:00
|
|
|
_muc_user_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
|
|
|
void * const userdata)
|
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);
|
2014-01-25 16:20:56 -05:00
|
|
|
char *room = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
if (room == NULL) {
|
2013-03-24 17:46:00 -04:00
|
|
|
log_warning("Message received with no from attribute, ignoring");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
if (invite != NULL) {
|
2013-03-24 17:46:00 -04:00
|
|
|
char *invitor_jid = xmpp_stanza_get_attribute(invite, STANZA_ATTR_FROM);
|
|
|
|
if (invitor_jid == NULL) {
|
|
|
|
log_warning("Chat room invite received with no from attribute");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Jid *jidp = jid_create(invitor_jid);
|
2013-08-03 07:38:38 -04:00
|
|
|
if (jidp == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-25 19:36:36 -04:00
|
|
|
char *invitor = jidp->barejid;
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-04-25 19:36:36 -04:00
|
|
|
char *reason = NULL;
|
2013-03-24 17:46:00 -04:00
|
|
|
xmpp_stanza_t *reason_st = xmpp_stanza_get_child_by_name(invite, STANZA_NAME_REASON);
|
|
|
|
if (reason_st != NULL) {
|
|
|
|
reason = xmpp_stanza_get_text(reason_st);
|
|
|
|
}
|
|
|
|
|
2014-01-05 16:33:12 -05:00
|
|
|
handle_room_invite(INVITE_MEDIATED, invitor, room, reason);
|
2013-03-24 17:46:00 -04:00
|
|
|
jid_destroy(jidp);
|
2013-06-09 07:20:48 -04:00
|
|
|
if (reason != NULL) {
|
|
|
|
xmpp_free(ctx, reason);
|
|
|
|
}
|
2014-01-25 16:20:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_conference_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
|
|
|
void * const userdata)
|
|
|
|
{
|
|
|
|
xmpp_stanza_t *xns_conference = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE);
|
|
|
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
|
|
|
char *room = NULL;
|
|
|
|
char *invitor = NULL;
|
|
|
|
char *reason = NULL;
|
|
|
|
|
|
|
|
if (from == NULL) {
|
|
|
|
log_warning("Message received with no from attribute, ignoring");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
|
|
|
// XEP-0429
|
2014-01-25 16:20:56 -05:00
|
|
|
room = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_JID);
|
|
|
|
if (room == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
Jid *jidp = jid_create(from);
|
|
|
|
if (jidp == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
invitor = jidp->barejid;
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
reason = xmpp_stanza_get_attribute(xns_conference, STANZA_ATTR_REASON);
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
handle_room_invite(INVITE_DIRECT, invitor, room, reason);
|
|
|
|
|
|
|
|
jid_destroy(jidp);
|
2013-03-24 17:46:00 -04:00
|
|
|
|
2014-01-25 16:20:56 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_captcha_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
|
|
|
void * const userdata)
|
|
|
|
{
|
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
|
|
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
|
|
|
|
|
|
|
if (from == NULL) {
|
|
|
|
log_warning("Message received with no from attribute, ignoring");
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-09 09:05:37 -04:00
|
|
|
|
|
|
|
// XEP-0158
|
2014-01-25 16:20:56 -05:00
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
|
|
|
if (body != NULL) {
|
|
|
|
char *message = xmpp_stanza_get_text(body);
|
|
|
|
if (message != NULL) {
|
|
|
|
handle_room_broadcast(from, message);
|
|
|
|
xmpp_free(ctx, message);
|
2013-06-09 09:05:37 -04:00
|
|
|
}
|
2013-03-24 17:46:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:45:03 -05:00
|
|
|
static int
|
2014-01-25 16:20:56 -05:00
|
|
|
_groupchat_handler(xmpp_conn_t * const conn,
|
2013-01-28 19:37:50 -05:00
|
|
|
xmpp_stanza_t * const stanza, void * const userdata)
|
|
|
|
{
|
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;
|
|
|
|
char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
|
|
|
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);
|
|
|
|
if (subject != NULL) {
|
|
|
|
message = xmpp_stanza_get_text(subject);
|
2014-10-04 22:05:46 -04:00
|
|
|
handle_room_subject(jid->barejid, jid->resourcepart, message);
|
|
|
|
xmpp_free(ctx, message);
|
2014-04-12 22:11:46 -04:00
|
|
|
|
|
|
|
jid_destroy(jid);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
// handle room broadcasts
|
|
|
|
if (jid->resourcepart == NULL) {
|
2014-04-12 22:11:46 -04:00
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
|
|
|
if (body != NULL) {
|
|
|
|
message = xmpp_stanza_get_text(body);
|
2013-01-28 19:37:50 -05:00
|
|
|
if (message != NULL) {
|
2014-04-12 22:11:46 -04:00
|
|
|
handle_room_broadcast(room_jid, message);
|
2013-08-25 07:54:34 -04:00
|
|
|
xmpp_free(ctx, message);
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-12 22:11:46 -04:00
|
|
|
jid_destroy(jid);
|
|
|
|
return 1;
|
|
|
|
}
|
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);
|
2013-01-28 19:37:50 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2013-01-28 19:37:50 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine if the notifications happened whilst offline
|
|
|
|
GTimeVal tv_stamp;
|
|
|
|
gboolean delayed = stanza_get_delay(stanza, &tv_stamp);
|
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
|
|
|
|
|
|
|
// check for and deal with message
|
|
|
|
if (body != NULL) {
|
2013-06-09 07:20:48 -04:00
|
|
|
message = xmpp_stanza_get_text(body);
|
|
|
|
if (message != NULL) {
|
|
|
|
if (delayed) {
|
2014-01-05 16:33:12 -05:00
|
|
|
handle_room_history(jid->barejid, jid->resourcepart, tv_stamp, message);
|
2013-06-09 07:20:48 -04:00
|
|
|
} else {
|
2014-01-05 16:33:12 -05:00
|
|
|
handle_room_message(jid->barejid, jid->resourcepart, message);
|
2013-06-09 07:20:48 -04:00
|
|
|
}
|
|
|
|
xmpp_free(ctx, message);
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jid_destroy(jid);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:28 -04:00
|
|
|
void
|
|
|
|
_message_send_receipt(const char * const fulljid, const char * const message_id)
|
|
|
|
{
|
|
|
|
xmpp_conn_t * const conn = connection_get_conn();
|
|
|
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
|
|
|
xmpp_stanza_t *message = xmpp_stanza_new(ctx);
|
|
|
|
char *id = create_unique_id("receipt");
|
|
|
|
xmpp_stanza_set_name(message, STANZA_NAME_MESSAGE);
|
|
|
|
xmpp_stanza_set_id(message, id);
|
|
|
|
xmpp_stanza_set_attribute(message, STANZA_ATTR_TO, fulljid);
|
|
|
|
|
|
|
|
xmpp_stanza_t *receipt = xmpp_stanza_new(ctx);
|
|
|
|
xmpp_stanza_set_name(receipt, "received");
|
|
|
|
xmpp_stanza_set_ns(receipt, STANZA_NS_RECEIPTS);
|
|
|
|
xmpp_stanza_set_attribute(receipt, STANZA_ATTR_ID, message_id);
|
|
|
|
|
|
|
|
xmpp_stanza_add_child(message, receipt);
|
|
|
|
xmpp_stanza_release(receipt);
|
|
|
|
|
|
|
|
xmpp_send(conn, message);
|
|
|
|
xmpp_stanza_release(message);
|
|
|
|
}
|
|
|
|
|
2013-01-28 19:37:50 -05:00
|
|
|
static int
|
2014-01-25 16:20:56 -05:00
|
|
|
_chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
2013-02-10 14:39:19 -05:00
|
|
|
void * const userdata)
|
2013-01-28 19:37:50 -05:00
|
|
|
{
|
2015-01-04 12:54:56 -05:00
|
|
|
// ignore if type not chat or absent
|
|
|
|
char *type = xmpp_stanza_get_type(stanza);
|
|
|
|
if (!(g_strcmp0(type, "chat") == 0 || type == NULL)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:28 -04:00
|
|
|
// check if message receipt
|
|
|
|
xmpp_stanza_t *receipt = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS);
|
|
|
|
if (receipt) {
|
|
|
|
char *name = xmpp_stanza_get_name(receipt);
|
|
|
|
if (g_strcmp0(name, "received") == 0) {
|
|
|
|
char *id = xmpp_stanza_get_attribute(receipt, STANZA_ATTR_ID);
|
|
|
|
if (id) {
|
|
|
|
char *fulljid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
|
|
|
if (fulljid) {
|
|
|
|
Jid *jidp = jid_create(fulljid);
|
|
|
|
handle_message_receipt(jidp->barejid, id);
|
|
|
|
jid_destroy(jidp);
|
2015-03-14 19:25:20 -04:00
|
|
|
return 1;
|
2015-03-11 19:18:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:10:05 -05:00
|
|
|
// check if carbon message
|
2015-03-11 19:18:28 -04:00
|
|
|
xmpp_stanza_t *carbons = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CARBONS);
|
2015-03-14 12:37:33 -04:00
|
|
|
if(carbons) {
|
|
|
|
char *name = xmpp_stanza_get_name(carbons);
|
2015-03-14 12:44:21 -04:00
|
|
|
if ((g_strcmp0(name, "received") == 0) || (g_strcmp0(name, "sent")) == 0){
|
2015-03-14 12:37:33 -04:00
|
|
|
xmpp_stanza_t *forwarded = xmpp_stanza_get_child_by_ns(carbons, STANZA_NS_FORWARD);
|
|
|
|
xmpp_stanza_t *message = xmpp_stanza_get_child_by_name(forwarded, STANZA_NAME_MESSAGE);
|
2015-02-02 05:10:05 -05:00
|
|
|
|
2015-03-14 12:37:33 -04:00
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2015-02-02 05:10:05 -05:00
|
|
|
|
2015-03-14 12:37:33 -04:00
|
|
|
gchar *to = xmpp_stanza_get_attribute(message, STANZA_ATTR_TO);
|
|
|
|
gchar *from = xmpp_stanza_get_attribute(message, STANZA_ATTR_FROM);
|
2015-02-21 16:22:21 -05:00
|
|
|
|
2015-03-14 12:37:33 -04:00
|
|
|
// happens when receive a carbon of a self sent message
|
|
|
|
if(to == NULL) {
|
|
|
|
to = from;
|
|
|
|
}
|
2015-02-02 05:10:05 -05:00
|
|
|
|
2015-03-14 12:37:33 -04:00
|
|
|
Jid *jid_from = jid_create(from);
|
|
|
|
Jid *jid_to = jid_create(to);
|
|
|
|
Jid *my_jid = jid_create(jabber_get_fulljid());
|
|
|
|
|
|
|
|
// check for and deal with message
|
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(message, STANZA_NAME_BODY);
|
|
|
|
if (body != NULL) {
|
|
|
|
char *message = xmpp_stanza_get_text(body);
|
|
|
|
if (message != NULL) {
|
|
|
|
// if we are the recipient, treat as standard incoming message
|
|
|
|
if(g_strcmp0(my_jid->barejid, jid_to->barejid) == 0){
|
|
|
|
handle_incoming_message(jid_from->barejid, jid_from->resourcepart, message);
|
|
|
|
}
|
|
|
|
// else treat as a sent message
|
|
|
|
else{
|
|
|
|
handle_carbon(jid_to->barejid, message);
|
|
|
|
}
|
|
|
|
xmpp_free(ctx, message);
|
2015-02-02 05:10:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 12:37:33 -04:00
|
|
|
jid_destroy(jid_from);
|
|
|
|
jid_destroy(jid_to);
|
|
|
|
jid_destroy(my_jid);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-02-02 05:10:05 -05: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 *mucuser = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_MUC_USER);
|
|
|
|
xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA);
|
|
|
|
if (conf || mucuser || captcha) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-09 07:20:48 -04:00
|
|
|
xmpp_ctx_t *ctx = connection_get_ctx();
|
2013-01-28 19:37:50 -05:00
|
|
|
gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
2015-01-04 12:54:56 -05:00
|
|
|
|
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)) {
|
2013-01-28 19:37:50 -05:00
|
|
|
// determine if the notifications happened whilst offline
|
|
|
|
GTimeVal tv_stamp;
|
|
|
|
gboolean delayed = stanza_get_delay(stanza, &tv_stamp);
|
|
|
|
|
|
|
|
// check for and deal with message
|
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
|
|
|
if (body != NULL) {
|
|
|
|
char *message = xmpp_stanza_get_text(body);
|
2013-06-09 07:20:48 -04:00
|
|
|
if (message != NULL) {
|
|
|
|
if (delayed) {
|
2014-12-10 17:23:33 -05:00
|
|
|
handle_delayed_private_message(jid->str, message, tv_stamp);
|
2013-06-09 07:20:48 -04:00
|
|
|
} else {
|
2014-12-10 17:23:33 -05:00
|
|
|
handle_incoming_private_message(jid->str, message);
|
2013-06-09 07:20:48 -04:00
|
|
|
}
|
|
|
|
xmpp_free(ctx, message);
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 07:20:48 -04:00
|
|
|
jid_destroy(jid);
|
2013-01-28 19:37:50 -05:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
// standard chat message, use jid without resource
|
|
|
|
} else {
|
|
|
|
// determine if the notifications happened whilst offline
|
|
|
|
GTimeVal tv_stamp;
|
|
|
|
gboolean delayed = stanza_get_delay(stanza, &tv_stamp);
|
2015-03-11 19:18:28 -04:00
|
|
|
char *id = xmpp_stanza_get_id(stanza);
|
2013-01-28 19:37:50 -05:00
|
|
|
|
|
|
|
// check for and deal with message
|
|
|
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
|
|
|
if (body != NULL) {
|
|
|
|
char *message = xmpp_stanza_get_text(body);
|
2013-06-09 07:20:48 -04:00
|
|
|
if (message != NULL) {
|
|
|
|
if (delayed) {
|
2014-12-10 17:23:33 -05:00
|
|
|
handle_delayed_message(jid->barejid, message, tv_stamp);
|
2013-06-09 07:20:48 -04:00
|
|
|
} else {
|
2015-01-10 13:35:59 -05:00
|
|
|
handle_incoming_message(jid->barejid, jid->resourcepart, message);
|
2013-06-09 07:20:48 -04:00
|
|
|
}
|
2015-03-19 18:57:51 -04:00
|
|
|
if (id && prefs_get_boolean(PREF_RECEIPTS_SEND)) {
|
2015-03-11 19:18:28 -04:00
|
|
|
xmpp_stanza_t *receipts = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_RECEIPTS);
|
|
|
|
if (receipts) {
|
|
|
|
char *receipts_name = xmpp_stanza_get_name(receipts);
|
|
|
|
if (g_strcmp0(receipts_name, "request") == 0) {
|
|
|
|
_message_send_receipt(jid->fulljid, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-09 07:20:48 -04:00
|
|
|
xmpp_free(ctx, message);
|
2013-01-28 19:37:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 15:20:17 -05:00
|
|
|
// handle chat sessions and states
|
|
|
|
if (!delayed && jid->resourcepart) {
|
|
|
|
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) {
|
2015-01-11 18:04:22 -05:00
|
|
|
handle_gone(jid->barejid, jid->resourcepart);
|
2015-01-11 15:20:17 -05:00
|
|
|
} else if (typing) {
|
|
|
|
handle_typing(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (paused) {
|
|
|
|
handle_paused(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (inactive) {
|
|
|
|
handle_inactive(jid->barejid, jid->resourcepart);
|
|
|
|
} else if (stanza_contains_chat_state(stanza)) {
|
|
|
|
handle_activity(jid->barejid, jid->resourcepart, TRUE);
|
|
|
|
} else {
|
|
|
|
handle_activity(jid->barejid, jid->resourcepart, FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 07:20:48 -04:00
|
|
|
jid_destroy(jid);
|
2013-01-28 19:37:50 -05:00
|
|
|
return 1;
|
|
|
|
}
|
2015-02-23 04:47:54 -05:00
|
|
|
}
|