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 whowasrecs
[WW_MAXENTRIES
];
15 whowas
*whowas_fromnick(nick
*np
, int standalone
) {
18 struct irc_in_addr ipaddress_canonical
;
20 /* Create a new record. */
22 ww
= malloc(sizeof(whowas
));
24 ww
= &whowasrecs
[whowasoffset
];
26 whowasoffset
= (whowasoffset
+ 1) % WW_MAXENTRIES
;
29 memset(ww
, 0, sizeof(whowas
));
32 memset(wnp
, 0, sizeof(nick
));
33 strncpy(wnp
->nick
, np
->nick
, NICKLEN
+ 1);
34 wnp
->numeric
= np
->numeric
;
35 strncpy(wnp
->ident
, np
->ident
, USERLEN
+ 1);
37 wnp
->host
= newhost();
38 memset(wnp
->host
, 0, sizeof(host
));
39 wnp
->host
->name
= getsstring(np
->host
->name
->content
, HOSTLEN
);
41 wnp
->realname
= newrealname();
42 memset(wnp
->realname
, 0, sizeof(realname
));
43 wnp
->realname
->name
= getsstring(np
->realname
->name
->content
, REALLEN
);
44 wnp
->shident
= np
->shident
? getsstring(np
->shident
->content
, 512) : NULL
;
45 wnp
->sethost
= np
->sethost
? getsstring(np
->sethost
->content
, 512) : NULL
;
46 wnp
->opername
= np
->opername
? getsstring(np
->opername
->content
, 512) : NULL
;
47 wnp
->umodes
= np
->umodes
;
49 wnp
->auth
= newauthname();
50 memset(wnp
->auth
, 0, sizeof(authname
));
51 wnp
->auth
->userid
= np
->auth
->userid
;
52 strncpy(wnp
->auth
->name
, np
->auth
->name
, ACCOUNTLEN
+ 1);
53 wnp
->authname
= wnp
->auth
->name
;
55 wnp
->timestamp
= np
->timestamp
;
56 wnp
->accountts
= np
->accountts
;
57 wnp
->away
= np
->away
? getsstring(np
->away
->content
, 512) : NULL
;
59 memcpy(&wnp
->ipaddress
, &np
->ipaddress
, sizeof(struct irc_in_addr
));
61 ip_canonicalize_tunnel(&ipaddress_canonical
, &np
->ipaddress
);
62 wnp
->ipnode
= refnode(iptree
, &ipaddress_canonical
, PATRICIA_MAXBITS
);
64 wnp
->next
= (nick
*)ww
; /* Yuck. */
66 ww
->timestamp
= getnettime();
67 ww
->type
= WHOWAS_USED
;
72 void whowas_clean(whowas
*ww
) {
75 if (!ww
|| ww
->type
== WHOWAS_UNUSED
)
79 freesstring(np
->host
->name
);
81 freesstring(np
->realname
->name
);
82 freerealname(np
->realname
);
83 freesstring(np
->shident
);
84 freesstring(np
->sethost
);
85 freesstring(np
->opername
);
86 freeauthname(np
->auth
);
87 freesstring(np
->away
);
89 freesstring(ww
->reason
);
90 ww
->type
= WHOWAS_UNUSED
;
93 void whowas_free(whowas
*ww
) {
98 static void whowas_handlequitorkill(int hooknum
, void *arg
) {
101 char *reason
= args
[1];
106 /* Create a new record. */
107 ww
= whowas_fromnick(np
, 0);
109 if (hooknum
== HOOK_NICK_KILL
) {
110 if ((rreason
= strchr(reason
, ' '))) {
111 sprintf(resbuf
, "Killed%s", rreason
);
115 ww
->type
= WHOWAS_KILL
;
117 if (strncmp(reason
, "G-lined", 7) == 0)
118 ww
->type
= WHOWAS_KILL
;
120 ww
->type
= WHOWAS_QUIT
;
123 ww
->reason
= getsstring(reason
, WW_REASONLEN
);
126 static void whowas_handlerename(int hooknum
, void *arg
) {
129 char *oldnick
= args
[1];
133 ww
= whowas_fromnick(np
, 0);
134 ww
->type
= WHOWAS_RENAME
;
136 ww
->newnick
= getsstring(wnp
->nick
, NICKLEN
);
137 strncpy(wnp
->nick
, oldnick
, NICKLEN
+ 1);
140 whowas
*whowas_chase(const char *target
, int maxage
) {
148 for (i
= whowasoffset
+ WW_MAXENTRIES
- 1; i
>= whowasoffset
; i
--) {
149 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
151 if (ww
->type
== WHOWAS_UNUSED
)
156 if (ww
->timestamp
< now
- maxage
)
157 break; /* records are in timestamp order, we're done */
159 if (ircd_strcmp(wnp
->nick
, target
) == 0)
166 const char *whowas_format(whowas
*ww
) {
167 nick
*np
= &ww
->nick
;
168 static char buf
[512];
172 snprintf(hostmask
, sizeof(hostmask
), "%s!%s@%s%s%s [%s] (%s)",
173 np
->nick
, np
->ident
, np
->host
->name
->content
,
174 np
->auth
? "/" : "", np
->auth
? np
->authname
: "",
175 IPtostr(np
->ipaddress
),
176 printflags(np
->umodes
, umodeflags
));
177 strftime(timebuf
, sizeof(timebuf
), "%d/%m/%y %H:%M:%S", localtime(&(ww
->timestamp
)));
179 if (ww
->type
== WHOWAS_RENAME
)
180 snprintf(buf
, sizeof(buf
), "[%s] NICK %s r(%s) -> %s", timebuf
, hostmask
, np
->realname
->name
->content
, ww
->newnick
->content
);
182 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
);
187 unsigned int nextwhowasmarker() {
190 static unsigned int whowasmarker
=0;
195 /* If we wrapped to zero, zap the marker on all records */
196 for (i
= 0; i
< WW_MAXENTRIES
; i
++) {
197 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
208 registerhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
209 registerhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
210 registerhook(HOOK_NICK_RENAME
, whowas_handlerename
);
217 deregisterhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
218 deregisterhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
219 deregisterhook(HOOK_NICK_RENAME
, whowas_handlerename
);
221 for (i
= 0; i
< WW_MAXENTRIES
; i
++) {