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

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

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


Revision 1.21 - (show annotations) (download) (as text)
Tue Dec 23 22:23:04 2003 UTC (15 years, 10 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 /* 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
20 #ifndef lint
21 static const char rcsid[] = "$Id: core_party.c,v 1.20 2003/12/20 00:34:37 stdarg Exp $";
22 #endif
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
26 #endif
27
28 #include <eggdrop/eggdrop.h>
29 #include <ctype.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include "core_config.h"
34 #include "core_binds.h"
35 #include "logfile.h"
36
37 #ifdef HAVE_UNAME
38 # include <sys/utsname.h>
39 #endif
40
41 extern char pid_file[];
42
43 static int party_join(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
44 {
45 if (!text || !*text) {
46 partymember_printf(p, _("Syntax: join <channel>"));
47 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 if (!text || !*text) chan = partychan_get_default(p);
58 else chan = partychan_lookup_name(text);
59 partychan_part(chan, p, "parting");
60 return(0);
61 }
62
63 static int party_quit(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
64 {
65 partymember_printf(p, "Goodbye!");
66 if (!text || !*text) partymember_delete(p, "Quit");
67 else partymember_delete(p, text);
68 return(0);
69 }
70
71 static int party_whisper(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
72 {
73 char *who;
74 const char *next;
75 partymember_t *dest;
76
77 egg_get_arg(text, &next, &who);
78 if (!who || !next || !*who || !*next) {
79 partymember_printf(p, _("Syntax: whisper <partylineuser> <msg>"));
80 goto done;
81 }
82
83 dest = partymember_lookup_nick(who);
84 if (!dest) {
85 partymember_printf(p, _("No such user '%s'."), who);
86 goto done;
87 }
88
89 partymember_msg(dest, p, next, -1);
90 done:
91 if (who) free(who);
92 return(0);
93 }
94
95 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 partymember_printf(p, _("That setting does not exist."));
105 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 partymember_printf(p, _("You cannot access that setting."));
118 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 partymember_printf(p, _("Syntax: get <path>"));
128 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 partymember_printf(p, _("==> null (unset)"));
140 }
141 return(0);
142 }
143
144 static int party_set(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
145 {
146 void *root;
147 char *path, *str;
148 const char *next;
149
150 egg_get_arg(text, &next, &path);
151 if (!next || !*next || !path || !*path) {
152 partymember_printf(p, _("Syntax: set <path> <new value>"));
153 if (path) free(path);
154 return(0);
155 }
156
157 root = lookup_and_check(p, path);
158 free(path);
159 if (!root) return(0);
160
161 config_get_str(&str, root, NULL);
162 partymember_printf(p, _("Old value: '%s'"), str);
163 config_set_str(next, root, NULL);
164 config_get_str(&str, root, NULL);
165 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 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 partymember_printf(p, _("I am %1$s, running Eggdrop %2$s."), core_config.botname, VERSION);
200 partymember_printf(p, _("Owner: %s"), core_config.owner);
201 if (core_config.admin) partymember_printf(p, _("Admin: %s"), core_config.admin);
202 #ifdef HAVE_UNAME
203 if (!uname(&un)) partymember_printf(p, _("OS: %1$s %2$s"), un.sysname, un.release);
204 #endif
205 partymember_printf(p, "");
206 check_bind_status(p, text);
207 return(0);
208 }
209
210 static int party_save(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
211 {
212 putlog(LOG_MISC, "*", _("Saving user file..."));
213 user_save(core_config.userfile);
214 putlog(LOG_MISC, "*", _("Saving config file..."));
215 core_config_save();
216 return(1);
217 }
218
219 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 partymember_printf(p, _("Please use at least 6 characters."));
223 return(0);
224 }
225 user_set_pass(p->user, text);
226 partymember_printf(p, _("Changed password to '%s'."), text);
227 return(0);
228 }
229
230 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 int *pids, len, i, width = 0;
239
240 partymember_printf(p, _("Partyline members:"));
241 partymember_who(&pids, &len);
242 qsort(pids, len, sizeof(int), intsorter);
243 if (len > 0) {
244 i = pids[len-1];
245 if (!i) i++;
246 while (i != 0) {
247 i /= 10;
248 width++;
249 }
250 }
251 for (i = 0; i < len; i++) {
252 who = partymember_lookup_pid(pids[i]);
253 partymember_printf(p, " [%*d] %s (%s@%s)", width, who->pid, who->nick, who->ident, who->host);
254 }
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 putlog(LOG_MISC, "*", _("Saving user file..."));
262 user_save(core_config.userfile);
263 if (text && *text) putlog(LOG_MISC, "*", _("Bot shutting down: %s"), text);
264 else putlog(LOG_MISC, "*", _("Bot shutting down."));
265 flushlogs();
266 unlink(pid_file);
267 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 partymember_printf(p, _("Syntax: +user <handle>"));
276 return(0);
277 }
278 if (user_lookup_by_handle(text)) {
279 partymember_printf(p, _("User '%s' already exists!"));
280 return(0);
281 }
282 newuser = user_new(text);
283 if (newuser) partymember_printf(p, _("User '%s' created."), text);
284 else partymember_printf(p, _("Could not create user '%s'."), text);
285 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 partymember_printf(p, _("Syntax: -user <handle>"));
294 return(0);
295 }
296 who = user_lookup_by_handle(text);
297 if (!who) partymember_printf(p, _("User '%s' not found."));
298 else {
299 partymember_printf(p, _("Deleting user '%s'."), who->handle);
300 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 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 /* Syntax: chattr <user> [chan] <flags> */
316 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 n = egg_get_args(text, &next, &who, &chan, &flags, NULL);
326 if (!chan || !*chan) {
327 if (who) free(who);
328 partymember_printf(p, _("Syntax: chattr <user> [channel] <+/-flags>"));
329 return(0);
330 }
331 if (!flags || !*flags) {
332 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 partymember_printf(p, _("Flags for %s are now '%s'."), who, flagstr);
341 }
342 else {
343 partymember_printf(p, _("'%s' is not a valid user."), who);
344 }
345 free(who);
346 free(flags);
347 if (chan) free(chan);
348 return(0);
349 }
350
351 static int party_modules(partymember_t *p, const char *nick, user_t *u, const char *cmd, const char *text)
352 {
353 const char **modules;
354 int nummods, ctr;
355
356 nummods = module_list(&modules);
357 partymember_printf(p, _("Loaded modules:"));
358 for (ctr = 0; ctr < nummods; ctr++) partymember_printf(p, " %s", modules[ctr]);
359 free(modules);
360 return(0);
361 }
362
363 static bind_list_t core_party_binds[] = {
364 {NULL, "join", party_join},
365 {NULL, "whisper", party_whisper},
366 {NULL, "newpass", party_newpass},
367 {NULL, "part", party_part},
368 {NULL, "quit", party_quit},
369 {NULL, "who", party_who},
370 {"n", "get", party_get},
371 {"n", "set", party_set},
372 {"n", "unset", party_unset},
373 {"n", "status", party_status},
374 {"n", "save", party_save},
375 {"n", "die", party_die},
376 {"n", "+user", party_plus_user},
377 {"n", "-user", party_minus_user},
378 {"n", "+host", party_plus_host},
379 {"n", "-host", party_minus_host},
380 {"n", "chattr", party_chattr},
381 {"n", "modules", party_modules},
382 {"m", "+host", party_plus_host},
383 {"m", "-host", party_minus_host},
384 {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