summaryrefslogtreecommitdiff
path: root/src/modules/ns_maxemail.c
blob: 6629e32c630ff0887d4dc28ed11c7296ce08bbc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* ns_maxemail.c - Limit the amount of times an email address
 *                 can be used for a NickServ account.
 * 
 * (C) 2003-2013 Anope Team
 * Contact us at team@anope.org
 * 
 * Included in the Anope module pack since Anope 1.7.9
 * Anope Coder: GeniusDex <geniusdex@anope.org>
 * 
 * Please read COPYING and README for further details.
 *
 * Send any bug reports to the Anope Coder, as he will be able
 * to deal with it best.
 */

#include "module.h"

#define AUTHOR "Anope"
#define VERSION VERSION_STRING

static void my_load_config(void);
static void my_add_languages(void);
static int my_ns_register(User * u);
static int my_ns_set(User * u);
static int my_event_reload(int argc, char **argv);
static int my_event_addcommand(int argc, char **argv);
static int my_event_delcommand(int argc, char **argv);

static int NSEmailMax = 0;
static int added_register = 0;

#define LNG_NUM_STRINGS		2
#define LNG_NSEMAILMAX_REACHED		0
#define LNG_NSEMAILMAX_REACHED_ONE	1

int AnopeInit(int argc, char **argv)
{
    Command *c;
    EvtHook *evt;
    int status;

    moduleAddAuthor(AUTHOR);
    moduleAddVersion(VERSION);
    moduleSetType(SUPPORTED);

    /* Only add the command if REGISTER is actually available.
     * If it s not available, hooking to it will suppress anopes
     * "Unknown Command" response.. ~ Viper */
    if (findCommand(NICKSERV, "REGISTER")) {
        c = createCommand("REGISTER", my_ns_register, NULL, -1, -1, -1, -1,
                          -1);
        if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
            alog("[ns_maxemail] Unable to create REGISTER command: %d",
                 status);
            return MOD_STOP;
        }
        added_register = 1;
    }

    c = createCommand("SET", my_ns_set, NULL, -1, -1, -1, -1, -1);
    if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
        alog("[ns_maxemail] Unable to create SET command: %d", status);
        return MOD_STOP;
    }

    evt = createEventHook(EVENT_RELOAD, my_event_reload);
    if ((status = moduleAddEventHook(evt))) {
        alog("[ns_maxemail] Unable to hook to EVENT_RELOAD: %d", status);
        return MOD_STOP;
    }

    /* If the REGISTER command is added after initial load, provide hooks.. */
    evt = createEventHook(EVENT_ADDCOMMAND, my_event_addcommand);
    if ((status = moduleAddEventHook(evt))) {
        alog("[ns_maxemail] Unable to hook to EVENT_ADDCOMMAND: %d", status);
        return MOD_STOP;
    }

    /* If the REGISTER command is deleted after initial load, remove hooks.. */
    evt = createEventHook(EVENT_DELCOMMAND, my_event_delcommand);
    if ((status = moduleAddEventHook(evt))) {
        alog("[ns_maxemail] Unable to hook to EVENT_DELCOMMAND: %d", status);
        return MOD_STOP;
    }

    my_load_config();
    my_add_languages();

    return MOD_CONT;
}

void AnopeFini(void)
{
    /* Nothing to do while unloading */
}

static int count_email_in_use(char *email, User * u)
{
    NickCore *nc;
    int i;
    int count = 0;

    if (!email)
        return 0;

    for (i = 0; i < 1024; i++) {
        for (nc = nclists[i]; nc; nc = nc->next) {
            if (!(u->na && u->na->nc && (u->na->nc == nc)) && nc->email && (stricmp(nc->email, email) == 0))
                count++;
        }
    }

    return count;
}

static int check_email_limit_reached(char *email, User * u)
{
    if ((NSEmailMax < 1) || !email || is_services_admin(u))
        return MOD_CONT;

    if (count_email_in_use(email, u) < NSEmailMax)
        return MOD_CONT;

    if (NSEmailMax == 1)
        moduleNoticeLang(s_NickServ, u, LNG_NSEMAILMAX_REACHED_ONE);
    else
        moduleNoticeLang(s_NickServ, u, LNG_NSEMAILMAX_REACHED,
                         NSEmailMax);

    return MOD_STOP;
}

static int my_ns_register(User * u)
{
    char *cur_buffer;
    char *email;
    int ret;

    cur_buffer = moduleGetLastBuffer();
    email = myStrGetToken(cur_buffer, ' ', 1);
    if (!email)
        return MOD_CONT;

    ret = check_email_limit_reached(email, u);
    free(email);

    return ret;
}

