Debian Codegraph @

modules/statserv/server.c

raw
1 /*
2 * Copyright (c) 2011 Alexandria Wolcott
3 * Released under the same terms as Atheme itself.
4 *
5 * Gather information about networked servers.
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1("statserv/server", false, _modinit, _moddeinit,
12 PACKAGE_STRING, "Alexandria Wolcott <[email protected]>");
13
14 static void ss_cmd_server(sourceinfo_t * si, int parc, char *parv[]);
15 static void ss_cmd_server_info(sourceinfo_t * si, int parc, char *parv[]);
16 static void ss_cmd_server_list(sourceinfo_t * si, int parc, char *parv[]);
17 static void ss_cmd_server_count(sourceinfo_t * si, int parc, char *parv[]);
18
19 command_t ss_server =
20 { "SERVER", N_("Obtain information about servers on the network."), AC_NONE, 3, ss_cmd_server, {.path = "statserv/server"} };
21
22 command_t ss_server_list =
23 { "LIST", N_("Obtain a list of servers."), AC_NONE, 1, ss_cmd_server_list, {.path = ""} };
24
25 command_t ss_server_count =
26 { "COUNT", N_("Count the amount of servers connected to the network."), AC_NONE, 1, ss_cmd_server_count, {.path = ""} };
27
28 command_t ss_server_info =
29 { "INFO", N_("Obtain information about a specified server."), AC_NONE, 2, ss_cmd_server_info, {.path = ""} };
30
31 mowgli_patricia_t *ss_server_cmds;
32
33 void _modinit(module_t * m)
34 {
35 service_named_bind_command("statserv", &ss_server);
36 ss_server_cmds = mowgli_patricia_create(strcasecanon);
37 command_add(&ss_server_list, ss_server_cmds);
38 command_add(&ss_server_count, ss_server_cmds);
39 command_add(&ss_server_info, ss_server_cmds);
40 }
41
42 void _moddeinit(module_unload_intent_t intent)
43 {
44 service_named_unbind_command("statserv", &ss_server);
45 command_delete(&ss_server_list, ss_server_cmds);
46 command_delete(&ss_server_count, ss_server_cmds);
47 command_delete(&ss_server_info, ss_server_cmds);
48 mowgli_patricia_destroy(ss_server_cmds, NULL, NULL);
49 }
50
51 static void ss_cmd_server(sourceinfo_t * si, int parc, char *parv[])
52 {
53 command_t *c;
54 char *cmd = parv[0];
55
56 if (!cmd)
57 {
58 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SERVER");
59 command_fail(si, fault_needmoreparams,
60 _("Syntax: SERVER [INFO|LIST|COUNT] [parameters]"));
61 return;
62 }
63
64 c = command_find(ss_server_cmds, cmd);
65 if (c == NULL)
66 {
67 command_fail(si, fault_badparams,
68 _("Invalid command. Use \2/%s%s help\2 for a command listing."),
69 (ircd->uses_rcommand == false) ? "msg " : "", si->service->disp);
70 return;
71 }
72
73 command_exec(si->service, si, c, parc - 1, parv + 1);
74 }
75
76 static void ss_cmd_server_list(sourceinfo_t * si, int parc, char *parv[])
77 {
78 server_t *s;
79 int i = 0;
80 mowgli_patricia_iteration_state_t state;
81 MOWGLI_PATRICIA_FOREACH(s, &state, servlist)
82 {
83 if ((!(s->flags & SF_HIDE)) || (has_priv(si, PRIV_SERVER_AUSPEX)))
84 {
85 i++;
86 command_success_nodata(si, _("%d: %s [%s]"), i, s->name, s->desc);
87 }
88 }
89 command_success_nodata(si, _("End of server list."));
90 }
91
92 static void ss_cmd_server_info(sourceinfo_t * si, int parc, char *parv[])
93 {
94 server_t *s;
95 char *name = parv[0];
96
97 if (!name)
98 {
99 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SERVER INFO");
100 command_fail(si, fault_needmoreparams, _("Syntax: SERVER INFO <server>"));
101 return;
102 }
103
104 if (!(s = mowgli_patricia_retrieve(servlist, name)))
105 {
106 command_fail(si, fault_nosuch_target, _("Server \2%s\2 does not exist."), name);
107 return;
108 }
109
110 if ((s->flags & SF_HIDE) && (!(has_priv(si, PRIV_SERVER_AUSPEX))))
111 {
112 command_fail(si, fault_nosuch_target, _("Server \2%s\2 does not exist."), name);
113 return;
114 }
115
116 command_success_nodata(si, _("Information for server %s:"), s->name);
117 command_success_nodata(si, _("Server description: %s"), s->desc);
118 command_success_nodata(si, _("Current users: %u (%u invisible)"), s->users, s->invis);
119 command_success_nodata(si, _("Online operators: %u"), s->opers);
120 if (has_priv(si, PRIV_SERVER_AUSPEX))
121 {
122 if (s->uplink != NULL && s->uplink->name != NULL)
123 command_success_nodata(si, _("Server uplink: %s"), s->uplink->name);
124 command_success_nodata(si, _("Servers linked from %s: %u"), name, (unsigned int)s->children.count);
125 }
126 command_success_nodata(si, _("End of server info."));
127 }
128
129
130 static void ss_cmd_server_count(sourceinfo_t * si, int parc, char *parv[])
131 {
132 command_success_nodata(si, _("Network size: %u servers"), mowgli_patricia_size(servlist));
133 }