Now inbound_cap_ls() can enable extensions when a bouncer uses a namespace for the...
[rofl0r-ixchat.git] / src / common / plugin-timer.c
blobd2f88f323c539be1bb6067d74b138c49ad13adee
1 #include <stdlib.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "xchat-plugin.h"
6 static xchat_plugin *ph; /* plugin handle */
7 static GSList *timer_list = NULL;
9 #define STATIC
10 #define HELP \
11 "Usage: TIMER [-refnum <num>] [-repeat <num>] <seconds> <command>\n" \
12 " TIMER [-quiet] -delete <num>"
14 typedef struct
16 xchat_hook *hook;
17 xchat_context *context;
18 char *command;
19 int ref;
20 int repeat;
21 float timeout;
22 unsigned int forever:1;
23 } timer;
25 static void
26 timer_del (timer *tim)
28 timer_list = g_slist_remove (timer_list, tim);
29 free (tim->command);
30 xchat_unhook (ph, tim->hook);
31 free (tim);
34 static void
35 timer_del_ref (int ref, int quiet)
37 GSList *list;
38 timer *tim;
40 list = timer_list;
41 while (list)
43 tim = list->data;
44 if (tim->ref == ref)
46 timer_del (tim);
47 if (!quiet)
48 xchat_printf (ph, "Timer %d deleted.\n", ref);
49 return;
51 list = list->next;
53 if (!quiet)
54 xchat_print (ph, "No such ref number found.\n");
57 static int
58 timeout_cb (timer *tim)
60 if (xchat_set_context (ph, tim->context))
62 xchat_command (ph, tim->command);
64 if (tim->forever)
65 return 1;
67 tim->repeat--;
68 if (tim->repeat > 0)
69 return 1;
72 timer_del (tim);
73 return 0;
76 static void
77 timer_add (int ref, float timeout, int repeat, char *command)
79 timer *tim;
80 GSList *list;
82 if (ref == 0)
84 ref = 1;
85 list = timer_list;
86 while (list)
88 tim = list->data;
89 if (tim->ref >= ref)
90 ref = tim->ref + 1;
91 list = list->next;
95 tim = malloc (sizeof (timer));
96 tim->ref = ref;
97 tim->repeat = repeat;
98 tim->timeout = timeout;
99 tim->command = strdup (command);
100 tim->context = xchat_get_context (ph);
101 tim->forever = FALSE;
103 if (repeat == 0)
104 tim->forever = TRUE;
106 tim->hook = xchat_hook_timer (ph, timeout * 1000.0, (void *)timeout_cb, tim);
107 timer_list = g_slist_append (timer_list, tim);
110 static void
111 timer_showlist (void)
113 GSList *list;
114 timer *tim;
116 if (timer_list == NULL)
118 xchat_print (ph, "No timers installed.\n");
119 xchat_print (ph, HELP);
120 return;
122 /* 00000 00000000 0000000 abc */
123 xchat_print (ph, "\026 Ref# Seconds Repeat Command \026\n");
124 list = timer_list;
125 while (list)
127 tim = list->data;
128 xchat_printf (ph, "%5d %8.1f %7d %s\n", tim->ref, tim->timeout,
129 tim->repeat, tim->command);
130 list = list->next;
134 static int
135 timer_cb (char *word[], char *word_eol[], void *userdata)
137 int repeat = 1;
138 float timeout;
139 int offset = 0;
140 int ref = 0;
141 int quiet = FALSE;
142 char *command;
144 if (!word[2][0])
146 timer_showlist ();
147 return XCHAT_EAT_XCHAT;
150 if (strcasecmp (word[2], "-quiet") == 0)
152 quiet = TRUE;
153 offset++;
156 if (strcasecmp (word[2 + offset], "-delete") == 0)
158 timer_del_ref (atoi (word[3 + offset]), quiet);
159 return XCHAT_EAT_XCHAT;
162 if (strcasecmp (word[2 + offset], "-refnum") == 0)
164 ref = atoi (word[3 + offset]);
165 offset += 2;
168 if (strcasecmp (word[2 + offset], "-repeat") == 0)
170 repeat = atoi (word[3 + offset]);
171 offset += 2;
174 timeout = atof (word[2 + offset]);
175 command = word_eol[3 + offset];
177 if (timeout < 0.1 || !command[0])
178 xchat_print (ph, HELP);
179 else
180 timer_add (ref, timeout, repeat, command);
182 return XCHAT_EAT_XCHAT;
186 #ifdef STATIC
187 timer_plugin_init
188 #else
189 xchat_plugin_init
190 #endif
191 (xchat_plugin *plugin_handle, char **plugin_name,
192 char **plugin_desc, char **plugin_version, char *arg)
194 /* we need to save this for use with any xchat_* functions */
195 ph = plugin_handle;
197 *plugin_name = "Timer";
198 *plugin_desc = "IrcII style /TIMER command";
199 *plugin_version = "";
201 xchat_hook_command (ph, "TIMER", XCHAT_PRI_NORM, timer_cb, HELP, 0);
203 return 1; /* return 1 for success */