author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 20 Aug 2012 18:20:07 +0300 | |
changeset 18 | aeb36f2945de |
parent 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 |
|
17
82331654a753
Use gslices and build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
36 |
module_info_t info_templatecmd = { |
82331654a753
Use gslices and build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
37 |
.branch = MCABBER_BRANCH, |
82331654a753
Use gslices and build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
38 |
.api = MCABBER_API_VERSION, |
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 |
|
0 | 47 |
typedef struct { |
48 |
gchar *name; |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
49 |
#ifdef MCABBER_API_HAVE_CMD_ID |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
50 |
gpointer id; |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
51 |
#endif |
0 | 52 |
gchar *template; |
53 |
int maxarg; |
|
54 |
gboolean stararg; |
|
55 |
} tcmd_t; |
|
56 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
57 |
#ifdef MCABBER_API_HAVE_CMD_ID |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
58 |
static gpointer tcmd_cmid = NULL; |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
59 |
static gboolean tcmd_set_safe = FALSE; |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
60 |
#endif |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
61 |
|
0 | 62 |
static GSList *template_commands = NULL; |
63 |
||
64 |
static void tcmd_callback (char *arg, gpointer udata) |
|
65 |
{ |
|
66 |
tcmd_t *tcmd = (tcmd_t *) udata; |
|
67 |
char **args = NULL; |
|
68 |
GString *command = g_string_new (NULL); |
|
69 |
char *template = tcmd -> template; |
|
70 |
char *p; |
|
71 |
||
72 |
if (tcmd -> maxarg > 0) |
|
73 |
args = split_arg (arg, tcmd -> maxarg, TRUE); |
|
74 |
||
75 |
for (p = template; *p; ++p) { |
|
76 |
if (*p != '$' || !*(p + 1) || !strchr ("*$0123456789", *(p + 1))) { |
|
77 |
g_string_append_c (command, *p); |
|
78 |
continue; |
|
79 |
} |
|
80 |
||
81 |
++p; |
|
82 |
||
83 |
if (*p == '*') |
|
84 |
g_string_append (command, arg); |
|
85 |
else if (*p == '$') |
|
86 |
g_string_append_c (command, '$'); |
|
87 |
else { |
|
88 |
int argnum = atoi (p); |
|
89 |
||
90 |
if (argnum == 0) |
|
91 |
g_string_append (command, tcmd -> name); |
|
92 |
else if (args[argnum - 1]) |
|
93 |
g_string_append (command, args[argnum - 1]); |
|
94 |
||
95 |
while (*(p + 1) && strchr ("0123456789", *(p + 1))) |
|
96 |
++p; |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
{ |
|
101 |
char *cmd = g_string_free (command, FALSE); |
|
102 |
process_command (cmd, 1); |
|
103 |
g_free (cmd); |
|
104 |
} |
|
105 |
||
106 |
if (args) |
|
107 |
free_arg_lst (args); |
|
108 |
} |
|
109 |
||
110 |
static void do_templatecmd (char *arg) |
|
111 |
{ |
|
112 |
if (!*arg) { // list tcmds |
|
113 |
||
114 |
GSList *tel; |
|
115 |
||
116 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
117 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
118 |
scr_log_print (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template); |
0 | 119 |
} |
120 |
||
121 |
} else { |
|
122 |
||
123 |
char *nend = strchr (arg, '='); |
|
124 |
||
125 |
if (!nend) { // show tcmd |
|
126 |
||
127 |
GSList *tel; |
|
128 |
||
129 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
130 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
131 |
||
132 |
if (!strcmp (arg, tcmd -> name)) { |
|
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
133 |
scr_log_print (LPRINT_NORMAL, "Templatecmd %s = %s", tcmd -> name, tcmd -> template); |
0 | 134 |
return; |
135 |
} |
|
136 |
} |
|
137 |
||
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
138 |
scr_log_print (LPRINT_NORMAL, "No template with such name."); |
0 | 139 |
|
140 |
} else if (nend == arg) // error |
|
141 |
||
7
b87a5d8cd8ad
Use new scr_* functions
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
142 |
scr_log_print (LPRINT_NORMAL, "You must specify command name."); |
0 | 143 |
|
144 |
else { // new/modify tcmd |
|
145 |
||
146 |
GSList *tel; |
|
147 |
tcmd_t *template_command = NULL; |
|
148 |
char *astart = nend + 1; |
|
149 |
int len; |
|
150 |
||
151 |
for (--nend; nend > arg && *nend == ' '; --nend); |
|
152 |
len = nend + 1 - arg; |
|
153 |
||
154 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
155 |
tcmd_t *tcmd = (tcmd_t *) tel -> data; |
|
156 |
||
157 |
if (!strncmp (arg, tcmd -> name, len)) { |
|
158 |
template_command = tcmd; |
|
159 |
break; |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
for (;*astart && *astart == ' '; ++astart); |
|
164 |
||
165 |
if (!*astart) { // delete tcmd |
|
166 |
if (template_command) { |
|
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
167 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 168 |
cmd_del (template_command -> name); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
169 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
170 |
if (template_command -> id) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
171 |
cmd_del (template_command -> id); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
172 |
#endif |
0 | 173 |
template_commands = g_slist_remove (template_commands, template_command); |
174 |
g_free (template_command -> name); |
|
175 |
g_free (template_command -> template); |
|
17
82331654a753
Use gslices and build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
176 |
g_slice_free (tcmd_t, template_command); |
0 | 177 |
} |
178 |
return; |
|
179 |
} |
|
180 |
||
181 |
if (!template_command) { |
|
17
82331654a753
Use gslices and build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
182 |
template_command = g_slice_new (tcmd_t); |
0 | 183 |
template_command -> name = g_strndup (arg, len); |
184 |
} else |
|
185 |
g_free (template_command -> template); |
|
186 |
||
187 |
template_command -> template = g_strdup (astart); |
|
188 |
||
189 |
{ |
|
190 |
int maxarg = -1; |
|
191 |
gboolean stararg = FALSE; |
|
192 |
||
193 |
for (astart = strchr (astart, '$'); astart; astart = strchr (astart, '$')) { |
|
194 |
++astart; |
|
195 |
||
196 |
if (!*astart) |
|
197 |
break; |
|
198 |
||
199 |
if (strchr ("0123456789", *astart)) { |
|
200 |
int anum = atoi (astart); |
|
201 |
||
202 |
if (maxarg < anum) |
|
203 |
maxarg = anum; |
|
204 |
||
205 |
} else if (*astart == '*') |
|
206 |
stararg = TRUE; |
|
207 |
} |
|
208 |
||
209 |
template_command -> maxarg = maxarg; |
|
210 |
template_command -> stararg = stararg; |
|
211 |
} |
|
212 |
||
213 |
template_commands = g_slist_append (template_commands, template_command); |
|
214 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
215 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 216 |
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
|
217 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
218 |
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
|
219 |
#endif |
0 | 220 |
} |
221 |
} |
|
222 |
} |
|
223 |
||
224 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
225 |
void tcmd_init (void) |
0 | 226 |
{ |
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
227 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 228 |
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
|
229 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
230 |
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
|
231 |
tcmd_set_safe = cmd_set_safe ("templatecmd", TRUE); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
232 |
#endif |
0 | 233 |
} |
234 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
235 |
void tcmd_uninit (void) |
0 | 236 |
{ |
237 |
GSList *tel; |
|
238 |
||
14
5d8f38cc86ac
Update versions, add AVV description
Myhailo Danylenko <isbear@ukrpost.net>
parents:
11
diff
changeset
|
239 |
#ifndef MCABBER_API_HAVE_CMD_ID |
0 | 240 |
cmd_del ("templatecmd"); |
10
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
241 |
#else |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
242 |
if (tcmd_cmid) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
243 |
cmd_del (tcmd_cmid); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
244 |
if (tcmd_set_safe) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
245 |
cmd_set_safe ("templatecmd", FALSE); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
246 |
#endif |
0 | 247 |
|
248 |
for (tel = template_commands; tel; tel = tel -> next) { |
|
249 |
tcmd_t *template_command = (tcmd_t *) tel -> data; |
|
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 (template_command -> name); |
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 (template_command -> id) |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
254 |
cmd_del (template_command -> id); |
b0511d0cd16e
Use new commands interface
Myhailo Danylenko <isbear@ukrpost.net>
parents:
9
diff
changeset
|
255 |
#endif |
0 | 256 |
g_free (template_command -> name); |
257 |
g_free (template_command -> template); |
|
17
82331654a753
Use gslices and build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
16
diff
changeset
|
258 |
g_slice_free (tcmd_t, template_command); |
0 | 259 |
} |
260 |
||
261 |
g_slist_free (template_commands); |
|
262 |
} |
|
263 |
||
264 |
/* vim: se ts=4 sw=4: */ |