Debian Codegraph @

modules/nickserv/set_property.c

raw
1 /*
2 * Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net>
3 * Copyright (c) 2007 Jilles Tjoelker
4 * Rights to this code are as documented in doc/LICENSE.
5 *
6 * Manipulates metadata entries associated with an account.
7 *
8 */
9
10 #include "atheme.h"
11 #include "uplink.h"
12
13 DECLARE_MODULE_V1
14 (
15 "nickserv/set_property", false, _modinit, _moddeinit,
16 PACKAGE_STRING,
17 VENDOR_STRING
18 );
19
20 mowgli_patricia_t **ns_set_cmdtree;
21
22 static void ns_cmd_set_property(sourceinfo_t *si, int parc, char *parv[]);
23
24 command_t ns_set_property = { "PROPERTY", N_("Manipulates metadata entries associated with an account."), AC_NONE, 2, ns_cmd_set_property, { .path = "nickserv/set_property" } };
25
26 void _modinit(module_t *m)
27 {
28 MODULE_TRY_REQUEST_SYMBOL(m, ns_set_cmdtree, "nickserv/set_core", "ns_set_cmdtree");
29
30 command_add(&ns_set_property, *ns_set_cmdtree);
31 }
32
33 void _moddeinit(module_unload_intent_t intent)
34 {
35 command_delete(&ns_set_property, *ns_set_cmdtree);
36 }
37
38 /* SET PROPERTY <property> [value] */
39 static void ns_cmd_set_property(sourceinfo_t *si, int parc, char *parv[])
40 {
41 char *property = strtok(parv[0], " ");
42 char *value = strtok(NULL, "");
43 unsigned int count;
44 mowgli_patricia_iteration_state_t state;
45 metadata_t *md;
46 hook_metadata_change_t mdchange;
47
48 if (!property)
49 {
50 command_fail(si, fault_needmoreparams, _("Syntax: SET PROPERTY <property> [value]"));
51 return;
52 }
53
54 if (strchr(property, ':') && !has_priv(si, PRIV_METADATA))
55 {
56 command_fail(si, fault_badparams, _("Invalid property name."));
57 return;
58 }
59
60 if (strchr(property, ':'))
61 logcommand(si, CMDLOG_SET, "SET:PROPERTY: \2%s\2: \2%s\2/\2%s\2", entity(si->smu)->name, property, value);
62
63 if (!value)
64 {
65 md = metadata_find(si->smu, property);
66
67 if (!md)
68 {
69 command_fail(si, fault_nosuch_target, _("Metadata entry \2%s\2 was not set."), property);
70 return;
71 }
72
73 mdchange.target = si->smu;
74 mdchange.name = md->name;
75 mdchange.value = md->value;
76 hook_call_metadata_change(&mdchange);
77
78 metadata_delete(si->smu, property);
79 logcommand(si, CMDLOG_SET, "SET:PROPERTY: \2%s\2 (deleted)", property);
80 command_success_nodata(si, _("Metadata entry \2%s\2 has been deleted."), property);
81 return;
82 }
83
84 count = 0;
85 MOWGLI_PATRICIA_FOREACH(md, &state, object(si->smu)->metadata)
86 {
87 if (strncmp(md->name, "private:", 8))
88 count++;
89 }
90 if (count >= me.mdlimit)
91 {
92 command_fail(si, fault_toomany, _("Cannot add \2%s\2 to \2%s\2 metadata table, it is full."),
93 property, entity(si->smu)->name);
94 return;
95 }
96
97 if (strlen(property) > 32 || strlen(value) > 300 || has_ctrl_chars(property))
98 {
99 command_fail(si, fault_badparams, _("Parameters are too long. Aborting."));
100 return;
101 }
102
103 md = metadata_add(si->smu, property, value);
104 if (md != NULL)
105 {
106 mdchange.target = si->smu;
107 mdchange.name = md->name;
108 mdchange.value = md->value;
109 hook_call_metadata_change(&mdchange);
110 }
111 logcommand(si, CMDLOG_SET, "SET:PROPERTY: \2%s\2 to \2%s\2", property, value);
112 command_success_nodata(si, _("Metadata entry \2%s\2 added."), property);
113 }
114
115 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
116 * vim:ts=8
117 * vim:sw=8
118 * vim:noexpandtab
119 */