static int my_ns_set(User * u)
{
    char *cur_buffer;
    char *set;
    char *email;
    int ret;

    cur_buffer = moduleGetLastBuffer();
    set = myStrGetToken(cur_buffer, ' ', 0);

    if (!set)
        return MOD_CONT;

    if (stricmp(set, "email") != 0) {
        free(set);
        return MOD_CONT;
    }

    free(set);
    email = myStrGetToken(cur_buffer, ' ', 1);
    if (!email)
        return MOD_CONT;

    ret = check_email_limit_reached(email, u);
    free(email);

    return ret;
}

static int my_event_reload(int argc, char **argv)
{
    if ((argc > 0) && (stricmp(argv[0], EVENT_START) == 0))
        my_load_config();

    return MOD_CONT;
}

static int my_event_addcommand(int argc, char **argv)
{
    Command *c;
    int status;

    if (argc == 2 && stricmp(argv[0], "ns_maxemail")
            && !stricmp(argv[1], "REGISTER") && !added_register) {
        c = createCommand("REGISTER", my_ns_register, NULL, -1, -1, -1, -1,
                          -1);
        if ((status = moduleAddCommand(NICKSERV, c, MOD_HEAD))) {
            alog("[ns_maxemail] Unable to create REGISTER command: %d",
                 status);
            return MOD_CONT;
        }
        added_register = 1;
    }

    return MOD_CONT;
}

static int my_event_delcommand(int argc, char **argv)
{
    if (argc == 2 && stricmp(argv[0], "ns_maxemail")
            && !stricmp(argv[1], "REGISTER") && added_register) {
        moduleDelCommand(NICKSERV, "REGISTER");
        added_register = 0;
    }

    return MOD_CONT;
}

static void my_load_config(void)
{
    Directive confvalues[] = {
        {"NSEmailMax", {{PARAM_INT, PARAM_RELOAD, &NSEmailMax}}}
    };

    moduleGetConfigDirective(confvalues);

    if (debug)
        alog("debug: [ns_maxemail] NSEmailMax set to %d", NSEmailMax);
}

static void my_add_languages(void)
{
    char *langtable_en_us[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "The given email address has reached its usage limit of %d users.",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "The given email address has reached its usage limit of 1 user."
    };

    char *langtable_nl[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "Het gegeven email adres heeft de limiet van %d gebruikers bereikt.",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "Het gegeven email adres heeft de limiet van 1 gebruiker bereikt."
    };

   char *langtable_de[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "Die angegebene eMail hat die limit Begrenzung von %d User erreicht.",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "Die angegebene eMail hat die limit Begrenzung von 1 User erreicht."
   };

    char *langtable_pt[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "O endereço de email fornecido alcançou seu limite de uso de %d usuários.",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "O endereço de email fornecido alcançou seu limite de uso de 1 usuário."
    };

    char *langtable_ru[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "Óêàçàííûé âàìè email-àäðåñ èñïîëüçóåòñÿ ìàêñèìàëüíî äîïóñòèìîå êîë-âî ðàç: %d",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "Óêàçàííûé âàìè email-àäðåñ óæå êåì-òî èñïîëüçóåòñÿ."
    };

    char *langtable_it[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "L'indirizzo email specificato ha raggiunto il suo limite d'utilizzo di %d utenti.",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "L'indirizzo email specificato ha raggiunto il suo limite d'utilizzo di 1 utente."
    };

    char *langtable_fr[] = {
        /* LNG_NSEMAILMAX_REACHED */
        "L'adresse e-mail indiquée a atteint la limite fixée à %d utilisateurs.",
        /* LNG_NSEMAILMAX_REACHED_ONE */
        "L'adresse e-mail indiquée a atteint la limite fixée à 1 utilisateur."
    };

    moduleInsertLanguage(LANG_EN_US, LNG_NUM_STRINGS, langtable_en_us);
    moduleInsertLanguage(LANG_NL, LNG_NUM_STRINGS, langtable_nl);
    moduleInsertLanguage(LANG_DE, LNG_NUM_STRINGS, langtable_de);
    moduleInsertLanguage(LANG_PT, LNG_NUM_STRINGS, langtable_pt);
    moduleInsertLanguage(LANG_RU, LNG_NUM_STRINGS, langtable_ru);
    moduleInsertLanguage(LANG_IT, LNG_NUM_STRINGS, langtable_it);
    moduleInsertLanguage(LANG_FR, LNG_NUM_STRINGS, langtable_fr);
}

/* EOF */