]> jfr.im git - irc/quakenet/newserv.git/blame - chanserv/chanserv_protect.c
A4STATS: remove E style escapes and switch to createtable for indices
[irc/quakenet/newserv.git] / chanserv / chanserv_protect.c
CommitLineData
c86edd1d
Q
1/*
2 * Nick protection system for the chanserv
3 */
4
df196dd2 5#include <stdio.h>
c86edd1d
Q
6#include "chanserv.h"
7#include "../core/schedule.h"
8#include "../localuser/localuser.h"
e9f04cff 9#include "../lib/irc_string.h"
7f32dbdf
P
10#include "../lib/version.h"
11
12MODULE_VERSION(QVERSION)
c86edd1d
Q
13
14#define PROTECTTIME 60 /* How long you have to renick if you encroach.. */
15
16void csp_handlenick(int hooknum, void *arg);
2fdef282 17void csp_handlerename(int hooknum, void *arg);
c86edd1d
Q
18void csp_freenick(int hooknum, void *arg);
19void csp_timerfunc (void *arg);
20int csp_doclaimnick(void *source, int cargc, char **cargv);
21
22void _init() {
23 registerhook(HOOK_NICK_NEWNICK, csp_handlenick);
2fdef282 24 registerhook(HOOK_NICK_RENAME, csp_handlerename);
c86edd1d
Q
25
26 registerhook(HOOK_NICK_NEWNICK, csp_freenick);
27 registerhook(HOOK_NICK_ACCOUNT, csp_freenick);
28
7be5f783 29 chanservaddcommand("claimnick", QCMD_STAFF, 0, csp_doclaimnick, "Reclaims your nickname if it has been stolen.","");
c86edd1d
Q
30}
31
32void _fini() {
33 reguser *rup;
34 int i;
35
36 deregisterhook(HOOK_NICK_NEWNICK, csp_handlenick);
2fdef282 37 deregisterhook(HOOK_NICK_RENAME, csp_handlerename);
c86edd1d
Q
38
39 deregisterhook(HOOK_NICK_NEWNICK, csp_freenick);
40 deregisterhook(HOOK_NICK_ACCOUNT, csp_freenick);
41
42 chanservremovecommand("claimnick", csp_doclaimnick);
43 deleteallschedules(csp_timerfunc);
44
45 /* Kill off all fakeusers too */
46 for (i=0;i<REGUSERHASHSIZE;i++)
47 for (rup=regusernicktable[i];rup;rup=rup->nextbyname)
48 if (rup->fakeuser) {
49 if (getnickbynick(rup->username) == rup->fakeuser) {
50 deregisterlocaluser(rup->fakeuser,NULL);
51 }
52 rup->fakeuser=NULL;
53 }
54}
55
2fdef282
CP
56void csp_handlerename(int hooknum, void *arg) {
57 void **harg = (void **)arg;
58 csp_handlenick(hooknum, harg[0]);
59}
60
c86edd1d
Q
61void csp_handlenick(int hooknum, void *arg) {
62 nick *np=arg;
63 reguser *rup;
9cf19945 64 char userhostbuf[USERLEN+HOSTLEN+2];
c86edd1d
Q
65
66 /* Check that it's a protected nick */
67 if (!(rup=findreguserbynick(np->nick)) || !UIsProtect(rup))
68 return;
69
70 /* If we already had a timer running, this means someone renamed off and back.
71 * Clear the old timer.
72 */
73 if (rup->checkshd) {
74 deleteschedule(rup->checkshd, csp_timerfunc, rup);
75 rup->checkshd=NULL;
76 }
77
78 /* If they're an oper, or the legal user of the nick, it's OK */
79 /* Also, don't warn them if they are a fakeuser */
80 if (getreguserfromnick(np)==rup) {
81 rup->stealcount=0;
82 return;
83 }
9cf19945 84
85 if (rup->lastuserhost) {
86 sprintf(userhostbuf,"%s@%s",np->ident,np->host->name->content);
87 if (!ircd_strcmp(userhostbuf, rup->lastuserhost->content))
88 return;
89 }
c86edd1d
Q
90
91 if (IsOper(np) || homeserver(np->numeric)==mylongnum)
92 return;
93
94 /* OK, at this stage we've established that:
95 * - This is a protected nick
96 * - The person using it isn't authed as the correct user
97 */
98
99 /* Send warning */
100 chanservstdmessage(np, QM_PROTECTEDNICK, rup->username);
101
102 /* Schedule checkup */
103 rup->checkshd=scheduleoneshot(time(NULL)+PROTECTTIME, csp_timerfunc, rup);
104}
105
106void csp_freenick(int hooknum, void *arg) {
107 nick *np=arg;
108 reguser *rup=getreguserfromnick(np);
109
110 if (!rup || !UIsProtect(rup))
111 return;
112
113 if (rup->checkshd) {
114 deleteschedule(rup->checkshd, csp_timerfunc, rup);
115 rup->checkshd=NULL;
116 }
117
118 rup->stealcount=0;
119 if (rup->fakeuser) {
120 /* Before killing, check the user is valid */
121 if (getnickbynick(rup->username) == rup->fakeuser) {
122 /* Free up the fakeuser */
123 deregisterlocaluser(rup->fakeuser, NULL);
124 rup->fakeuser=NULL;
125 chanservstdmessage(np, QM_NICKWASFAKED, rup->username);
126 }
127 }
128}
129
130void csp_timerfunc (void *arg) {
131 reguser *rup=arg;
132 nick *np;
133
134 rup->checkshd=NULL;
135
136 /* Check that we still have a user with this name and
137 * that they're not now opered or authed.. */
138 if (!(np=getnickbynick(rup->username)) || IsOper(np) || (getreguserfromnick(np)==rup))
139 return;
140
141 /* KILL! KILL! KILL! */
142 killuser(chanservnick, np, "Protected nick.");
143
144 rup->stealcount++;
145
146 /* If it's been stolen too much, create a fake user.. */
147 if (rup->stealcount >= 3) {
148 rup->fakeuser=registerlocaluser(rup->username, "reserved", "nick", "Protected nick.", NULL, UMODE_INV, NULL);
149 }
150}
151
152int csp_doclaimnick(void *source, int cargc, char **cargv) {
153 nick *sender=source;
154 reguser *rup=getreguserfromnick(sender);
155 nick *target;
156
157 if (!rup)
158 return CMD_ERROR;
159
160 if (!UIsProtect(rup)) {
161 chanservstdmessage(sender, QM_NOTPROTECTED,rup->username);
162 return CMD_ERROR;
163 }
164
165 if (!(target=getnickbynick(rup->username))) {
166 chanservstdmessage(sender, QM_UNKNOWNUSER, rup->username);
167 return CMD_ERROR;
168 }
169
170 if (getreguserfromnick(target)==rup) {
171 chanservstdmessage(sender, QM_SAMEAUTH, target->nick, rup->username);
172 return CMD_ERROR;
173 }
174
175 if (rup->fakeuser==target) {
176 deregisterlocaluser(rup->fakeuser, NULL);
177 rup->fakeuser=NULL;
178 } else {
179 if (!IsOper(target))
180 killuser(chanservnick, target, "Protected nick.");
181 }
182
183 chanservstdmessage(sender, QM_DONE);
184 return CMD_OK;
185}