Anope IRC Services  Version 1.8
ns_maxemail.c
Go to the documentation of this file.
1 /* ns_maxemail.c - Limit the amount of times an email address
2  * can be used for a NickServ account.
3  *
4  * (C) 2003-2014 Anope Team
5  * Contact us at team@anope.org
6  *
7  * Included in the Anope module pack since Anope 1.7.9
8  * Anope Coder: GeniusDex <geniusdex@anope.org>
9  *
10  * Please read COPYING and README for further details.
11  *
12  * Send any bug reports to the Anope Coder, as he will be able
13  * to deal with it best.
14  */
15 
16 #include "module.h"
17 
18 #define AUTHOR "Anope"
19 #define VERSION VERSION_STRING
20 
21 static void my_load_config(void);
22 static void my_add_languages(void);
23 static int my_ns_register(User * u);
24 static int my_ns_set(User * u);
25 static int my_event_reload(int argc, char **argv);
26 static int my_event_addcommand(int argc, char **argv);
27 static int my_event_delcommand(int argc, char **argv);
28 
29 static int NSEmailMax = 0;
30 static int added_register = 0;
31 
32 #define LNG_NUM_STRINGS 2
33 #define LNG_NSEMAILMAX_REACHED 0
34 #define LNG_NSEMAILMAX_REACHED_ONE 1
35 
36 int AnopeInit(int argc, char **argv)
37 {
38  Command *c;
39  EvtHook *evt;
40  int status;
41 
45 
46  /* Only add the command if REGISTER is actually available.
47  * If it s not available, hooking to it will suppress anopes
48  * "Unknown Command" response.. ~ Viper */
49  if (findCommand(NICKSERV, "REGISTER")) {
50  c = createCommand("REGISTER", my_ns_register, NULL, -1, -1, -1, -1,
51  -1);
52  if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
53  alog("[ns_maxemail] Unable to create REGISTER command: %d",
54  status);
55  return MOD_STOP;
56  }
57  added_register = 1;
58  }
59 
60  c = createCommand("SET", my_ns_set, NULL, -1, -1, -1, -1, -1);
61  if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
62  alog("[ns_maxemail] Unable to create SET command: %d", status);
63  return MOD_STOP;
64  }
65 
67  if ((status = moduleAddEventHook(evt))) {
68  alog("[ns_maxemail] Unable to hook to EVENT_RELOAD: %d", status);
69  return MOD_STOP;
70  }
71 
72  /* If the REGISTER command is added after initial load, provide hooks.. */
74  if ((status = moduleAddEventHook(evt))) {
75  alog("[ns_maxemail] Unable to hook to EVENT_ADDCOMMAND: %d", status);
76  return MOD_STOP;
77  }
78 
79  /* If the REGISTER command is deleted after initial load, remove hooks.. */
81  if ((status = moduleAddEventHook(evt))) {
82  alog("[ns_maxemail] Unable to hook to EVENT_DELCOMMAND: %d", status);
83  return MOD_STOP;
84  }
85 
88 
89  return MOD_CONT;
90 }
91 
92 void AnopeFini(void)
93 {
94  /* Nothing to do while unloading */
95 }
96 
97 static int count_email_in_use(char *email, User * u)
98 {
99  NickCore *nc;
100  int i;
101  int count = 0;
102 
103  if (!email)
104  return 0;
105 
106  for (i = 0; i < 1024; i++) {
107  for (nc = nclists[i]; nc; nc = nc->next) {
108  if (!(u->na && u->na->nc && (u->na->nc == nc)) && nc->email && (stricmp(nc->email, email) == 0))
109  count++;
110  }
111  }
112 
113  return count;
114 }
115 
116 static int check_email_limit_reached(char *email, User * u)
117 {
118  if ((NSEmailMax < 1) || !email || is_services_admin(u))
119  return MOD_CONT;
120 
121  if (count_email_in_use(email, u) < NSEmailMax)
122  return MOD_CONT;
123 
124  if (NSEmailMax == 1)
126  else
128  NSEmailMax);
129 
130  return MOD_STOP;
131 }
132 
133 static int my_ns_register(User * u)
134 {
135  char *cur_buffer;
136  char *email;
137  int ret;
138 
139  cur_buffer = moduleGetLastBuffer();
140  email = myStrGetToken(cur_buffer, ' ', 1);
141  if (!email)
142  return MOD_CONT;
143 
144  ret = check_email_limit_reached(email, u);
145  free(email);
146 
147  return ret;
148 }
149 
150 static int my_ns_set(User * u)
151 {
152  char *cur_buffer;
153  char *set;
154  char *email;
155  int ret;
156 
157  cur_buffer = moduleGetLastBuffer();
158  set = myStrGetToken(cur_buffer, ' ', 0);
159 
160  if (!set)
161  return MOD_CONT;
162 
163  if (stricmp(set, "email") != 0) {
164  free(set);
165  return MOD_CONT;
166  }
167 
168  free(set);
169  email = myStrGetToken(cur_buffer, ' ', 1);
170  if (!email)
171  return MOD_CONT;
172 
173  ret = check_email_limit_reached(email, u);
174  free(email);
175 
176  return ret;
177 }
178 
179 static int my_event_reload(int argc, char **argv)
180 {
181  if ((argc > 0) && (stricmp(argv[0], EVENT_START) == 0))
182  my_load_config();
183 
184  return MOD_CONT;
185 }
186 
187 static int my_event_addcommand(int argc, char **argv)
188 {
189  Command *c;
190  int status;
191 
192  if (argc == 2 && stricmp(argv[0], "ns_maxemail")
193  && !stricmp(argv[1], "REGISTER") && !added_register) {
194  c = createCommand("REGISTER", my_ns_register, NULL, -1, -1, -1, -1,
195  -1);
196  if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
197  alog("[ns_maxemail] Unable to create REGISTER command: %d",
198  status);
199  return MOD_CONT;
200  }
201  added_register = 1;
202  }
203 
204  return MOD_CONT;
205 }
206 
207 static int my_event_delcommand(int argc, char **argv)
208 {
209  if (argc == 2 && stricmp(argv[0], "ns_maxemail")
210  && !stricmp(argv[1], "REGISTER") && added_register) {
211  moduleDelCommand(NICKSERV, "REGISTER");
212  added_register = 0;
213  }
214 
215  return MOD_CONT;
216 }
217 
218 static void my_load_config(void)
219 {
220  Directive confvalues[] = {
221  {"NSEmailMax", {{PARAM_INT, PARAM_RELOAD, &NSEmailMax}}}
222  };
223 
224  moduleGetConfigDirective(confvalues);
225 
226  if (debug)
227  alog("debug: [ns_maxemail] NSEmailMax set to %d", NSEmailMax);
228 }
229 
230 static void my_add_languages(void)
231 {
232  char *langtable_en_us[] = {
233  /* LNG_NSEMAILMAX_REACHED */
234  "The given email address has reached its usage limit of %d users.",
235  /* LNG_NSEMAILMAX_REACHED_ONE */
236  "The given email address has reached its usage limit of 1 user."
237  };
238 
239  char *langtable_nl[] = {
240  /* LNG_NSEMAILMAX_REACHED */
241  "Het gegeven email adres heeft de limiet van %d gebruikers bereikt.",
242  /* LNG_NSEMAILMAX_REACHED_ONE */
243  "Het gegeven email adres heeft de limiet van 1 gebruiker bereikt."
244  };
245 
246  char *langtable_de[] = {
247  /* LNG_NSEMAILMAX_REACHED */
248  "Die angegebene eMail hat die limit Begrenzung von %d User erreicht.",
249  /* LNG_NSEMAILMAX_REACHED_ONE */
250  "Die angegebene eMail hat die limit Begrenzung von 1 User erreicht."
251  };
252 
253  char *langtable_pt[] = {
254  /* LNG_NSEMAILMAX_REACHED */
255  "O endereo de email fornecido alcanou seu limite de uso de %d usurios.",
256  /* LNG_NSEMAILMAX_REACHED_ONE */
257  "O endereo de email fornecido alcanou seu limite de uso de 1 usurio."
258  };
259 
260  char *langtable_ru[] = {
261  /* LNG_NSEMAILMAX_REACHED */
262  " email- - : %d",
263  /* LNG_NSEMAILMAX_REACHED_ONE */
264  " email- - ."
265  };
266 
267  char *langtable_it[] = {
268  /* LNG_NSEMAILMAX_REACHED */
269  "L'indirizzo email specificato ha raggiunto il suo limite d'utilizzo di %d utenti.",
270  /* LNG_NSEMAILMAX_REACHED_ONE */
271  "L'indirizzo email specificato ha raggiunto il suo limite d'utilizzo di 1 utente."
272  };
273 
274  char *langtable_fr[] = {
275  /* LNG_NSEMAILMAX_REACHED */
276  "L'adresse e-mail indique a atteint la limite fixe %d utilisateurs.",
277  /* LNG_NSEMAILMAX_REACHED_ONE */
278  "L'adresse e-mail indique a atteint la limite fixe 1 utilisateur."
279  };
280 
288 }
289 
290 /* EOF */
#define LANG_DE
Definition: services.h:1324
static int added_register
Definition: ns_maxemail.c:30
static void my_load_config(void)
Definition: ns_maxemail.c:218
MDE void moduleNoticeLang(char *source, User *u, int number,...)
Definition: modules.c:2680
static int my_event_reload(int argc, char **argv)
Definition: ns_maxemail.c:179
#define LANG_RU
Definition: services.h:1328
#define AUTHOR
Definition: ns_maxemail.c:18
E int stricmp(const char *s1, const char *s2)
Definition: compat.c:58
#define LANG_NL
Definition: services.h:1327
#define EVENT_RELOAD
Definition: events.h:40
#define LNG_NUM_STRINGS
Definition: ns_maxemail.c:32
void AnopeFini(void)
Definition: ns_maxemail.c:92
#define LNG_NSEMAILMAX_REACHED
Definition: ns_maxemail.c:33
#define LNG_NSEMAILMAX_REACHED_ONE
Definition: ns_maxemail.c:34
#define PARAM_RELOAD
Definition: services.h:454
MDE void moduleAddAuthor(const char *author)
Definition: modules.c:1772
MDE void moduleSetType(MODType type)
Definition: modules.c:818
MDE char * moduleGetLastBuffer(void)
Definition: modules.c:1988
NickCore * nc
Definition: services.h:533
static int my_event_delcommand(int argc, char **argv)
Definition: ns_maxemail.c:207
static int my_ns_set(User *u)
Definition: ns_maxemail.c:150
static int check_email_limit_reached(char *email, User *u)
Definition: ns_maxemail.c:116
#define MOD_STOP
Definition: modules.h:53
#define LANG_FR
Definition: services.h:1321
NickCore * next
Definition: services.h:540
#define EVENT_DELCOMMAND
Definition: events.h:72
MDE void moduleAddVersion(const char *version)
Definition: modules.c:1760
MDE EvtHook * createEventHook(char *name, int(*func)(int argc, char **argv))
Definition: events.c:305
Command * c
Definition: ns_recover.c:17
E void alog(const char *fmt,...) FORMAT(printf
#define EVENT_ADDCOMMAND
Definition: events.h:71
#define MOD_CONT
Definition: modules.h:54
#define EVENT_START
Definition: events.h:14
#define LANG_IT
Definition: services.h:1323
E int debug
Definition: extern.h:775
E int is_services_admin(User *u)
Definition: operserv.c:591
MDE void moduleInsertLanguage(int langNumber, int ac, char **av)
Definition: modules.c:2650
static int NSEmailMax
Definition: ns_maxemail.c:29
MDE Command * findCommand(CommandHash *cmdTable[], const char *name)
Definition: modules.c:1442
static int count_email_in_use(char *email, User *u)
Definition: ns_maxemail.c:97
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 VERSION
Definition: ns_maxemail.c:19
E char * myStrGetToken(const char *str, const char dilim, int token_number)
Definition: misc.c:654
static int my_event_addcommand(int argc, char **argv)
Definition: ns_maxemail.c:187
#define LANG_EN_US
Definition: services.h:1315
MDE int moduleGetConfigDirective(Directive *h)
Definition: modules.c:2553
E NickCore * nclists[1024]
Definition: extern.h:924
static int my_ns_register(User *u)
Definition: ns_maxemail.c:133
int AnopeInit(int argc, char **argv)
Definition: ns_maxemail.c:36
static void my_add_languages(void)
Definition: ns_maxemail.c:230
E char * s_NickServ
Definition: extern.h:284
MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos)
Definition: modules.c:1082
#define MOD_HEAD
Definition: module.h:12
MDE int moduleDelCommand(CommandHash *cmdTable[], char *name)
Definition: modules.c:1161
#define LANG_PT
Definition: services.h:1320
MDE int moduleAddEventHook(EvtHook *evh)
Definition: events.c:528
#define NICKSERV
Definition: modules.h:59
NickAlias * na
Definition: services.h:892
char * email
Definition: services.h:544
#define PARAM_INT
Definition: services.h:440