3 #include "../core/hooks.h"
4 #include "../control/control.h"
5 #include "../irc/irc.h"
6 #include "../lib/irc_string.h"
7 #include "../lib/version.h"
12 whowas
*whowas_head
= NULL
, *whowas_tail
= NULL
;
15 whowas
*whowas_fromnick(nick
*np
) {
18 struct irc_in_addr ipaddress_canonical
;
20 /* Create a new record. */
21 ww
= malloc(sizeof(whowas
));
22 memset(ww
, 0, sizeof(whowas
));
25 memset(wnp
, 0, sizeof(nick
));
26 strncpy(wnp
->nick
, np
->nick
, NICKLEN
+ 1);
27 wnp
->numeric
= np
->numeric
;
28 strncpy(wnp
->ident
, np
->ident
, USERLEN
+ 1);
30 wnp
->host
= newhost();
31 memset(wnp
->host
, 0, sizeof(host
));
32 wnp
->host
->name
= getsstring(np
->host
->name
->content
, HOSTLEN
);
34 wnp
->realname
= newrealname();
35 memset(wnp
->realname
, 0, sizeof(realname
));
36 wnp
->realname
->name
= getsstring(np
->realname
->name
->content
, REALLEN
);
37 wnp
->shident
= np
->away
? getsstring(np
->shident
->content
, 512) : NULL
;
38 wnp
->sethost
= np
->away
? getsstring(np
->sethost
->content
, 512) : NULL
;
39 wnp
->opername
= np
->away
? getsstring(np
->opername
->content
, 512) : NULL
;
40 wnp
->umodes
= np
->umodes
;
42 wnp
->auth
= newauthname();
43 memset(wnp
->auth
, 0, sizeof(authname
));
44 wnp
->auth
->userid
= np
->auth
->userid
;
45 strncpy(wnp
->auth
->name
, np
->auth
->name
, ACCOUNTLEN
+ 1);
48 wnp
->authname
= wnp
->auth
->name
;
49 wnp
->timestamp
= np
->timestamp
;
50 wnp
->accountts
= np
->accountts
;
51 wnp
->away
= np
->away
? getsstring(np
->away
->content
, 512) : NULL
;
53 memcpy(&wnp
->ipaddress
, &np
->ipaddress
, sizeof(struct irc_in_addr
));
55 ip_canonicalize_tunnel(&ipaddress_canonical
, &np
->ipaddress
);
56 wnp
->ipnode
= refnode(iptree
, &ipaddress_canonical
, PATRICIA_MAXBITS
);
58 wnp
->next
= (nick
*)ww
; /* Yuck. */
61 ww
->timestamp
= getnettime();
66 void whowas_linkrecord(whowas
*ww
) {
68 whowas_head
->prev
= ww
;
70 ww
->next
= whowas_head
;
81 void whowas_unlinkrecord(whowas
*ww
) {
83 whowas_tail
= ww
->prev
;
86 ww
->prev
->next
= NULL
;
88 whowas_head
= ww
->prev
;
93 void whowas_free(whowas
*ww
) {
101 freesstring(np
->host
->name
);
103 freesstring(np
->realname
->name
);
104 freerealname(np
->realname
);
105 freesstring(np
->shident
);
106 freesstring(np
->sethost
);
107 freesstring(np
->opername
);
108 freeauthname(np
->auth
);
109 freesstring(np
->away
);
111 freesstring(ww
->reason
);
115 static void whowas_cleanup(void) {
121 /* Clean up old records. */
122 while (whowas_tail
&& (whowas_tail
->timestamp
< now
- WW_MAXAGE
|| whowas_count
>= WW_MAXENTRIES
)) {
124 whowas_unlinkrecord(ww
);
129 static void whowas_handlequitorkill(int hooknum
, void *arg
) {
132 char *reason
= args
[1];
139 /* Create a new record. */
140 ww
= whowas_fromnick(np
);
142 if (hooknum
== HOOK_NICK_KILL
) {
143 if ((rreason
= strchr(reason
, ' '))) {
144 sprintf(resbuf
, "Killed%s", rreason
);
148 ww
->type
= WHOWAS_KILL
;
150 ww
->type
= WHOWAS_QUIT
;
152 ww
->reason
= getsstring(reason
, WW_REASONLEN
);
154 whowas_linkrecord(ww
);
157 static void whowas_handlerename(int hooknum
, void *arg
) {
160 char *oldnick
= args
[1];
166 ww
= whowas_fromnick(np
);
167 ww
->type
= WHOWAS_RENAME
;
169 ww
->newnick
= getsstring(wnp
->nick
, NICKLEN
);
170 strncpy(wnp
->nick
, oldnick
, NICKLEN
+ 1);
172 whowas_linkrecord(ww
);
175 whowas
*whowas_chase(const char *nick
, int maxage
) {
181 for (ww
= whowas_head
; ww
; ww
= ww
->next
) {
182 if (ww
->timestamp
< now
- maxage
)
183 break; /* records are in timestamp order, we're done */
185 if (ircd_strcmp(ww
->nick
->nick
, nick
) == 0)
192 const char *whowas_format(whowas
*ww
) {
194 static char buf
[512];
198 snprintf(hostmask
, sizeof(hostmask
), "%s!%s@%s%s%s [%s]",
199 np
->nick
, np
->ident
, np
->host
->name
->content
,
200 np
->auth
? "/" : "", np
->auth
? np
->authname
: "",
201 IPtostr(np
->p_ipaddr
));
202 strftime(timebuf
, sizeof(timebuf
), "%d/%m/%y %H:%M:%S", localtime(&(ww
->timestamp
)));
204 if (ww
->type
== WHOWAS_RENAME
)
205 snprintf(buf
, sizeof(buf
), "[%s] NICK %s r(%s) -> %s", timebuf
, hostmask
, np
->realname
->name
->content
, ww
->newnick
->content
);
207 snprintf(buf
, sizeof(buf
), "[%s] %s %s r(%s): %s", timebuf
, (ww
->type
== WHOWAS_QUIT
) ? "QUIT" : "KILL", hostmask
, np
->realname
->name
->content
, ww
->reason
->content
);
212 unsigned int nextwhowasmarker() {
214 static unsigned int whowasmarker
=0;
219 /* If we wrapped to zero, zap the marker on all records */
220 for (ww
= whowas_head
; ww
; ww
= ww
->next
)
229 registerhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
230 registerhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
231 registerhook(HOOK_NICK_RENAME
, whowas_handlerename
);
235 deregisterhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
236 deregisterhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
237 deregisterhook(HOOK_NICK_RENAME
, whowas_handlerename
);
240 whowas_unlinkrecord(whowas_head
);