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
;
21 /* Create a new record. */
23 ww
= malloc(sizeof(whowas
));
25 ww
= &whowasrecs
[whowasoffset
];
27 whowasoffset
= (whowasoffset
+ 1) % WW_MAXENTRIES
;
30 memset(ww
, 0, sizeof(whowas
));
33 memset(wnp
, 0, sizeof(nick
));
34 strncpy(wnp
->nick
, np
->nick
, NICKLEN
+ 1);
35 wnp
->numeric
= np
->numeric
;
36 strncpy(wnp
->ident
, np
->ident
, USERLEN
+ 1);
38 wnp
->host
= newhost();
39 memset(wnp
->host
, 0, sizeof(host
));
40 wnp
->host
->name
= getsstring(np
->host
->name
->content
, HOSTLEN
);
42 wnp
->realname
= newrealname();
43 memset(wnp
->realname
, 0, sizeof(realname
));
44 wnp
->realname
->name
= getsstring(np
->realname
->name
->content
, REALLEN
);
45 wnp
->shident
= np
->shident
? getsstring(np
->shident
->content
, 512) : NULL
;
46 wnp
->sethost
= np
->sethost
? getsstring(np
->sethost
->content
, 512) : NULL
;
47 wnp
->opername
= np
->opername
? getsstring(np
->opername
->content
, 512) : NULL
;
48 wnp
->umodes
= np
->umodes
;
50 wnp
->auth
= newauthname();
51 memset(wnp
->auth
, 0, sizeof(authname
));
52 wnp
->auth
->userid
= np
->auth
->userid
;
53 strncpy(wnp
->auth
->name
, np
->auth
->name
, ACCOUNTLEN
+ 1);
54 wnp
->authname
= wnp
->auth
->name
;
56 wnp
->timestamp
= np
->timestamp
;
57 wnp
->accountts
= np
->accountts
;
58 wnp
->away
= np
->away
? getsstring(np
->away
->content
, 512) : NULL
;
60 memcpy(&wnp
->ipaddress
, &np
->ipaddress
, sizeof(struct irc_in_addr
));
62 ip_canonicalize_tunnel(&ipaddress_canonical
, &np
->ipaddress
);
63 wnp
->ipnode
= refnode(iptree
, &ipaddress_canonical
, PATRICIA_MAXBITS
);
65 wnp
->next
= (nick
*)ww
; /* Yuck. */
67 ww
->timestamp
= getnettime();
68 ww
->type
= WHOWAS_USED
;
72 triggerhook(HOOK_WHOWAS_NEWRECORD
, args
);
77 void whowas_clean(whowas
*ww
) {
80 if (!ww
|| ww
->type
== WHOWAS_UNUSED
)
83 triggerhook(HOOK_WHOWAS_LOSTRECORD
, ww
);
86 freesstring(np
->host
->name
);
88 freesstring(np
->realname
->name
);
89 freerealname(np
->realname
);
90 freesstring(np
->shident
);
91 freesstring(np
->sethost
);
92 freesstring(np
->opername
);
93 freeauthname(np
->auth
);
94 freesstring(np
->away
);
96 freesstring(ww
->reason
);
97 ww
->type
= WHOWAS_UNUSED
;
100 void whowas_free(whowas
*ww
) {
105 static void whowas_handlequitorkill(int hooknum
, void *arg
) {
108 char *reason
= args
[1];
113 /* Create a new record. */
114 ww
= whowas_fromnick(np
, 0);
116 if (hooknum
== HOOK_NICK_KILL
) {
117 if ((rreason
= strchr(reason
, ' '))) {
118 sprintf(resbuf
, "Killed%s", rreason
);
122 ww
->type
= WHOWAS_KILL
;
124 if (strncmp(reason
, "G-lined", 7) == 0)
125 ww
->type
= WHOWAS_KILL
;
127 ww
->type
= WHOWAS_QUIT
;
130 ww
->reason
= getsstring(reason
, WW_REASONLEN
);
133 static void whowas_handlerename(int hooknum
, void *arg
) {
136 char *oldnick
= args
[1];
140 ww
= whowas_fromnick(np
, 0);
141 ww
->type
= WHOWAS_RENAME
;
143 ww
->newnick
= getsstring(wnp
->nick
, NICKLEN
);
144 strncpy(wnp
->nick
, oldnick
, NICKLEN
+ 1);
147 whowas
*whowas_chase(const char *target
, int maxage
) {
155 for (i
= whowasoffset
+ WW_MAXENTRIES
- 1; i
>= whowasoffset
; i
--) {
156 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
158 if (ww
->type
== WHOWAS_UNUSED
)
163 if (ww
->timestamp
< now
- maxage
)
164 break; /* records are in timestamp order, we're done */
166 if (ircd_strcmp(wnp
->nick
, target
) == 0)
173 const char *whowas_format(whowas
*ww
) {
174 nick
*np
= &ww
->nick
;
175 static char buf
[512];
179 snprintf(hostmask
, sizeof(hostmask
), "%s!%s@%s%s%s [%s] (%s)",
180 np
->nick
, np
->ident
, np
->host
->name
->content
,
181 np
->auth
? "/" : "", np
->auth
? np
->authname
: "",
182 IPtostr(np
->ipaddress
),
183 printflags(np
->umodes
, umodeflags
));
184 strftime(timebuf
, sizeof(timebuf
), "%d/%m/%y %H:%M:%S", localtime(&(ww
->timestamp
)));
186 if (ww
->type
== WHOWAS_RENAME
)
187 snprintf(buf
, sizeof(buf
), "[%s] NICK %s r(%s) -> %s", timebuf
, hostmask
, np
->realname
->name
->content
, ww
->newnick
->content
);
189 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
);
194 const char *whowas_formatchannels(whowas
*ww
) {
195 static char buf
[512];
198 strcpy(buf
, "Channels: ");
200 for (i
= 0; i
< WW_MAXCHANNELS
; i
++) {
201 if (!ww
->channels
[i
])
205 strncat(buf
, ", ", sizeof(buf
));
209 strncat(buf
, ww
->channels
[i
]->name
->content
, sizeof(buf
));
212 if (!ww
->channels
[0])
213 strncat(buf
, "(No channels.)", sizeof(buf
));
215 buf
[sizeof(buf
) - 1] = '\0';
220 unsigned int nextwhowasmarker() {
223 static unsigned int whowasmarker
=0;
228 /* If we wrapped to zero, zap the marker on all records */
229 for (i
= 0; i
< WW_MAXENTRIES
; i
++) {
230 ww
= &whowasrecs
[i
% WW_MAXENTRIES
];
241 memset(whowasrecs
, 0, sizeof(whowasrecs
));
243 registerhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
244 registerhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
245 registerhook(HOOK_NICK_RENAME
, whowas_handlerename
);
252 deregisterhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
253 deregisterhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
254 deregisterhook(HOOK_NICK_RENAME
, whowas_handlerename
);
256 for (i
= 0; i
< WW_MAXENTRIES
; i
++) {