2015-01-11 15:20:17 -05:00
|
|
|
/*
|
|
|
|
* chat_state.c
|
2019-11-13 06:11:05 -05:00
|
|
|
* vim: expandtab:ts=4:sts=4:sw=4
|
2015-01-11 15:20:17 -05:00
|
|
|
*
|
2019-01-22 05:31:45 -05:00
|
|
|
* Copyright (C) 2012 - 2019 James Booth <boothj5@gmail.com>
|
2015-01-11 15:20:17 -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/>.
|
2015-01-11 15:20:17 -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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-03-26 14:51:46 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2020-07-07 03:43:28 -04:00
|
|
|
#include <stdlib.h>
|
2020-07-07 07:53:30 -04:00
|
|
|
#include <assert.h>
|
2015-01-11 15:20:17 -05:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2016-07-24 10:43:51 -04:00
|
|
|
#include "config/preferences.h"
|
2020-07-07 03:43:28 -04:00
|
|
|
#include "ui/window_list.h"
|
2020-07-07 07:53:30 -04:00
|
|
|
#include "ui/win_types.h"
|
2020-07-07 03:43:28 -04:00
|
|
|
#include "xmpp/xmpp.h"
|
2020-07-07 07:53:30 -04:00
|
|
|
#include "xmpp/chat_state.h"
|
|
|
|
#include "xmpp/chat_session.h"
|
2015-01-11 15:20:17 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
#define PAUSED_TIMEOUT 10.0
|
2015-01-11 18:25:50 -05:00
|
|
|
#define INACTIVE_TIMEOUT 30.0
|
2015-01-11 15:20:17 -05:00
|
|
|
|
2020-07-07 08:18:57 -04:00
|
|
|
static void _send_if_supported(const char* const barejid, void (*send_func)(const char* const));
|
2015-01-11 15:20:17 -05:00
|
|
|
|
|
|
|
ChatState*
|
|
|
|
chat_state_new(void)
|
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
ChatState* new_state = malloc(sizeof(struct prof_chat_state_t));
|
2015-01-11 15:20:17 -05:00
|
|
|
new_state->type = CHAT_STATE_GONE;
|
|
|
|
new_state->timer = g_timer_new();
|
|
|
|
|
|
|
|
return new_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
chat_state_free(ChatState* state)
|
2015-01-11 15:20:17 -05:00
|
|
|
{
|
2020-07-07 08:18:57 -04:00
|
|
|
if (state && state->timer != NULL) {
|
2015-01-11 15:20:17 -05:00
|
|
|
g_timer_destroy(state->timer);
|
|
|
|
}
|
|
|
|
free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
chat_state_handle_idle(const char* const barejid, ChatState* state)
|
2015-01-11 15:20:17 -05:00
|
|
|
{
|
2015-01-11 18:25:50 -05:00
|
|
|
gdouble elapsed = g_timer_elapsed(state->timer, NULL);
|
|
|
|
|
2015-01-11 15:20:17 -05:00
|
|
|
// TYPING -> PAUSED
|
2015-01-11 18:25:50 -05:00
|
|
|
if (state->type == CHAT_STATE_COMPOSING && elapsed > PAUSED_TIMEOUT) {
|
2015-01-11 15:20:17 -05:00
|
|
|
state->type = CHAT_STATE_PAUSED;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) {
|
|
|
|
_send_if_supported(barejid, message_send_paused);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PAUSED|ACTIVE -> INACTIVE
|
2015-01-11 18:25:50 -05:00
|
|
|
if ((state->type == CHAT_STATE_PAUSED || state->type == CHAT_STATE_ACTIVE) && elapsed > INACTIVE_TIMEOUT) {
|
2015-01-11 15:20:17 -05:00
|
|
|
state->type = CHAT_STATE_INACTIVE;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
if (prefs_get_boolean(PREF_STATES)) {
|
|
|
|
_send_if_supported(barejid, message_send_inactive);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INACTIVE -> GONE
|
2015-01-11 18:25:50 -05:00
|
|
|
if (state->type == CHAT_STATE_INACTIVE) {
|
|
|
|
if (prefs_get_gone() != 0 && (elapsed > (prefs_get_gone() * 60.0))) {
|
2020-07-07 08:18:57 -04:00
|
|
|
ChatSession* session = chat_session_get(barejid);
|
2015-01-11 18:25:50 -05:00
|
|
|
if (session) {
|
|
|
|
// never move to GONE when resource override
|
|
|
|
if (!session->resource_override) {
|
|
|
|
if (prefs_get_boolean(PREF_STATES)) {
|
|
|
|
_send_if_supported(barejid, message_send_gone);
|
|
|
|
}
|
|
|
|
chat_session_remove(barejid);
|
|
|
|
state->type = CHAT_STATE_GONE;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-11 15:20:17 -05:00
|
|
|
if (prefs_get_boolean(PREF_STATES)) {
|
2015-01-11 18:25:50 -05:00
|
|
|
message_send_gone(barejid);
|
2015-01-11 15:20:17 -05:00
|
|
|
}
|
|
|
|
state->type = CHAT_STATE_GONE;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
}
|
2015-01-11 18:25:50 -05:00
|
|
|
return;
|
2015-01-11 15:20:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
chat_state_handle_typing(const char* const barejid, ChatState* state)
|
2015-01-11 15:20:17 -05:00
|
|
|
{
|
|
|
|
// ACTIVE|INACTIVE|PAUSED|GONE -> COMPOSING
|
|
|
|
if (state->type != CHAT_STATE_COMPOSING) {
|
|
|
|
state->type = CHAT_STATE_COMPOSING;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
if (prefs_get_boolean(PREF_STATES) && prefs_get_boolean(PREF_OUTTYPE)) {
|
|
|
|
_send_if_supported(barejid, message_send_composing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
chat_state_active(ChatState* state)
|
2015-01-11 15:20:17 -05:00
|
|
|
{
|
|
|
|
state->type = CHAT_STATE_ACTIVE;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-07-07 08:18:57 -04:00
|
|
|
chat_state_gone(const char* const barejid, ChatState* state)
|
2015-01-11 15:20:17 -05:00
|
|
|
{
|
|
|
|
if (state->type != CHAT_STATE_GONE) {
|
|
|
|
if (prefs_get_boolean(PREF_STATES)) {
|
|
|
|
_send_if_supported(barejid, message_send_gone);
|
|
|
|
}
|
|
|
|
state->type = CHAT_STATE_GONE;
|
|
|
|
g_timer_start(state->timer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 08:56:11 -04:00
|
|
|
void
|
|
|
|
chat_state_idle(void)
|
|
|
|
{
|
|
|
|
jabber_conn_status_t status = connection_get_status();
|
|
|
|
if (status == JABBER_CONNECTED) {
|
2020-07-07 08:18:57 -04:00
|
|
|
GSList* recipients = wins_get_chat_recipients();
|
|
|
|
GSList* curr = recipients;
|
2016-07-24 08:56:11 -04:00
|
|
|
|
|
|
|
while (curr) {
|
2020-07-07 08:18:57 -04:00
|
|
|
char* barejid = curr->data;
|
|
|
|
ProfChatWin* chatwin = wins_get_chat(barejid);
|
2016-07-24 08:56:11 -04:00
|
|
|
chat_state_handle_idle(chatwin->barejid, chatwin->state);
|
|
|
|
curr = g_slist_next(curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recipients) {
|
|
|
|
g_slist_free(recipients);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chat_state_activity(void)
|
|
|
|
{
|
|
|
|
jabber_conn_status_t status = connection_get_status();
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfWin* current = wins_get_current();
|
2016-07-24 08:56:11 -04:00
|
|
|
|
|
|
|
if ((status == JABBER_CONNECTED) && (current->type == WIN_CHAT)) {
|
2020-07-07 08:18:57 -04:00
|
|
|
ProfChatWin* chatwin = (ProfChatWin*)current;
|
2016-07-24 08:56:11 -04:00
|
|
|
assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
|
|
|
|
chat_state_handle_typing(chatwin->barejid, chatwin->state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-11 15:20:17 -05:00
|
|
|
static void
|
2020-07-07 08:18:57 -04:00
|
|
|
_send_if_supported(const char* const barejid, void (*send_func)(const char* const))
|
2015-01-11 15:20:17 -05:00
|
|
|
{
|
|
|
|
gboolean send = TRUE;
|
2020-07-07 08:18:57 -04:00
|
|
|
GString* jid = g_string_new(barejid);
|
|
|
|
ChatSession* session = chat_session_get(barejid);
|
2015-01-11 15:20:17 -05:00
|
|
|
if (session) {
|
|
|
|
if (session->send_states) {
|
|
|
|
g_string_append(jid, "/");
|
|
|
|
g_string_append(jid, session->resource);
|
|
|
|
} else {
|
|
|
|
send = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (send) {
|
|
|
|
send_func(jid->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_free(jid, TRUE);
|
2015-10-25 20:52:33 -04:00
|
|
|
}
|