author | Myhailo Danylenko <isbear@ukrpost.net> |
Thu, 30 Jun 2011 16:23:20 +0300 | |
changeset 16 | e7c65942ce5c |
parent 14 | 5d8f38cc86ac |
child 17 | 82331654a753 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
* |
|
4 |
* This file is part of mcabber-templatecmd |
|
5 |
* |
|
6 |
* mcabber-templatecmd is free software: you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation, either version 2 of the License, or |
|
9 |
* (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
18 |
||
19 |
#include <glib.h> |
|
20 |
#include <stdlib.h> |
|
21 |
#include <string.h> |
|
22 |
||
1
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
23 |
#include <mcabber/compl.h> |
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
24 |
#include <mcabber/commands.h> |
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
25 |
#include <mcabber/utils.h> |
9eb794ebef85
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
26 |
#include <mcabber/logprint.h> |
5
c403a516dc04
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
27 |
#include <mcabber/modules.h> |
c403a516dc04
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
28 |
|
6
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
29 |
#include "config.h" |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
30 |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
31 |
void tcmd_init (void); |
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
32 |
void tcmd_uninit (void); |
5
c403a516dc04
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
33 |
|
6
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
34 |
#define DESCRIPTION ( "Templated commands\nProvides command /templatecmd (and any commands, you define with it)" ) |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
35 |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
36 |
static module_info_t info_templatecmd_dev = { |
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
37 |
.branch = "dev", |
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
38 |
.api = 20, |
6
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
39 |
.version = PROJECT_VERSION, |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
40 |
.description = DESCRIPTION, |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
41 |
.requires = NULL, |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
42 |
.init = tcmd_init, |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
43 |
.uninit = tcmd_uninit, |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
44 |
.next = NULL, |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
45 |
}; |
2cc481d6b686
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
46 |
|
11
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
47 |
module_info_t info_templatecmd = { |
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
48 |
.branch = "0.10.1", |
11
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
49 |
.api = 1, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
50 |
.version = PROJECT_VERSION, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
51 |
.description = DESCRIPTION, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
52 |
.requires = NULL, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
53 |
.init = tcmd_init, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
54 |
.uninit = tcmd_uninit, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
55 |
.next = &info_templatecmd_dev, |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
56 |
}; |
32564a4369f2
Add info struct for stable
Myhailo Danylenko <isbear@ukrpost.net>
parents:
10
diff
changeset
|
57 |
|
0 | 58 |
typedef struct { |
59 |
gchar *name; |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
60 |
#ifdef MCABBER_API_HAVE_CMD_ID |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
61 |
gpointer id; |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
62 |
#endif |
0 | 63 |
gchar *template; |
64 |
int maxarg; |
|
65 |
gboolean stararg; |
|
66 |
} tcmd_t; |
|
67 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
68 |
#ifdef MCABBER_API_HAVE_CMD_ID |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
69 |
static gpointer tcmd_cmid = NULL; |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
70 |
static gboolean tcmd_set_safe = FALSE; |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
71 |
#endif |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
72 |
|
0 | 73 |
static GSList *template_commands = NULL; |
74 |
||
75 |
static void tcmd_callback (char *arg, gpointer udata) |
|
76 |
{ |
|
77 |
tcmd_t *tcmd = (tcmd_t *) udata; |
|
78 |
char **args = NULL; |
|
79 |
GString *command = g_string_new (NULL); |
|
80 |
char *template = tcmd -> template; |
|
81 |
char *p; |
|
82 |
||
83 |
if (tcmd -> maxarg > 0) |
|
84 |
args = split_arg (arg, tcmd -> maxarg, TRUE); |
|
85 |
||
86 |
for (p = template; *p; ++p) { |
|
87 |
if (*p != '$' || !*(p + 1) || !strchr ("*$0123456789", *(p + 1))) { |
|
88 |
g_string_append_c (command, *p); |
|
89 |
continue; |
|
90 |
} |
|
91 |
||
92 |
++p; |
|
93 |
||
94 |
if (*p == '*') |
|
95 |
g_string_append (command, arg); |
|
96 |
else if (*p == '$') |
|
97 |
g_string_append_c (command, '$'); |
|
98 |
else { |
|
99 |
int argnum = atoi (p); |
|
100 |
||
101 |
if (argnum == 0) |
|
102 |
g_string_append (command, tcmd -> name); |
|
103 |
else if (args[argnum - 1]) |
|
104 |
g_string_append (command, args[argnum - 1]); |
|
105 |
||
106 |
while (*(p + 1) && strchr ("0123456789", *(p + 1))) |
|
107 |
++p; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
{ |
|
112 |
char *cmd = g_string_free (command, FALSE); |
|
113 |
process_command (cmd, 1); |
|
114 |
g_free (cmd); |
|
115 |
} |
|
116 |
||
117 |
if (args) |
|
118 |
free_arg_lst (args); |
|
119 |
} |
|
120 |
||
121 |
static void do_templatecmd (char *arg) |
|
122 |
{ |
|
123 |
if (!*arg) { // list tcmds |
|
124 |
||
125 |
GSList *tel; |
|
126 |
||
127 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
128 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
129 |
scr_log_print (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template); |
0 | 130 |
} |
131 |
||
132 |
} else { |
|
133 |
||
134 |
char *nend = strchr (arg, '='); |
|
135 |
||
136 |
if (!nend) { // show tcmd |
|
137 |
||
138 |
GSList *tel; |
|
139 |
||
140 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
141 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
142 |
||
143 |
if (!strcmp (arg, tcmd -> name)) { |
|
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
144 |
scr_log_print (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template); |
0 | 145 |
return; |
146 |
} |
|
147 |
} |
|
148 |
||
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
149 |
scr_log_print (LPRINT_NORMAL, "No template with such name."); |
0 | 150 |
|
151 |
} else if (nend == arg) // error |
|
152 |
||
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
153 |
scr_log_print (LPRINT_NORMAL, "You must specify command name."); |
0 | 154 |
|
155 |
else { // new/modify tcmd |
|
156 |
||
157 |
GSList *tel; |
|
158 |
tcmd_t *template_command = NULL; |
|
159 |
char *astart = nend + 1; |
|
160 |
int len; |
|
161 |
||
162 |
for (--nend; nend > arg && *nend == ' '; --nend); |
|
163 |
len = nend + 1 - arg; |
|
164 |
||
165 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
166 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
167 |
||
168 |
if (!strncmp (arg, tcmd -> name, len)) { |
|
169 |
template_command = tcmd; |
|
170 |
break; |
|
171 |
} |
|
172 |
} |
|
173 |
||
174 |
for (;*astart && *astart == ' '; ++astart); |
|
175 |
||
176 |
if (!*astart) { // delete tcmd |
|
177 |
if (template_command) { |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
178 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 179 |
cmd_del (template_command -> name); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
180 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
181 |
if (template_command -> id) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
182 |
cmd_del (template_command -> id); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
183 |
#endif |
0 | 184 |
template_commands = g_slist_remove (template_commands, template_command); |
185 |
g_free (template_command -> name); |
|
186 |
g_free (template_command -> template); |
|
187 |
g_free (template_command); |
|
188 |
} |
|
189 |
return; |
|
190 |
} |
|
191 |
||
192 |
if (!template_command) { |
|
193 |
template_command = g_new (tcmd_t, 1); |
|
194 |
template_command -> name = g_strndup (arg, len); |
|
195 |
} else |
|
196 |
g_free (template_command -> template); |
|
197 |
||
198 |
template_command -> template = g_strdup (astart); |
|
199 |
||
200 |
{ |
|
201 |
int maxarg = -1; |
|
202 |
gboolean stararg = FALSE; |
|
203 |
||
204 |
for (astart = strchr (astart, '$'); astart; astart = strchr (astart, '$')) { |
|
205 |
++astart; |
|
206 |
||
207 |
if (!*astart) |
|
208 |
break; |
|
209 |
||
210 |
if (strchr ("0123456789", *astart)) { |
|
211 |
int anum = atoi (astart); |
|
212 |
||
213 |
if (maxarg < anum) |
|
214 |
maxarg = anum; |
|
215 |
||
216 |
} else if (*astart == '*') |
|
217 |
stararg = TRUE; |
|
218 |
} |
|
219 |
||
220 |
template_command -> maxarg = maxarg; |
|
221 |
template_command -> stararg = stararg; |
|
222 |
} |
|
223 |
||
224 |
template_commands = g_slist_append (template_commands, template_command); |
|
225 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
226 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 227 |
cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
228 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
229 |
template_command -> id = cmd_add (template_command -> name, "", 0, 0, (void (*) (char *arg)) tcmd_callback, template_command); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
230 |
#endif |
0 | 231 |
} |
232 |
} |
|
233 |
} |
|
234 |
||
235 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
236 |
void tcmd_init (void) |
0 | 237 |
{ |
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
238 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 239 |
cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
240 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
241 |
tcmd_cmid = cmd_add ("templatecmd", "", COMPL_CMD, COMPL_CMD, do_templatecmd, NULL); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
242 |
tcmd_set_safe = cmd_set_safe ("templatecmd", TRUE); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
243 |
#endif |
0 | 244 |
} |
245 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
246 |
void tcmd_uninit (void) |
0 | 247 |
{ |
248 |
GSList *tel; |
|
249 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
250 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 251 |
cmd_del ("templatecmd"); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
252 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
253 |
if (tcmd_cmid) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
254 |
cmd_del (tcmd_cmid); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
255 |
if (tcmd_set_safe) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
256 |
cmd_set_safe ("templatecmd", FALSE); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
257 |
#endif |
0 | 258 |
|
259 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
260 |
tcmd_t *template_command = (tcmd_t *) tel -> data; |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
261 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 262 |
cmd_del (template_command -> name); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
263 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
264 |
if (template_command -> id) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
265 |
cmd_del (template_command -> id); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
266 |
#endif |
0 | 267 |
g_free (template_command -> name); |
268 |
g_free (template_command -> template); |
|
269 |
g_free (template_command); |
|
270 |
} |
|
271 |
||
272 |
g_slist_free (template_commands); |
|
273 |
} |
|
274 |
||
275 |
/* vim: se ts=4 sw=4: */ |