1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-23 21:45:30 +00:00
profanity/src/ui/chatwin.c

475 lines
14 KiB
C
Raw Normal View History

2015-10-26 22:35:30 +00:00
/*
* chatwin.c
*
2018-01-21 15:00:02 +00:00
* Copyright (C) 2012 - 2018 James Booth <boothj5@gmail.com>
2015-10-26 22:35:30 +00: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-24 00:14:49 +00:00
* along with Profanity. If not, see <https://www.gnu.org/licenses/>.
2015-10-26 22:35:30 +00: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.
*
*/
2016-03-31 20:05:02 +00:00
#include "config.h"
2015-10-31 23:38:08 +00:00
2015-10-26 22:35:30 +00:00
#include <string.h>
#include <stdlib.h>
#include <assert.h>
2015-10-26 22:35:30 +00:00
2016-07-24 13:23:55 +00:00
#include "xmpp/chat_session.h"
2015-10-26 22:35:30 +00:00
#include "window_list.h"
2016-07-24 14:08:47 +00:00
#include "xmpp/roster_list.h"
2015-10-26 22:35:30 +00:00
#include "log.h"
#include "config/preferences.h"
#include "ui/ui.h"
#include "ui/window.h"
#include "ui/titlebar.h"
#include "plugins/plugins.h"
2016-03-31 20:05:02 +00:00
#ifdef HAVE_LIBOTR
2015-10-31 23:38:08 +00:00
#include "otr/otr.h"
#endif
2015-10-26 22:35:30 +00:00
static void _chatwin_history(ProfChatWin *chatwin, const char *const contact);
2015-10-26 22:35:30 +00:00
ProfChatWin*
chatwin_new(const char *const barejid)
{
ProfWin *window = wins_new_chat(barejid);
ProfChatWin *chatwin = (ProfChatWin *)window;
if (prefs_get_boolean(PREF_CHLOG) && prefs_get_boolean(PREF_HISTORY)) {
_chatwin_history(chatwin, barejid);
}
// if the contact is offline, show a message
PContact contact = roster_get_contact(barejid);
if (contact) {
if (strcmp(p_contact_presence(contact), "offline") == 0) {
const char * const show = p_contact_presence(contact);
const char * const status = p_contact_status(contact);
win_show_status_string(window, barejid, show, status, NULL, "--", "offline");
}
}
return chatwin;
}
2015-10-26 22:35:30 +00:00
void
2015-10-27 21:23:56 +00:00
chatwin_receipt_received(ProfChatWin *chatwin, const char *const id)
2015-10-26 22:35:30 +00:00
{
assert(chatwin != NULL);
2015-10-26 22:54:06 +00:00
ProfWin *win = (ProfWin*) chatwin;
win_mark_received(win, id);
2015-10-26 22:35:30 +00:00
}
2016-03-31 20:05:02 +00:00
#ifdef HAVE_LIBOTR
2015-10-26 22:35:30 +00:00
void
2015-10-27 21:23:56 +00:00
chatwin_otr_secured(ProfChatWin *chatwin, gboolean trusted)
2015-10-26 22:35:30 +00:00
{
assert(chatwin != NULL);
2015-10-26 22:35:30 +00:00
chatwin->is_otr = TRUE;
chatwin->otr_is_trusted = trusted;
2015-10-26 23:04:45 +00:00
ProfWin *window = (ProfWin*) chatwin;
2015-10-26 22:35:30 +00:00
if (trusted) {
2016-10-15 17:29:02 +00:00
win_println(window, THEME_OTR_STARTED_TRUSTED, '!', "OTR session started (trusted).");
2015-10-26 22:35:30 +00:00
} else {
2016-10-15 17:29:02 +00:00
win_println(window, THEME_OTR_STARTED_UNTRUSTED, '!', "OTR session started (untrusted).");
2015-10-26 22:35:30 +00:00
}
if (wins_is_current(window)) {
title_bar_switch();
} else {
int num = wins_get_num(window);
2018-03-09 21:11:59 +00:00
status_bar_new(num, WIN_CHAT, chatwin->barejid);
2015-10-26 22:35:30 +00:00
int ui_index = num;
if (ui_index == 10) {
ui_index = 0;
}
2015-10-26 23:04:45 +00:00
cons_show("%s started an OTR session (%d).", chatwin->barejid, ui_index);
2015-10-26 22:35:30 +00:00
cons_alert();
}
}
void
2015-10-27 21:23:56 +00:00
chatwin_otr_unsecured(ProfChatWin *chatwin)
2015-10-26 22:35:30 +00:00
{
assert(chatwin != NULL);
2015-10-26 23:10:30 +00:00
chatwin->is_otr = FALSE;
chatwin->otr_is_trusted = FALSE;
2015-10-26 22:35:30 +00:00
2015-10-26 23:10:30 +00:00
ProfWin *window = (ProfWin*)chatwin;
2016-10-15 17:29:02 +00:00
win_println(window, THEME_OTR_ENDED, '!', "OTR session ended.");
2015-10-26 23:10:30 +00:00
if (wins_is_current(window)) {
title_bar_switch();
2015-10-26 22:35:30 +00:00
}
}
void
2015-10-27 22:25:02 +00:00
chatwin_otr_smp_event(ProfChatWin *chatwin, prof_otr_smp_event_t event, void *data)
2015-10-26 22:35:30 +00:00
{
assert(chatwin != NULL);
2015-10-27 22:25:02 +00:00
switch (event) {
case PROF_OTR_SMP_INIT:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!',
2015-10-27 22:25:02 +00:00
"%s wants to authenticate your identity, use '/otr secret <secret>'.", chatwin->barejid);
break;
case PROF_OTR_SMP_INIT_Q:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!',
2015-10-27 22:25:02 +00:00
"%s wants to authenticate your identity with the following question:", chatwin->barejid);
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', " %s", (char*)data);
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "use '/otr answer <answer>'.");
2015-10-27 22:25:02 +00:00
break;
case PROF_OTR_SMP_SENDER_FAIL:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!',
2015-10-27 22:25:02 +00:00
"Authentication failed, the secret you entered does not match the secret entered by %s.",
chatwin->barejid);
break;
case PROF_OTR_SMP_RECEIVER_FAIL:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!',
2015-10-27 22:25:02 +00:00
"Authentication failed, the secret entered by %s does not match yours.", chatwin->barejid);
break;
case PROF_OTR_SMP_ABORT:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "SMP session aborted.");
2015-10-27 22:25:02 +00:00
break;
case PROF_OTR_SMP_SUCCESS:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "Authentication successful.");
2015-10-27 22:25:02 +00:00
break;
case PROF_OTR_SMP_SUCCESS_Q:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "%s successfully authenticated you.", chatwin->barejid);
2015-10-27 22:25:02 +00:00
break;
case PROF_OTR_SMP_FAIL_Q:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "%s failed to authenticate you.", chatwin->barejid);
2015-10-27 22:25:02 +00:00
break;
case PROF_OTR_SMP_AUTH:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "Authenticating %s...", chatwin->barejid);
2015-10-27 22:25:02 +00:00
break;
case PROF_OTR_SMP_AUTH_WAIT:
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '!', "Awaiting authentication from %s...", chatwin->barejid);
2015-10-27 22:25:02 +00:00
break;
default:
break;
}
2015-10-26 22:35:30 +00:00
}
void
2015-10-27 21:23:56 +00:00
chatwin_otr_trust(ProfChatWin *chatwin)
2015-10-26 22:35:30 +00:00
{
assert(chatwin != NULL);
chatwin->is_otr = TRUE;
chatwin->otr_is_trusted = TRUE;
ProfWin *window = (ProfWin*)chatwin;
2016-10-15 17:29:02 +00:00
win_println(window, THEME_OTR_TRUSTED, '!', "OTR session trusted.");
if (wins_is_current(window)) {
title_bar_switch();
2015-10-26 22:35:30 +00:00
}
}
void
2015-10-27 21:23:56 +00:00
chatwin_otr_untrust(ProfChatWin *chatwin)
2015-10-26 22:35:30 +00:00
{
assert(chatwin != NULL);
2015-10-26 22:35:30 +00:00
chatwin->is_otr = TRUE;
chatwin->otr_is_trusted = FALSE;
ProfWin *window = (ProfWin*)chatwin;
2016-10-15 17:29:02 +00:00
win_println(window, THEME_OTR_UNTRUSTED, '!', "OTR session untrusted.");
if (wins_is_current(window)) {
title_bar_switch();
2015-10-26 22:35:30 +00:00
}
}
2015-10-31 23:22:23 +00:00
#endif
2015-10-26 22:35:30 +00:00
void
chatwin_recipient_gone(ProfChatWin *chatwin)
2015-10-26 22:35:30 +00:00
{
2015-10-27 23:25:18 +00:00
assert(chatwin != NULL);
2015-10-27 23:05:20 +00:00
const char *display_usr = NULL;
PContact contact = roster_get_contact(chatwin->barejid);
if (contact) {
if (p_contact_name(contact)) {
display_usr = p_contact_name(contact);
} else {
display_usr = chatwin->barejid;
2015-10-26 22:35:30 +00:00
}
2015-10-27 23:05:20 +00:00
} else {
display_usr = chatwin->barejid;
2015-10-26 22:35:30 +00:00
}
2015-10-27 23:05:20 +00:00
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_GONE, '!', "<- %s has left the conversation.", display_usr);
2015-10-26 22:35:30 +00:00
}
void
chatwin_incoming_msg(ProfChatWin *chatwin, const char *const resource, const char *const message, GDateTime *timestamp, gboolean win_created, prof_enc_t enc_mode)
2015-10-26 22:35:30 +00:00
{
2015-10-27 23:25:18 +00:00
assert(chatwin != NULL);
char *plugin_message = plugins_pre_chat_message_display(chatwin->barejid, resource, message);
2015-10-26 22:35:30 +00:00
ProfWin *window = (ProfWin*)chatwin;
int num = wins_get_num(window);
char *display_name = roster_get_msg_display_name(chatwin->barejid, resource);
gboolean is_current = wins_is_current(window);
gboolean notify = prefs_do_chat_notify(is_current);
2015-10-26 22:35:30 +00:00
// currently viewing chat window with sender
if (wins_is_current(window)) {
win_print_incoming(window, timestamp, display_name, plugin_message, enc_mode);
2015-10-26 22:35:30 +00:00
title_bar_set_typing(FALSE);
2018-03-09 21:11:59 +00:00
status_bar_active(num, WIN_CHAT, chatwin->barejid);
2015-10-26 22:35:30 +00:00
// not currently viewing chat window with sender
} else {
2018-03-09 21:11:59 +00:00
status_bar_new(num, WIN_CHAT, chatwin->barejid);
2016-02-03 23:16:42 +00:00
cons_show_incoming_message(display_name, num, chatwin->unread);
2015-10-26 22:35:30 +00:00
if (prefs_get_boolean(PREF_FLASH)) {
flash();
}
chatwin->unread++;
2015-10-26 22:35:30 +00:00
if (prefs_get_boolean(PREF_CHLOG) && prefs_get_boolean(PREF_HISTORY)) {
_chatwin_history(chatwin, chatwin->barejid);
2015-10-26 22:35:30 +00:00
}
// show users status first, when receiving message via delayed delivery
if (timestamp && win_created) {
PContact pcontact = roster_get_contact(chatwin->barejid);
if (pcontact) {
win_show_contact(window, pcontact);
}
}
win_print_incoming(window, timestamp, display_name, plugin_message, enc_mode);
2015-10-26 22:35:30 +00:00
}
if (prefs_get_boolean(PREF_BEEP)) {
beep();
}
2016-02-04 00:41:53 +00:00
if (notify) {
notify_message(display_name, num, plugin_message);
2015-10-26 22:35:30 +00:00
}
free(display_name);
plugins_post_chat_message_display(chatwin->barejid, resource, plugin_message);
free(plugin_message);
2015-10-26 22:35:30 +00:00
}
void
chatwin_outgoing_msg(ProfChatWin *chatwin, const char *const message, char *id, prof_enc_t enc_mode,
gboolean request_receipt)
2015-10-26 22:35:30 +00:00
{
2015-10-27 23:25:18 +00:00
assert(chatwin != NULL);
2015-10-26 22:35:30 +00:00
char enc_char = '-';
if (chatwin->outgoing_char) {
enc_char = chatwin->outgoing_char[0];
} else if (enc_mode == PROF_MSG_OTR) {
2015-10-26 22:35:30 +00:00
enc_char = prefs_get_otr_char();
} else if (enc_mode == PROF_MSG_PGP) {
enc_char = prefs_get_pgp_char();
}
if (request_receipt && id) {
win_print_with_receipt((ProfWin*)chatwin, enc_char, "me", message, id);
2015-10-26 22:35:30 +00:00
} else {
2016-10-15 19:07:33 +00:00
win_print_outgoing((ProfWin*)chatwin, enc_char, "%s", message);
2015-10-26 22:35:30 +00:00
}
}
void
chatwin_outgoing_carbon(ProfChatWin *chatwin, const char *const message, prof_enc_t enc_mode)
2015-10-26 22:35:30 +00:00
{
2015-10-27 23:25:18 +00:00
assert(chatwin != NULL);
2015-10-26 22:35:30 +00:00
char enc_char = '-';
if (enc_mode == PROF_MSG_PGP) {
enc_char = prefs_get_pgp_char();
}
2018-03-08 19:55:17 +00:00
ProfWin *window = (ProfWin*)chatwin;
win_print_outgoing(window, enc_char, "%s", message);
int num = wins_get_num(window);
2018-03-09 21:11:59 +00:00
status_bar_active(num, WIN_CHAT, chatwin->barejid);
2015-10-26 22:35:30 +00:00
}
void
2015-10-27 23:39:26 +00:00
chatwin_contact_online(ProfChatWin *chatwin, Resource *resource, GDateTime *last_activity)
2015-10-26 22:35:30 +00:00
{
2015-10-27 23:39:26 +00:00
assert(chatwin != NULL);
2015-10-26 22:35:30 +00:00
const char *show = string_from_resource_presence(resource->presence);
2015-10-27 23:39:26 +00:00
PContact contact = roster_get_contact(chatwin->barejid);
2015-10-26 22:35:30 +00:00
char *display_str = p_contact_create_display_string(contact, resource->name);
2015-10-27 23:39:26 +00:00
win_show_status_string((ProfWin*)chatwin, display_str, show, resource->status, last_activity, "++", "online");
2015-10-26 22:35:30 +00:00
free(display_str);
}
void
2015-10-27 23:39:26 +00:00
chatwin_contact_offline(ProfChatWin *chatwin, char *resource, char *status)
2015-10-26 22:35:30 +00:00
{
2015-10-27 23:39:26 +00:00
assert(chatwin != NULL);
PContact contact = roster_get_contact(chatwin->barejid);
2015-10-26 22:35:30 +00:00
char *display_str = p_contact_create_display_string(contact, resource);
2015-10-27 23:39:26 +00:00
win_show_status_string((ProfWin*)chatwin, display_str, "offline", status, NULL, "--", "offline");
2015-10-26 22:35:30 +00:00
free(display_str);
}
2015-11-29 23:58:52 +00:00
char*
chatwin_get_string(ProfChatWin *chatwin)
{
assert(chatwin != NULL);
GString *res = g_string_new("Chat ");
2016-05-05 22:51:49 +00:00
jabber_conn_status_t conn_status = connection_get_status();
if (conn_status == JABBER_CONNECTED) {
PContact contact = roster_get_contact(chatwin->barejid);
if (contact == NULL) {
g_string_append(res, chatwin->barejid);
} else {
const char *display_name = p_contact_name_or_jid(contact);
g_string_append(res, display_name);
g_string_append_printf(res, " - %s", p_contact_presence(contact));
}
2015-11-29 23:58:52 +00:00
} else {
g_string_append(res, chatwin->barejid);
2015-11-29 23:58:52 +00:00
}
if (chatwin->unread > 0) {
g_string_append_printf(res, ", %d unread", chatwin->unread);
}
char *resstr = res->str;
g_string_free(res, FALSE);
return resstr;
}
void
chatwin_set_enctext(ProfChatWin *chatwin, const char *const enctext)
{
if (chatwin->enctext) {
free(chatwin->enctext);
}
chatwin->enctext = strdup(enctext);
}
void
chatwin_unset_enctext(ProfChatWin *chatwin)
{
if (chatwin->enctext) {
free(chatwin->enctext);
chatwin->enctext = NULL;
}
}
void
chatwin_set_incoming_char(ProfChatWin *chatwin, const char *const ch)
{
if (chatwin->incoming_char) {
free(chatwin->incoming_char);
}
chatwin->incoming_char = strdup(ch);
}
void
chatwin_unset_incoming_char(ProfChatWin *chatwin)
{
if (chatwin->incoming_char) {
free(chatwin->incoming_char);
chatwin->incoming_char = NULL;
}
}
void
chatwin_set_outgoing_char(ProfChatWin *chatwin, const char *const ch)
{
if (chatwin->outgoing_char) {
free(chatwin->outgoing_char);
}
chatwin->outgoing_char = strdup(ch);
}
void
chatwin_unset_outgoing_char(ProfChatWin *chatwin)
{
if (chatwin->outgoing_char) {
free(chatwin->outgoing_char);
chatwin->outgoing_char = NULL;
}
}
2015-10-26 22:35:30 +00:00
static void
_chatwin_history(ProfChatWin *chatwin, const char *const contact)
2015-10-26 22:35:30 +00:00
{
if (!chatwin->history_shown) {
2016-05-06 00:12:54 +00:00
Jid *jid = jid_create(connection_get_fulljid());
2015-10-26 22:35:30 +00:00
GSList *history = chat_log_get_previous(jid->barejid, contact);
jid_destroy(jid);
GSList *curr = history;
while (curr) {
char *line = curr->data;
// entry
if (line[2] == ':') {
char hh[3]; memcpy(hh, &line[0], 2); hh[2] = '\0'; int ihh = atoi(hh);
char mm[3]; memcpy(mm, &line[3], 2); mm[2] = '\0'; int imm = atoi(mm);
char ss[3]; memcpy(ss, &line[6], 2); ss[2] = '\0'; int iss = atoi(ss);
GDateTime *timestamp = g_date_time_new_local(2000, 1, 1, ihh, imm, iss);
2016-10-15 19:55:45 +00:00
win_print_history((ProfWin*)chatwin, timestamp, "%s", curr->data+11);
2015-10-26 22:35:30 +00:00
g_date_time_unref(timestamp);
// header
} else {
2016-10-15 17:29:02 +00:00
win_println((ProfWin*)chatwin, THEME_DEFAULT, '-', "%s", curr->data);
2015-10-26 22:35:30 +00:00
}
curr = g_slist_next(curr);
}
chatwin->history_shown = TRUE;
g_slist_free_full(history, free);
}
}