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