Merge branch 'master' into SpamServ
[srvx.git] / src / spamserv.c
1 /* spamserv.c - anti spam service
2  * Copyright 2004 feigling
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.  Important limitations are
8  * listed in the COPYING file that accompanies this software.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, email srvx-maintainers@srvx.net.
17  *
18  * $Id: spamserv.c,v 1.08 2004/06/27 22:21:00 feigling Exp $
19  */
20
21 #include "conf.h"
22 #include "spamserv.h"
23 #include "chanserv.h"
24 #include "global.h"
25 #include "modcmd.h"
26 #include "saxdb.h"
27 #include "timeq.h"
28 #include "gline.h"
29
30 #define SPAMSERV_CONF_NAME           "services/spamserv"
31
32 #define KEY_EXCEPTIONS               "exceptions"
33 #define KEY_FLAGS                    "flags"
34 #define KEY_INFO                     "info"
35 #define KEY_EXCEPTLEVEL              "exceptlevel"
36 #define KEY_EXPIRY                   "expiry"
37 #define KEY_LASTBADWORDID            "last_badword_id"
38 #define KEY_BADWORDS                 "badwords"
39
40 #define KEY_BADWORD_MASK             "mask"
41 #define KEY_BADWORD_TRIGGERED        "count"
42 #define KEY_BADWORD_ACTION           "action"
43 #define KEY_BADWORDID                "badwordid"
44
45 #define KEY_DEBUG_CHANNEL            "debug_channel"
46 #define KEY_OPER_CHANNEL            "oper_channel"
47 #define KEY_GLOBAL_EXCEPTIONS        "global_exceptions"
48 #define KEY_NETWORK_RULES            "network_rules"
49 #define KEY_TRIGGER                  "trigger"
50 #define KEY_SHORT_BAN_DURATION       "short_ban_duration"
51 #define KEY_LONG_BAN_DURATION        "long_ban_duration"
52 #define KEY_GLINE_DURATION           "gline_duration"
53 #define KEY_EXCEPTION_MAX            "exception_max"
54 #define KEY_EXCEPTION_MIN_LEN        "exception_min_len"
55 #define KEY_EXCEPTION_MAX_LEN        "exception_max_len"
56 #define KEY_ADV_CHAN_MUST_EXIST      "adv_chan_must_exist"
57 #define KEY_STRIP_MIRC_CODES         "strip_mirc_codes"
58 #define KEY_ALLOW_MOVE_MERGE         "allow_move_merge"
59
60 #define SPAMSERV_FUNC(NAME)     MODCMD_FUNC(NAME)
61 #define SPAMSERV_SYNTAX()       svccmd_send_help(user, spamserv, cmd)
62 #define SPAMSERV_MIN_PARMS(N) do { \
63 (void)argv; \
64   if(argc < N) { \
65     ss_reply(MSG_MISSING_PARAMS, argv[0]); \
66     SPAMSERV_SYNTAX(); \
67     return 0; } } while(0)
68
69 struct userNode                 *spamserv;
70 static struct module    *spamserv_module;
71 static struct service   *spamserv_service;
72 static struct log_type  *SS_LOG;
73 static unsigned long    crc_table[256];
74
75 dict_t registered_channels_dict;
76 dict_t connected_users_dict;
77 dict_t killed_users_dict;
78
79 #define spamserv_notice(target, format...) send_message(target , spamserv , ## format)
80 #define spamserv_debug(format...) do { if(spamserv_conf.debug_channel) send_channel_message(spamserv_conf.debug_channel , spamserv , ## format); } while(0)
81 #define spamserv_oper_message(format...) do { if(spamserv_conf.oper_channel) send_channel_message(spamserv_conf.oper_channel , spamserv , ## format); } while(0)
82 #define ss_reply(format...)     send_message(user , spamserv , ## format)
83
84 #define SET_SUBCMDS_SIZE 10
85
86 const char *set_subcommands[SET_SUBCMDS_SIZE] = {"SPAMLIMIT", "ADVREACTION", "WARNREACTION", "ADVSCAN", "SPAMSCAN", "FLOODSCAN", "JOINFLOODSCAN", "EXCEPTLEVEL","SCANCHANOPS", "SCANVOICED"};
87
88 static void spamserv_clear_spamNodes(struct chanNode *channel);
89 static void spamserv_punish(struct chanNode *channel, struct userNode *user, time_t expires, char *reason, int ban);
90 static unsigned long crc32(const char *text);
91
92 #define BINARY_OPTION(arguments...)     return binary_option(arguments, user, channel, argc, argv);
93 #define MULTIPLE_OPTION(arguments...)   return multiple_option(arguments, values, ArrayLength(values), user, channel, argc, argv);
94
95 static const struct message_entry msgtab[] = {
96     { "SSMSG_CHANNEL_OPTIONS",         "Channel Options:" },
97     { "SSMSG_STRING_VALUE",            "$b%s$b%s" },
98     { "SSMSG_NUMERIC_VALUE",           "$b%s$b%d - %s" },
99     { "SSMSG_EASYNUMERIC_VALUE",       "$b%s$b%d" },
100     { "SSMSG_INVALID_NUM_SET",         "$b'%d'$b is an invalid %s setting." },
101     { "SSMSG_INVALID_OPTION",          "$b%s$b is not a valid %s option." },
102     { "SSMSG_INVALID_BINARY",          "$b%s$b is an invalid binary value." },
103
104     { "SSMSG_NOT_REGISTERED",          "$b%s$b has not been registered with $b$X$b." },
105     { "SSMSG_NOT_REGISTERED_CS",       "$b%s$b has not been registered with $b$C$b." },
106     { "SSMSG_ALREADY_REGISTERED",      "$b%s$b is already registered." },
107     { "SSMSG_DEBUG_CHAN",              "You may not register the debug channel." },
108     { "SSMSG_SUSPENDED_CS",            "$b$C$b access to $b%s$b has been temporarily suspended, thus you can't %s it." },
109     { "SSMSG_SUSPENDED",               "$b$X$b access to $b%s$b has been temporarily suspended." },
110     { "SSMSG_NO_REGISTER",             "Due to an error it was not possible to register $b%s$b." },
111     { "SSMSG_REG_SUCCESS",             "Channel $b%s$b registered." },
112     { "SSMSG_UNREG_SUCCESS",           "$b%s$b has been unregistered." },
113     { "SSMSG_NO_ACCESS",               "You lack sufficient access to use this command." },
114     { "SSMSG_MUST_BE_OPER",            "You must be an irc operator to set this option." },
115     { "SSMSG_CONFIRM_UNREG",           "To confirm this unregistration, you must append 'CONFIRM' to the end of your command. For example, 'unregister CONFIRM'." },
116
117     { "SSMSG_NO_EXCEPTIONS",           "No words found in the exception list." },
118     { "SSMSG_NO_SUCH_EXCEPTION",       "Word $b%s$b not found in the exception list." },
119     { "SSMSG_EXCEPTION_LIST",          "The following words are in the exception list:" },
120     { "SSMSG_EXCEPTION_ADDED",         "Word $b%s$b added to the exception list." },
121     { "SSMSG_EXCEPTION_DELETED",       "Word $b%s$b deleted from the exception list." },
122     { "SSMSG_EXCEPTION_IN_LIST",       "The word $b%s$b is already in the exception list." },
123     { "SSMSG_EXCEPTION_MAX",           "The exception list has reached the maximum exceptions (max %lu). Delete a word to add another one." },
124     { "SSMSG_EXCEPTION_TOO_SHORT",     "The word must be at least %lu characters long." },
125     { "SSMSG_EXCEPTION_TOO_LONG",      "The word may not be longer than %lu characters." },
126
127     { "SSMSG_STATUS",                  "$bStatus:$b" },
128     { "SSMSG_STATUS_USERS",            "Total Users Online:  %u" },
129     { "SSMSG_STATUS_CHANNELS",         "Registered Channels: %u" },
130     { "SSMSG_STATUS_MEMORY",           "$bMemory Information:$b" },
131     { "SSMSG_STATUS_CHANNEL_LIST",     "$bRegistered Channels:$b" },
132     { "SSMSG_STATUS_NO_CHANNEL",       "No channels registered." },
133     
134     { "SSMSG_BADWORD_ALREADY_ADDED",   "$b%s$b is already added. (ID: %s)" },
135     { "SSMSG_BADWORD_ADDED",           "added '$b%s$b' to the badword list with ID %s." },
136     { "SSMSG_BADWORD_SET_DONE",        "Done." },
137     { "SSMSG_BADWORD_SET_INVALID",     "Invalid Option for setting %s" },
138     { "SSMSG_BADWORD_SET",             "Settings for BadWord entry $b%s$b" },
139     { "SSMSG_BADWORD_SET_MASK",        "$bMASK$b:   %s" },
140     { "SSMSG_BADWORD_SET_ACTION",      "$bACTION$b: %s" },
141     { "SSMSG_BADWORD_NOT_FOUND",       "badword with ID %s does not exist." },
142     { "SSMSG_BADWORD_REMOVED",         "badword ID $b%s$b has been removed (mask: '%s')" },
143         { NULL, NULL }
144 };
145
146 #define SSMSG_DEBUG_KICK              "Kicked user $b%s$b from $b%s$b, reason: %s"
147 #define SSMSG_DEBUG_BAN               "Banned user $b%s$b from $b%s$b, reason: %s"
148 #define SSMSG_DEBUG_KILL              "Killed user $b%s$b, last violation in $b%s$b"
149 #define SSMSG_DEBUG_GLINE             "Glined user $b%s$b, host $b%s$b, last violation in $b%s$b"
150 #define SSMSG_DEBUG_RECONNECT         "Killed user $b%s$b reconnected to the network"
151 #define SSMSG_SPAM                    "Spamming"
152 #define SSMSG_FLOOD                   "Flooding the channel/network"
153 #define SSMSG_ADV                     "Advertising"
154 #define SSMSG_JOINFLOOD               "Join flooding the channel"
155 #define SSMSG_WARNING                 "%s is against the network rules"
156 #define SSMSG_WARNING_2               "You are violating the network rules"
157 #define SSMSG_WARNING_RULES           "%s is against the network rules. Read the network rules at %s"
158 #define SSMSG_WARNING_RULES_2         "You are violating the network rules. Read the network rules at %s"
159 #define SSMSG_BADWORD_DETECTED            "Your message contained a forbidden word."
160 #define SSMSG_CHANNEL_REGISTERED      "%s (channel %s) registered by %s."
161 #define SSMSG_CHANNEL_UNREGISTERED    "%s (channel %s) unregistered by %s."
162
163 static struct
164 {
165         struct chanNode *debug_channel;
166         struct chanNode *oper_channel;
167         struct string_list *global_exceptions;
168         const char *network_rules;
169         unsigned char trigger;
170         unsigned long short_ban_duration;
171         unsigned long long_ban_duration;
172         unsigned long gline_duration;
173         unsigned long exception_max;
174         unsigned long exception_min_len;
175         unsigned long exception_max_len;
176         unsigned int adv_chan_must_exist : 1;
177         unsigned int strip_mirc_codes : 1;
178         unsigned int allow_move_merge : 1;
179 } spamserv_conf;
180
181 /***********************************************/
182 /*                   Channel                   */
183 /***********************************************/
184
185 struct chanInfo*
186 get_chanInfo(const char *channelname)
187 {
188         return dict_find(registered_channels_dict, channelname, 0);
189 }
190
191 static void
192 spamserv_join_channel(struct chanNode *channel)
193 {
194         struct mod_chanmode change;
195         mod_chanmode_init(&change);
196         change.argc = 1;
197         change.args[0].mode = MODE_CHANOP;
198         change.args[0].u.member = AddChannelUser(spamserv, channel);
199         mod_chanmode_announce(spamserv, channel, &change);
200 }
201
202 static void
203 spamserv_part_channel(struct chanNode *channel, char *reason)
204 {
205         /* we only have to clear the spamNodes because every other node expires on it's own */
206         spamserv_clear_spamNodes(channel);
207         DelChannelUser(spamserv, channel, reason, 0);
208 }
209
210 static struct chanInfo*
211 spamserv_register_channel(struct chanNode *channel, struct string_list *exceptions, unsigned int flags, char *info)
212 {
213         struct chanInfo *cInfo = malloc(sizeof(struct chanInfo));
214         
215         if(!cInfo)
216         {
217                 log_module(SS_LOG, LOG_ERROR, "Couldn't allocate memory for cInfo; channel: %s", channel->name);
218                 return NULL;
219         }
220
221         cInfo->channel = channel;
222         cInfo->exceptions = exceptions ? string_list_copy(exceptions) : alloc_string_list(1);
223         cInfo->flags = flags;
224     cInfo->exceptlevel = 400;
225         safestrncpy(cInfo->info, info, sizeof(cInfo->info));
226         cInfo->suspend_expiry = 0;
227         dict_insert(registered_channels_dict, cInfo->channel->name, cInfo);
228
229         return cInfo;
230 }
231
232 static void
233 spamserv_unregister_channel(struct chanInfo *cInfo)
234 {
235         if(!cInfo)
236                 return;
237
238         dict_remove(registered_channels_dict, cInfo->channel->name);
239         free_string_list(cInfo->exceptions);
240         free(cInfo);
241 }
242
243 void
244 spamserv_cs_suspend(struct chanNode *channel, time_t expiry, int suspend, char *reason)
245 {
246         struct chanInfo *cInfo = get_chanInfo(channel->name);
247
248         if(cInfo)
249         {
250                 if(suspend)
251                 {
252                         cInfo->flags |= CHAN_SUSPENDED;
253                         cInfo->suspend_expiry = expiry;
254                         spamserv_part_channel(channel, reason);
255                 }
256                 else
257                 {
258                         if(CHECK_SUSPENDED(cInfo))
259                         {
260                                 cInfo->flags &= ~CHAN_SUSPENDED;
261                                 cInfo->suspend_expiry = 0;
262                         }
263                 }
264         }
265 }
266
267 int
268 spamserv_cs_move_merge(struct userNode *user, struct chanNode *channel, struct chanNode *target, int move)
269 {
270         struct chanInfo *cInfo = get_chanInfo(channel->name);
271
272         if(cInfo)
273         {
274                 char reason[MAXLEN];
275
276                 if(!spamserv_conf.allow_move_merge || get_chanInfo(target->name))
277                 {
278                         if(move)
279                                 snprintf(reason, sizeof(reason), "unregistered due to a channel move to %s", target->name);
280                         else
281                                 snprintf(reason, sizeof(reason), "unregistered due to a channel merge into %s", target->name);
282
283                         spamserv_cs_unregister(user, channel, manually, reason);
284                         return 0;
285                 }
286
287                 cInfo->channel = target;
288
289                 dict_remove(registered_channels_dict, channel->name);
290                 dict_insert(registered_channels_dict, target->name, cInfo);
291
292                 if(move)
293                 {
294                         snprintf(reason, sizeof(reason), "Channel moved to %s by %s.", target->name, user->handle_info->handle);
295                 }
296                 else
297                 {
298                         spamserv_join_channel(target);
299                         snprintf(reason, sizeof(reason), "%s merged into %s by %s.", channel->name, target->name, user->handle_info->handle);   
300                 }
301
302                 if(!CHECK_SUSPENDED(cInfo))
303                         spamserv_part_channel(channel, reason);
304
305                 if(move)
306                         snprintf(reason, sizeof(reason), "$X (channel %s) moved to %s by %s.", channel->name, target->name, user->handle_info->handle);
307                 else
308                         snprintf(reason, sizeof(reason), "$X (channel %s) merged into %s by %s.", channel->name, target->name, user->handle_info->handle);
309
310                 /*global_message(MESSAGE_RECIPIENT_OPERS | MESSAGE_RECIPIENT_HELPERS, reason);*/
311                 spamserv_oper_message("%s", reason);
312                 return 1;
313         }
314
315         return 0;
316 }
317
318 void
319 spamserv_cs_unregister(struct userNode *user, struct chanNode *channel, enum cs_unreg type, char *reason)
320 {
321         struct chanInfo *cInfo = get_chanInfo(channel->name);
322
323         if(cInfo)
324         {
325                 char global[MAXLEN], partmsg[MAXLEN];
326
327                 switch (type)
328                 {
329                 case manually:
330                         snprintf(global, sizeof(global), "$X (channel %s) %s by %s.", channel->name, reason, user->handle_info->handle);
331                         snprintf(partmsg, sizeof(partmsg), "%s %s by %s.", channel->name, reason, user->handle_info->handle);                   
332                         break;
333                 case expire:
334                         snprintf(global, sizeof(global), "$X (channel %s) registration expired.", channel->name);
335                         snprintf(partmsg, sizeof(partmsg), "%s registration expired.", channel->name);                  
336                         break;
337                 case lost_all_users:
338                         snprintf(global, sizeof(global), "$X (channel %s) lost all users.", channel->name);
339                         snprintf(partmsg, sizeof(partmsg), "%s lost all users.", channel->name);                        
340                         break;
341                 }
342
343                 if(!CHECK_SUSPENDED(cInfo))
344                         spamserv_part_channel(channel, partmsg);
345                 
346                 spamserv_unregister_channel(cInfo);
347                 spamserv_oper_message(SSMSG_CHANNEL_UNREGISTERED, spamserv->nick, channel->name, user->handle_info->handle);
348         }
349 }
350
351 /***********************************************/
352 /*                    User                     */
353 /***********************************************/
354
355 static struct userInfo*
356 get_userInfo(const char *nickname)
357 {
358         return dict_find(connected_users_dict, nickname, 0);
359 }
360
361 static void
362 spamserv_create_spamNode(struct chanNode *channel, struct userInfo *uInfo, char *text)
363 {
364         struct spamNode *sNode = malloc(sizeof(struct spamNode));
365
366         if(!sNode)
367         {
368                 log_module(SS_LOG, LOG_ERROR, "Couldn't allocate memory for sNode; channel: %s; user: %s", channel->name, uInfo->user->nick);
369                 return;
370         }
371
372         sNode->channel = channel;       
373         sNode->crc32 = crc32(text);
374         sNode->count = 1;
375         sNode->next = NULL;
376
377         if(uInfo->spam)
378         {
379                 struct spamNode *temp = uInfo->spam;
380                 
381                 while(temp->next)
382                         temp = temp->next;
383
384                 sNode->prev = temp;
385                 temp->next = sNode;
386         }
387         else
388         {
389                 sNode->prev = NULL;
390                 uInfo->spam = sNode;
391         }
392 }
393
394 static void
395 spamserv_delete_spamNode(struct userInfo *uInfo, struct spamNode *sNode)
396 {
397         if(!sNode)
398                 return;
399
400         if(sNode == uInfo->spam)
401                 uInfo->spam = sNode->next;
402         
403         if(sNode->next)
404                  sNode->next->prev = sNode->prev;
405         if(sNode->prev)
406                  sNode->prev->next = sNode->next;
407
408         free(sNode);
409 }
410
411 static void
412 spamserv_clear_spamNodes(struct chanNode *channel)
413 {
414         struct userInfo *uInfo;
415         struct spamNode *sNode;
416         unsigned int i;
417
418         for(i = 0; i < channel->members.used; i++)
419         {
420                 if((uInfo = get_userInfo(channel->members.list[i]->user->nick)))
421                 {
422                         if((sNode = uInfo->spam))
423                         {
424                                 for(; sNode; sNode = sNode->next)
425                                         if(sNode->channel == channel)
426                                                 break;
427                                         
428                                 if(sNode)
429                                         spamserv_delete_spamNode(uInfo, sNode);
430                         }
431                 }
432         }
433 }
434
435 static void
436 spamserv_create_floodNode(struct chanNode *channel, struct userNode *user, struct floodNode **uI_fNode)
437 {
438         struct floodNode *fNode = malloc(sizeof(struct floodNode));
439
440         if(!fNode)
441         {
442                 log_module(SS_LOG, LOG_ERROR, "Couldn't allocate memory for fNode; channel: %s; user: %s", channel->name, user->nick);
443                 return;
444         }
445
446         fNode->channel = channel;
447         fNode->owner = user;
448         fNode->count = 1;
449         fNode->time = now;      
450         fNode->next = NULL;
451
452         if(*uI_fNode)
453         {
454                 struct floodNode *temp = *uI_fNode;
455                 
456                 while(temp->next)
457                         temp = temp->next;
458                 
459                 fNode->prev = temp;
460                 temp->next = fNode;
461         }
462         else
463         {
464                 fNode->prev = NULL;
465                 *uI_fNode = fNode;
466         }
467 }
468
469 static void
470 spamserv_delete_floodNode(struct floodNode **uI_fNode, struct floodNode *fNode)
471 {
472         if(!fNode)
473                 return;
474
475         if(fNode == *uI_fNode)
476                 *uI_fNode = fNode->next;
477         
478         if(fNode->next)
479                  fNode->next->prev = fNode->prev;
480         if(fNode->prev)
481                  fNode->prev->next = fNode->next;
482
483         free(fNode);
484 }
485
486 static void
487 spamserv_create_user(struct userNode *user)
488 {
489         struct userInfo *uInfo = malloc(sizeof(struct userInfo));
490         struct killNode *kNode = dict_find(killed_users_dict, irc_ntoa(&user->ip), 0);
491
492         if(!uInfo)
493         {
494                 log_module(SS_LOG, LOG_ERROR, "Couldn't allocate memory for uInfo; nick: %s", user->nick);
495                 return;
496         }
497
498         if(kNode)
499                 spamserv_debug(SSMSG_DEBUG_RECONNECT, user->nick);
500
501         uInfo->user = user;
502         uInfo->spam = NULL;
503         uInfo->flood = NULL;
504         uInfo->joinflood = NULL;
505         uInfo->flags = kNode ? USER_KILLED : 0;
506         uInfo->warnlevel = kNode ? kNode->warnlevel : 0;
507         uInfo->lastadv = 0;
508
509         dict_insert(connected_users_dict, user->nick, uInfo);
510
511         if(kNode)
512         {
513                 dict_remove(killed_users_dict, irc_ntoa(&user->ip));
514                 free(kNode);
515         }
516 }
517
518 static void
519 spamserv_delete_user(struct userInfo *uInfo)
520 {
521         if(!uInfo)
522                 return;
523
524         if(uInfo->spam)
525                 while(uInfo->spam)
526                         spamserv_delete_spamNode(uInfo, uInfo->spam);   
527
528         if(uInfo->flood)
529                 while(uInfo->flood)
530                         spamserv_delete_floodNode(&uInfo->flood, uInfo->flood);
531
532         if(uInfo->joinflood)
533                 while(uInfo->joinflood)
534                         spamserv_delete_floodNode(&uInfo->joinflood, uInfo->joinflood);
535
536         dict_remove(connected_users_dict, uInfo->user->nick);
537         free(uInfo);
538 }
539
540 static void
541 spamserv_new_user_func(struct userNode *user)
542 {
543         if(!IsLocal(user))
544                 spamserv_create_user(user);
545 }
546
547 static void
548 spamserv_del_user_func(struct userNode *user, struct userNode *killer, UNUSED_ARG(const char *why))
549 {
550         struct userInfo *uInfo = get_userInfo(user->nick);
551         struct killNode *kNode;
552
553         if(killer == spamserv)
554         {
555                 kNode = malloc(sizeof(struct killNode));
556
557                 if(!kNode)
558                 {
559                         log_module(SS_LOG, LOG_ERROR, "Couldn't allocate memory for killNode - nickname %s", user->nick);
560                         spamserv_delete_user(uInfo);                    
561                         return;
562                 }
563
564                 if(uInfo->warnlevel > KILL_WARNLEVEL)
565                         kNode->warnlevel = uInfo->warnlevel - KILL_WARNLEVEL;
566                 else
567                         kNode->warnlevel = 0;
568
569                 kNode->time = now;
570
571                 dict_insert(killed_users_dict, irc_ntoa(&user->ip), kNode);
572         }
573
574         spamserv_delete_user(uInfo);    
575 }
576
577 static void
578 spamserv_nick_change_func(struct userNode *user, const char *old_nick)
579 {
580         struct userInfo *uInfo = get_userInfo(old_nick);
581
582         dict_remove(connected_users_dict, old_nick);
583         dict_insert(connected_users_dict, user->nick, uInfo);
584 }
585
586 static int
587 spamserv_user_join(struct modeNode *mNode)
588 {
589         struct chanNode *channel = mNode->channel;
590         struct userNode *user = mNode->user;    
591         struct chanInfo *cInfo;
592         struct userInfo *uInfo;
593         struct floodNode *jfNode;
594
595         if(user->uplink->burst || !(cInfo = get_chanInfo(channel->name)) || !CHECK_JOINFLOOD(cInfo) || !(uInfo = get_userInfo(user->nick)))
596                 return 0;
597
598         if(IsOper(user))
599         {
600                 return 0;
601         }
602         
603     if(!CHECK_CHANOPS(cInfo))
604         {
605                 //struct modeNode *mn = GetUserMode(channel, user);
606                 //if(mn->modes & MODE_CHANOP)
607                 //      return;
608         if(check_user_level(channel, user, lvlGiveOps, 1, 0)) 
609             return 0;
610         }
611     
612     if(!CHECK_VOICED(cInfo))
613         {
614         if(check_user_level(channel, user, lvlGiveVoice, 1, 0)) 
615             return 0;
616     }
617     
618     if(cInfo->exceptlevel == 0)
619         return 0;
620     if(cInfo->exceptlevel < 501) {
621       struct userData *uData;
622        if((uData = GetChannelUser(channel->channel_info, user->handle_info)) && (uData->access >= cInfo->exceptlevel)) {
623         return 0;
624        }
625     }
626
627         if(!(jfNode = uInfo->joinflood))
628         {
629                 spamserv_create_floodNode(channel, user, &uInfo->joinflood);
630         }
631         else
632         {
633                 for(; jfNode; jfNode = jfNode->next)
634                         if(jfNode->channel == channel)
635                                 break;
636
637                 if(!jfNode)
638                 {
639                         spamserv_create_floodNode(channel, user, &uInfo->joinflood);
640                 }
641                 else
642                 {
643                         jfNode->count++;
644                         jfNode->time = now;             
645
646                         if(jfNode->count > JOINFLOOD_MAX)
647                         {
648                                 char reason[MAXLEN];
649
650                                 spamserv_delete_floodNode(&uInfo->joinflood, jfNode);
651                                 snprintf(reason, sizeof(reason), spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_JOINFLOOD, spamserv_conf.network_rules);
652                                 spamserv_punish(channel, user, JOINFLOOD_B_DURATION, reason, 1);
653                         }
654                 }
655         }
656
657         return 0;
658 }
659
660 static void
661 spamserv_user_part(struct modeNode *mn, UNUSED_ARG(const char *reason))
662 {
663         struct userNode *user = mn->user;
664         struct chanNode *channel = mn->channel;
665         struct userInfo *uInfo;
666         struct spamNode *sNode;
667         struct floodNode *fNode;
668
669         if(user->dead || !get_chanInfo(channel->name) || !(uInfo = get_userInfo(user->nick)))
670                 return;
671
672         if((sNode = uInfo->spam))
673         {
674                 for(; sNode; sNode = sNode->next)
675                         if(sNode->channel == channel)
676                                 break;
677
678                 if(sNode)
679                         spamserv_delete_spamNode(uInfo, sNode);
680         }
681
682         if((fNode = uInfo->flood))
683         {
684                 for(; fNode; fNode = fNode->next)
685                         if(fNode->channel == channel)
686                                 break;
687
688                 if(fNode)
689                         spamserv_delete_floodNode(&uInfo->flood, fNode);
690         }
691 }
692
693 static struct badword*
694 add_badword(struct chanInfo *cInfo, const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
695 {
696     struct badword *badword;
697
698     badword = calloc(1, sizeof(*badword));
699     if (!badword)
700         return NULL;
701
702     if(!id) {
703         cInfo->last_badword_id++;
704         badword->id = strtab(cInfo->last_badword_id);
705     } else
706         badword->id = strdup(id);
707     badword->badword_mask = strdup(badword_mask);
708     badword->triggered = triggered;
709     badword->action = action;
710     dict_insert(cInfo->badwords, badword->id, badword);
711     return badword;
712 }
713
714 /***********************************************/
715 /*                 Other Stuff                 */
716 /***********************************************/
717
718 static void
719 crc32_init(void)
720 {
721         unsigned long crc;
722         int i, j;
723
724         for(i = 0; i < 256; i++)
725         {
726                 crc = i;
727
728                 for(j = 8; j > 0; j--)
729                 {
730                         if(crc & 1)
731                         {
732                                 crc = (crc >> 1) ^ 0xEDB88320L;
733                         }
734                         else
735                         {
736                                 crc >>= 1;
737                         }
738                 }
739
740                 crc_table[i] = crc;
741         }
742 }
743
744 static unsigned long
745 crc32(const char *text)
746 {
747         register unsigned long crc = 0xFFFFFFFF;
748         unsigned int c, i = 0;
749         
750         while((c = (unsigned int)text[i++]) != 0)
751                 crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_table[(crc^c) & 0xFF];
752  
753         return (crc^0xFFFFFFFF);
754 }
755
756 static void
757 timeq_flood(UNUSED_ARG(void *data))
758 {
759         dict_iterator_t         it;
760         struct userInfo         *uInfo;
761         struct floodNode        *fNode;
762
763         for(it = dict_first(connected_users_dict); it; it = iter_next(it))
764         {
765                 uInfo = iter_data(it);
766
767                 if(!(fNode = uInfo->flood))
768                         continue;
769
770                 for(; fNode; fNode = fNode->next)
771                 {
772                         if(now - fNode->time > FLOOD_EXPIRE)
773                         {
774                                 if(!(--fNode->count))
775                                         spamserv_delete_floodNode(&uInfo->flood, fNode);
776                         }
777                 }
778         }
779         
780         timeq_add(now + FLOOD_TIMEQ_FREQ, timeq_flood, NULL);
781 }
782
783 static void
784 timeq_joinflood(UNUSED_ARG(void *data))
785 {
786         dict_iterator_t it;
787         struct userInfo *uInfo;
788         struct floodNode *fNode;
789
790         for(it = dict_first(connected_users_dict); it; it = iter_next(it))
791         {
792                 uInfo = iter_data(it);
793
794                 if(!(fNode = uInfo->joinflood))
795                         continue;
796
797                 for(; fNode; fNode = fNode->next)
798                 {
799                         if(now - fNode->time > JOINFLOOD_EXPIRE)
800                         {
801                                 if(!(--fNode->count))
802                                         spamserv_delete_floodNode(&uInfo->joinflood, fNode);                            
803                         }
804                 }
805         }
806
807         timeq_add(now + JOINFLOOD_TIMEQ_FREQ, timeq_joinflood, NULL);
808 }
809
810 static void
811 timeq_adv(UNUSED_ARG(void *data))
812 {
813         dict_iterator_t it;
814         struct userInfo *uInfo;
815
816         for(it = dict_first(connected_users_dict); it; it = iter_next(it))
817         {
818                 uInfo = iter_data(it);
819
820                 if(uInfo->lastadv && uInfo->lastadv - now > ADV_EXPIRE)
821                 {
822                         uInfo->lastadv = 0;
823                         uInfo->flags &= ~USER_ADV_WARNED;
824                 }
825         }
826
827         timeq_add(now + ADV_TIMEQ_FREQ, timeq_adv, NULL);
828 }
829
830 static void
831 timeq_warnlevel(UNUSED_ARG(void *data))
832 {
833         dict_iterator_t it;
834         struct userInfo *uInfo;
835
836         for(it = dict_first(connected_users_dict); it; it = iter_next(it))
837         {
838                 uInfo = iter_data(it);
839
840                 if(uInfo->warnlevel > 0)
841                         uInfo->warnlevel--;
842         }
843
844         timeq_add(now + WARNLEVEL_TIMEQ_FREQ, timeq_warnlevel, NULL);
845 }
846
847 static void
848 timeq_kill(UNUSED_ARG(void *data))
849 {
850         dict_iterator_t it;
851         struct killNode *kNode;
852
853         for(it = dict_first(killed_users_dict); it; it = iter_next(it))
854         {
855                 kNode = iter_data(it);
856
857                 if(kNode->time - now > KILL_EXPIRE)
858                         free(kNode);
859         }
860
861         timeq_add(now + KILL_TIMEQ_FREQ, timeq_kill, NULL);
862 }
863
864 static int
865 binary_option(char *name, unsigned long mask, struct userNode *user, struct chanNode *channel, int argc, char *argv[])
866 {
867         struct chanInfo *cInfo = get_chanInfo(channel->name);
868         int value;
869
870         if(argc > 1)
871         {
872                 if(enabled_string(argv[1]))
873                 {
874                         cInfo->flags |= mask;
875                         value = 1;
876                 }
877                 else if(disabled_string(argv[1]))
878                 {
879                     cInfo->flags &= ~mask;
880                     value = 0;
881                 }
882                 else
883                 {
884                    spamserv_notice(user, "SSMSG_INVALID_BINARY", argv[1]);
885                    return 0;
886                 }
887         }
888         else
889         {
890                 value = (cInfo->flags & mask) ? 1 : 0;
891         }
892
893         spamserv_notice(user, "SSMSG_STRING_VALUE", name, value ? "Enabled." : "Disabled.");
894         return 1;
895 }
896
897 struct valueData
898 {
899         char *description;
900         char value;
901         int  oper_only : 1;
902 };
903
904 static int
905 multiple_option(char *name, char *description, enum channelinfo info, struct valueData *values, int count, struct userNode *user, struct chanNode *channel, int argc, char *argv[])
906 {
907         struct chanInfo *cInfo = get_chanInfo(channel->name);
908         int index;
909
910         if(argc > 1)
911         {
912                 index = atoi(argv[1]);
913                 
914                 if(index < 0 || index >= count)
915                 {
916                         spamserv_notice(user, "SSMSG_INVALID_NUM_SET", index, description);
917
918             for(index = 0; index < count; index++)
919                 spamserv_notice(user, "SSMSG_NUMERIC_VALUE", name, index, values[index].description);
920
921                         return 0;
922                 }
923
924                 if(values[index].oper_only && !IsOper(user))
925                 {
926                         spamserv_notice(user, "SSMSG_MUST_BE_OPER");
927                         return 0;
928                 }
929                 
930                 cInfo->info[info] = values[index].value;
931         }
932         else
933         {
934                 for(index = 0; index < count && cInfo->info[info] != values[index].value; index++);
935         }
936
937         spamserv_notice(user, "SSMSG_NUMERIC_VALUE", name, index, values[index].description);
938         return 1;
939 }
940
941 static int
942 show_exceptions(struct userNode *user, struct chanInfo *cInfo)
943 {
944         struct helpfile_table table;
945         unsigned int i;
946
947         if(!cInfo->exceptions->used)
948         {
949                 spamserv_notice(user, "SSMSG_NO_EXCEPTIONS");
950                 return 0;
951         }
952
953         spamserv_notice(user, "SSMSG_EXCEPTION_LIST");
954
955         table.length = 0;
956         table.width = 1;
957         table.flags = TABLE_REPEAT_ROWS | TABLE_NO_FREE | TABLE_NO_HEADERS;
958         table.contents = alloca(cInfo->exceptions->used * sizeof(*table.contents));
959
960         for(i = 0; i < cInfo->exceptions->used; i++)
961         {
962                 table.contents[table.length] = alloca(table.width * sizeof(**table.contents));
963                 table.contents[table.length][0] = cInfo->exceptions->list[i];
964                 table.length++;
965         }
966         
967         table_send(spamserv, user->nick, 0, NULL, table);
968
969         return 1;
970 }
971
972 static void
973 show_memory_usage(struct userNode *user)
974 {
975         dict_iterator_t it;
976         struct helpfile_table table;
977         struct chanInfo *cInfo;
978         struct userInfo *uInfo;
979         struct spamNode *sNode;
980         struct floodNode *fNode;
981         double channel_size = 0, user_size, size;
982         unsigned int spamcount = 0, floodcount = 0, i, j;
983         char buffer[64];
984
985         for(it = dict_first(registered_channels_dict); it; it = iter_next(it))
986         {
987                 cInfo = iter_data(it);
988
989                 if(!cInfo->exceptions->used)
990                         continue;
991
992                 for(i = 0; i < cInfo->exceptions->used; i++)
993                         channel_size += strlen(cInfo->exceptions->list[i]) * sizeof(char);              
994         }
995
996         for(it = dict_first(connected_users_dict); it; it = iter_next(it))
997         {
998                 uInfo = iter_data(it);
999
1000                 for(sNode = uInfo->spam; sNode; sNode = sNode->next, spamcount++);
1001                 for(fNode = uInfo->flood; fNode; fNode = fNode->next, floodcount++);
1002                 for(fNode = uInfo->joinflood; fNode; fNode = fNode->next, floodcount++);
1003         }
1004
1005         channel_size += dict_size(registered_channels_dict) * sizeof(struct chanInfo);
1006         
1007         user_size = dict_size(connected_users_dict) * sizeof(struct userInfo) +
1008                                 dict_size(killed_users_dict) * sizeof(struct killNode) +
1009                                 spamcount * sizeof(struct spamNode)     +
1010                                 floodcount *  sizeof(struct floodNode);
1011
1012         size = channel_size + user_size;
1013         
1014         ss_reply("SSMSG_STATUS_MEMORY");
1015         
1016         table.length = 3;
1017         table.width = 4;
1018         table.flags = TABLE_NO_FREE | TABLE_NO_HEADERS | TABLE_PAD_LEFT;
1019         table.contents = calloc(table.length, sizeof(char**));
1020
1021         // chanInfo
1022         table.contents[0] = calloc(table.width, sizeof(char*));
1023         snprintf(buffer, sizeof(buffer), "Channel Memory Usage:");
1024         table.contents[0][0] = strdup(buffer);
1025         snprintf(buffer, sizeof(buffer), " %g Byte; ", channel_size);
1026         table.contents[0][1] = strdup(buffer);
1027         snprintf(buffer, sizeof(buffer), "%g KiloByte; ", channel_size / 1024);
1028         table.contents[0][2] = strdup(buffer);
1029         snprintf(buffer, sizeof(buffer), "%g MegaByte", channel_size / 1024 / 1024);
1030         table.contents[0][3] = strdup(buffer);
1031
1032         // userInfo
1033         table.contents[1] = calloc(table.width, sizeof(char*));
1034         snprintf(buffer, sizeof(buffer), "User Memory Usage   :");
1035         table.contents[1][0] = strdup(buffer);
1036         snprintf(buffer, sizeof(buffer), " %g Byte; ", user_size);
1037         table.contents[1][1] = strdup(buffer);
1038         snprintf(buffer, sizeof(buffer), "%g KiloByte; ", user_size / 1024);
1039         table.contents[1][2] = strdup(buffer);
1040         snprintf(buffer, sizeof(buffer), "%g MegaByte", user_size / 1024 / 1024);
1041         table.contents[1][3] = strdup(buffer);
1042
1043         // total memory usage
1044         table.contents[2] = calloc(table.width, sizeof(char*));
1045         snprintf(buffer, sizeof(buffer), "Total Memory Usage  :");
1046         table.contents[2][0] = strdup(buffer);
1047         snprintf(buffer, sizeof(buffer), " %g Byte; ", size);
1048         table.contents[2][1] = strdup(buffer);
1049         snprintf(buffer, sizeof(buffer), "%g KiloByte; ", size / 1024);
1050         table.contents[2][2] = strdup(buffer);
1051         snprintf(buffer, sizeof(buffer), "%g MegaByte", size / 1024 / 1024);
1052         table.contents[2][3] = strdup(buffer);
1053
1054         table_send(spamserv, user->nick, 0, NULL, table);
1055         
1056         for(i = 0; i < table.length; i++)
1057         {
1058                 for(j = 0; j < table.width; j++)
1059                         free((char*)table.contents[i][j]);
1060
1061         free(table.contents[i]);
1062         }
1063
1064         free(table.contents);
1065 }
1066
1067 static void
1068 show_registered_channels(struct userNode *user)
1069 {
1070         struct helpfile_table table;
1071         dict_iterator_t it;
1072
1073         spamserv_notice(user, "SSMSG_STATUS_CHANNEL_LIST");
1074
1075         if(!dict_size(registered_channels_dict))
1076         {
1077                 spamserv_notice(user, "SSMSG_STATUS_NO_CHANNEL");
1078                 return;
1079         }
1080
1081         table.length = 0;
1082         table.width = 1;
1083         table.flags = TABLE_REPEAT_ROWS | TABLE_NO_FREE | TABLE_NO_HEADERS;
1084         table.contents = alloca(dict_size(registered_channels_dict) * sizeof(*table.contents));
1085
1086         for(it = dict_first(registered_channels_dict); it; it = iter_next(it))
1087         {
1088                 struct chanInfo *cInfo = iter_data(it);
1089
1090                 table.contents[table.length] = alloca(table.width * sizeof(**table.contents));
1091                 table.contents[table.length][0] = cInfo->channel->name;
1092                 table.length++;
1093         }
1094         
1095         table_send(spamserv, user->nick, 0, NULL, table);
1096 }
1097
1098 /***********************************************/
1099 /*                SpamServ_Func                */
1100 /***********************************************/
1101
1102 static 
1103 SPAMSERV_FUNC(cmd_register)
1104 {
1105         struct chanInfo *cInfo;
1106
1107         if(!channel || !channel->channel_info)
1108         {
1109                 ss_reply("SSMSG_NOT_REGISTERED_CS", channel->name);
1110                 return 0;
1111         }
1112
1113         if(get_chanInfo(channel->name))
1114         {
1115                 ss_reply("SSMSG_ALREADY_REGISTERED", channel->name);
1116                 return 0;
1117         }
1118
1119         if(IsSuspended(channel->channel_info))
1120         {
1121                 ss_reply("SSMSG_SUSPENDED_CS", channel->name, "register");
1122                 return 0;
1123         }
1124
1125         if(channel == spamserv_conf.debug_channel)
1126         {
1127                 ss_reply("SSMSG_DEBUG_CHAN");
1128                 return 0;
1129         }
1130
1131         if(!(cInfo = spamserv_register_channel(channel, spamserv_conf.global_exceptions, CHAN_FLAGS_DEFAULT , CHAN_INFO_DEFAULT)))
1132         {
1133                 ss_reply("SSMSG_NO_REGISTER", channel->name);
1134                 return 0;
1135         }
1136
1137         spamserv_join_channel(cInfo->channel);
1138         
1139         spamserv_oper_message(SSMSG_CHANNEL_REGISTERED, spamserv->nick, channel->name, user->handle_info->handle);
1140         ss_reply("SSMSG_REG_SUCCESS", channel->name);
1141
1142         return 1;
1143 }
1144
1145 static 
1146 SPAMSERV_FUNC(cmd_unregister)
1147 {
1148         struct chanInfo *cInfo;
1149         struct chanData *cData;
1150         struct userData *uData;
1151         char reason[MAXLEN];
1152
1153         if(!channel || !(cData = channel->channel_info) || !(cInfo = get_chanInfo(channel->name)))
1154         {
1155                 ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1156                 return 0;
1157         }
1158
1159         if(!(uData = GetChannelUser(cData, user->handle_info)) || (uData->access < UL_OWNER))
1160         {
1161         ss_reply("SSMSG_NO_ACCESS");
1162         return 0;
1163         }
1164
1165         if(!IsHelping(user))
1166         {
1167         if(IsSuspended(cData))
1168         {
1169             ss_reply("SSMSG_SUSPENDED_CS", channel->name, "unregister");
1170             return 0;
1171         }
1172
1173                 if(argc < 2 || strcasecmp(argv[1], "CONFIRM"))
1174                 {
1175                         ss_reply("SSMSG_CONFIRM_UNREG");
1176                         return 0;
1177                 }
1178         }
1179
1180         if(!CHECK_SUSPENDED(cInfo))
1181         {
1182                 snprintf(reason, sizeof(reason), "%s unregistered by %s.", spamserv->nick, user->handle_info->handle);          
1183                 spamserv_part_channel(channel, reason);
1184         }
1185         
1186         spamserv_unregister_channel(cInfo);     
1187
1188         spamserv_oper_message(SSMSG_CHANNEL_UNREGISTERED, spamserv->nick, channel->name, user->handle_info->handle);
1189         ss_reply("SSMSG_UNREG_SUCCESS", channel->name);
1190
1191         return 1;
1192 }
1193
1194 static 
1195 SPAMSERV_FUNC(cmd_status)
1196 {
1197         ss_reply("SSMSG_STATUS");
1198         ss_reply("SSMSG_STATUS_USERS", dict_size(connected_users_dict));
1199         ss_reply("SSMSG_STATUS_CHANNELS", dict_size(registered_channels_dict));
1200
1201         if(IsOper(user) && argc > 1)
1202         {
1203                 if(!irccasecmp(argv[1], "memory"))
1204                         show_memory_usage(user);
1205                 else if(!irccasecmp(argv[1], "channels"))
1206                         show_registered_channels(user);         
1207         }
1208         
1209         return 1;
1210 }
1211
1212 static 
1213 SPAMSERV_FUNC(cmd_addexception)
1214 {
1215         struct chanInfo *cInfo = get_chanInfo(channel->name);
1216         struct userData *uData;
1217         unsigned int i;
1218
1219         if(!cInfo || !channel->channel_info)
1220         {
1221                 ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1222                 return 0;
1223         }
1224
1225         if(CHECK_SUSPENDED(cInfo))
1226         {
1227                 ss_reply("SSMSG_SUSPENDED", channel->name);
1228                 return 0;
1229         }
1230
1231         if(!(uData = GetChannelUser(channel->channel_info, user->handle_info)) || (uData->access < 400))
1232         {
1233                 ss_reply("SSMSG_NO_ACCESS");
1234                 return 0;
1235         }
1236
1237         if(argc < 2)
1238                 return show_exceptions(user, cInfo);
1239
1240         if(cInfo->exceptions->used == spamserv_conf.exception_max && !IsOper(user))
1241         {
1242                 ss_reply("SSMSG_EXCEPTION_MAX", spamserv_conf.exception_max);
1243                 return 0;
1244         }
1245
1246         if(strlen(argv[1]) < spamserv_conf.exception_min_len)
1247         {
1248                 ss_reply("SSMSG_EXCEPTION_TOO_SHORT", spamserv_conf.exception_min_len);
1249                 return 0;
1250         }
1251         else if(strlen(argv[1]) > spamserv_conf.exception_max_len)
1252         {
1253                 ss_reply("SSMSG_EXCEPTION_TOO_LONG", spamserv_conf.exception_max_len);
1254                 return 0;
1255         }
1256
1257         for(i = 0; i < cInfo->exceptions->used; i++)
1258         {
1259                 if(!irccasecmp(argv[1], cInfo->exceptions->list[i]))
1260                 {
1261                         ss_reply("SSMSG_EXCEPTION_IN_LIST", argv[1]);
1262                         return 0;
1263                 }
1264         }
1265
1266         string_list_append(cInfo->exceptions, strdup(argv[1]));
1267         ss_reply("SSMSG_EXCEPTION_ADDED", argv[1]);
1268
1269         return 1;
1270 }
1271
1272 static 
1273 SPAMSERV_FUNC(cmd_delexception)
1274 {
1275         struct chanInfo *cInfo = get_chanInfo(channel->name);
1276         struct userData *uData;
1277         unsigned int i;
1278         int found = -1;
1279
1280         if(!cInfo || !channel->channel_info)
1281         {
1282                 ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1283                 return 0;
1284         }
1285
1286         if(CHECK_SUSPENDED(cInfo))
1287         {
1288                 ss_reply("SSMSG_SUSPENDED", channel->name);
1289                 return 0;
1290         }
1291
1292         if(!(uData = GetChannelUser(channel->channel_info, user->handle_info)) || (uData->access < 400))
1293         {
1294                 ss_reply("SSMSG_NO_ACCESS");
1295                 return 0;
1296         }
1297
1298         if(argc < 2)
1299                 return show_exceptions(user, cInfo);
1300
1301         for(i = 0; i < cInfo->exceptions->used; i++)
1302         {
1303                 if(!irccasecmp(argv[1], cInfo->exceptions->list[i]))
1304                 {
1305                         found = i;
1306                         break;
1307                 }
1308         }
1309         
1310         if(found == -1)
1311         {
1312                 ss_reply("SSMSG_NO_SUCH_EXCEPTION", argv[1]);
1313                 return 0;
1314         }
1315
1316         string_list_delete(cInfo->exceptions, i);
1317         ss_reply("SSMSG_EXCEPTION_DELETED", argv[1]);
1318
1319         return 1;
1320 }
1321
1322 static 
1323 SPAMSERV_FUNC(cmd_set)
1324 {
1325         struct chanInfo *cInfo = get_chanInfo(channel->name);
1326         struct svccmd   *subcmd;        
1327         char cmd_name[MAXLEN];
1328         unsigned int i;
1329
1330         if(!cInfo)
1331         {
1332                 ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1333                 return 0;
1334         }
1335
1336         if(CHECK_SUSPENDED(cInfo))
1337         {
1338                 ss_reply("SSMSG_SUSPENDED", channel->name);
1339                 return 0;
1340         }
1341
1342     if(!check_user_level(channel,user,lvlSetters,1,0))
1343         {
1344                 ss_reply("SSMSG_NO_ACCESS");
1345                 return 0;
1346         }
1347         
1348         if(argc < 2)
1349         {
1350                 ss_reply("SSMSG_CHANNEL_OPTIONS");
1351
1352                 for(i = 0; i < SET_SUBCMDS_SIZE; i++)
1353                 {
1354                         sprintf(cmd_name, "%s %s", cmd->name, set_subcommands[i]);
1355
1356                         if((subcmd = dict_find(cmd->parent->commands, cmd_name, NULL)))
1357                                 subcmd->command->func(user, channel, 1, argv + 1, subcmd);
1358                 }
1359
1360                 return 1;
1361         }
1362
1363         sprintf(cmd_name, "%s %s", cmd->name, argv[1]);
1364         subcmd = dict_find(cmd->parent->commands, cmd_name, NULL);
1365
1366         if(!subcmd)
1367         {
1368                 reply("SSMSG_INVALID_OPTION", argv[1], argv[0]);
1369                 return 0;
1370         }
1371
1372         return subcmd->command->func(user, channel, argc - 1, argv + 1, subcmd);
1373 }
1374
1375 static 
1376 SPAMSERV_FUNC(opt_spamlimit)
1377 {
1378         struct valueData values[] =
1379         {
1380                 {"Users may send the same message $b2$b times.", 'a', 0},
1381                 {"Users may send the same message $b3$b times.", 'b', 0},
1382                 {"Users may send the same message $b4$b times.", 'c', 0},
1383                 {"Users may send the same message $b5$b times.", 'd', 0},
1384                 {"Users may send the same message $b6$b times.", 'e', 0}
1385         };
1386
1387         MULTIPLE_OPTION("SpamLimit     ", "SpamLimit", ci_SpamLimit);
1388 }
1389
1390 static 
1391 SPAMSERV_FUNC(opt_advreaction)
1392 {
1393         struct valueData values[] =
1394         {
1395                 {"Kick on disallowed advertising.", 'k', 0},
1396                 {"Kickban on disallowed advertising.", 'b', 0},
1397                 {"Short timed ban on disallowed advertising.", 's', 0},
1398                 {"Long timed ban on disallowed advertising.", 'l', 0},
1399                 {"Kill on disallowed advertising.", 'd', 1}
1400         };
1401
1402         MULTIPLE_OPTION("AdvReaction   ", "AdvReaction", ci_AdvReaction);
1403 }
1404
1405 static 
1406 SPAMSERV_FUNC(opt_warnreaction)
1407 {
1408         struct valueData values[] =
1409         {
1410                 {"Kick after warning.", 'k', 0},
1411                 {"Kickban after warning.", 'b', 0},
1412                 {"Short timed ban after warning.", 's', 0},
1413                 {"Long timed ban after warning.", 'l', 0},
1414                 {"Kill after warning.", 'd', 1}
1415         };
1416
1417         MULTIPLE_OPTION("WarnReaction  ", "WarnReaction", ci_WarnReaction);
1418 }
1419
1420 static 
1421 SPAMSERV_FUNC(opt_advscan)
1422 {
1423         BINARY_OPTION("AdvScan       ", CHAN_ADV_SCAN);
1424 }
1425
1426 static 
1427 SPAMSERV_FUNC(opt_spamscan)
1428 {
1429         BINARY_OPTION("SpamScan      ", CHAN_SPAMSCAN);
1430 }
1431
1432 static 
1433 SPAMSERV_FUNC(opt_floodscan)
1434 {
1435         BINARY_OPTION("FloodScan     ", CHAN_FLOODSCAN);
1436 }
1437
1438 static 
1439 SPAMSERV_FUNC(opt_joinflood)
1440 {
1441         BINARY_OPTION("JoinFloodScan ", CHAN_JOINFLOOD);
1442 }
1443
1444 static 
1445 SPAMSERV_FUNC(opt_scanops)
1446 {
1447         BINARY_OPTION("ScanChanOps   ", CHAN_SCAN_CHANOPS);
1448 }
1449
1450 static 
1451 SPAMSERV_FUNC(opt_scanvoiced)
1452 {
1453         BINARY_OPTION("ScanVoiced    ", CHAN_SCAN_VOICED);
1454 }
1455
1456 static 
1457 SPAMSERV_FUNC(opt_exceptlevel)
1458 {
1459  struct chanInfo *cInfo = get_chanInfo(channel->name);
1460  struct userData *uData;
1461  int index;
1462  if(argc > 1)
1463         {
1464                 index = atoi(argv[1]);
1465                 if(index < 1 || index > 501)
1466                 {
1467                         spamserv_notice(user, "SSMSG_INVALID_NUM_SET", index, "ExceptLevel");
1468                         return 0;
1469                 }
1470         if(!(uData = GetChannelUser(channel->channel_info, user->handle_info)) || (uData->access < cInfo->exceptlevel || index > uData->access))
1471         {
1472             ss_reply("SSMSG_NO_ACCESS");
1473             return 0;
1474         }
1475         cInfo->exceptlevel=index;
1476     } else {
1477      index=cInfo->exceptlevel;
1478     }
1479     spamserv_notice(user, "SSMSG_EASYNUMERIC_VALUE", "ExceptLevel   ", index);
1480     return 1;
1481 }
1482
1483 static SPAMSERV_FUNC(cmd_addbad)
1484 {
1485          struct chanInfo *cInfo = get_chanInfo(channel->name);
1486          struct userData *uData;
1487
1488          if(!cInfo)
1489          {
1490                 ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1491                 return 0;
1492          }
1493
1494          if(CHECK_SUSPENDED(cInfo))
1495          {
1496                  ss_reply("SSMSG_SUSPENDED", channel->name);
1497              return 0;
1498          }
1499
1500          if(!check_user_level(channel,user,lvlSetters,1,0))
1501          {
1502                 ss_reply("SSMSG_NO_ACCESS");
1503                 return 0;
1504          }
1505
1506          dict_iterator_t it;
1507          char *mask = unsplit_string(argv + 1, argc - 1, NULL);
1508          for (it = dict_first(cInfo->badwords); it; it = iter_next(it)) {
1509                  struct badword *badword = iter_data(it);
1510                  if(match_ircglob(mask,badword->badword_mask)) {
1511                          reply("SSMSG_BADWORD_ALREADY_ADDED", mask, badword->id);
1512                          return 1;
1513                  }
1514          }
1515
1516          struct badword *new_badword = add_badword(cInfo, mask, 0, BADACTION_KICK, NULL);
1517          for (it = dict_first(cInfo->badwords); it; it = iter_next(it)) {
1518                  struct badword *badword = iter_data(it);
1519                  if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
1520                          dict_remove(cInfo->badwords, badword->id);
1521                  }
1522          }
1523
1524          reply("SSMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
1525          return 1;
1526 }
1527
1528 static SPAMSERV_FUNC(cmd_delbad)
1529 {
1530          struct chanInfo *cInfo = get_chanInfo(channel->name);
1531          struct userData *uData;
1532          unsigned int n;
1533
1534          if(!cInfo)
1535          {
1536                  ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1537                  return 0;
1538          }
1539
1540          if(CHECK_SUSPENDED(cInfo))
1541          {
1542                  ss_reply("SSMSG_SUSPENDED", channel->name);
1543              return 0;
1544          }
1545
1546          if(!check_user_level(channel,user,lvlSetters,1,0))
1547          {
1548                  ss_reply("SSMSG_NO_ACCESS");
1549                  return 0;
1550          }
1551
1552          for (n=1; n<argc; n++) {
1553                  struct badword *badword = dict_find(cInfo->badwords, argv[n], NULL);
1554                  if (!badword) {
1555                          reply("SSMSG_BADWORD_NOT_FOUND", argv[n]);
1556                          continue;
1557                  }
1558                  reply("SSMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
1559                  dict_remove(cInfo->badwords, argv[n]);
1560          }
1561          return 1;
1562 }
1563
1564 static SPAMSERV_FUNC(cmd_setbad)
1565 {
1566          struct chanInfo *cInfo = get_chanInfo(channel->name);
1567          struct userData *uData;
1568          struct badword *badword;
1569
1570          if(!cInfo)
1571          {
1572                  ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1573                  return 0;
1574          }
1575
1576          if(CHECK_SUSPENDED(cInfo))
1577          {
1578                  ss_reply("SSMSG_SUSPENDED", channel->name);
1579                  return 0;
1580          }
1581
1582          if(!check_user_level(channel,user,lvlSetters,1,0))
1583          {
1584                  ss_reply("SSMSG_NO_ACCESS");
1585                  return 0;
1586          }
1587
1588          if ((badword = dict_find(cInfo->badwords, argv[1], NULL))) {
1589                  if (argc > 3) {
1590                          unsigned int ii;
1591                          char *setting = argv[2];
1592                          char *value = argv[3];
1593                          for( ii = 0; setting[ ii ]; ii++)
1594                                  setting[ ii ] = toupper( setting[ ii ] );
1595                          for( ii = 0; value[ ii ]; ii++)
1596                                  value[ ii ] = toupper( value[ ii ] );
1597                          if(!strcmp("MASK",setting)) {
1598                                    free(badword->badword_mask);
1599                                    badword->badword_mask = strdup(argv[3]);
1600                                    badword->triggered = 0;
1601                                    reply("SSMSG_BADWORD_SET_DONE");
1602                          }
1603                          else if(!strcmp("ACTION",setting)) {
1604                                   if (!strcmp("1",value) || !strcmp("KICK",value)) {
1605                                          badword->action = BADACTION_KICK;
1606                                          reply("SSMSG_BADWORD_SET_DONE");
1607                                   } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
1608                                          badword->action = BADACTION_BAN;
1609                                          reply("SSMSG_BADWORD_SET_DONE");
1610                                   } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
1611                                          badword->action = BADACTION_KILL;
1612                                          reply("SSMSG_BADWORD_SET_DONE");
1613                                   } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
1614                                          badword->action = BADACTION_GLINE;
1615                                          reply("SSMSG_BADWORD_SET_DONE");
1616                                   } else {
1617                                          reply("SSMSG_BADWORD_SET_INVALID", setting);
1618                                   }
1619                          } else {
1620                                   reply("SSMSG_BADWORD_SETTING_INVALID", setting);
1621                          }
1622
1623                  } else {
1624                          reply("SSMSG_BADWORD_SET", badword->id);
1625                          reply("SSMSG_BADWORD_SET_MASK", badword->badword_mask);
1626                          switch(badword->action) {
1627                                  case BADACTION_KICK:
1628                                    reply("SSMSG_BADWORD_SET_ACTION", "KICK");
1629                                    break;
1630                                  case BADACTION_BAN:
1631                                    reply("SSMSG_BADWORD_SET_ACTION", "BAN");
1632                                    break;
1633                                  case BADACTION_KILL:
1634                                    reply("SSMSG_BADWORD_SET_ACTION", "KILL");
1635                                    break;
1636                                  case BADACTION_GLINE:
1637                                    reply("SSMSG_BADWORD_SET_ACTION", "GLINE");
1638                                    break;
1639                                  default:
1640                                    reply("SSMSG_BADWORD_SET_ACTION", "*undef*");
1641                          }
1642                  }
1643          } else {
1644                  reply("SSMSG_BADWORD_NOT_FOUND", argv[1]);
1645                  return 0;
1646          }
1647          return 1;
1648 }
1649
1650 int
1651 ss_badwords_sort(const void *pa, const void *pb)
1652 {
1653         struct badword *a = *(struct badword**)pa;
1654         struct badword *b = *(struct badword**)pb;
1655
1656         return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
1657 }
1658
1659 static SPAMSERV_FUNC(cmd_listbad)
1660 {
1661          struct helpfile_table tbl;
1662          struct chanInfo *cInfo = get_chanInfo(channel->name);
1663          struct userData *uData;
1664          struct badword **badwords;
1665          unsigned int count = 0, ii = 0;
1666
1667          if(!cInfo)
1668          {
1669              ss_reply("SSMSG_NOT_REGISTERED", channel->name);
1670                  return 0;
1671          }
1672
1673      if(CHECK_SUSPENDED(cInfo))
1674          {
1675          ss_reply("SSMSG_SUSPENDED", channel->name);
1676                  return 0;
1677          }
1678
1679          if(!check_user_level(channel,user,lvlSetters,1,0))
1680          {
1681                  ss_reply("SSMSG_NO_ACCESS");
1682                  return 0;
1683          }
1684
1685          dict_iterator_t it;
1686          for (it = dict_first(cInfo->badwords); it; it = iter_next(it)) {
1687                  count++;
1688          }
1689
1690          tbl.length = count+1;
1691          tbl.width = 4;
1692          tbl.flags = 0;
1693          tbl.flags = TABLE_NO_FREE;
1694          tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
1695          tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
1696          tbl.contents[0][0] = "#";
1697          tbl.contents[0][1] = "Badword";
1698          tbl.contents[0][2] = "Action";
1699          tbl.contents[0][3] = "(Triggered)";
1700          if(!count)
1701          {
1702                  table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
1703              reply("MSG_NONE");
1704              free(tbl.contents[0]);
1705              free(tbl.contents);
1706              return 0;
1707          }
1708          badwords = alloca(count * sizeof(badwords[0]));
1709          for (it = dict_first(cInfo->badwords); it; it = iter_next(it)) {
1710              struct badword *bw = iter_data(it);
1711              badwords[ii++] = bw;
1712           }
1713
1714          qsort(badwords, count, sizeof(badwords[0]), ss_badwords_sort);
1715          for (ii = 1; ii <= count; ii++) {
1716                  struct badword *bw = badwords[ii-1];
1717                  tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
1718                  tbl.contents[ii][0] = strdup(bw->id);
1719                  tbl.contents[ii][1] = strdup(bw->badword_mask);
1720                  switch(bw->action) {
1721                          case BADACTION_KICK:
1722                            tbl.contents[ii][2] = "KICK";
1723                            break;
1724                          case BADACTION_BAN:
1725                            tbl.contents[ii][2] = "BAN";
1726                            break;
1727                          case BADACTION_KILL:
1728                            tbl.contents[ii][2] = "KILL";
1729                            break;
1730                          case BADACTION_GLINE:
1731                            tbl.contents[ii][2] = "GLINE";
1732                            break;
1733                          default:
1734                            tbl.contents[ii][2] = "*undef*";
1735                  }
1736                  tbl.contents[ii][3] = strtab(bw->triggered);
1737          }
1738
1739          table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
1740          for(ii = 1; ii < tbl.length; ++ii)
1741          {
1742                  free(tbl.contents[ii]);
1743          }
1744          free(tbl.contents[0]);
1745          free(tbl.contents);
1746          return 1;
1747 }
1748
1749 static void 
1750 to_lower(char *message)
1751 {
1752         unsigned int i, diff = 'a' - 'A';
1753
1754         for(i = 0; i < strlen(message); i++)
1755         {
1756                 if((message[i] >= 'A') && (message[i] <= 'Z'))
1757                         message[i] = message[i] + diff;
1758         }
1759 }
1760
1761 static char *
1762 strip_mirc_codes(char *text)
1763 {
1764         // taken from xchat and modified
1765         int nc = 0, i = 0, col = 0, len = strlen(text);
1766         static char new_str[MAXLEN];
1767
1768         while(len > 0)
1769         {
1770                 if((col && isdigit(*text) && nc < 2) ||
1771                         (col && *text == ',' && isdigit(*(text + 1)) && nc < 3))
1772                 {
1773                         nc++;
1774
1775                         if(*text == ',')
1776                                 nc = 0;
1777                 }
1778                 else
1779                 {
1780                         col = 0;
1781
1782                         switch(*text)
1783                         {
1784                         case '\003':
1785                                 col = 1;
1786                                 nc = 0;
1787                                 break;
1788                         case '\002':
1789                         case '\022':
1790                         case '\026':                    
1791                         case '\031':
1792                         case '\037':
1793                                 break;
1794                         default:
1795                                 new_str[i] = *text;
1796                                 i++;
1797                         }
1798                 }
1799
1800                 text++;
1801                 len--;
1802         }
1803
1804         new_str[i] = '\0';
1805
1806         return new_str;
1807 }
1808
1809 static int
1810 is_in_exception_list(struct chanInfo *cInfo, char *message)
1811 {
1812         unsigned int i;
1813
1814         for(i = 0; i < cInfo->exceptions->used; i++)
1815                 if(strstr(message, cInfo->exceptions->list[i]))
1816                         return 1;
1817
1818         return 0;
1819 }
1820
1821 static int
1822 check_advertising(struct chanInfo *cInfo, char *message)
1823 {
1824         unsigned int i = 0;
1825
1826         if(spamserv_conf.strip_mirc_codes)
1827                 message = strip_mirc_codes(message);
1828
1829         if(is_in_exception_list(cInfo, message))
1830                 return 0;
1831
1832         while(message[i] != 0)
1833         {
1834                 if(message[i] == '#')
1835                 {
1836                         char channelname[CHANNELLEN];
1837                         unsigned int j = 0;
1838
1839                         if(!spamserv_conf.adv_chan_must_exist)
1840                                 return 1;
1841
1842                         /* only return 1, if the channel does exist */  
1843
1844                         while((message[i] != 0) && (message[i] != ' '))
1845                         {
1846                                 channelname[j] = message[i];
1847                                 i++;
1848                                 j++;                            
1849                         }
1850
1851                         channelname[j] = '\0';
1852
1853                         if(GetChannel(channelname))
1854                                 return 1;
1855                 }
1856                 else if((message[i] == 'w') && (message[i+1] == 'w') && (message[i+2] == 'w') && (message[i+3] == '.'))
1857                         return 1;
1858                 else if((message[i] == 'h') && (message[i+1] == 't') && (message[i+2] == 't') && (message[i+3] == 'p') && (message[i+4] == ':'))
1859                         return 1;
1860                 else if((message[i] == 'f') && (message[i+1] == 't') && (message[i+2] == 'p') && ((message[i+3] == '.') || (message[i+3] == ':')))
1861                         return 1;
1862
1863                 i++;
1864         }
1865
1866         return 0;
1867 }
1868
1869 struct banData *add_channel_ban(struct chanData *channel, const char *mask, char *owner, unsigned long set, unsigned long triggered, unsigned long expires, char *reason);
1870
1871 static void
1872 spamserv_punish(struct chanNode *channel, struct userNode *user, time_t expires, char *reason, int ban)
1873 {
1874         if(ban)
1875         {
1876                 struct mod_chanmode change;
1877                 char *hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
1878
1879                 sanitize_ircmask(hostmask);
1880
1881                 if(expires)
1882                         add_channel_ban(channel->channel_info, hostmask, spamserv->nick, now, now, now + expires, reason);
1883
1884                 mod_chanmode_init(&change);
1885                 change.argc = 1;
1886                 change.args[0].mode = MODE_BAN;
1887       change.args[0].u.hostmask = hostmask;
1888                 mod_chanmode_announce(spamserv, channel, &change);        
1889
1890                 free(hostmask);
1891
1892                 spamserv_debug(SSMSG_DEBUG_BAN, user->nick, channel->name, reason);
1893         }
1894         else
1895                 spamserv_debug(SSMSG_DEBUG_KICK, user->nick, channel->name, reason);
1896
1897         KickChannelUser(user, channel, spamserv, reason);       
1898 }
1899
1900 static void
1901 spamserv_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword)
1902 {
1903     char *hostmask;
1904     char *reason = SSMSG_BADWORD_DETECTED;
1905     char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
1906     switch(badword->action) {
1907         case BADACTION_BAN:
1908             hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
1909             sanitize_ircmask(hostmask);
1910             if(chan->channel_info) {
1911             //registered channel
1912             add_channel_ban(chan->channel_info, hostmask, spamserv->nick, now, now, now + spamserv_conf.long_ban_duration, reason);
1913             }
1914             struct mod_chanmode change;
1915             mod_chanmode_init(&change);
1916             change.argc = 1;
1917             change.args[0].mode = MODE_BAN;
1918             change.args[0].u.hostmask = hostmask;
1919             mod_chanmode_announce(spamserv, chan, &change);
1920             free(hostmask);
1921             break;
1922         case BADACTION_KICK:
1923             if(GetUserMode(chan, user))
1924             KickChannelUser(user, chan, spamserv, reason);
1925             break;
1926         case BADACTION_KILL:
1927             DelUser(user, spamserv, 1, reason);
1928             break;
1929         case BADACTION_GLINE:
1930             irc_ntop(mask + 2, sizeof(mask) - 2, &user->ip);
1931             gline_add(spamserv->nick, mask, spamserv_conf.gline_duration, reason, now, now, 0, 1);
1932             break;
1933         default:
1934             //error?
1935             break;
1936     }
1937 }
1938
1939 void
1940 spamserv_channel_message(struct chanNode *channel, struct userNode *user, char *text)
1941 {
1942         struct chanInfo *cInfo;
1943         struct userInfo *uInfo;
1944         struct spamNode *sNode;
1945         struct floodNode *fNode;
1946         unsigned int violation = 0;
1947         char reason[MAXLEN];
1948
1949         /* make sure: spamserv is not disabled; srvx is running; spamserv is in the chan; chan is regged, user does exist */
1950         if(!spamserv || quit_services || !GetUserMode(channel, spamserv) || !(cInfo = get_chanInfo(channel->name)) || !(uInfo = get_userInfo(user->nick)))
1951                 return;
1952
1953         if(IsOper(user))
1954         {
1955                 return;
1956         }
1957
1958         if(!CHECK_CHANOPS(cInfo))
1959         {
1960                 struct modeNode *mn = GetUserMode(channel, user);
1961                 if(mn && mn->modes & MODE_CHANOP)
1962                         return;
1963         //if(check_user_level(channel, user, lvlGiveOps, 1, 0)) 
1964         //    return;
1965         }
1966         
1967         if(!CHECK_VOICED(cInfo))
1968         {
1969                 struct modeNode *mn = GetUserMode(channel, user);
1970                 if(mn && (mn->modes & MODE_VOICE) && !(mn->modes & MODE_CHANOP))
1971                         return;
1972         }
1973     
1974     if(cInfo->exceptlevel == 0)
1975         return;
1976     if(cInfo->exceptlevel < 501) {
1977       struct userData *uData;
1978        if((uData = GetChannelUser(channel->channel_info, user->handle_info)) && (uData->access >= cInfo->exceptlevel)) {
1979         return;
1980        }
1981     }
1982
1983         to_lower(text);
1984
1985         if(CHECK_SPAM(cInfo))
1986         {
1987                 if(!(sNode = uInfo->spam))
1988                 {
1989                         spamserv_create_spamNode(channel, uInfo, text);
1990                 }
1991                 else
1992                 {
1993                         for(; sNode; sNode = sNode->next)
1994                                 if(sNode->channel == channel)
1995                                         break;
1996
1997                         if(!sNode)
1998                         {
1999                                 spamserv_create_spamNode(channel, uInfo, text);
2000                         }
2001                         else
2002                         {
2003                                 unsigned long crc = crc32(text);
2004
2005                                 if(crc == sNode->crc32)
2006                                 {
2007                                         unsigned int spamlimit = 2;
2008                                         sNode->count++;
2009
2010                                         switch(cInfo->info[ci_SpamLimit])
2011                                         {
2012                                                 case 'a': spamlimit = 2; break;
2013                                                 case 'b': spamlimit = 3; break;
2014                                                 case 'c': spamlimit = 4; break;
2015                                                 case 'd': spamlimit = 5; break;
2016                                                 case 'e': spamlimit = 6; break;
2017                                         }
2018
2019                                         if(sNode->count == spamlimit)
2020                                         {
2021                                                 uInfo->warnlevel += SPAM_WARNLEVEL;
2022
2023                                                 if(uInfo->warnlevel < MAX_WARNLEVEL)
2024                                                         spamserv_notice(user, spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_SPAM, spamserv_conf.network_rules);
2025                                         }
2026                                         else if(sNode->count > spamlimit)
2027                                         {
2028                                                 switch(cInfo->info[ci_WarnReaction])
2029                                                 {
2030                                                         case 'k': uInfo->flags |= USER_KICK; break;
2031                                                         case 'b': uInfo->flags |= USER_KICKBAN; break;
2032                                                         case 's': uInfo->flags |= USER_SHORT_TBAN; break;
2033                                                         case 'l': uInfo->flags |= USER_LONG_TBAN; break;
2034                                                         case 'd': uInfo->flags |= CHECK_KILLED(uInfo) ? USER_GLINE : USER_KILL; break;
2035                                                 }
2036
2037                                                 spamserv_delete_spamNode(uInfo, sNode);
2038                                                 uInfo->warnlevel += SPAM_WARNLEVEL;
2039                                                 violation = 1;
2040                                         }
2041                                 }
2042                                 else
2043                                 {
2044                                         sNode->crc32 = crc;                                     
2045                                         sNode->count = 1;
2046                                 }
2047                         }
2048                 }
2049         }
2050
2051         if(CHECK_FLOOD(cInfo))
2052         {
2053                 if(!(fNode = uInfo->flood))
2054                 {
2055                         spamserv_create_floodNode(channel, user, &uInfo->flood);
2056                 }
2057                 else
2058                 {
2059                         for(; fNode; fNode = fNode->next)
2060                                 if(fNode->channel == channel)
2061                                         break;
2062                                 
2063                         if(!fNode)
2064                         {
2065                                 spamserv_create_floodNode(channel, user, &uInfo->flood);
2066                         }
2067                         else
2068                         {
2069                                 if(((now - fNode->time) < FLOOD_EXPIRE))
2070                                 {
2071                                         fNode->count++;
2072                                         
2073                                         if(fNode->count == FLOOD_MAX_LINES - 1)
2074                                         {
2075                                                 uInfo->warnlevel += FLOOD_WARNLEVEL;
2076
2077                                                 if(uInfo->warnlevel < MAX_WARNLEVEL)
2078                                                         spamserv_notice(user, spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_FLOOD, spamserv_conf.network_rules);
2079                                         }
2080                                         else if(fNode->count > FLOOD_MAX_LINES)
2081                                         {
2082                                                 switch(cInfo->info[ci_WarnReaction])
2083                                                 {
2084                                                         case 'k': uInfo->flags |= USER_KICK; break;
2085                                                         case 'b': uInfo->flags |= USER_KICKBAN; break;
2086                                                         case 's': uInfo->flags |= USER_SHORT_TBAN; break;
2087                                                         case 'l': uInfo->flags |= USER_LONG_TBAN; break;
2088                                                         case 'd': uInfo->flags |= CHECK_KILLED(uInfo) ? USER_GLINE : USER_KILL; break;
2089                                                 }
2090
2091                                                 spamserv_delete_floodNode(&uInfo->flood, fNode);
2092                                                 uInfo->warnlevel += FLOOD_WARNLEVEL;
2093                                                 violation = 2;                                          
2094                                         }
2095                                 }
2096
2097                                 fNode->time = now;
2098                         }
2099                 }
2100         }
2101     
2102     dict_iterator_t it;
2103
2104         for (it = dict_first(cInfo->badwords); it; it = iter_next(it)) {
2105                 struct badword *badword = iter_data(it);
2106                 if(match_ircglob(text, badword->badword_mask)) {
2107                         spamserv_detected_badword(user, channel, badword);
2108                 }
2109         }
2110
2111         if(CHECK_ADV(cInfo) && check_advertising(cInfo, text))
2112         {
2113                 if(CHECK_ADV_WARNED(uInfo))
2114                 {
2115                         switch(cInfo->info[ci_AdvReaction])
2116                         {
2117                                 case 'k': uInfo->flags |= USER_KICK; break;
2118                                 case 'b': uInfo->flags |= USER_KICKBAN; break;
2119                                 case 's': uInfo->flags |= USER_SHORT_TBAN; break;
2120                                 case 'l': uInfo->flags |= USER_LONG_TBAN; break;
2121                                 case 'd': uInfo->flags |= CHECK_KILLED(uInfo) ? USER_GLINE : USER_KILL; break;
2122                         }
2123
2124                         uInfo->warnlevel += ADV_WARNLEVEL;
2125                         violation = 3;
2126                 }
2127                 else
2128                 {               
2129                         uInfo->flags |= USER_ADV_WARNED;
2130                         uInfo->lastadv = now;
2131                         uInfo->warnlevel += ADV_WARNLEVEL;
2132
2133                         if(uInfo->warnlevel < MAX_WARNLEVEL)
2134                                 spamserv_notice(user, spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_ADV, spamserv_conf.network_rules);
2135                 }               
2136         }
2137
2138         if(!CHECK_WARNED(uInfo) && !CHECK_KILL(uInfo) && !CHECK_GLINE(uInfo) && uInfo->warnlevel == MAX_WARNLEVEL)
2139         {
2140                 uInfo->flags |= USER_WARNED;
2141                 snprintf(reason, sizeof(reason), spamserv_conf.network_rules ? SSMSG_WARNING_RULES_2 : SSMSG_WARNING_2, spamserv_conf.network_rules);
2142                 irc_notice(spamserv, user->numeric, reason);
2143                 irc_privmsg(spamserv, user->numeric, reason);
2144         }
2145         else if(uInfo->warnlevel > MAX_WARNLEVEL)
2146         {
2147                 if(CHECK_KILLED(uInfo))
2148                         uInfo->flags |= USER_GLINE;
2149                 else
2150                         uInfo->flags |= USER_KILL;
2151
2152                 violation = 5;
2153         }
2154
2155         if(!violation)
2156                 return;
2157
2158         switch(violation)
2159         {
2160                 case 1: snprintf(reason, sizeof(reason), spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_SPAM, spamserv_conf.network_rules); break;
2161                 case 2: snprintf(reason, sizeof(reason), spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_FLOOD, spamserv_conf.network_rules); break;
2162                 case 3: snprintf(reason, sizeof(reason), spamserv_conf.network_rules ? SSMSG_WARNING_RULES : SSMSG_WARNING, SSMSG_ADV, spamserv_conf.network_rules); break;
2163                 default: snprintf(reason, sizeof(reason), spamserv_conf.network_rules ? SSMSG_WARNING_RULES_2 : SSMSG_WARNING_2, spamserv_conf.network_rules); break;
2164         }
2165
2166         if(CHECK_GLINE(uInfo))
2167         {
2168                 int size = strlen(user->hostname) + 3;
2169                 char *mask = alloca(size);
2170                 snprintf(mask, size, "*@%s", user->hostname);
2171                 gline_add(spamserv->nick, mask, spamserv_conf.gline_duration, reason, now, now, 0, 1);
2172                 spamserv_debug(SSMSG_DEBUG_GLINE, user->nick, user->hostname, channel->name);
2173         }
2174         else if(CHECK_KILL(uInfo))
2175         {
2176                 DelUser(user, spamserv, 1, reason);
2177                 spamserv_debug(SSMSG_DEBUG_KILL, user->nick, channel->name);
2178         }
2179         else if(CHECK_LONG_TBAN(uInfo))
2180         {
2181                 spamserv_punish(channel, user, spamserv_conf.long_ban_duration, reason, 1);
2182         }
2183         else if(CHECK_SHORT_TBAN(uInfo))
2184         {
2185                 spamserv_punish(channel, user, spamserv_conf.short_ban_duration, reason, 1);
2186         }
2187         else if(CHECK_KICKBAN(uInfo))
2188         {
2189                 spamserv_punish(channel, user, 0, reason, 1);
2190         }
2191         else if(CHECK_KICK(uInfo))
2192         {
2193                 spamserv_punish(channel, user, 0, reason, 0);
2194         }
2195 }
2196
2197 static int
2198 spamserv_saxdb_read_shitlist(const char *name, void *data, void *extra)
2199 {
2200         struct record_data *rd = data;
2201         struct chanInfo *chan = extra;
2202         char *badword;
2203         char *triggered, *action;
2204
2205         if (rd->type == RECDB_OBJECT) {
2206                 dict_t obj = GET_RECORD_OBJECT(rd);
2207                 /* new style structure */
2208                 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
2209                 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
2210                 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
2211
2212                 add_badword(chan, badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
2213         }
2214         return 0;
2215 }
2216
2217 static int
2218 spamserv_saxdb_read(struct dict *database)
2219 {
2220         dict_iterator_t it;
2221         struct dict *badwords;
2222         struct record_data *hir;
2223         struct chanNode *channel;
2224         struct chanInfo *cInfo;
2225         struct string_list *strlist;
2226         unsigned int flags,exceptlevel,badwordid;
2227         char *str, *info;
2228         unsigned long expiry;    
2229
2230         for(it = dict_first(database); it; it = iter_next(it))
2231         {
2232                 hir = iter_data(it);
2233
2234                 if(hir->type != RECDB_OBJECT)
2235                 {
2236                         log_module(SS_LOG, LOG_WARNING, "Unexpected rectype %d for %s.", hir->type, iter_key(it));
2237                         continue;
2238                 }
2239
2240                 channel = GetChannel(iter_key(it));
2241                 strlist = database_get_data(hir->d.object, KEY_EXCEPTIONS, RECDB_STRING_LIST);
2242
2243                 str = database_get_data(hir->d.object, KEY_FLAGS, RECDB_QSTRING);
2244                 flags = str ? atoi(str) : 0;
2245
2246                 info = database_get_data(hir->d.object, KEY_INFO, RECDB_QSTRING);
2247         str = database_get_data(hir->d.object, KEY_EXCEPTLEVEL, RECDB_QSTRING);
2248         exceptlevel = str ? atoi(str) : 400;
2249         
2250                 str = database_get_data(hir->d.object, KEY_EXPIRY, RECDB_QSTRING);
2251                 expiry = str ? strtoul(str, NULL, 0) : 0;
2252
2253                 if(channel && info)
2254                 {
2255                         if((cInfo = spamserv_register_channel(channel, strlist, flags, info)))
2256                         {
2257                                 /* if the channel is suspended and expiry = 0 it means: channel will
2258                                    never expire ! it does NOT mean, the channel is not suspended */
2259                                 if(CHECK_SUSPENDED(cInfo) && expiry && (expiry < now))
2260                                 {
2261                                         cInfo->flags &= ~CHAN_SUSPENDED;
2262                                         spamserv_join_channel(cInfo->channel);
2263                                 }
2264                                 else if(!CHECK_SUSPENDED(cInfo))
2265                                         spamserv_join_channel(cInfo->channel);
2266                                 else
2267                                         cInfo->suspend_expiry = expiry;                 
2268                 cInfo->exceptlevel=exceptlevel;
2269                 cInfo->badwords = dict_new();
2270                 str = database_get_data(hir->d.object, KEY_LASTBADWORDID, RECDB_QSTRING);
2271                 badwordid = str ? atoi(str) : 0;
2272                 cInfo->last_badword_id = badwordid;
2273                 if ((badwords = database_get_data(hir->d.object, KEY_BADWORDS, RECDB_OBJECT)))
2274                         dict_foreach(badwords, spamserv_saxdb_read_shitlist, cInfo);
2275                         }
2276                 }
2277                 else
2278                         log_module(SS_LOG, LOG_ERROR, "Couldn't register channel %s. Channel or info invalid.", iter_key(it));  
2279         }
2280
2281         return 0;
2282 }
2283
2284 static int
2285 spamserv_saxdb_write(struct saxdb_context *ctx)
2286 {
2287         dict_iterator_t it;
2288
2289         for(it = dict_first(registered_channels_dict); it; it = iter_next(it))
2290         {
2291                 struct chanInfo *cInfo = iter_data(it);
2292
2293                 saxdb_start_record(ctx, cInfo->channel->name, 1);
2294
2295                 if(cInfo->exceptions->used)
2296                         saxdb_write_string_list(ctx, KEY_EXCEPTIONS, cInfo->exceptions);
2297
2298                 if(cInfo->flags)
2299                         saxdb_write_int(ctx, KEY_FLAGS, cInfo->flags);  
2300
2301                 saxdb_write_string(ctx, KEY_INFO, cInfo->info);         
2302         saxdb_write_int(ctx, KEY_EXCEPTLEVEL, cInfo->exceptlevel);              
2303
2304                 if(cInfo->suspend_expiry)
2305                         saxdb_write_int(ctx, KEY_EXPIRY, cInfo->suspend_expiry);
2306
2307                 saxdb_write_int(ctx, KEY_LASTBADWORDID, cInfo->last_badword_id);
2308                 saxdb_start_record(ctx, KEY_BADWORDS, 1);
2309                 dict_iterator_t itbad;
2310                 for (itbad = dict_first(cInfo->badwords); itbad; itbad = iter_next(itbad)) {
2311                         struct badword *badword = iter_data(itbad);
2312                         saxdb_start_record(ctx, badword->id, 1);
2313                         saxdb_write_string(ctx, KEY_BADWORDID, badword->id);
2314                         saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
2315                         saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
2316                         saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
2317                         saxdb_end_record(ctx);
2318                 }
2319                 saxdb_end_record(ctx);
2320                 saxdb_end_record(ctx);          
2321         }
2322         return 0;
2323 }
2324
2325 static void
2326 spamserv_conf_read(void)
2327 {
2328         dict_t conf_node;
2329         const char *str; 
2330
2331         if(!(conf_node = conf_get_data(SPAMSERV_CONF_NAME, RECDB_OBJECT)))
2332         {
2333                 log_module(SS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", SPAMSERV_CONF_NAME);
2334                 return;
2335         }
2336
2337         str = database_get_data(conf_node, KEY_DEBUG_CHANNEL, RECDB_QSTRING);
2338         if(str)
2339         {
2340                 spamserv_conf.debug_channel = AddChannel(str, now, "+tinms", NULL);
2341
2342                 if(spamserv_conf.debug_channel)
2343                         spamserv_join_channel(spamserv_conf.debug_channel);
2344         }
2345         else
2346         {
2347                 spamserv_conf.debug_channel = NULL;
2348         }
2349
2350         str = database_get_data(conf_node, KEY_OPER_CHANNEL, RECDB_QSTRING);
2351         if(str)
2352         {
2353                 spamserv_conf.oper_channel = AddChannel(str, now, "+tinms", NULL);
2354         }
2355         else
2356         {
2357                 spamserv_conf.oper_channel = NULL;
2358         }
2359
2360         spamserv_conf.global_exceptions = database_get_data(conf_node, KEY_GLOBAL_EXCEPTIONS, RECDB_STRING_LIST);
2361
2362         str = database_get_data(conf_node, KEY_NETWORK_RULES, RECDB_QSTRING);
2363         spamserv_conf.network_rules = str ? str : NULL;
2364
2365         str = database_get_data(conf_node, KEY_TRIGGER, RECDB_QSTRING);
2366         spamserv_conf.trigger = str ? str[0] : 0;
2367
2368         str = database_get_data(conf_node, KEY_SHORT_BAN_DURATION, RECDB_QSTRING);
2369         spamserv_conf.short_ban_duration = str ? ParseInterval(str) : ParseInterval("15m");
2370
2371         str = database_get_data(conf_node, KEY_LONG_BAN_DURATION, RECDB_QSTRING);
2372         spamserv_conf.long_ban_duration = str ? ParseInterval(str) : ParseInterval("1h");
2373
2374         str = database_get_data(conf_node, KEY_GLINE_DURATION, RECDB_QSTRING);
2375         spamserv_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
2376
2377         str = database_get_data(conf_node, KEY_EXCEPTION_MAX, RECDB_QSTRING);
2378         spamserv_conf.exception_max = str ? strtoul(str, NULL, 0) : 10;
2379
2380         str = database_get_data(conf_node, KEY_EXCEPTION_MIN_LEN, RECDB_QSTRING);
2381         spamserv_conf.exception_min_len = str ? strtoul(str, NULL, 0) : 4;
2382
2383         str = database_get_data(conf_node, KEY_EXCEPTION_MAX_LEN, RECDB_QSTRING);
2384         spamserv_conf.exception_max_len = str ? strtoul(str, NULL, 0) : 15;
2385
2386         str = database_get_data(conf_node, KEY_ADV_CHAN_MUST_EXIST, RECDB_QSTRING);
2387         spamserv_conf.adv_chan_must_exist = str ? enabled_string(str) : 1;
2388
2389         str = database_get_data(conf_node, KEY_STRIP_MIRC_CODES, RECDB_QSTRING);
2390         spamserv_conf.strip_mirc_codes = str ? enabled_string(str) : 0;
2391
2392         str = database_get_data(conf_node, KEY_ALLOW_MOVE_MERGE, RECDB_QSTRING);
2393         spamserv_conf.allow_move_merge = str ? enabled_string(str) : 0;
2394 }
2395
2396 static void
2397 spamserv_db_cleanup(void)
2398 {
2399         dict_iterator_t it;
2400
2401         while((it = dict_first(registered_channels_dict)))
2402         {
2403                 spamserv_unregister_channel(iter_data(it));
2404         }
2405
2406         while((it = dict_first(killed_users_dict)))
2407         {
2408                 free(iter_data(it));
2409         }
2410         
2411         dict_delete(registered_channels_dict);
2412         dict_delete(connected_users_dict);
2413         dict_delete(killed_users_dict);
2414 }
2415
2416 void
2417 init_spamserv(const char *nick)
2418 {
2419         if(!nick)
2420                 return;
2421
2422         const char *modes = conf_get_data("services/spamserv/modes", RECDB_QSTRING);    
2423         spamserv = AddLocalUser(nick, nick, NULL, "Anti Spam Services", modes);
2424         spamserv_service = service_register(spamserv);
2425         service_register(spamserv)->trigger = spamserv_conf.trigger;
2426
2427         conf_register_reload(spamserv_conf_read);
2428
2429         SS_LOG = log_register_type("SpamServ", "file:spamserv.log");    
2430
2431         registered_channels_dict = dict_new();
2432         connected_users_dict = dict_new();
2433         killed_users_dict = dict_new();
2434
2435         reg_new_user_func(spamserv_new_user_func);
2436         reg_del_user_func(spamserv_del_user_func);
2437         reg_nick_change_func(spamserv_nick_change_func);
2438         reg_join_func(spamserv_user_join);
2439         reg_part_func(spamserv_user_part);
2440
2441         timeq_add(now + FLOOD_TIMEQ_FREQ, timeq_flood, NULL);
2442         timeq_add(now + JOINFLOOD_TIMEQ_FREQ, timeq_joinflood, NULL);
2443         timeq_add(now + ADV_TIMEQ_FREQ, timeq_adv, NULL);
2444         timeq_add(now + WARNLEVEL_TIMEQ_FREQ, timeq_warnlevel, NULL);
2445         timeq_add(now + KILL_TIMEQ_FREQ, timeq_kill, NULL);
2446
2447         spamserv_module = module_register("SpamServ", SS_LOG, "spamserv.help", NULL);
2448         modcmd_register(spamserv_module, "REGISTER", cmd_register, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, "flags", "+acceptchan,+helping", NULL);
2449         modcmd_register(spamserv_module, "UNREGISTER", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, "flags", "+loghostmask", NULL);
2450         modcmd_register(spamserv_module, "ADDEXCEPTION", cmd_addexception, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2451         modcmd_register(spamserv_module, "DELEXCEPTION", cmd_delexception, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2452         modcmd_register(spamserv_module, "STATUS", cmd_status, 1, 0, NULL);
2453     modcmd_register(spamserv_module, "ADDBAD", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2454     modcmd_register(spamserv_module, "DELBAD", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2455     modcmd_register(spamserv_module, "SETBAD", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2456     modcmd_register(spamserv_module, "LISTBAD", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2457         modcmd_register(spamserv_module, "SET", cmd_set, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2458         modcmd_register(spamserv_module, "SET SPAMLIMIT", opt_spamlimit, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2459         modcmd_register(spamserv_module, "SET ADVREACTION", opt_advreaction, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2460         modcmd_register(spamserv_module, "SET WARNREACTION", opt_warnreaction, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2461         modcmd_register(spamserv_module, "SET ADVSCAN", opt_advscan, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2462         modcmd_register(spamserv_module, "SET SPAMSCAN", opt_spamscan, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2463         modcmd_register(spamserv_module, "SET FLOODSCAN", opt_floodscan, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2464         modcmd_register(spamserv_module, "SET JOINFLOODSCAN", opt_joinflood, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2465         modcmd_register(spamserv_module, "SET SCANCHANOPS", opt_scanops, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2466         modcmd_register(spamserv_module, "SET SCANVOICED", opt_scanvoiced, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2467     modcmd_register(spamserv_module, "SET EXCEPTLEVEL", opt_exceptlevel, 1, MODCMD_REQUIRE_AUTHED|MODCMD_REQUIRE_CHANNEL, NULL);
2468
2469         saxdb_register("SpamServ", spamserv_saxdb_read, spamserv_saxdb_write);
2470         reg_exit_func(spamserv_db_cleanup);
2471         message_register_table(msgtab);
2472         crc32_init();
2473 }