1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00
profanity/src/event/server_events.c

1311 lines
39 KiB
C
Raw Normal View History

2014-01-05 15:52:45 -05:00
/*
* server_events.c
2019-11-13 06:11:05 -05:00
* vim: expandtab:ts=4:sts=4:sw=4
2014-01-05 15:52:45 -05:00
*
2019-01-22 05:31:45 -05:00
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
2021-01-08 10:36:30 -05:00
* Copyright (C) 2019 - 2021 Michael Vetter <jubalh@iodoru.org>
2014-01-05 15:52:45 -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/>.
2014-01-05 15:52:45 -05: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.
*
2014-01-05 15:52:45 -05:00
*/
2016-03-31 16:05:02 -04:00
#include "config.h"
2015-10-31 19:38:08 -04:00
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "config/accounts.h"
#include "profanity.h"
#include "log.h"
#include "database.h"
#include "config/preferences.h"
#include "config/tlscerts.h"
#include "config/account.h"
#include "config/scripts.h"
2016-07-24 10:43:51 -04:00
#include "event/client_events.h"
#include "event/common.h"
#include "plugins/plugins.h"
#include "ui/window_list.h"
#include "tools/bookmark_ignore.h"
#include "xmpp/xmpp.h"
#include "xmpp/muc.h"
#include "xmpp/chat_session.h"
2016-07-24 10:43:51 -04:00
#include "xmpp/roster_list.h"
#include "xmpp/avatar.h"
2016-03-31 16:05:02 -04:00
#ifdef HAVE_LIBOTR
2014-02-12 17:19:21 -05:00
#include "otr/otr.h"
#endif
2016-07-24 10:43:51 -04:00
2016-03-31 16:05:02 -04:00
#ifdef HAVE_LIBGPGME
#include "pgp/gpg.h"
#endif
2014-01-05 15:52:45 -05:00
#ifdef HAVE_OMEMO
#include "omemo/omemo.h"
#endif
2014-04-06 16:35:17 -04:00
#include "ui/ui.h"
2020-07-07 08:18:57 -04:00
static void _clean_incoming_message(ProfMessage* message);
static void _sv_ev_incoming_plain(ProfChatWin* chatwin, gboolean new_win, ProfMessage* message, gboolean logit);
void
2020-07-07 08:18:57 -04:00
sv_ev_login_account_success(char* account_name, gboolean secured)
{
2020-07-07 08:18:57 -04:00
ProfAccount* account = accounts_get_account(account_name);
2014-04-07 15:41:06 -04:00
bookmark_ignore_on_connect(account->jid);
roster_create();
2016-03-31 16:05:02 -04:00
#ifdef HAVE_LIBOTR
otr_on_connect(account);
#endif
2016-03-31 16:05:02 -04:00
#ifdef HAVE_LIBGPGME
2015-06-23 18:29:10 -04:00
p_gpg_on_connect(account->jid);
#endif
#ifdef HAVE_OMEMO
omemo_on_connect(account);
#endif
2020-03-23 07:18:25 -04:00
log_database_init(account);
avatar_pep_subscribe();
2015-10-13 19:23:12 -04:00
ui_handle_login_account_success(account, secured);
2014-04-07 15:41:06 -04:00
// attempt to rejoin all rooms
2020-07-07 08:18:57 -04:00
GList* rooms = muc_rooms();
GList* curr = rooms;
while (curr) {
2020-07-07 08:18:57 -04:00
char* password = muc_password(curr->data);
char* nick = muc_nick(curr->data);
presence_join_room(curr->data, nick, password);
curr = g_list_next(curr);
}
g_list_free(rooms);
2014-04-07 15:41:06 -04:00
log_info("%s logged in successfully", account->jid);
// if we have been connected before
2020-07-07 08:18:57 -04:00
if (ev_was_connected_already()) {
cons_show("Connection re-established.");
wins_reestablished_connection();
}
ev_inc_connection_counter();
if (account->startscript) {
scripts_exec(account->startscript);
}
2014-01-22 17:22:01 -05:00
account_free(account);
2014-11-19 19:32:33 -05:00
}
2014-11-19 19:32:33 -05:00
void
2015-04-28 18:38:56 -04:00
sv_ev_roster_received(void)
2014-11-19 19:32:33 -05:00
{
roster_process_pending_presence();
if (prefs_get_boolean(PREF_ROSTER)) {
ui_show_roster();
}
2020-07-07 08:18:57 -04:00
char* account_name = session_get_account_name();
2015-12-14 19:38:16 -05:00
2016-03-31 16:05:02 -04:00
#ifdef HAVE_LIBGPGME
2015-12-14 19:38:16 -05:00
// check pgp key valid if specified
2020-07-07 08:18:57 -04:00
ProfAccount* account = accounts_get_account(account_name);
2015-12-14 19:38:16 -05:00
if (account && account->pgp_keyid) {
2020-07-07 08:18:57 -04:00
char* err_str = NULL;
2015-12-14 19:38:16 -05:00
if (!p_gpg_valid_key(account->pgp_keyid, &err_str)) {
2015-12-19 17:55:33 -05:00
cons_show_error("Invalid PGP key ID specified: %s, %s", account->pgp_keyid, err_str);
2015-12-14 19:38:16 -05:00
}
free(err_str);
// Redraw the screen after entry of the PGP secret key, but not init
2020-07-07 08:18:57 -04:00
ProfWin* win = wins_get_current();
char* theme = prefs_get_string(PREF_THEME);
win_clear(win);
theme_init(theme);
g_free(theme);
ui_resize();
ui_show_roster();
2015-12-14 19:38:16 -05:00
}
2016-02-13 20:49:57 -05:00
account_free(account);
#endif
// send initial presence
2015-12-14 19:38:16 -05:00
resource_presence_t conn_presence = accounts_get_login_presence(account_name);
2020-07-07 08:18:57 -04:00
char* last_activity_str = accounts_get_last_activity(account_name);
char* status_message = accounts_get_login_status(account_name);
int diff_secs = 0;
if (prefs_get_boolean(PREF_LASTACTIVITY) && last_activity_str) {
GTimeVal lasttv;
2020-07-07 08:18:57 -04:00
GDateTime* nowdt = g_date_time_new_now_utc();
gboolean res = g_time_val_from_iso8601(last_activity_str, &lasttv);
if (res) {
2020-07-07 08:18:57 -04:00
GDateTime* lastdt = g_date_time_new_from_timeval_utc(&lasttv);
GTimeSpan diff_micros = g_date_time_difference(nowdt, lastdt);
diff_secs = (diff_micros / 1000) / 1000;
g_date_time_unref(lastdt);
}
g_date_time_unref(nowdt);
}
connection_set_presence_msg(status_message);
cl_ev_presence_send(conn_presence, diff_secs);
g_free(status_message);
g_free(last_activity_str);
2020-07-07 08:18:57 -04:00
const char* fulljid = connection_get_fulljid();
plugins_on_connect(account_name, fulljid);
}
void
sv_ev_connection_features_received(void)
{
#ifdef HAVE_OMEMO
omemo_publish_crypto_materials();
#endif
}
void
2015-04-28 18:38:56 -04:00
sv_ev_lost_connection(void)
{
cons_show_error("Lost connection.");
2016-01-03 12:23:36 -05:00
2016-03-31 16:05:02 -04:00
#ifdef HAVE_LIBOTR
2020-07-07 08:18:57 -04:00
GSList* recipients = wins_get_chat_recipients();
GSList* curr = recipients;
while (curr) {
2020-07-07 08:18:57 -04:00
char* barejid = curr->data;
ProfChatWin* chatwin = wins_get_chat(barejid);
if (chatwin && otr_is_secure(barejid)) {
chatwin_otr_unsecured(chatwin);
otr_end_session(barejid);
}
curr = g_slist_next(curr);
}
if (recipients) {
g_slist_free(recipients);
}
#endif
ev_disconnect_cleanup();
}
void
2015-04-28 18:38:56 -04:00
sv_ev_failed_login(void)
{
cons_show_error("Login failed.");
log_info("Login failed");
tlscerts_clear_current();
}
void
2020-07-07 08:18:57 -04:00
sv_ev_room_invite(jabber_invite_t invite_type, const char* const invitor, const char* const room,
const char* const reason, const char* const password)
{
2014-09-28 17:09:20 -04:00
if (!muc_active(room) && !muc_invites_contain(room)) {
cons_show_room_invite(invitor, room, reason);
2015-03-28 22:16:41 -04:00
muc_invites_add(room, password);
}
}
void
2020-07-07 08:18:57 -04:00
sv_ev_room_broadcast(const char* const room_jid, const char* const message)
{
2014-09-28 17:09:20 -04:00
if (muc_roster_complete(room_jid)) {
2020-07-07 08:18:57 -04:00
ProfMucWin* mucwin = wins_get_muc(room_jid);
2015-11-01 19:36:25 -05:00
if (mucwin) {
mucwin_broadcast(mucwin, message);
}
} else {
2014-09-28 17:09:20 -04:00
muc_pending_broadcasts_add(room_jid, message);
}
}
void
2020-07-07 08:18:57 -04:00
sv_ev_room_subject(const char* const room, const char* const nick, const char* const subject)
{
2014-10-04 22:05:46 -04:00
muc_set_subject(room, subject);
2020-07-07 08:18:57 -04:00
ProfMucWin* mucwin = wins_get_muc(room);
if (mucwin && muc_roster_complete(room) && ev_is_first_connect()) {
2015-11-01 19:29:35 -05:00
mucwin_subject(mucwin, nick, subject);
}
}
void
2020-07-07 08:18:57 -04:00
sv_ev_room_history(ProfMessage* message)
{
2020-07-07 08:18:57 -04:00
ProfMucWin* mucwin = wins_get_muc(message->from_jid->barejid);
2015-11-01 19:19:46 -05:00
if (mucwin) {
// if this is the first successful connection
if (ev_is_first_connect()) {
// save timestamp of last received muc message
// so we dont display, if there was no activity in channel, once we reconnect
if (mucwin->last_msg_timestamp) {
g_date_time_unref(mucwin->last_msg_timestamp);
}
2020-07-07 08:18:57 -04:00
mucwin->last_msg_timestamp = g_date_time_new_now_local();
}
gboolean younger = g_date_time_compare(mucwin->last_msg_timestamp, message->timestamp) < 0 ? TRUE : FALSE;
2020-07-07 08:18:57 -04:00
if (ev_is_first_connect() || younger) {
mucwin_history(mucwin, message);
}
2015-11-01 19:19:46 -05:00
}
}
2020-07-07 08:18:57 -04:00
static void
_log_muc(ProfMessage* message)
{
if (message->enc == PROF_MSG_ENC_OMEMO) {
groupchat_log_omemo_msg_in(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
} else {
groupchat_log_msg_in(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
}
log_database_add_incoming(message);
}
void
2020-07-07 08:18:57 -04:00
sv_ev_room_message(ProfMessage* message)
{
2020-07-07 08:18:57 -04:00
ProfMucWin* mucwin = wins_get_muc(message->from_jid->barejid);
if (!mucwin) {
return;
}
2020-07-07 08:18:57 -04:00
char* mynick = muc_nick(mucwin->roomjid);
2019-10-19 17:24:33 -04:00
// only log message not coming from this client (but maybe same account, different client)
// our messages are logged when outgoing
if (!(g_strcmp0(mynick, message->from_jid->resourcepart) == 0 && message_is_sent_by_us(message, TRUE))) {
_log_muc(message);
}
2020-07-07 08:18:57 -04:00
char* old_plain = message->plain;
message->plain = plugins_pre_room_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
2020-07-07 08:18:57 -04:00
GSList* mentions = get_mentions(prefs_get_boolean(PREF_NOTIFY_MENTION_WHOLE_WORD), prefs_get_boolean(PREF_NOTIFY_MENTION_CASE_SENSITIVE), message->plain, mynick);
gboolean mention = g_slist_length(mentions) > 0;
2020-07-07 08:18:57 -04:00
GList* triggers = prefs_message_get_triggers(message->plain);
_clean_incoming_message(message);
mucwin_incoming_msg(mucwin, message, mentions, triggers, TRUE);
2016-04-06 20:14:12 -04:00
g_slist_free(mentions);
2020-07-07 08:18:57 -04:00
ProfWin* window = (ProfWin*)mucwin;
int num = wins_get_num(window);
gboolean is_current = FALSE;
// currently in groupchat window
if (wins_is_current(window)) {
is_current = TRUE;
2018-03-09 16:11:59 -05:00
status_bar_active(num, WIN_MUC, mucwin->roomjid);
if ((g_strcmp0(mynick, message->from_jid->resourcepart) != 0) && (prefs_get_boolean(PREF_BEEP))) {
beep();
}
2020-07-07 08:18:57 -04:00
// not currently on groupchat window
} else {
2018-03-09 16:11:59 -05:00
status_bar_new(num, WIN_MUC, mucwin->roomjid);
if ((g_strcmp0(mynick, message->from_jid->resourcepart) != 0) && (prefs_get_boolean(PREF_FLASH))) {
flash();
}
cons_show_incoming_room_message(message->from_jid->resourcepart, mucwin->roomjid, num, mention, triggers, mucwin->unread, window);
mucwin->unread++;
if (mention) {
mucwin->unread_mentions = TRUE;
}
if (triggers) {
mucwin->unread_triggers = TRUE;
}
}
// save timestamp of last received muc message
if (mucwin->last_msg_timestamp) {
g_date_time_unref(mucwin->last_msg_timestamp);
}
2020-07-07 08:18:57 -04:00
mucwin->last_msg_timestamp = g_date_time_new_now_local();
if (prefs_do_room_notify(is_current, mucwin->roomjid, mynick, message->from_jid->resourcepart, message->plain, mention, triggers != NULL)) {
2020-07-07 08:18:57 -04:00
Jid* jidp = jid_create(mucwin->roomjid);
if (jidp) {
notify_room_message(message->from_jid->resourcepart, jidp->localpart, num, message->plain);
jid_destroy(jidp);
}
}
if (triggers) {
g_list_free_full(triggers, free);
}
2016-01-01 21:41:51 -05:00
rosterwin_roster();
plugins_post_room_message_display(message->from_jid->barejid, message->from_jid->resourcepart, message->plain);
free(message->plain);
message->plain = old_plain;
}
void
2020-07-07 08:18:57 -04:00
sv_ev_incoming_private_message(ProfMessage* message)
{
2020-07-07 08:18:57 -04:00
char* old_plain = message->plain;
message->plain = plugins_pre_priv_message_display(message->from_jid->fulljid, message->plain);
2020-07-07 08:18:57 -04:00
ProfPrivateWin* privatewin = wins_get_private(message->from_jid->fulljid);
if (privatewin == NULL) {
2020-07-07 08:18:57 -04:00
ProfWin* window = wins_new_private(message->from_jid->fulljid);
privatewin = (ProfPrivateWin*)window;
}
_clean_incoming_message(message);
privwin_incoming_msg(privatewin, message);
log_database_add_incoming(message);
chat_log_msg_in(message);
plugins_post_priv_message_display(message->from_jid->fulljid, message->plain);
free(message->plain);
message->plain = old_plain;
2016-01-23 19:33:24 -05:00
rosterwin_roster();
}
void
2020-07-07 08:18:57 -04:00
sv_ev_delayed_private_message(ProfMessage* message)
{
2020-07-07 08:18:57 -04:00
char* old_plain = message->plain;
message->plain = plugins_pre_priv_message_display(message->from_jid->fulljid, message->plain);
2020-07-07 08:18:57 -04:00
ProfPrivateWin* privatewin = wins_get_private(message->from_jid->fulljid);
if (privatewin == NULL) {
2020-07-07 08:18:57 -04:00
ProfWin* window = wins_new_private(message->from_jid->fulljid);
privatewin = (ProfPrivateWin*)window;
}
_clean_incoming_message(message);
privwin_incoming_msg(privatewin, message);
2019-10-29 05:52:11 -04:00
chat_log_msg_in(message);
plugins_post_priv_message_display(message->from_jid->fulljid, message->plain);
free(message->plain);
message->plain = old_plain;
}
2015-02-02 05:10:05 -05:00
void
2020-07-07 08:18:57 -04:00
sv_ev_outgoing_carbon(ProfMessage* message)
{
2020-07-07 08:18:57 -04:00
ProfChatWin* chatwin = wins_get_chat(message->to_jid->barejid);
2015-10-27 19:25:18 -04:00
if (!chatwin) {
chatwin = chatwin_new(message->to_jid->barejid);
2015-10-27 19:25:18 -04:00
}
chat_state_active(chatwin->state);
2019-06-21 07:49:17 -04:00
if (message->enc == PROF_MSG_ENC_OMEMO) {
chatwin_outgoing_carbon(chatwin, message);
} else if (message->encrypted) {
#ifdef HAVE_LIBGPGME
2019-06-21 07:49:17 -04:00
message->plain = p_gpg_decrypt(message->encrypted);
if (message->plain) {
message->enc = PROF_MSG_ENC_PGP;
chatwin_outgoing_carbon(chatwin, message);
} else {
2019-06-21 07:49:17 -04:00
if (!message->body) {
log_error("Couldn't decrypt GPG message and body was empty");
return;
}
message->enc = PROF_MSG_ENC_NONE;
2019-06-21 07:49:17 -04:00
message->plain = strdup(message->body);
chatwin_outgoing_carbon(chatwin, message);
}
#endif
} else {
message->enc = PROF_MSG_ENC_NONE;
2019-06-21 07:49:17 -04:00
message->plain = strdup(message->body);
chatwin_outgoing_carbon(chatwin, message);
}
if (message->plain) {
if (message->type == PROF_MSG_TYPE_MUCPM) {
// MUC PM, should have resource (nick) in filename
chat_log_msg_out(message->to_jid->barejid, message->plain, message->from_jid->resourcepart);
} else {
chat_log_msg_out(message->to_jid->barejid, message->plain, NULL);
}
log_database_add_incoming(message);
}
return;
}
2015-08-26 18:15:10 -04:00
static void
2020-07-07 08:18:57 -04:00
_sv_ev_incoming_pgp(ProfChatWin* chatwin, gboolean new_win, ProfMessage* message, gboolean logit)
2015-08-26 18:15:10 -04:00
{
#ifdef HAVE_LIBGPGME
message->plain = p_gpg_decrypt(message->encrypted);
if (message->plain) {
message->enc = PROF_MSG_ENC_PGP;
_clean_incoming_message(message);
chatwin_incoming_msg(chatwin, message, new_win);
log_database_add_incoming(message);
if (logit) {
chat_log_pgp_msg_in(message);
}
chatwin->pgp_recv = TRUE;
p_gpg_free_decrypted(message->plain);
message->plain = NULL;
2015-08-26 18:15:10 -04:00
} else {
2019-06-21 07:49:17 -04:00
if (!message->body) {
log_error("Couldn't decrypt GPG message and body was empty");
return;
}
message->enc = PROF_MSG_ENC_NONE;
2019-06-21 07:49:17 -04:00
message->plain = strdup(message->body);
_clean_incoming_message(message);
chatwin_incoming_msg(chatwin, message, new_win);
log_database_add_incoming(message);
chat_log_msg_in(message);
chatwin->pgp_recv = FALSE;
2015-08-26 18:15:10 -04:00
}
#endif
}
2015-08-26 18:15:10 -04:00
static void
2020-07-07 08:18:57 -04:00
_sv_ev_incoming_ox(ProfChatWin* chatwin, gboolean new_win, ProfMessage* message, gboolean logit)
{
#ifdef HAVE_LIBGPGME
2020-07-07 08:18:57 -04:00
//_clean_incoming_message(message);
chatwin_incoming_msg(chatwin, message, new_win);
log_database_add_incoming(message);
if (logit) {
chat_log_pgp_msg_in(message);
}
chatwin->pgp_recv = TRUE;
//p_gpg_free_decrypted(message->plain);
message->plain = NULL;
#endif
}
2015-08-26 18:15:10 -04:00
static void
2020-07-07 08:18:57 -04:00
_sv_ev_incoming_otr(ProfChatWin* chatwin, gboolean new_win, ProfMessage* message)
2015-08-26 18:15:10 -04:00
{
#ifdef HAVE_LIBOTR
2015-08-26 18:15:10 -04:00
gboolean decrypted = FALSE;
message->plain = otr_on_message_recv(message->from_jid->barejid, message->from_jid->resourcepart, message->body, &decrypted);
if (message->plain) {
2015-08-26 19:37:48 -04:00
if (decrypted) {
message->enc = PROF_MSG_ENC_OTR;
chatwin->pgp_send = FALSE;
2015-08-26 18:15:10 -04:00
} else {
message->enc = PROF_MSG_ENC_NONE;
2015-08-26 18:15:10 -04:00
}
_clean_incoming_message(message);
chatwin_incoming_msg(chatwin, message, new_win);