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