]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | #include <time.h> |
2 | #include <string.h> | |
3 | #include <stdlib.h> | |
4 | #include <assert.h> | |
5 | ||
6 | #include "../channel/channel.h" | |
7 | #include "../nick/nick.h" | |
8 | #include "../lib/sstring.h" | |
9 | #include "../localuser/localuserchannel.h" | |
10 | ||
11 | #include "hban.h" | |
12 | #include "hchannel.h" | |
13 | #include "huser.h" | |
14 | #include "helpmod.h" | |
15 | #include "hgen.h" | |
16 | ||
17 | hban *hban_add(const char* pat, const char* rsn, time_t exp, int now) | |
18 | { | |
19 | hban *ptr; | |
20 | chanban *tmp; | |
21 | ||
22 | if (hban_get(pat) || exp <= time(NULL)) | |
23 | return NULL; | |
24 | ||
25 | if ((tmp = makeban(pat)) == NULL) /* bad ban */ | |
26 | return NULL; | |
27 | ||
28 | ptr = (hban*)malloc(sizeof(hban)); | |
29 | ptr->real_ban = tmp; | |
30 | ptr->expiration = exp; | |
31 | ||
32 | if (rsn == NULL) | |
33 | ptr->reason = NULL; | |
34 | else | |
35 | ptr->reason = getsstring(rsn, strlen(rsn)); | |
36 | ||
37 | ptr->next = hbans; | |
38 | hbans = ptr; | |
39 | ||
40 | { /* additional logic here */ | |
41 | huser* tmpu; | |
42 | for (tmpu = husers;tmpu;tmpu = tmpu->next) | |
43 | if (nickmatchban(tmpu->real_user, tmp) && !IsOper(tmpu->real_user)) | |
44 | { | |
45 | hchannel *assert_hchan = NULL; | |
46 | while (tmpu->hchannels) | |
47 | { | |
48 | if (tmpu->hchannels->hchan == assert_hchan) | |
49 | { | |
50 | Error("helpmod", ERR_ERROR, "hban.c hban_add() current channel is the previous channel. Preventing lockup."); | |
51 | break; | |
52 | } | |
53 | assert_hchan = tmpu->hchannels->hchan; | |
54 | helpmod_setban(tmpu->hchannels->hchan, bantostring(ptr->real_ban), HELPMOD_BAN_DURATION, MCB_ADD, now); | |
55 | helpmod_kick(tmpu->hchannels->hchan, tmpu, hban_get_reason(ptr)); | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | return ptr; | |
61 | } | |
62 | ||
63 | hban *hban_huser(void *target, const char* rsn, time_t exp, int now) | |
64 | { | |
65 | const char *banmask; | |
66 | huser *husr = (huser*)target; | |
67 | ||
68 | banmask = hban_ban_string(husr->real_user, HBAN_REAL_HOST); | |
69 | ||
70 | return hban_add(banmask, rsn, exp, now); | |
71 | } | |
72 | ||
73 | hban *hban_del(hban* hb, int now) | |
74 | { | |
75 | hban **ptr = &hbans; | |
76 | for (;*ptr;ptr = &(*ptr)->next) | |
77 | if (*ptr == hb) | |
78 | { | |
79 | hban *tmp = (*ptr)->next; | |
80 | ||
81 | { /* additional logic here */ | |
82 | hchannel *hchan; | |
83 | for (hchan = hchannels;hchan;hchan = hchan->next) | |
84 | helpmod_setban(hchan, bantostring((*ptr)->real_ban), 0, MCB_DEL, now); | |
85 | } | |
86 | ||
87 | freechanban((*ptr)->real_ban); /* evil ? */ | |
88 | freesstring((*ptr)->reason); | |
89 | free(*ptr); | |
90 | ||
91 | *ptr = tmp; | |
92 | return NULL; | |
93 | } | |
94 | return hb; | |
95 | } | |
96 | ||
97 | hban *hban_get(const char* pat) | |
98 | { | |
99 | hban *tmp = hbans; | |
100 | for (;tmp;tmp = tmp->next) | |
101 | if (!ci_strcmp(bantostring(tmp->real_ban),pat)) | |
102 | return tmp; | |
103 | ||
104 | return NULL; | |
105 | } | |
106 | ||
107 | int hban_count(void) | |
108 | { | |
109 | int count = 0; | |
110 | hban *hb = hbans; | |
111 | for (;hb;hb = hb->next) | |
112 | count++; | |
113 | return count; | |
114 | } | |
115 | ||
116 | hban *hban_check(nick *nck) | |
117 | { | |
118 | hban *ptr = hbans; | |
119 | for (;ptr;ptr = ptr->next) | |
120 | if (nickmatchban(nck, ptr->real_ban)) | |
121 | return ptr; | |
122 | return NULL; | |
123 | } | |
124 | ||
125 | void hban_remove_expired(void) | |
126 | { | |
127 | hban **ptr = &hbans; | |
128 | while (*ptr) | |
129 | if ((*ptr)->expiration <= time(NULL)) /* expired */ | |
130 | hban_del(*ptr, 0); | |
131 | else | |
132 | ptr = &(*ptr)->next; | |
133 | hcommit_modes(); | |
134 | } | |
135 | ||
136 | void hban_del_all(void) | |
137 | { | |
138 | while (hbans) | |
139 | hban_del(hbans, 1); | |
140 | } | |
141 | ||
142 | const char *hban_get_reason(hban* hb) | |
143 | { | |
144 | if (hb == NULL) | |
145 | return "error (please contact strutsi)"; | |
146 | else if (hb->reason == NULL) | |
147 | return "banned"; | |
148 | else | |
149 | return hb->reason->content; | |
150 | } | |
151 | ||
152 | const char *hban_ban_string(nick *nck, int banflags) | |
153 | { | |
154 | static char buffer[256]; | |
155 | buffer[0] = '\0'; | |
156 | ||
157 | if (banflags & HBAN_NICK) | |
158 | strcat(buffer, nck->nick); | |
159 | else | |
160 | strcat(buffer, "*"); | |
161 | ||
162 | strcat(buffer, "!"); | |
163 | ||
164 | if (banflags & HBAN_IDENT) | |
165 | strcat(buffer, nck->ident); | |
166 | else | |
167 | strcat(buffer, "*"); | |
168 | ||
169 | strcat(buffer, "@"); | |
170 | ||
171 | if (banflags & HBAN_HOST) | |
172 | strcat(buffer, nck->host->name->content); | |
173 | else if (banflags & HBAN_REAL_HOST) | |
174 | strcat(buffer, nck->host->name->content); | |
175 | else | |
176 | strcat(buffer, "*"); | |
177 | ||
178 | assert(strcmp(buffer, "*!*@*")); | |
179 | ||
180 | return buffer; | |
181 | } |