1 | /* |
1 | /* |
2 | * pmodule.C: Protocol command management. |
2 | * pmodule.C: Protocol command management. |
3 | * Rights to this code are documented in doc/pod/license.pod. |
3 | * Rights to this code are documented in doc/pod/license.pod. |
4 | * |
4 | * |
5 | * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
5 | * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 | */ |
6 | */ |
7 | |
7 | |
8 | static char const rcsid[] = "$Id: pmodule.C,v 1.3 2007/07/21 13:23:22 pippijn Exp $"; |
8 | static char const rcsid[] = "$Id: pmodule.C,v 1.4 2007/08/28 17:08:12 pippijn Exp $"; |
|
|
9 | |
|
|
10 | #include <map> |
9 | |
11 | |
10 | #include "atheme.h" |
12 | #include "atheme.h" |
11 | #include "pmodule.h" |
13 | #include "pmodule.h" |
12 | |
14 | |
13 | dictionary_tree_t *pcommands; |
15 | typedef std::map<char const * const, pcommand_t const *, str_lt> pcommand_map; |
14 | BlockHeap *pcommand_heap; |
16 | pcommand_map pcommands; |
15 | BlockHeap *messagetree_heap; |
17 | |
16 | cmode_t *mode_list; |
18 | cmode_t *mode_list; |
17 | extmode *ignore_mode_list; |
19 | extmode *ignore_mode_list; |
18 | cmode_t *status_mode_list; |
20 | cmode_t *status_mode_list; |
19 | cmode_t *prefix_mode_list; |
21 | cmode_t *prefix_mode_list; |
20 | ircd_t *ircd; |
22 | protocol::ircd_t *ircd; |
21 | bool pmodule_loaded = false; |
23 | bool protocol::handler::loaded = false; |
22 | bool backend_loaded = false; |
|
|
23 | |
24 | |
24 | void |
25 | void |
25 | pcommand_init (void) |
26 | pcommand_add (pcommand_t const &pcmd) |
26 | { |
27 | { |
27 | pcommand_heap = BlockHeapCreate (sizeof (pcommand_t), 64); |
28 | if (pcommand_find (pcmd.token)) |
28 | |
|
|
29 | if (!pcommand_heap) |
|
|
30 | { |
29 | { |
31 | slog (LG_INFO, "pcommand_init(): block allocator failed."); |
30 | slog (LG_INFO, "pcommand_add(): token %s is already registered", pcmd.token); |
32 | exit (EXIT_FAILURE); |
31 | return; |
33 | } |
32 | } |
34 | |
33 | |
35 | pcommands = dictionary_create ("pcommand", HASH_COMMAND, strcmp); |
34 | pcommands[pcmd.token] = &pcmd; |
36 | } |
35 | } |
37 | |
36 | |
38 | void |
37 | void |
39 | pcommand_add (char *token, void (*handler) (sourceinfo_t *si, int parc, char *parv[]), int minparc, int sourcetype) |
38 | pcommand_delete (pcommand_t const &pcmd) |
40 | { |
39 | { |
41 | pcommand_t *pcmd; |
|
|
42 | |
|
|
43 | if ((pcmd = pcommand_find (token))) |
40 | if (!pcommand_find (pcmd.token)) |
44 | { |
41 | { |
45 | slog (LG_INFO, "pcommand_add(): token %s is already registered", token); |
42 | slog (LG_INFO, "pcommand_delete(): token %s is not registered", pcmd.token); |
46 | return; |
43 | return; |
47 | } |
44 | } |
48 | |
45 | |
49 | pcmd = static_cast<pcommand_t *> (BlockHeapAlloc (pcommand_heap)); |
46 | pcommands.erase (pcmd.token); |
50 | pcmd->token = sstrdup (token); |
|
|
51 | pcmd->handler = handler; |
|
|
52 | pcmd->minparc = minparc; |
|
|
53 | pcmd->sourcetype = sourcetype; |
|
|
54 | |
|
|
55 | dictionary_add (pcommands, pcmd->token, pcmd); |
|
|
56 | } |
47 | } |
57 | |
48 | |
58 | void |
49 | pcommand_t const * |
59 | pcommand_delete (char *token) |
50 | pcommand_find (char const * const token) |
60 | { |
51 | { |
61 | pcommand_t *pcmd; |
52 | pcommand_map::iterator it; |
62 | |
53 | if ((it = pcommands.find (token)) != pcommands.end ()) |
63 | if (!(pcmd = pcommand_find (token))) |
54 | return it->second; |
64 | { |
55 | return NULL; |
65 | slog (LG_INFO, "pcommand_delete(): token %s is not registered", token); |
|
|
66 | return; |
|
|
67 | } |
|
|
68 | |
|
|
69 | dictionary_delete (pcommands, pcmd->token); |
|
|
70 | |
|
|
71 | free (pcmd->token); |
|
|
72 | pcmd->handler = NULL; |
|
|
73 | BlockHeapFree (pcommand_heap, pcmd); |
|
|
74 | } |
56 | } |
75 | |
|
|
76 | pcommand_t * |
|
|
77 | pcommand_find (char *token) |
|
|
78 | { |
|
|
79 | return static_cast<pcommand_t *> (dictionary_retrieve (pcommands, token)); |
|
|
80 | } |
|
|