]> jfr.im git - irc/quakenet/newserv.git/blob - fakeusers/fakeusers.c
Merge chanserv-live into default.
[irc/quakenet/newserv.git] / fakeusers / fakeusers.c
1 /*
2 * NOperserv Fakeuser module
3 *
4 * Allows fakeusers to be added so as to block nicks, for example.
5 *
6 * Copyright (c) Tim Gordon 2006.
7 */
8
9 #include "../core/schedule.h"
10 #include "../localuser/localuser.h"
11 #include "../localuser/localuserchannel.h"
12 #include "../irc/irc_config.h"
13 #include "../lib/irc_string.h"
14 #include "../control/control.h"
15 #include "../channel/channel.h"
16 #include "../dbapi2/dbapi2.h"
17 #include "../lib/strlfunc.h"
18 #include "../lib/version.h"
19 #include <string.h>
20 #include <stdlib.h>
21
22 MODULE_VERSION("");
23
24 #define KILL_WAIT 10
25 #define KILL_TIME 60
26
27 typedef struct fakeuser {
28 char nick[NICKLEN + 1];
29 char ident[USERLEN + 1];
30 char host[HOSTLEN + 1];
31 char realname[REALLEN + 1];
32 nick *user;
33 time_t lastkill;
34 struct fakeuser *next;
35 } fakeuser;
36
37 static fakeuser *fakeuserlist;
38 static DBAPIConn *nofudb;
39
40 static void reconnectfakeuser(void *arg);
41
42 static fakeuser *findfakeuserbynick(char *nick) {
43 fakeuser *fake;
44
45 for (fake = fakeuserlist; fake; fake = fake->next)
46 if (!ircd_strcmp(nick, fake->nick))
47 return fake;
48
49 return NULL;
50 }
51
52 static void fake_free(fakeuser *fake) {
53 fakeuser **pnext;
54
55 if (fake->user)
56 deregisterlocaluser(fake->user, "Signing off");
57
58 deleteschedule(NULL, &reconnectfakeuser, fake);
59
60 for (pnext = &fakeuserlist; *pnext; pnext = &((*pnext)->next)) {
61 if (*pnext == fake) {
62 *pnext = fake->next;
63 break;
64 }
65 }
66
67 free(fake);
68 }
69
70 static void fake_remove(fakeuser *fake) {
71 nofudb->squery(nofudb, "DELETE FROM ? WHERE nick = ?", "Ts", "fakeusers", fake->nick);
72
73 fake_free(fake);
74 }
75
76 static void fakeuser_handler(nick *user, int command, void **params) {
77 if (command == LU_KILLED) {
78 fakeuser *fake;
79 time_t timenow = time(NULL);
80
81 fake = findfakeuserbynick(user->nick);
82
83 if (!fake) {
84 controlwall(NO_OPER, NL_FAKEUSERS, "Error: A fakeuser was killed, but wasn't found in the list");
85 Error("fakeuser", ERR_ERROR, "A fakeuser was killed, but wasn't found in the list");
86 return;
87 }
88
89 fake->user = NULL;
90
91 if (timenow - fake->lastkill < KILL_TIME) {
92 controlwall(NO_OPER, NL_FAKEUSERS, "Fake user %s!%s@%s (%s) KILL'ed twice under in %d seconds. Removing.",
93 fake->nick, fake->ident, fake->host, fake->realname, KILL_TIME);
94 fake_remove(fake);
95 return;
96 }
97
98 fake->lastkill = timenow;
99
100 scheduleoneshot(time(NULL) + KILL_WAIT, &reconnectfakeuser, fake);
101 }
102 }
103
104 static void reconnectfakeuser(void *arg) {
105 fakeuser *fake = arg;
106 nick *user;
107
108 if (fake->user)
109 return;
110
111 if ((user = getnickbynick(fake->nick)) && (IsOper(user) || IsService(user) || IsXOper(user))) {
112 fake_remove(fake);
113 return;
114 }
115
116 fake->user = registerlocaluser(fake->nick, fake->ident, fake->host, fake->realname,
117 NULL, UMODE_INV | UMODE_DEAF, &fakeuser_handler);
118 }
119
120 static fakeuser *fake_add(const char *nick, const char *ident, const char *host, const char *realname) {
121 fakeuser *fake;
122
123 fake = malloc(sizeof(fakeuser));
124
125 if (!fake)
126 return NULL;
127
128 strlcpy(fake->nick, nick, NICKLEN + 1);
129 strlcpy(fake->ident, ident, USERLEN + 1);
130 strlcpy(fake->host, host, HOSTLEN + 1);
131 strlcpy(fake->realname, realname, REALLEN + 1);
132
133 fake->user = NULL;
134 fake->lastkill = 0;
135
136 fake->next = fakeuserlist;
137 fakeuserlist = fake;
138
139 scheduleoneshot(time(NULL) + 1, reconnectfakeuser, fake);
140
141 return fake;
142 }
143
144 static fakeuser *fake_create(const char *nick, const char *ident, const char *host, const char *realname) {
145 fakeuser *fake;
146
147 fake = fake_add(nick, ident, host, realname);
148
149 if (!fake)
150 return NULL;
151
152 nofudb->squery(nofudb, "INSERT INTO ? (nick, ident, host, realname) VALUES (?,?,?,?)", "Tssss", "fakeusers",
153 fake->nick, fake->ident, fake->host, fake->realname);
154
155 return fake;
156 }
157
158 static void fakeusers_load(const DBAPIResult *res, void *arg) {
159 if (!res)
160 return;
161
162 if (!res->success) {
163 Error("fakeuser", ERR_ERROR, "Error loading fakeuser list.");
164 res->clear(res);
165 return;
166 }
167
168 while (res->next(res))
169 fake_add(res->get(res, 0), res->get(res, 1), res->get(res, 2), res->get(res, 3));
170
171 res->clear(res);
172 }
173
174 static int fakeuser_loaddb() {
175 if (!nofudb) {
176 nofudb = dbapi2open(DBAPI2_DEFAULT, "fakeusers");
177
178 if (!nofudb) {
179 Error("fakeuser", ERR_STOP, "Could not connect to database.");
180 return 0;
181 }
182 }
183
184 nofudb->createtable(nofudb, NULL, NULL,
185 "CREATE TABLE ? ("
186 "nick VARCHAR(?) NOT NULL,"
187 "ident VARCHAR(?) NOT NULL,"
188 "host VARCHAR(?) NOT NULL,"
189 "realname VARCHAR(?) NOT NULL,"
190 "PRIMARY KEY (nick))", "Tdddd", "fakeusers", NICKLEN, USERLEN, HOSTLEN, REALLEN);
191
192 nofudb->query(nofudb, fakeusers_load, NULL,
193 "SELECT nick, ident, host, realname FROM ?", "T", "fakeusers");
194
195 return 1;
196 }
197
198 static int fakeadd(void *source, int cargc, char **cargv) {
199 nick *sender = source;
200 fakeuser *fake;
201 char *nick, *ident, *realname;
202 char host[HOSTLEN + 1];
203
204 if (cargc == 0)
205 return CMD_USAGE;
206
207 fake = findfakeuserbynick(cargv[0]);
208
209 if (fake) {
210 controlreply(sender, "Fake User with nick %s already found", cargv[0]);
211 return CMD_ERROR;
212 }
213
214 nick = cargv[0];
215
216 if (cargc < 4)
217 realname = cargv[0];
218 else
219 realname = cargv[3];
220
221 if (cargc < 3) {
222 strlcpy(host, cargv[0], NICKLEN + 1);
223 strlcat(host, ".fakeusers.quakenet.org", HOSTLEN + 1);
224 } else
225 strlcpy(host, cargv[2], HOSTLEN + 1);
226
227 if (cargc < 2)
228 ident = cargv[0];
229 else
230 ident = cargv[1];
231
232 fake = fake_create(nick, ident, host, realname);
233
234 if (!fake)
235 return CMD_ERROR;
236
237 controlreply(sender, "Added fake user %s", fake->nick);
238 controlwall(NO_OPER, NL_FAKEUSERS, "%s added fake user: %s!%s@%s (%s)", controlid(sender),
239 fake->nick, fake->ident, fake->host, fake->realname);
240
241 scheduleoneshot(time(NULL) + 1, &reconnectfakeuser, fake);
242
243 return CMD_OK;
244 }
245
246 static int fakelist(void *sender, int cargc, char **cargv) {
247 fakeuser *fake;
248 int fakeusercount = 0;
249
250 for (fake = fakeuserlist; fake; fake = fake->next) {
251 if (!fake->user)
252 controlreply(sender, "%s!%s@%s (%s) - RECONNECTING", fake->nick, fake->ident,
253 fake->host, fake->realname);
254 else
255 controlreply(sender, "%s!%s@%s (%s)", fake->nick, fake->ident,
256 fake->host, fake->realname);
257
258 fakeusercount++;
259 }
260
261 controlreply(sender, "%d fakeusers are currently connected", fakeusercount);
262
263 return CMD_OK;
264 }
265
266 static int fakekill(void *sender, int cargc, char **cargv) {
267 fakeuser *fake;
268
269 if (cargc == 0)
270 return CMD_USAGE;
271
272 fake = findfakeuserbynick(cargv[0]);
273
274 if (!fake) {
275 controlreply(sender, "No Fake User with nick %s found", cargv[0]);
276 return CMD_ERROR;
277 }
278
279 controlreply(sender, "Killed fake user %s", fake->nick);
280 controlwall(NO_OPER, NL_FAKEUSERS, "Fake user %s!%s@%s (%s) removed by %s/%s", fake->nick, fake->ident,
281 fake->host, fake->realname, ((nick *)sender)->nick, ((nick *)sender)->authname);
282
283 fake_remove(fake);
284
285 return CMD_OK;
286 }
287
288 void _init() {
289 if (!fakeuser_loaddb()) {
290 Error("fakeuser", ERR_FATAL, "Cannot load database");
291 return;
292 }
293
294 registercontrolhelpcmd("fakeuser", NO_OPER, 4, &fakeadd, "Usage: FAKEUSER nick <ident> <host> <realname>\nCreates a fake user.");
295 registercontrolhelpcmd("fakelist", NO_OPER, 0, &fakelist, "Usage: FAKELIST\nLists all fake users.");
296 registercontrolhelpcmd("fakekill", NO_OPER, 2, &fakekill, "Usage: FAKEKILL nick\nRemoves a fake user");
297 }
298
299 void _fini() {
300 fakeuser *fake, *next;
301
302 for (fake = fakeuserlist; fake; fake = next) {
303 next = fake->next;
304 fake_free(fake);
305 }
306
307 deregistercontrolcmd("fakeuser", &fakeadd);
308 deregistercontrolcmd("fakelist", &fakelist);
309 deregistercontrolcmd("fakekill", &fakekill);
310 }