/[cvs]/eggdrop1.9/src/core_party.c
ViewVC logotype

Annotation of /eggdrop1.9/src/core_party.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.21 - (hide annotations) (download) (as text)
Tue Dec 23 22:23:04 2003 UTC (15 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.20: +5 -6 lines
File MIME type: text/x-chdr
* egg_get_arg() retrieves an arg from a string and understands quotes
single quotes, and escape sequences in a bash-like manner.

1 wcc 1.15 /* core_party.c: core partyline commands
2     *
3     * Copyright (C) 2003, 2004 Eggheads Development Team
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19 wcc 1.14
20 wcc 1.15 #ifndef lint
21 stdarg 1.21 static const char rcsid[] = "$Id: core_party.c,v 1.20 2003/12/20 00:34:37 stdarg Exp $";
22 wcc 1.15 #endif
23 wcc 1.14
24     #ifdef HAVE_CONFIG_H
25     # include <config.h>
26     #endif
27    
28 wcc 1.15 #include <eggdrop/eggdrop.h>
29     #include <ctype.h>
30     #include <unistd.h>
31 wcc 1.17 #include <string.h>
32     #include <stdlib.h>
33 wcc 1.15 #include "core_config.h"
34 stdarg 1.20 #include "core_binds.h"
35 wcc 1.15 #include "logfile.h"
36    
37 wcc 1.14 #ifdef HAVE_UNAME
38     # include <sys/utsname.h>
39     #endif
40    
41     extern char pid_file[];
42 stdarg 1.1
43 stdarg 1.4 static int party_join(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
44     {
45 wcc 1.12 if (!text || !*text) {
46 wcc 1.16 partymember_printf(p, _("Syntax: join <channel>"));
47 stdarg 1.4 return(0);
48     }
49     partychan_join_name(text, p);
50     return(0);
51     }
52    
53     static int party_part(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
54     {
55     partychan_t *chan;
56    
57 wcc 1.12 if (!text || !*text) chan = partychan_get_default(p);
58 stdarg 1.4 else chan = partychan_lookup_name(text);
59     partychan_part(chan, p, "parting");
60     return(0);
61     }
62    
63 stdarg 1.3 static int party_quit(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
64 stdarg 1.1 {
65 wcc 1.18 partymember_printf(p, "Goodbye!");
66     if (!text || !*text) partymember_delete(p, "Quit");
67 wcc 1.12 else partymember_delete(p, text);
68 stdarg 1.1 return(0);
69     }
70    
71 wcc 1.12 static int party_whisper(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
72 stdarg 1.1 {
73 stdarg 1.13 char *who;
74     const char *next;
75     partymember_t *dest;
76    
77 stdarg 1.21 egg_get_arg(text, &next, &who);
78 stdarg 1.13 if (!who || !next || !*who || !*next) {
79 wcc 1.16 partymember_printf(p, _("Syntax: whisper <partylineuser> <msg>"));
80 stdarg 1.13 goto done;
81     }
82    
83     dest = partymember_lookup_nick(who);
84     if (!dest) {
85 wcc 1.16 partymember_printf(p, _("No such user '%s'."), who);
86 stdarg 1.13 goto done;
87     }
88    
89     partymember_msg(dest, p, next, -1);
90     done:
91     if (who) free(who);
92 stdarg 1.1 return(0);
93     }
94    
95 stdarg 1.11 static void *lookup_and_check(partymember_t *p, const char *path)
96     {
97     void *root;
98     char *flags;
99     flags_t uflags, rflags;
100    
101     root = config_get_root("eggdrop");
102     root = config_exists(root, path, 0, NULL);
103     if (!root) {
104 stdarg 1.20 partymember_printf(p, _("That setting does not exist."));
105 stdarg 1.11 return(NULL);
106     }
107    
108     config_get_str(&flags, root, "flags", 0, NULL);
109     if (flags) {
110     flag_from_str(&rflags, flags);
111     if (!p->user) goto nope;
112     user_get_flags(p->user, NULL, &uflags);
113     if (!flag_match_partial(&uflags, &rflags)) goto nope;
114     }
115     return(root);
116     nope:
117 wcc 1.16 partymember_printf(p, _("You cannot access that setting."));
118 stdarg 1.11 return(NULL);
119     }
120    
121     static int party_get(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
122     {
123     void *root;
124     char *str = NULL;
125    
126     if (!text || !*text) {
127 wcc 1.16 partymember_printf(p, _("Syntax: get <path>"));
128 stdarg 1.11 return(0);
129     }
130    
131     root = lookup_and_check(p, text);
132     if (!root) return(0);
133    
134     config_get_str(&str, root, NULL);
135     if (str) {
136     partymember_printf(p, "==> '%s'", str);
137     }
138     else {
139 stdarg 1.20 partymember_printf(p, _("==> null (unset)"));
140 stdarg 1.11 }
141     return(0);
142     }
143    
144 stdarg 1.6 static int party_set(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
145 stdarg 1.2 {
146 stdarg 1.11 void *root;
147     char *path, *str;
148     const char *next;
149    
150 stdarg 1.21 egg_get_arg(text, &next, &path);
151 wcc 1.14 if (!next || !*next || !path || !*path) {
152 wcc 1.16 partymember_printf(p, _("Syntax: set <path> <new value>"));
153 stdarg 1.11 if (path) free(path);
154     return(0);
155     }
156 wcc 1.14
157 stdarg 1.11 root = lookup_and_check(p, path);
158     free(path);
159     if (!root) return(0);
160    
161     config_get_str(&str, root, NULL);
162 wcc 1.16 partymember_printf(p, _("Old value: '%s'"), str);
163 stdarg 1.11 config_set_str(next, root, NULL);
164     config_get_str(&str, root, NULL);
165 stdarg 1.20 if (str) partymember_printf(p, _("New value: '%s'"), str);
166     else partymember_printf(p, _("New value: null (unset)"));
167     return(0);
168     }
169    
170     static int party_unset(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
171     {
172     void *root;
173     char *str;
174    
175     if (!text || !*text) {
176     partymember_printf(p, _("Syntax: unset <path>"));
177     return(0);
178     }
179    
180     root = lookup_and_check(p, text);
181     if (!root) return(0);
182    
183     config_get_str(&str, root, NULL);
184     if (str) partymember_printf(p, _("Old value: '%s'"), str);
185     else partymember_printf(p, _("Old value: null (unset)"));
186     config_set_str(NULL, root, NULL);
187     config_get_str(&str, root, NULL);
188     if (str) partymember_printf(p, _("New value: '%s'"), str);
189     else partymember_printf(p, _("New value: null (unset)"));
190 wcc 1.14 return(0);
191     }
192    
193     static int party_status(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
194     {
195     #ifdef HAVE_UNAME
196     struct utsname un;
197     #endif
198    
199 stdarg 1.20 partymember_printf(p, _("I am %1$s, running Eggdrop %2$s."), core_config.botname, VERSION);
200 wcc 1.16 partymember_printf(p, _("Owner: %s"), core_config.owner);
201     if (core_config.admin) partymember_printf(p, _("Admin: %s"), core_config.admin);
202 wcc 1.14 #ifdef HAVE_UNAME
203 stdarg 1.20 if (!uname(&un)) partymember_printf(p, _("OS: %1$s %2$s"), un.sysname, un.release);
204 wcc 1.14 #endif
205 stdarg 1.20 partymember_printf(p, "");
206     check_bind_status(p, text);
207 stdarg 1.2 return(0);
208     }
209    
210 stdarg 1.6 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
211 stdarg 1.2 {
212 wcc 1.16 putlog(LOG_MISC, "*", _("Saving user file..."));
213 stdarg 1.5 user_save(core_config.userfile);
214 wcc 1.16 putlog(LOG_MISC, "*", _("Saving config file..."));
215 stdarg 1.2 core_config_save();
216     return(1);
217     }
218    
219 stdarg 1.5 static int party_newpass(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
220     {
221     if (!text || strlen(text) < 6) {
222 wcc 1.16 partymember_printf(p, _("Please use at least 6 characters."));
223 stdarg 1.5 return(0);
224     }
225     user_set_pass(p->user, text);
226 wcc 1.16 partymember_printf(p, _("Changed password to '%s'."), text);
227 stdarg 1.5 return(0);
228     }
229    
230 stdarg 1.7 static int intsorter(const void *left, const void *right)
231     {
232     return(*(int *)left - *(int *)right);
233     }
234    
235     static int party_who(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
236     {
237     partymember_t *who;
238 stdarg 1.20 int *pids, len, i, width = 0;
239 stdarg 1.7
240 wcc 1.16 partymember_printf(p, _("Partyline members:"));
241 stdarg 1.7 partymember_who(&pids, &len);
242     qsort(pids, len, sizeof(int), intsorter);
243 stdarg 1.20 if (len > 0) {
244     i = pids[len-1];
245     if (!i) i++;
246     while (i != 0) {
247     i /= 10;
248     width++;
249     }
250     }
251 stdarg 1.7 for (i = 0; i < len; i++) {
252     who = partymember_lookup_pid(pids[i]);
253 stdarg 1.20 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
254 stdarg 1.7 }
255     free(pids);
256     return(0);
257     }
258    
259     static int party_die(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
260     {
261 wcc 1.16 putlog(LOG_MISC, "*", _("Saving user file..."));
262 wcc 1.12 user_save(core_config.userfile);
263 stdarg 1.19 if (text && *text) putlog(LOG_MISC, "*", _("Bot shutting down: %s"), text);
264 wcc 1.16 else putlog(LOG_MISC, "*", _("Bot shutting down."));
265 wcc 1.14 flushlogs();
266     unlink(pid_file);
267 stdarg 1.7 exit(0);
268     }
269    
270     static int party_plus_user(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
271     {
272     user_t *newuser;
273    
274     if (!text || !*text) {
275 wcc 1.16 partymember_printf(p, _("Syntax: +user <handle>"));
276 stdarg 1.7 return(0);
277     }
278     if (user_lookup_by_handle(text)) {
279 wcc 1.16 partymember_printf(p, _("User '%s' already exists!"));
280 stdarg 1.7 return(0);
281     }
282     newuser = user_new(text);
283 wcc 1.16 if (newuser) partymember_printf(p, _("User '%s' created."), text);
284     else partymember_printf(p, _("Could not create user '%s'."), text);
285 stdarg 1.7 return(0);
286     }
287    
288     static int party_minus_user(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
289     {
290     user_t *who;
291    
292     if (!text || !*text) {
293 wcc 1.16 partymember_printf(p, _("Syntax: -user <handle>"));
294 stdarg 1.7 return(0);
295     }
296     who = user_lookup_by_handle(text);
297 wcc 1.16 if (!who) partymember_printf(p, _("User '%s' not found."));
298 stdarg 1.7 else {
299 wcc 1.16 partymember_printf(p, _("Deleting user '%s'."), who->handle);
300 stdarg 1.7 user_delete(who);
301     }
302     return(0);
303     }
304    
305     static int party_plus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
306     {
307     return(0);
308     }
309    
310 stdarg 1.9 static int party_minus_host(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
311     {
312     return(0);
313     }
314    
315 stdarg 1.21 /* Syntax: chattr <user> [chan] <flags> */
316 stdarg 1.9 static int party_chattr(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
317     {
318     const char *next;
319     char *who, *flags, *chan;
320     user_t *dest;
321     flags_t flagstruct;
322     char flagstr[64];
323     int n;
324    
325 stdarg 1.21 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
326 wcc 1.14 if (!chan || !*chan) {
327 stdarg 1.10 if (who) free(who);
328 wcc 1.16 partymember_printf(p, _("Syntax: chattr <user> [channel] <+/-flags>"));
329 stdarg 1.9 return(0);
330     }
331 wcc 1.14 if (!flags || !*flags) {
332 stdarg 1.9 flags = chan;
333     chan = NULL;
334     }
335     dest = user_lookup_by_handle(who);
336     if (dest) {
337     user_set_flag_str(dest, chan, flags);
338     user_get_flags(dest, chan, &flagstruct);
339     flag_to_str(&flagstruct, flagstr);
340 wcc 1.16 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
341 stdarg 1.9 }
342     else {
343 wcc 1.16 partymember_printf(p, _("'%s' is not a valid user."), who);
344 stdarg 1.9 }
345     free(who);
346     free(flags);
347     if (chan) free(chan);
348     return(0);
349     }
350    
351 wcc 1.12 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
352     {
353 stdarg 1.19 const char **modules;
354 wcc 1.12 int nummods, ctr;
355    
356 stdarg 1.19 nummods = module_list(&modules);
357 wcc 1.16 partymember_printf(p, _("Loaded modules:"));
358 wcc 1.12 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
359     free(modules);
360     return(0);
361     }
362    
363 stdarg 1.1 static bind_list_t core_party_binds[] = {
364 stdarg 1.6 {NULL, "join", party_join},
365 wcc 1.12 {NULL, "whisper", party_whisper},
366 stdarg 1.6 {NULL, "newpass", party_newpass},
367     {NULL, "part", party_part},
368     {NULL, "quit", party_quit},
369 stdarg 1.7 {NULL, "who", party_who},
370 stdarg 1.11 {"n", "get", party_get},
371 stdarg 1.6 {"n", "set", party_set},
372 stdarg 1.20 {"n", "unset", party_unset},
373 wcc 1.14 {"n", "status", party_status},
374 stdarg 1.6 {"n", "save", party_save},
375 stdarg 1.7 {"n", "die", party_die},
376     {"n", "+user", party_plus_user},
377     {"n", "-user", party_minus_user},
378 stdarg 1.11 {"n", "+host", party_plus_host},
379     {"n", "-host", party_minus_host},
380 stdarg 1.9 {"n", "chattr", party_chattr},
381 wcc 1.12 {"n", "modules", party_modules},
382     {"m", "+host", party_plus_host},
383     {"m", "-host", party_minus_host},
384 stdarg 1.1 {0}
385     };
386    
387     void core_party_init()
388     {
389     bind_add_list("party", core_party_binds);
390     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23