]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | #include <stdlib.h> |
2 | #include <stdio.h> /* for debug */ | |
3 | #include <string.h> | |
4 | ||
5 | #include "hcensor.h" | |
3a839281 | 6 | #include "hcommands.h" |
643deb1b | 7 | #include "helpmod.h" |
c86edd1d | 8 | #include "hgen.h" |
3a839281 | 9 | #include "hban.h" |
c86edd1d Q |
10 | |
11 | hcensor *hcensor_get_by_pattern(hcensor *hcens, const char *pat) | |
12 | { | |
13 | for (;hcens;hcens = hcens->next) | |
14 | if (!ci_strcmp(pat, hcens->pattern->content)) | |
15 | return hcens; | |
16 | return NULL; | |
17 | } | |
18 | ||
19 | hcensor *hcensor_get_by_index(hcensor *hcens, int index) | |
20 | { | |
21 | if (index > 512) /* some sanity */ | |
22 | return NULL; | |
23 | ||
24 | while (index && hcens) | |
ce659137 | 25 | { |
26 | hcens = hcens->next; | |
27 | index--; | |
28 | } | |
c86edd1d Q |
29 | |
30 | return hcens; | |
31 | } | |
32 | ||
33 | hcensor *hcensor_check(hcensor *hcens, const char *str) | |
34 | { | |
35 | for (;hcens;hcens = hcens->next) | |
36 | if (strregexp(str, hcens->pattern->content)) | |
37 | return hcens; | |
38 | return NULL; | |
39 | } | |
40 | ||
3a839281 | 41 | hcensor *hcensor_add(hcensor **hcens, const char *pat, const char *rsn, hcensor_type type) |
c86edd1d Q |
42 | { |
43 | hcensor *tmp; | |
44 | ||
45 | if (hcensor_get_by_pattern(*hcens, pat)) | |
46 | return NULL; | |
47 | ||
48 | tmp = malloc(sizeof(hcensor)); | |
49 | tmp->next = *hcens; | |
50 | ||
51 | tmp->pattern = getsstring(pat, strlen(pat)); | |
3a839281 | 52 | tmp->type = type; |
53 | ||
c86edd1d Q |
54 | if (rsn) |
55 | tmp->reason = getsstring(rsn, strlen(rsn)); | |
56 | else | |
57 | tmp->reason = NULL; | |
58 | ||
59 | *hcens = tmp; | |
60 | ||
61 | return tmp; | |
62 | } | |
63 | ||
64 | hcensor *hcensor_del(hcensor **hcens, hcensor *ptr) | |
65 | { | |
66 | for (;*hcens;hcens = &(*hcens)->next) | |
67 | if (*hcens == ptr) | |
68 | { | |
69 | hcensor *tmp = (*hcens)->next; | |
70 | freesstring((*hcens)->pattern); | |
71 | freesstring((*hcens)->reason); | |
72 | free (*hcens); | |
73 | *hcens = tmp; | |
74 | return NULL; | |
75 | } | |
76 | return ptr; | |
77 | } | |
78 | ||
79 | void hcensor_del_all(hcensor **hcens) | |
80 | { | |
81 | while (*hcens) | |
82 | hcensor_del(hcens, *hcens); | |
83 | } | |
84 | ||
85 | int hcensor_count(hcensor *hcens) | |
86 | { | |
87 | int count = 0; | |
88 | for (;hcens;hcens = hcens->next, count++); | |
89 | return count; | |
90 | } | |
643deb1b | 91 | |
92 | int hcensor_match(hchannel *hchan, huser *husr, hcensor *hcens) | |
93 | { | |
3a839281 | 94 | switch (hcens->type) |
95 | { | |
96 | case HCENSOR_WARN: | |
97 | if (hcens->reason) | |
98 | helpmod_reply(husr, NULL, hcens->reason->content); | |
99 | return 0; | |
100 | case HCENSOR_KICK: | |
101 | helpmod_kick(hchan, husr, hcens->reason?hcens->reason->content:"Improper user"); | |
9af95c3d | 102 | return !0; |
103 | case HCENSOR_CHANBAN: | |
104 | helpmod_setban(hchan, hban_ban_string(husr->real_user, HBAN_HOST), HCMD_OUT_DEFAULT + time(NULL), MCB_ADD, HNOW); | |
105 | helpmod_kick(hchan, husr, hcens->reason?hcens->reason->content:"Censor violation"); | |
106 | return !0; | |
3a839281 | 107 | case HCENSOR_BAN: |
108 | hban_add(hban_ban_string(husr->real_user, HBAN_HOST), hcens->reason?hcens->reason->content:"Censor violation", HCMD_OUT_DEFAULT + time(NULL), 1); | |
109 | return !0; | |
110 | default: | |
111 | Error("helpmod", ERR_ERROR, "Unknown censor type %d", hcens->type); | |
112 | return !0; | |
113 | } | |
114 | } | |
115 | ||
116 | const char *hcensor_get_typename(hcensor_type type) | |
117 | { | |
118 | switch (type) | |
119 | { | |
120 | case HCENSOR_WARN: | |
121 | return "warn"; | |
122 | case HCENSOR_KICK: | |
123 | return "kick"; | |
9af95c3d | 124 | case HCENSOR_CHANBAN: |
125 | return "chanban"; | |
3a839281 | 126 | case HCENSOR_BAN: |
127 | return "ban"; | |
128 | default: | |
129 | return "error"; | |
130 | } | |
643deb1b | 131 | } |