configure: add stuff for spell checking
[rofl0r-ixchat.git] / src / common / plugin-timer.c
blobf09074a8538604f6d99923898ed94410c7ee4b0f
1 #include <stdlib.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "xchat-plugin.h"
6 #ifdef WIN32
7 #define strcasecmp stricmp
8 #endif
10 static xchat_plugin *ph; /* plugin handle */
11 static GSList *timer_list = NULL;
13 #define STATIC
14 #define HELP \
15 "Usage: TIMER [-refnum <num>] [-repeat <num>] <seconds> <command>\n" \
16 " TIMER [-quiet] -delete <num>"
18 typedef struct
20 xchat_hook *hook;
21 xchat_context *context;
22 char *command;
23 int ref;
24 int repeat;
25 float timeout;
26 unsigned int forever:1;
27 } timer;
29 static void
30 timer_del (timer *tim)
32 timer_list = g_slist_remove (timer_list, tim);
33 free (tim->command);
34 xchat_unhook (ph, tim->hook);
35 free (tim);
38 static void
39 timer_del_ref (int ref, int quiet)
41 GSList *list;
42 timer *tim;
44 list = timer_list;
45 while (list)
47 tim = list->data;
48 if (tim->ref == ref)
50 timer_del (tim);
51 if (!quiet)
52 xchat_printf (ph, "Timer %d deleted.\n", ref);
53 return;
55 list = list->next;
57 if (!quiet)
58 xchat_print (ph, "No such ref number found.\n");
61 static int
62 timeout_cb (timer *tim)
64 if (xchat_set_context (ph, tim->context))
66 xchat_command (ph, tim->command);
68 if (tim->forever)
69 return 1;
71 tim->repeat--;
72 if (tim->repeat > 0)
73 return 1;
76 timer_del (tim);
77 return 0;
80 static void
81 timer_add (int ref, float timeout, int repeat, char *command)
83 timer *tim;
84 GSList *list;
86 if (ref == 0)
88 ref = 1;
89 list = timer_list;
90 while (list)
92 tim = list->data;
93 if (tim->ref >= ref)
94 ref = tim->ref + 1;
95 list = list->next;
99 tim = malloc (sizeof (timer));
100 tim->ref = ref;
101 tim->repeat = repeat;
102 tim->timeout = timeout;
103 tim->command = strdup (command);
104 tim->context = xchat_get_context (ph);
105 tim->forever = FALSE;
107 if (repeat == 0)
108 tim->forever = TRUE;
110 tim->hook = xchat_hook_timer (ph, timeout * 1000.0, (void *)timeout_cb, tim);
111 timer_list = g_slist_append (timer_list, tim);
114 static void
115 timer_showlist (void)
117 GSList *list;
118 timer *tim;
120 if (timer_list == NULL)
122 xchat_print (ph, "No timers installed.\n");
123 xchat_print (ph, HELP);
124 return;
126 /* 00000 00000000 0000000 abc */
127 xchat_print (ph, "\026 Ref# Seconds Repeat Command \026\n");
128 list = timer_list;
129 while (list)
131 tim = list->data;
132 xchat_printf (ph, "%5d %8.1f %7d %s\n", tim->ref, tim->timeout,
133 tim->repeat, tim->command);
134 list = list->next;
138 static int
139 timer_cb (char *word[], char *word_eol[], void *userdata)
141 int repeat = 1;
142 float timeout;
143 int offset = 0;
144 int ref = 0;
145 int quiet = FALSE;
146 char *command;
148 if (!word[2][0])
150 timer_showlist ();
151 return XCHAT_EAT_XCHAT;
154 if (strcasecmp (word[2], "-quiet") == 0)
156 quiet = TRUE;
157 offset++;
160 if (strcasecmp (word[2 + offset], "-delete") == 0)
162 timer_del_ref (atoi (word[3 + offset]), quiet);
163 return XCHAT_EAT_XCHAT;
166 if (strcasecmp (word[2 + offset], "-refnum") == 0)
168 ref = atoi (word[3 + offset]);
169 offset += 2;
172 if (strcasecmp (word[2 + offset], "-repeat") == 0)
174 repeat = atoi (word[3 + offset]);
175 offset += 2;
178 timeout = atof (word[2 + offset]);
179 command = word_eol[3 + offset];
181 if (timeout < 0.1 || !command[0])
182 xchat_print (ph, HELP);
183 else
184 timer_add (ref, timeout, repeat, command);
186 return XCHAT_EAT_XCHAT;
190 #ifdef STATIC
191 timer_plugin_init
192 #else
193 xchat_plugin_init
194 #endif
195 (xchat_plugin *plugin_handle, char **plugin_name,
196 char **plugin_desc, char **plugin_version, char *arg)
198 /* we need to save this for use with any xchat_* functions */
199 ph = plugin_handle;
201 *plugin_name = "Timer";
202 *plugin_desc = "IrcII style /TIMER command";
203 *plugin_version = "";
205 xchat_hook_command (ph, "TIMER", XCHAT_PRI_NORM, timer_cb, HELP, 0);
207 return 1; /* return 1 for success */