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