mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-12-06 06:51:53 +00:00
177 lines
3.8 KiB
C
177 lines
3.8 KiB
C
/* ZoiteChat
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
*/
|
|
|
|
#include "zoitechat.h"
|
|
#include "zoitechatc.h"
|
|
#include "server.h"
|
|
#include "fe.h"
|
|
#include "autoaway.h"
|
|
|
|
static gint64 last_activity;
|
|
static int timer_tag;
|
|
static int previous_timeout;
|
|
static unsigned int previous_system;
|
|
|
|
void
|
|
autoaway_activity (void)
|
|
{
|
|
last_activity = g_get_monotonic_time ();
|
|
}
|
|
|
|
/* A manual /AWAY takes ownership even if the server already says away. */
|
|
void
|
|
autoaway_manual (server *serv)
|
|
{
|
|
serv->autoaway_owned = FALSE;
|
|
serv->autoaway_back_pending = FALSE;
|
|
autoaway_activity ();
|
|
}
|
|
|
|
void
|
|
autoaway_reset (server *serv)
|
|
{
|
|
if (!serv->autoaway_owned)
|
|
return;
|
|
|
|
serv->autoaway_owned = FALSE;
|
|
serv->autoaway_back_pending = FALSE;
|
|
serv->reconnect_away = FALSE;
|
|
g_clear_pointer (&serv->last_away_reason, g_free);
|
|
}
|
|
|
|
void
|
|
autoaway_ack_back (server *serv)
|
|
{
|
|
if (serv->autoaway_back_pending)
|
|
autoaway_reset (serv);
|
|
}
|
|
|
|
static void
|
|
autoaway_release (server *serv, gint64 now)
|
|
{
|
|
if (!serv->autoaway_owned)
|
|
return;
|
|
|
|
/* Retain ownership until RPL_UNAWAY, including across a late 306. */
|
|
if (serv->autoaway_back_pending &&
|
|
now - serv->autoaway_sent_at < 30 * G_USEC_PER_SEC)
|
|
return;
|
|
|
|
serv->autoaway_back_pending = TRUE;
|
|
serv->autoaway_sent_at = now;
|
|
serv->p_set_back (serv);
|
|
}
|
|
|
|
gboolean
|
|
autoaway_request_back (server *serv)
|
|
{
|
|
if (!serv->autoaway_owned)
|
|
return FALSE;
|
|
|
|
if (serv->connected && serv->end_of_motd)
|
|
autoaway_release (serv, g_get_monotonic_time ());
|
|
else
|
|
autoaway_reset (serv);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
static gboolean
|
|
autoaway_has_owned_servers (void)
|
|
{
|
|
GSList *list;
|
|
|
|
for (list = serv_list; list; list = list->next)
|
|
if (((server *)list->data)->autoaway_owned)
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
}
|
|
|
|
void
|
|
autoaway_check (gint64 now, gint64 desktop_idle)
|
|
{
|
|
GSList *list;
|
|
gint64 idle = MAX ((gint64)0, now - last_activity);
|
|
gint64 limit = (gint64)prefs.hex_away_auto_timeout * 60 * G_USEC_PER_SEC;
|
|
|
|
if (prefs.hex_away_auto_system && desktop_idle >= 0)
|
|
idle = MIN (idle, desktop_idle);
|
|
|
|
for (list = serv_list; list; list = list->next)
|
|
{
|
|
server *serv = list->data;
|
|
|
|
if (!serv->connected)
|
|
{
|
|
/* Re-evaluate idle after login instead of restoring stale autoaway. */
|
|
autoaway_reset (serv);
|
|
continue;
|
|
}
|
|
if (!serv->end_of_motd)
|
|
continue;
|
|
|
|
if (serv->autoaway_back_pending || limit <= 0 || idle < limit)
|
|
{
|
|
autoaway_release (serv, now);
|
|
continue;
|
|
}
|
|
if (serv->is_away || serv->reconnect_away)
|
|
continue;
|
|
|
|
/* Retry a rejected/lost request slowly, never once per poll. */
|
|
if (serv->autoaway_owned &&
|
|
now - serv->autoaway_sent_at < 30 * G_USEC_PER_SEC)
|
|
continue;
|
|
|
|
g_free (serv->last_away_reason);
|
|
serv->last_away_reason = g_strdup (prefs.hex_away_auto_reason[0] ?
|
|
prefs.hex_away_auto_reason : _("Idle"));
|
|
serv->autoaway_owned = TRUE;
|
|
serv->autoaway_sent_at = now;
|
|
serv->p_set_away (serv, serv->last_away_reason);
|
|
}
|
|
}
|
|
|
|
static gboolean
|
|
autoaway_timer (gpointer data)
|
|
{
|
|
(void)data;
|
|
autoaway_check (g_get_monotonic_time (),
|
|
prefs.hex_away_auto_system ? fe_get_idle_time () : -1);
|
|
if (prefs.hex_away_auto_timeout <= 0 && !autoaway_has_owned_servers ())
|
|
{
|
|
timer_tag = 0;
|
|
return G_SOURCE_REMOVE;
|
|
}
|
|
return G_SOURCE_CONTINUE;
|
|
}
|
|
|
|
void
|
|
autoaway_reinit (void)
|
|
{
|
|
if (previous_timeout != prefs.hex_away_auto_timeout ||
|
|
previous_system != prefs.hex_away_auto_system)
|
|
{
|
|
autoaway_activity ();
|
|
previous_timeout = prefs.hex_away_auto_timeout;
|
|
previous_system = prefs.hex_away_auto_system;
|
|
}
|
|
|
|
/* Disabling autoaway sends BACK immediately, but retries until ACK. */
|
|
if (prefs.hex_away_auto_timeout <= 0)
|
|
autoaway_check (g_get_monotonic_time (), -1);
|
|
|
|
if (prefs.hex_away_auto_timeout > 0 || autoaway_has_owned_servers ())
|
|
{
|
|
if (!timer_tag)
|
|
timer_tag = fe_timeout_add_seconds (5, autoaway_timer, NULL);
|
|
}
|
|
else if (timer_tag)
|
|
{
|
|
fe_timeout_remove (timer_tag);
|
|
timer_tag = 0;
|
|
}
|
|
}
|