]>
jfr.im git - irc/quakenet/newserv.git/blob - fakeusers/fakeusers.c
2 * NOperserv Fakeuser module
4 * Allows fakeusers to be added so as to block nicks, for example.
6 * Copyright (c) Tim Gordon 2006.
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"
27 typedef struct fakeuser
{
28 char nick
[NICKLEN
+ 1];
29 char ident
[USERLEN
+ 1];
30 char host
[HOSTLEN
+ 1];
31 char realname
[REALLEN
+ 1];
34 struct fakeuser
*next
;
37 fakeuser
*fakeuserlist
= NULL
;
39 static void fakeuser_cleanup();
40 static int fakeuser_loaddb();
41 static void fakeusers_load(const DBAPIResult
*res
, void *arg
);
42 static void fakeuser_handler(nick
*user
, int command
, void **params
);
43 static int fakeadd(void *sender
, int cargc
, char **cargv
);
44 static int fakelist(void *sender
, int cargc
, char **cargv
);
45 static int fakekill(void *sender
, int cargc
, char **cargv
);
46 static void schedulefakeuser(void *arg
);
47 static fakeuser
*findfakeuserbynick(char *nick
);
48 static void fake_remove(char *nickname
);
49 static fakeuser
*fake_add(fakeuser
*details
);
51 static DBAPIConn
*nofudb
;
53 void fakeuser_cleanup() {
57 for (fake
= fakeuserlist
; fake
; fake
= next
) {
58 deregisterlocaluser(fake
->user
, "Signing off");
66 int fakeuser_loaddb() {
68 nofudb
= dbapi2open(DBAPI2_DEFAULT
, "fakeusers");
71 Error("fakeuser", ERR_STOP
, "Could not connect to database.");
76 nofudb
->createtable(nofudb
, NULL
, NULL
,
78 "nick VARCHAR(?) NOT NULL,"
79 "ident VARCHAR(?) NOT NULL,"
80 "host VARCHAR(?) NOT NULL,"
81 "realname VARCHAR(?) NOT NULL,"
82 "PRIMARY KEY (nick))", "Tdddd", "fakeusers", NICKLEN
, USERLEN
, HOSTLEN
, REALLEN
);
84 nofudb
->query(nofudb
, fakeusers_load
, NULL
,
85 "SELECT nick, ident, host, realname FROM ?", "T", "fakeusers");
90 void fakeusers_load(const DBAPIResult
*res
, void *arg
) {
97 Error("fakeuser", ERR_ERROR
, "Error loading fakeuser list.");
102 while (res
->next(res
)) {
103 strlcpy(fakeuser
.nick
, res
->get(res
, 0), NICKLEN
+ 1);
104 strlcpy(fakeuser
.ident
, res
->get(res
, 1), USERLEN
+ 1);
105 strlcpy(fakeuser
.host
, res
->get(res
, 2), HOSTLEN
+ 1);
106 strlcpy(fakeuser
.realname
, res
->get(res
, 3), REALLEN
+ 1);
110 scheduleoneshot(time(NULL
) + 1, schedulefakeuser
, NULL
);
114 nick
*register_fakeuseronnet(fakeuser
*details
) {
117 if ((user
= getnickbynick(details
->nick
)) && (IsOper(user
) || IsService(user
) || IsXOper(user
))) {
121 return registerlocaluser(details
->nick
, details
->ident
, details
->host
, details
->realname
,
122 NULL
, UMODE_INV
| UMODE_DEAF
, &fakeuser_handler
);
125 fakeuser
*fake_add(fakeuser
*details
) {
128 newfake
= malloc(sizeof(fakeuser
));
134 memcpy(newfake
, details
, sizeof(fakeuser
));
136 newfake
->user
= NULL
;
137 newfake
->lastkill
= 0;
139 newfake
->next
= fakeuserlist
;
140 fakeuserlist
= newfake
;
144 void fake_remove(char *nickname
) {
145 fakeuser
*fake
, *prev
;
147 for (fake
= fakeuserlist
; fake
; fake
= fake
->next
) {
148 if (!ircd_strcmp(nickname
, fake
->nick
)) {
149 if (fake
== fakeuserlist
)
150 fakeuserlist
= fake
->next
;
152 prev
->next
= fake
->next
;
155 deregisterlocaluser(fake
->user
, "Signing off");
165 void fakeuser_handler(nick
*user
, int command
, void **params
) {
166 if (command
== LU_KILLED
) {
168 time_t timenow
= time(NULL
);
170 item
= findfakeuserbynick(user
->nick
);
173 controlwall(NO_OPER
, NL_FAKEUSERS
, "Error: A fakeuser was killed, but wasn't found in the list");
174 Error("fakeuser", ERR_ERROR
, "A fakeuser was killed, but wasn't found in the list");
180 if (timenow
- item
->lastkill
< KILL_TIME
) {
181 controlwall(NO_OPER
, NL_FAKEUSERS
, "Fake user %s!%s@%s (%s) KILL'ed twice under in %d seconds. Removing.", item
->nick
, item
->ident
, item
->host
, item
->realname
, KILL_TIME
);
182 nofudb
->squery(nofudb
, "DELETE FROM ? WHERE nick = ?", "Ts", "fakeusers", item
->nick
);
183 fake_remove(item
->nick
);
187 item
->lastkill
= timenow
;
189 scheduleoneshot(time(NULL
) + KILL_WAIT
, schedulefakeuser
, item
);
193 int fakeadd(void *sender
, int cargc
, char **cargv
) {
200 fake
= findfakeuserbynick(cargv
[0]);
203 controlreply(sender
, "Fake User with nick %s already found", cargv
[0]);
207 strlcpy(newfake
.nick
, cargv
[0], NICKLEN
+ 1);
210 strlcpy(newfake
.realname
, cargv
[0], REALLEN
+ 1);
212 strlcpy(newfake
.realname
, cargv
[3], REALLEN
+ 1);
215 strlcpy(newfake
.host
, cargv
[0], NICKLEN
+ 1);
216 strlcat(newfake
.host
, ".fakeusers.quakenet.org", HOSTLEN
+ 1);
218 strlcpy(newfake
.host
, cargv
[2], HOSTLEN
+ 1);
221 strlcpy(newfake
.ident
, cargv
[0], USERLEN
+ 1);
223 strlcpy(newfake
.ident
, cargv
[1], USERLEN
+ 1);
225 fake
= fake_add(&newfake
);
231 nofudb
->squery(nofudb
, "INSERT INTO ? (nick, ident, host, realname) VALUES (?,?,?,?)", "Tssss", "fakeusers", fake
->nick
, fake
->ident
, fake
->host
, fake
->realname
);
232 controlreply(sender
, "Added fake user %s", fake
->nick
);
233 controlwall(NO_OPER
, NL_FAKEUSERS
, "Fake user %s!%s@%s (%s) added by %s/%s", fake
->nick
, fake
->ident
,
234 fake
->host
, fake
->realname
, ((nick
*)sender
)->nick
, ((nick
*)sender
)->authname
);
236 scheduleoneshot(time(NULL
) + 1, schedulefakeuser
, fake
);
240 int fakelist(void *sender
, int cargc
, char **cargv
) {
242 int fakeusercount
= 0;
244 for (fake
= fakeuserlist
; fake
; fake
= fake
->next
) {
246 controlreply(sender
, "%s!%s@%s (%s) - RECONNECTING", fake
->nick
, fake
->ident
,
247 fake
->host
, fake
->realname
);
249 controlreply(sender
, "%s!%s@%s (%s)", fake
->nick
, fake
->ident
,
250 fake
->host
, fake
->realname
);
255 controlreply(sender
, "%d fakeusers are currently connected", fakeusercount
);
259 int fakekill(void *sender
, int cargc
, char **cargv
) {
265 fake
= findfakeuserbynick(cargv
[0]);
268 controlreply(sender
, "No Fake User with nick %s found", cargv
[0]);
272 nofudb
->squery(nofudb
, "DELETE FROM ? WHERE nick = ?", "Ts", "fakeusers", fake
->nick
);
273 controlreply(sender
, "Killed fake user %s", fake
->nick
);
274 controlwall(NO_OPER
, NL_FAKEUSERS
, "Fake user %s!%s@%s (%s) removed by %s/%s", fake
->nick
, fake
->ident
,
275 fake
->host
, fake
->realname
, ((nick
*)sender
)->nick
, ((nick
*)sender
)->authname
);
277 fake_remove(cargv
[0]);
281 void schedulefakeuser(void *arg
) {
284 for (fake
= fakeuserlist
; fake
; fake
= fake
->next
)
286 fake
->user
= register_fakeuseronnet(fake
);
289 fakeuser
*findfakeuserbynick(char *nick
) {
292 for (fake
= fakeuserlist
; fake
; fake
= fake
->next
)
293 if (!ircd_strcmp(nick
, fake
->nick
))
300 if (!fakeuser_loaddb()) {
301 Error("fakeuser", ERR_FATAL
, "Cannot load database");
305 registercontrolhelpcmd("fakeuser", NO_OPER
, 4, &fakeadd
, "Usage: FAKEUSER nick <ident> <host> <realname>\nCreates a fake user.");
306 registercontrolhelpcmd("fakelist", NO_OPER
, 0, &fakelist
, "Usage: FAKELIST\nLists all fake users.");
307 registercontrolhelpcmd("fakekill", NO_OPER
, 2, &fakekill
, "Usage: FAKEKILL nick\nRemoves a fake user");
312 deleteallschedules(schedulefakeuser
);
313 deregistercontrolcmd("fakeuser", &fakeadd
);
314 deregistercontrolcmd("fakelist", &fakelist
);
315 deregistercontrolcmd("fakekill", &fakekill
);