ns_maxemail.c

Go to the documentation of this file.
00001 /* ns_maxemail.c - Limit the amount of times an email address
00002  *                 can be used for a NickServ account.
00003  * 
00004  * (C) 2003-2013 Anope Team
00005  * Contact us at team@anope.org
00006  * 
00007  * Included in the Anope module pack since Anope 1.7.9
00008  * Anope Coder: GeniusDex <geniusdex@anope.org>
00009  * 
00010  * Please read COPYING and README for further details.
00011  *
00012  * Send any bug reports to the Anope Coder, as he will be able
00013  * to deal with it best.
00014  */
00015 
00016 #include "module.h"
00017 
00018 #define AUTHOR "Anope"
00019 #define VERSION VERSION_STRING
00020 
00021 static void my_load_config(void);
00022 static void my_add_languages(void);
00023 static int my_ns_register(User * u);
00024 static int my_ns_set(User * u);
00025 static int my_event_reload(int argc, char **argv);
00026 static int my_event_addcommand(int argc, char **argv);
00027 static int my_event_delcommand(int argc, char **argv);
00028 
00029 static int NSEmailMax = 0;
00030 static int added_register = 0;
00031 
00032 #define LNG_NUM_STRINGS         2
00033 #define LNG_NSEMAILMAX_REACHED          0
00034 #define LNG_NSEMAILMAX_REACHED_ONE      1
00035 
00036 int AnopeInit(int argc, char **argv)
00037 {
00038     Command *c;
00039     EvtHook *evt;
00040     int status;
00041 
00042     moduleAddAuthor(AUTHOR);
00043     moduleAddVersion(VERSION);
00044     moduleSetType(SUPPORTED);
00045 
00046     /* Only add the command if REGISTER is actually available.
00047      * If it s not available, hooking to it will suppress anopes
00048      * "Unknown Command" response.. ~ Viper */
00049     if (findCommand(NICKSERV, "REGISTER")) {
00050         c = createCommand("REGISTER", my_ns_register, NULL, -1, -1, -1, -1,
00051                           -1);
00052         if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
00053             alog("[ns_maxemail] Unable to create REGISTER command: %d",
00054                  status);
00055             return MOD_STOP;
00056         }
00057         added_register = 1;
00058     }
00059 
00060     c = createCommand("SET", my_ns_set, NULL, -1, -1, -1, -1, -1);
00061     if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
00062         alog("[ns_maxemail] Unable to create SET command: %d", status);
00063         return MOD_STOP;
00064     }
00065 
00066     evt = createEventHook(EVENT_RELOAD, my_event_reload);
00067     if ((status = moduleAddEventHook(evt))) {
00068         alog("[ns_maxemail] Unable to hook to EVENT_RELOAD: %d", status);
00069         return MOD_STOP;
00070     }
00071 
00072     /* If the REGISTER command is added after initial load, provide hooks.. */
00073     evt = createEventHook(EVENT_ADDCOMMAND, my_event_addcommand);
00074     if ((status = moduleAddEventHook(evt))) {
00075         alog("[ns_maxemail] Unable to hook to EVENT_ADDCOMMAND: %d", status);
00076         return MOD_STOP;
00077     }
00078 
00079     /* If the REGISTER command is deleted after initial load, remove hooks.. */
00080     evt = createEventHook(EVENT_DELCOMMAND, my_event_delcommand);
00081     if ((status = moduleAddEventHook(evt))) {
00082         alog("[ns_maxemail] Unable to hook to EVENT_DELCOMMAND: %d", status);
00083         return MOD_STOP;
00084     }
00085 
00086     my_load_config();
00087     my_add_languages();
00088 
00089     return MOD_CONT;
00090 }
00091 
00092 void AnopeFini(void)
00093 {
00094     /* Nothing to do while unloading */
00095 }
00096 
00097 static int count_email_in_use(char *email, User * u)
00098 {
00099     NickCore *nc;
00100     int i;
00101     int count = 0;
00102 
00103     if (!email)
00104         return 0;
00105 
00106     for (i = 0; i < 1024; i++) {
00107         for (nc = nclists[i]; nc; nc = nc->next) {
00108             if (!(u->na && u->na->nc && (u->na->nc == nc)) && nc->email && (stricmp(nc->email, email) == 0))
00109                 count++;
00110         }
00111     }
00112 
00113     return count;
00114 }
00115 
00116 static int check_email_limit_reached(char *email, User * u)
00117 {
00118     if ((NSEmailMax < 1) || !email || is_services_admin(u))
00119         return MOD_CONT;
00120 
00121     if (count_email_in_use(email, u) < NSEmailMax)
00122         return MOD_CONT;
00123 
00124     if (NSEmailMax == 1)
00125         moduleNoticeLang(s_NickServ, u, LNG_NSEMAILMAX_REACHED_ONE);
00126     else
00127         moduleNoticeLang(s_NickServ, u, LNG_NSEMAILMAX_REACHED,
00128                          NSEmailMax);
00129 
00130     return MOD_STOP;
00131 }
00132 
00133 static int my_ns_register(User * u)
00134 {
00135     char *cur_buffer;
00136     char *email;
00137     int ret;
00138 
00139     cur_buffer = moduleGetLastBuffer();
00140     email = myStrGetToken(cur_buffer, ' ', 1);
00141     if (!email)
00142         return MOD_CONT;
00143 
00144     ret = check_email_limit_reached(email, u);
00145     free(email);
00146 
00147     return ret;
00148 }
00149 
00150 static int my_ns_set(User * u)
00151 {
00152     char *cur_buffer;
00153     char *set;
00154     char *email;
00155     int ret;
00156 
00157     cur_buffer = moduleGetLastBuffer();
00158     set = myStrGetToken(cur_buffer, ' ', 0);
00159 
00160     if (!set)
00161         return MOD_CONT;
00162 
00163     if (stricmp(set, "email") != 0) {
00164         free(set);
00165         return MOD_CONT;
00166     }
00167 
00168     free(set);
00169     email = myStrGetToken(cur_buffer, ' ', 1);
00170     if (!email)
00171         return MOD_CONT;
00172 
00173     ret = check_email_limit_reached(email, u);
00174     free(email);
00175 
00176     return ret;
00177 }
00178 
00179 static int my_event_reload(int argc, char **argv)
00180 {
00181     if ((argc > 0) && (stricmp(argv[0], EVENT_START) == 0))
00182         my_load_config();
00183 
00184     return MOD_CONT;
00185 }
00186 
00187 static int my_event_addcommand(int argc, char **argv)
00188 {
00189     Command *c;
00190     int status;
00191 
00192     if (argc == 2 && stricmp(argv[0], "ns_maxemail")
00193             && !stricmp(argv[1], "REGISTER") && !added_register) {
00194         c = createCommand("REGISTER", my_ns_register, NULL, -1, -1, -1, -1,
00195                           -1);
00196         if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
00197             alog("[ns_maxemail] Unable to create REGISTER command: %d",
00198                  status);
00199             return MOD_CONT;
00200         }
00201         added_register = 1;
00202     }
00203 
00204     return MOD_CONT;
00205 }
00206 
00207 static int my_event_delcommand(int argc, char **argv)
00208 {
00209     if (argc == 2 && stricmp(argv[0], "ns_maxemail")
00210             && !stricmp(argv[1], "REGISTER") && added_register) {
00211         moduleDelCommand(NICKSERV, "REGISTER");
00212         added_register = 0;
00213     }
00214 
00215     return MOD_CONT;
00216 }
00217 
00218 static void my_load_config(void)
00219 {
00220     Directive confvalues[] = {
00221         {"NSEmailMax", {{PARAM_INT, PARAM_RELOAD, &NSEmailMax}}}
00222     };
00223 
00224     moduleGetConfigDirective(confvalues);
00225 
00226     if (debug)
00227         alog("debug: [ns_maxemail] NSEmailMax set to %d", NSEmailMax);
00228 }
00229 
00230 static void my_add_languages(void)
00231 {
00232     char *langtable_en_us[] = {
00233         /* LNG_NSEMAILMAX_REACHED */
00234         "The given email address has reached its usage limit of %d users.",
00235         /* LNG_NSEMAILMAX_REACHED_ONE */
00236         "The given email address has reached its usage limit of 1 user."
00237     };
00238 
00239     char *langtable_nl[] = {
00240         /* LNG_NSEMAILMAX_REACHED */
00241         "Het gegeven email adres heeft de limiet van %d gebruikers bereikt.",
00242         /* LNG_NSEMAILMAX_REACHED_ONE */
00243         "Het gegeven email adres heeft de limiet van 1 gebruiker bereikt."
00244     };
00245 
00246    char *langtable_de[] = {
00247         /* LNG_NSEMAILMAX_REACHED */
00248         "Die angegebene eMail hat die limit Begrenzung von %d User erreicht.",
00249         /* LNG_NSEMAILMAX_REACHED_ONE */
00250         "Die angegebene eMail hat die limit Begrenzung von 1 User erreicht."
00251    };
00252 
00253     char *langtable_pt[] = {
00254         /* LNG_NSEMAILMAX_REACHED */
00255         "O endereço de email fornecido alcançou seu limite de uso de %d usuários.",
00256         /* LNG_NSEMAILMAX_REACHED_ONE */
00257         "O endereço de email fornecido alcançou seu limite de uso de 1 usuário."
00258     };
00259 
00260     char *langtable_ru[] = {
00261         /* LNG_NSEMAILMAX_REACHED */
00262         "Óêàçàííûé âàìè email-àäðåñ èñïîëüçóåòñÿ ìàêñèìàëüíî äîïóñòèìîå êîë-âî ðàç: %d",
00263         /* LNG_NSEMAILMAX_REACHED_ONE */
00264         "Óêàçàííûé âàìè email-àäðåñ óæå êåì-òî èñïîëüçóåòñÿ."
00265     };
00266 
00267     char *langtable_it[] = {
00268         /* LNG_NSEMAILMAX_REACHED */
00269         "L'indirizzo email specificato ha raggiunto il suo limite d'utilizzo di %d utenti.",
00270         /* LNG_NSEMAILMAX_REACHED_ONE */
00271         "L'indirizzo email specificato ha raggiunto il suo limite d'utilizzo di 1 utente."
00272     };
00273 
00274     char *langtable_fr[] = {
00275         /* LNG_NSEMAILMAX_REACHED */
00276         "L'adresse e-mail indiquée a atteint la limite fixée à %d utilisateurs.",
00277         /* LNG_NSEMAILMAX_REACHED_ONE */
00278         "L'adresse e-mail indiquée a atteint la limite fixée à 1 utilisateur."
00279     };
00280 
00281     moduleInsertLanguage(LANG_EN_US, LNG_NUM_STRINGS, langtable_en_us);
00282     moduleInsertLanguage(LANG_NL, LNG_NUM_STRINGS, langtable_nl);
00283     moduleInsertLanguage(LANG_DE, LNG_NUM_STRINGS, langtable_de);
00284     moduleInsertLanguage(LANG_PT, LNG_NUM_STRINGS, langtable_pt);
00285     moduleInsertLanguage(LANG_RU, LNG_NUM_STRINGS, langtable_ru);
00286     moduleInsertLanguage(LANG_IT, LNG_NUM_STRINGS, langtable_it);
00287     moduleInsertLanguage(LANG_FR, LNG_NUM_STRINGS, langtable_fr);
00288 }
00289 
00290 /* EOF */