Anope IRC Services  Version 1.8
ms_list.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 static int do_list(User * u);
17 static int list_memo_callback(User * u, int num, va_list args);
18 static int list_memo(User * u, int index, MemoInfo * mi, int *sent_header,
19  int new, const char *chan);
20 static void myMemoServHelp(User * u);
21 
28 int AnopeInit(int argc, char **argv)
29 {
30  Command *c;
31 
32  moduleAddAuthor("Anope");
33  moduleAddVersion(VERSION_STRING);
35  c = createCommand("LIST", do_list, NULL, MEMO_HELP_LIST, -1, -1, -1,
36  -1);
39 
40  return MOD_CONT;
41 }
42 
46 void AnopeFini(void)
47 {
48 
49 }
50 
51 
52 
57 static void myMemoServHelp(User * u)
58 {
59  notice_lang(s_MemoServ, u, MEMO_HELP_CMD_LIST);
60 }
61 
67 static int do_list(User * u)
68 {
69  char *param = strtok(NULL, " "), *chan = NULL;
70  ChannelInfo *ci;
71  MemoInfo *mi;
72  Memo *m;
73  int i;
74 
75  if (param && *param == '#') {
76  chan = param;
77  param = strtok(NULL, " ");
78  if (!(ci = cs_findchan(chan))) {
79  notice_lang(s_MemoServ, u, CHAN_X_NOT_REGISTERED, chan);
80  return MOD_CONT;
81  } else if (ci->flags & CI_VERBOTEN) {
82  notice_lang(s_MemoServ, u, CHAN_X_FORBIDDEN, chan);
83  return MOD_CONT;
84  } else if (!check_access(u, ci, CA_MEMO)) {
85  notice_lang(s_MemoServ, u, ACCESS_DENIED);
86  return MOD_CONT;
87  }
88  mi = &ci->memos;
89  } else {
90  if (!nick_identified(u)) {
91  notice_lang(s_MemoServ, u, NICK_IDENTIFY_REQUIRED, s_NickServ);
92  return MOD_CONT;
93  }
94  mi = &u->na->nc->memos;
95  }
96  if (param && !isdigit(*param) && stricmp(param, "NEW") != 0) {
97  syntax_error(s_MemoServ, u, "LIST", MEMO_LIST_SYNTAX);
98  } else if (mi->memocount == 0) {
99  if (chan)
100  notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_MEMOS, chan);
101  else
102  notice_lang(s_MemoServ, u, MEMO_HAVE_NO_MEMOS);
103  } else {
104  int sent_header = 0;
105  if (param && isdigit(*param)) {
106  process_numlist(param, NULL, list_memo_callback, u,
107  mi, &sent_header, chan);
108  } else {
109  if (param) {
110  for (i = 0, m = mi->memos; i < mi->memocount; i++, m++) {
111  if (m->flags & MF_UNREAD)
112  break;
113  }
114  if (i == mi->memocount) {
115  if (chan)
116  notice_lang(s_MemoServ, u, MEMO_X_HAS_NO_NEW_MEMOS,
117  chan);
118  else
119  notice_lang(s_MemoServ, u, MEMO_HAVE_NO_NEW_MEMOS);
120  return MOD_CONT;
121  }
122  }
123  for (i = 0, m = mi->memos; i < mi->memocount; i++, m++) {
124  if (param && !(m->flags & MF_UNREAD))
125  continue;
126  list_memo(u, i, mi, &sent_header, param != NULL, chan);
127  }
128  }
129  }
130  return MOD_CONT;
131 }
132 
140 static int list_memo_callback(User * u, int num, va_list args)
141 {
142  MemoInfo *mi = va_arg(args, MemoInfo *);
143  int *sent_header = va_arg(args, int *);
144  const char *chan = va_arg(args, const char *);
145  int i;
146 
147  for (i = 0; i < mi->memocount; i++) {
148  if (mi->memos[i].number == num)
149  break;
150  }
151  /* Range checking done by list_memo() */
152  return list_memo(u, i, mi, sent_header, 0, chan);
153 }
154 
165 static int list_memo(User * u, int index, MemoInfo * mi, int *sent_header,
166  int new, const char *chan)
167 {
168  Memo *m;
169  char timebuf[64];
170  struct tm tm;
171 
172  if (index < 0 || index >= mi->memocount)
173  return 0;
174  if (!*sent_header) {
175  if (chan) {
177  new ? MEMO_LIST_CHAN_NEW_MEMOS :
178  MEMO_LIST_CHAN_MEMOS, chan, s_MemoServ, chan);
179  } else {
181  new ? MEMO_LIST_NEW_MEMOS : MEMO_LIST_MEMOS,
182  u->nick, s_MemoServ);
183  }
184  notice_lang(s_MemoServ, u, MEMO_LIST_HEADER);
185  *sent_header = 1;
186  }
187  m = &mi->memos[index];
188  tm = *localtime(&m->time);
189  strftime_lang(timebuf, sizeof(timebuf),
190  u, STRFTIME_DATE_TIME_FORMAT, &tm);
191  timebuf[sizeof(timebuf) - 1] = 0; /* just in case */
192  notice_lang(s_MemoServ, u, MEMO_LIST_FORMAT,
193  (m->flags & MF_UNREAD) ? '*' : ' ',
194  m->number, m->sender, timebuf);
195  return 1;
196 }
E int process_numlist(const char *numstr, int *count_ret, range_callback_t callback, User *u,...)
Definition: misc.c:292
char sender[NICKMAX]
Definition: services.h:494
char nick[NICKMAX]
Definition: services.h:875
static int do_list(User *u)
Definition: ms_list.c:67
E int nick_identified(User *u)
Definition: nickserv.c:1111
E int check_access(User *user, ChannelInfo *ci, int what)
Definition: chanserv.c:1974
E int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
#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
static void myMemoServHelp(User *u)
Definition: ms_list.c:57
void AnopeFini(void)
Definition: ms_list.c:46
NickCore * nc
Definition: services.h:533
uint16 flags
Definition: services.h:492
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
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
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
static int list_memo_callback(User *u, int num, va_list args)
Definition: ms_list.c:140
E ChannelInfo * cs_findchan(const char *chan)
Definition: db-merger.c:2000
static int list_memo(User *u, int index, MemoInfo *mi, int *sent_header, int new, const char *chan)
Definition: ms_list.c:165
time_t time
Definition: services.h:493
int AnopeInit(int argc, char **argv)
Definition: ms_list.c:28
Memo * memos
Definition: services.h:506
E int strftime_lang(char *buf, int size, User *u, int format, struct tm *tm)
Definition: language.c:240
E char * s_NickServ
Definition: extern.h:284
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
#define MF_UNREAD
Definition: services.h:1268
NickAlias * na
Definition: services.h:892
#define MOD_UNIQUE
Definition: module.h:11