]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/authtracker/authtracker_hooks.c
Some more tidying of authtracker and related things.
[irc/quakenet/newserv.git] / chanserv / authtracker / authtracker_hooks.c
1 /* authtracker module: Tracks when users come and go... */
2
3 #include "authtracker.h"
4 #include "../chanserv.h"
5 #include "../../core/hooks.h"
6 #include "../../nick/nick.h"
7 #include "../../lib/irc_string.h"
8
9 #include <time.h>
10 #include <string.h>
11
12 #define NTERFACER_AUTH "nterfacer"
13
14 /* OK, we need to deal separately with users who have definitely gone (QUIT
15 * or KILL) and those who has mysteriously vanished (lost in netsplit).
16 * There are hooks for all these things, but sadly QUIT and KILL also
17 * trigger the generic "lost nick" hook. Luckily, these things always
18 * happen one after the other without any intervening quits, so we will keep
19 * track of the last QUIT or KILL details and ignore the subsequent
20 * LOSTNICK. */
21
22 unsigned int at_lastnum;
23 time_t at_lastauthts;
24 unsigned long at_lastuserid;
25
26 unsigned long at_getuserid(nick *np) {
27 reguser *rup;
28
29 /* If they are not +r, forget it. */
30 if (!IsAccount(np))
31 return 0;
32
33 /* Ignore that pesky nterfacer */
34 #ifdef NTERFACER_AUTH
35 if (!ircd_strcmp(np->authname, NTERFACER_AUTH))
36 return 0;
37 #endif
38
39 /* Use the userid from authext only. */
40 if (np->auth)
41 return np->auth->userid;
42
43 Error("authtracker",ERR_WARNING,"Unable to get userID from IsAccount() user %s",np->nick);
44 return 0;
45 }
46
47 void at_handlequitorkill(int hooknum, void *arg) {
48 void **args=arg;
49 nick *np=args[0];
50 char *reason=args[1];
51 char *rreason;
52 unsigned long userid;
53
54 /* Ignore unauthed users, or those with no accountts */
55 if (!(userid=at_getuserid(np)) || !np->accountts)
56 return;
57
58 at_lastuserid=userid;
59 at_lastauthts=np->accountts;
60 at_lastnum=np->numeric;
61
62 if (hooknum==HOOK_NICK_KILL && (rreason=strchr(reason,'@')))
63 reason=rreason;
64
65 at_logquit(userid, np->accountts, time(NULL), reason);
66 }
67
68 void at_handlelostnick(int hooknum, void *arg) {
69 nick *np=arg;
70 unsigned long userid;
71
72 if (!(userid=at_getuserid(np)) || !np->accountts)
73 return;
74
75 /* Don't do this for a user that just left normally (see above) */
76 if ((userid==at_lastuserid) && (np->accountts == at_lastauthts) && (np->numeric==at_lastnum))
77 return;
78
79 at_lostnick(np->numeric, userid, np->accountts, time(NULL), AT_NETSPLIT);
80 }
81
82 void at_newnick(int hooknum, void *arg) {
83 nick *np=arg;
84 unsigned long userid;
85
86 /* Ignore unauthed users, or those with no TS */
87 if (!(userid=at_getuserid(np)) || !np->accountts)
88 return;
89
90 /* OK, we've seen an authed user appear. If it's a known ghost we can zap it */
91 if (!at_foundnick(np->numeric, userid, np->accountts)) {
92 /* If we didn't zap a ghost then it's a new session */
93 at_lognewsession(userid, np);
94 }
95 }
96
97 void at_serverlinked(int hooknum, void *arg) {
98 unsigned long servernum=(unsigned long)arg;
99
100 at_serverback(servernum);
101 }
102
103 void at_hookinit() {
104 registerhook(HOOK_NICK_QUIT, at_handlequitorkill);
105 registerhook(HOOK_NICK_KILL, at_handlequitorkill);
106 registerhook(HOOK_NICK_LOSTNICK, at_handlelostnick);
107 registerhook(HOOK_NICK_NEWNICK, at_newnick);
108 registerhook(HOOK_NICK_ACCOUNT, at_newnick);
109 registerhook(HOOK_SERVER_LINKED, at_serverlinked);
110 }
111
112 void at_hookfini() {
113 deregisterhook(HOOK_NICK_QUIT, at_handlequitorkill);
114 deregisterhook(HOOK_NICK_KILL, at_handlequitorkill);
115 deregisterhook(HOOK_NICK_LOSTNICK, at_handlelostnick);
116 deregisterhook(HOOK_NICK_NEWNICK, at_newnick);
117 deregisterhook(HOOK_NICK_ACCOUNT, at_newnick);
118 deregisterhook(HOOK_SERVER_LINKED, at_serverlinked);
119 }