Debian Codegraph @

modules/memoserv/delete.c

raw
1 /*
2 * Copyright (c) 2005 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * This file contains code for the Memoserv DELETE function
6 *
7 */
8
9 #include "atheme.h"
10
11 DECLARE_MODULE_V1
12 (
13 "memoserv/delete", false, _modinit, _moddeinit,
14 PACKAGE_STRING,
15 VENDOR_STRING
16 );
17
18 static void ms_cmd_delete(sourceinfo_t *si, int parc, char *parv[]);
19
20 command_t ms_delete = { "DELETE", N_("Deletes memos."),
21 AC_AUTHENTICATED, 1, ms_cmd_delete, { .path = "memoserv/delete" } };
22 command_t ms_del = { "DEL", N_("Alias for DELETE"),
23 AC_AUTHENTICATED, 1, ms_cmd_delete, { .path = "memoserv/delete" } };
24
25 void _modinit(module_t *m)
26 {
27 service_named_bind_command("memoserv", &ms_delete);
28 service_named_bind_command("memoserv", &ms_del);
29 }
30
31 void _moddeinit(module_unload_intent_t intent)
32 {
33 service_named_unbind_command("memoserv", &ms_delete);
34 service_named_unbind_command("memoserv", &ms_del);
35 }
36
37 static void ms_cmd_delete(sourceinfo_t *si, int parc, char *parv[])
38 {
39 /* Misc structs etc */
40 mowgli_node_t *n, *tn;
41 unsigned int i = 0, delcount = 0, memonum = 0;
42 unsigned int deleteall = 0, deleteold = 0;
43 mymemo_t *memo;
44 char *errptr = NULL;
45
46 /* We only take 1 arg, and we ignore all others */
47 char *arg1 = parv[0];
48
49 /* Does the arg exist? */
50 if (!arg1)
51 {
52 command_fail(si, fault_needmoreparams,
53 STR_INSUFFICIENT_PARAMS, "DELETE");
54
55 command_fail(si, fault_needmoreparams, _("Syntax: DELETE ALL|OLD|message id"));
56 return;
57 }
58
59 /* Do we have any memos? */
60 if (!si->smu->memos.count)
61 {
62 command_fail(si, fault_nochange, _("You have no memos to delete."));
63 return;
64 }
65
66 /* Do we want to delete all memos? */
67 if (!strcasecmp("all",arg1))
68 {
69 deleteall = 1;
70 }
71 else if (!strcasecmp("old",arg1))
72 {
73 deleteold = 1;
74 }
75 else
76 {
77 memonum = strtoul(arg1, &errptr, 10);
78
79 /* Make sure they didn't slip us an alphabetic index */
80 if (!memonum || (errptr && *errptr))
81 {
82 command_fail(si, fault_badparams, _("Invalid message index."));
83 return;
84 }
85
86 /* If int, does that index exist? And do we have something to delete? */
87 if (memonum > si->smu->memos.count)
88 {
89 command_fail(si, fault_nosuch_key, _("The specified memo doesn't exist."));
90 return;
91 }
92 }
93
94 delcount = 0;
95
96 /* Iterate through memos, doing deletion */
97 MOWGLI_ITER_FOREACH_SAFE(n, tn, si->smu->memos.head)
98 {
99 i++;
100 memo = (mymemo_t*) n->data;
101
102 if (i == memonum || deleteall ||
103 (deleteold && memo->status & MEMO_READ))
104 {
105 delcount++;
106
107 if (!(memo->status & MEMO_READ))
108 si->smu->memoct_new--;
109
110 /* Free to node pool, remove from chain */
111 mowgli_node_delete(n, &si->smu->memos);
112 mowgli_node_free(n);
113
114 free(memo);
115 }
116
117 }
118
119 command_success_nodata(si, ngettext(N_("%d memo deleted."), N_("%d memos deleted."), delcount), delcount);
120
121 return;
122 }
123
124 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
125 * vim:ts=8
126 * vim:sw=8
127 * vim:noexpandtab
128 */