Anope IRC Services  Version 1.8
ms_del.c
Go to the documentation of this file.
1 /* MemoServ core functions
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 /*************************************************************************/
14 
15 #include "module.h"
16 
17 static int do_del(User * u);
18 static int del_memo_callback(User * u, int num, va_list args);
19 static void myMemoServHelp(User * u);
20 
27 int AnopeInit(int argc, char **argv)
28 {
29  Command *c;
30 
31  moduleAddAuthor("Anope");
32  moduleAddVersion(VERSION_STRING);
34  c = createCommand("DEL", do_del, NULL, MEMO_HELP_DEL, -1, -1, -1, -1);
37 
38  return MOD_CONT;
39 }
40 
44 void AnopeFini(void)
45 {
46 
47 }
48 
49 
50 
55 static void myMemoServHelp(User * u)
56 {
57  notice_lang(s_MemoServ, u, MEMO_HELP_CMD_DEL);
58 }
59 
65 static int do_del(User * u)
66 {
67  MemoInfo *mi;
68  ChannelInfo *ci;
69  char *numstr = strtok(NULL, ""), *chan = NULL;
70  int last, last0, i;
71  char buf[BUFSIZE], *end;
72  int delcount, count, left;
73 
74  if (numstr && *numstr == '#') {
75  chan = strtok(numstr, " ");
76  numstr = strtok(NULL, "");
77  if (!(ci = cs_findchan(chan))) {
78  notice_lang(s_MemoServ, u, CHAN_X_NOT_REGISTERED, chan);
79  return MOD_CONT;
80  } else if (readonly) {
81  notice_lang(s_MemoServ, u, READ_ONLY_MODE);
82  return MOD_CONT;
83  } else if (ci->flags & CI_VERBOTEN) {
84  notice_lang(s_MemoServ, u, CHAN_X_FORBIDDEN, chan);
85  return MOD_CONT;
86  } else if (!check_access(u, ci, CA_MEMO)) {
87  notice_lang(s_MemoServ, u, ACCESS_DENIED);
88  return MOD_CONT;
89  }
90  mi = &ci->memos;
91  } else {
92  if (!nick_identified(u)) {
93  notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
94  return MOD_CONT;
95  }
96  mi = &u->na->nc->memos;
97  }
98  if (!numstr
99  || (!isdigit(*numstr) && stricmp(numstr, "ALL") != 0
100  && stricmp(numstr, "LAST") != 0)) {
101  syntax_error(s_MemoServ, u, "DEL", MEMO_DEL_SYNTAX);
102  } else if (mi->memocount == 0) {
103  if (chan)
104  notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_MEMOS, chan);
105  else
106  notice_lang(s_MemoServ, u, MEMO_HAVE_NO_MEMOS);
107  } else {
108  if (isdigit(*numstr)) {
109  /* Delete a specific memo or memos. */
110  last = -1; /* Last memo deleted */
111  last0 = -1; /* Beginning of range of last memos deleted */
112  end = buf;
113  left = sizeof(buf);
114  delcount =
115  process_numlist(numstr, &count, del_memo_callback, u, mi,
116  &last, &last0, &end, &left);
117  if (last != -1) {
118  /* Some memos got deleted; tell them which ones. */
119  if (delcount > 1) {
120  if (last0 != last)
121  end += snprintf(end, sizeof(buf) - (end - buf),
122  ",%d-%d", last0, last);
123  else
124  end += snprintf(end, sizeof(buf) - (end - buf),
125  ",%d", last);
126  /* "buf+1" here because *buf == ',' */
127  notice_lang(s_MemoServ, u, MEMO_DELETED_SEVERAL,
128  buf + 1);
129  } else {
130  notice_lang(s_MemoServ, u, MEMO_DELETED_ONE, last);
131  }
132  } else {
133  /* No memos were deleted. Tell them so. */
134  if (count == 1)
135  notice_lang(s_MemoServ, u, MEMO_DOES_NOT_EXIST,
136  atoi(numstr));
137  else
138  notice_lang(s_MemoServ, u, MEMO_DELETED_NONE);
139  }
140  } else if (stricmp(numstr, "LAST") == 0) {
141  /* Delete last memo. */
142  for (i = 0; i < mi->memocount; i++)
143  last = mi->memos[i].number;
144  delmemo(mi, last);
145  notice_lang(s_MemoServ, u, MEMO_DELETED_ONE, last);
146  } else {
147  /* Delete all memos. */
148  for (i = 0; i < mi->memocount; i++) {
149  free(mi->memos[i].text);
151  }
152  free(mi->memos);
153  mi->memos = NULL;
154  mi->memocount = 0;
155  if (chan)
156  notice_lang(s_MemoServ, u, MEMO_CHAN_DELETED_ALL, chan);
157  else
158  notice_lang(s_MemoServ, u, MEMO_DELETED_ALL);
159  }
160 
161  /* Reset the order */
162  for (i = 0; i < mi->memocount; i++)
163  mi->memos[i].number = i + 1;
164  }
165  return MOD_CONT;
166 }
167 
175 static int del_memo_callback(User * u, int num, va_list args)
176 {
177  MemoInfo *mi = va_arg(args, MemoInfo *);
178  int *last = va_arg(args, int *);
179  int *last0 = va_arg(args, int *);
180  char **end = va_arg(args, char **);
181  int *left = va_arg(args, int *);
182 
183  if (delmemo(mi, num)) {
184  if (num != (*last) + 1) {
185  if (*last != -1) {
186  int len;
187  if (*last0 != *last)
188  len = snprintf(*end, *left, ",%d-%d", *last0, *last);
189  else
190  len = snprintf(*end, *left, ",%d", *last);
191  *end += len;
192  *left -= len;
193  }
194  *last0 = num;
195  }
196  *last = num;
197  return 1;
198  } else {
199  return 0;
200  }
201 }
E int process_numlist(const char *numstr, int *count_ret, range_callback_t callback, User *u,...)
Definition: misc.c:292
E int readonly
Definition: extern.h:776
E int snprintf(char *buf, size_t size, const char *fmt,...)
Definition: compat.c:37
static int do_del(User *u)
Definition: ms_del.c:65
E int nick_identified(User *u)
Definition: nickserv.c:1111
E int check_access(User *user, ChannelInfo *ci, int what)
Definition: chanserv.c:1974
void AnopeFini(void)
Definition: ms_del.c:44
E int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
E void moduleCleanStruct(ModuleData **moduleData)
Definition: modules.c:2439
#define CI_VERBOTEN
Definition: services.h:725
MemoInfo memos
Definition: services.h:552
int16 memocount
Definition: services.h:505
MDE void moduleAddAuthor(const char *author)
Definition: modules.c:1772
E void syntax_error(char *service, User *u, const char *command, int msgnum)
Definition: language.c:295
E void notice_lang(char *source, User *dest, int message,...)
Definition: send.c:169
MDE void moduleSetType(MODType type)
Definition: modules.c:818
NickCore * nc
Definition: services.h:533
MDE void moduleSetMemoHelp(void(*func)(User *u))
Definition: modules.c:2102
E char * s_MemoServ
Definition: extern.h:286
uint32 flags
Definition: services.h:669
ModuleData * moduleData
Definition: services.h:496
static void myMemoServHelp(User *u)
Definition: ms_del.c:55
char * text
Definition: services.h:495
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
Command * c
Definition: ns_recover.c:17
uint32 number
Definition: services.h:491
#define MOD_CONT
Definition: modules.h:54
#define MEMOSERV
Definition: modules.h:58
E int delmemo(MemoInfo *mi, int num)
Definition: memoserv.c:364
Definition: modules.h:99
MDE Command * createCommand(const char *name, int(*func)(User *u), int(*has_priv)(User *u), int help_all, int help_reg, int help_oper, int help_admin, int help_root)
Definition: modules.c:987
#define CA_MEMO
Definition: services.h:761
MemoInfo memos
Definition: services.h:690
E ChannelInfo * cs_findchan(const char *chan)
Definition: db-merger.c:2000
Memo * memos
Definition: services.h:506
E char * s_NickServ
Definition: extern.h:284
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
int AnopeInit(int argc, char **argv)
Definition: ms_del.c:27
#define BUFSIZE
Definition: config.h:47
NickAlias * na
Definition: services.h:892
#define MOD_UNIQUE
Definition: module.h:11
static int del_memo_callback(User *u, int num, va_list args)
Definition: ms_del.c:175