hexchat/src/common/plugin-timer.c

232 lines
4.6 KiB
C
Raw Normal View History

/* HexChat
* Copyright (C) 1998-2010 Peter Zelezny.
* Copyright (C) 2009-2013 Berke Viktor.
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
2014-12-15 22:57:27 -05:00
#include "config.h"
2011-02-23 22:14:30 -05:00
#include <stdlib.h>
#include <string.h>
#include <glib.h>
2012-10-24 15:33:02 -04:00
#include "hexchat-plugin.h"
2011-02-23 22:14:30 -05:00
#ifdef WIN32
2012-06-16 07:01:47 -04:00
#define g_ascii_strcasecmp stricmp
2011-02-23 22:14:30 -05:00
#endif
2014-06-21 22:11:37 -04:00
#define _(x) hexchat_gettext(ph,x)
2012-10-30 03:42:48 -04:00
static hexchat_plugin *ph; /* plugin handle */
2011-02-23 22:14:30 -05:00
static GSList *timer_list = NULL;
#define STATIC
#define HELP \
"Usage: TIMER [-refnum <num>] [-repeat <num>] <seconds> <command>\n" \
" TIMER [-quiet] -delete <num>"
typedef struct
{
2012-10-30 03:42:48 -04:00
hexchat_hook *hook;
hexchat_context *context;
2011-02-23 22:14:30 -05:00
char *command;
int ref;
int repeat;
int timeout;
2011-02-23 22:14:30 -05:00
unsigned int forever:1;
} timer;
static void
timer_del (timer *tim)
{
timer_list = g_slist_remove (timer_list, tim);
g_free (tim->command);
2012-10-30 03:42:48 -04:00
hexchat_unhook (ph, tim->hook);
g_free (tim);
2011-02-23 22:14:30 -05:00
}
static void
timer_del_ref (int ref, int quiet)
{
GSList *list;
timer *tim;
list = timer_list;
while (list)
{
tim = list->data;
if (tim->ref == ref)
{
timer_del (tim);
if (!quiet)
2014-06-21 22:11:37 -04:00
hexchat_printf (ph, _("Timer %d deleted.\n"), ref);
2011-02-23 22:14:30 -05:00
return;
}
list = list->next;
}
if (!quiet)
2014-06-21 22:11:37 -04:00
hexchat_print (ph, _("No such ref number found.\n"));
2011-02-23 22:14:30 -05:00
}
static int
timeout_cb (timer *tim)
{
2012-10-30 03:42:48 -04:00
if (hexchat_set_context (ph, tim->context))
2011-02-23 22:14:30 -05:00
{
2012-10-30 03:42:48 -04:00
hexchat_command (ph, tim->command);
2011-02-23 22:14:30 -05:00
if (tim->forever)
return 1;
tim->repeat--;
if (tim->repeat > 0)
return 1;
}
timer_del (tim);
return 0;
}
static void
timer_add (int ref, int timeout, int repeat, char *command)
2011-02-23 22:14:30 -05:00
{
timer *tim;
GSList *list;
if (ref == 0)
{
ref = 1;
list = timer_list;
while (list)
{
tim = list->data;
if (tim->ref >= ref)
ref = tim->ref + 1;
list = list->next;
}
}
tim = g_new (timer, 1);
2011-02-23 22:14:30 -05:00
tim->ref = ref;
tim->repeat = repeat;
tim->timeout = timeout;
tim->command = g_strdup (command);
2012-10-30 03:42:48 -04:00
tim->context = hexchat_get_context (ph);
2011-02-23 22:14:30 -05:00
tim->forever = FALSE;
if (repeat == 0)
tim->forever = TRUE;
tim->hook = hexchat_hook_timer (ph, timeout, (void *)timeout_cb, tim);
2011-02-23 22:14:30 -05:00
timer_list = g_slist_append (timer_list, tim);
}
static void
timer_showlist (void)
{
GSList *list;
timer *tim;
if (timer_list == NULL)
{
2014-06-21 22:11:37 -04:00
hexchat_print (ph, _("No timers installed.\n"));
hexchat_print (ph, _(HELP));
2011-02-23 22:14:30 -05:00
return;
}
/* 00000 00000000 0000000 abc */
2014-06-21 22:11:37 -04:00
hexchat_print (ph, _("\026 Ref# Seconds Repeat Command \026\n"));
2011-02-23 22:14:30 -05:00
list = timer_list;
while (list)
{
tim = list->data;
hexchat_printf (ph, _("%5d %8.1f %7d %s\n"), tim->ref, tim->timeout / 1000.0f,
2011-02-23 22:14:30 -05:00
tim->repeat, tim->command);
list = list->next;
}
}
static int
timer_cb (char *word[], char *word_eol[], void *userdata)
{
int repeat = 1;
double timeout;
2011-02-23 22:14:30 -05:00
int offset = 0;
int ref = 0;
int quiet = FALSE;
char *command;
if (!word[2][0])
{
timer_showlist ();
2012-10-30 05:42:37 -04:00
return HEXCHAT_EAT_HEXCHAT;
2011-02-23 22:14:30 -05:00
}
2012-06-16 07:01:47 -04:00
if (g_ascii_strcasecmp (word[2], "-quiet") == 0)
2011-02-23 22:14:30 -05:00
{
quiet = TRUE;
offset++;
}
2012-06-16 07:01:47 -04:00
if (g_ascii_strcasecmp (word[2 + offset], "-delete") == 0)
2011-02-23 22:14:30 -05:00
{
timer_del_ref (atoi (word[3 + offset]), quiet);
2012-10-30 05:42:37 -04:00
return HEXCHAT_EAT_HEXCHAT;
2011-02-23 22:14:30 -05:00
}
2012-06-16 07:01:47 -04:00
if (g_ascii_strcasecmp (word[2 + offset], "-refnum") == 0)
2011-02-23 22:14:30 -05:00
{
ref = atoi (word[3 + offset]);
offset += 2;
}
2012-06-16 07:01:47 -04:00
if (g_ascii_strcasecmp (word[2 + offset], "-repeat") == 0)
2011-02-23 22:14:30 -05:00
{
repeat = atoi (word[3 + offset]);
offset += 2;
}
timeout = atof (word[2 + offset]);
command = word_eol[3 + offset];
if (timeout < 0.1 || timeout * 1000 > INT_MAX || !command[0])
2012-10-30 03:42:48 -04:00
hexchat_print (ph, HELP);
2011-02-23 22:14:30 -05:00
else
timer_add (ref, (int) timeout * 1000, repeat, command);
2011-02-23 22:14:30 -05:00
2012-10-30 05:42:37 -04:00
return HEXCHAT_EAT_HEXCHAT;
2011-02-23 22:14:30 -05:00
}
int
#ifdef STATIC
timer_plugin_init
#else
2012-10-30 03:42:48 -04:00
hexchat_plugin_init
2011-02-23 22:14:30 -05:00
#endif
2012-10-30 03:42:48 -04:00
(hexchat_plugin *plugin_handle, char **plugin_name,
2011-02-23 22:14:30 -05:00
char **plugin_desc, char **plugin_version, char *arg)
{
2012-10-30 06:35:39 -04:00
/* we need to save this for use with any hexchat_* functions */
2011-02-23 22:14:30 -05:00
ph = plugin_handle;
*plugin_name = "Timer";
*plugin_desc = "IrcII style /TIMER command";
*plugin_version = "";
2014-06-21 22:11:37 -04:00
hexchat_hook_command (ph, "TIMER", HEXCHAT_PRI_NORM, timer_cb, _(HELP), 0);
2011-02-23 22:14:30 -05:00
return 1; /* return 1 for success */
}