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"
8 #include "../core/config.h"
17 whowas
*whowas_fromnick(nick
*np
, int standalone
) {
20 struct irc_in_addr ipaddress_canonical
;
23 /* Create a new record. */
25 ww
= malloc(sizeof(whowas
));
27 ww
= &whowasrecs
[whowasoffset
];
29 whowasoffset
= (whowasoffset
+ 1) % whowasmax
;
32 memset(ww
, 0, sizeof(whowas
));
35 memset(wnp
, 0, sizeof(nick
));
36 strncpy(wnp
->nick
, np
->nick
, NICKLEN
+ 1);
37 wnp
->numeric
= np
->numeric
;
38 strncpy(wnp
->ident
, np
->ident
, USERLEN
+ 1);
40 wnp
->host
= newhost();
41 memset(wnp
->host
, 0, sizeof(host
));
42 wnp
->host
->name
= getsstring(np
->host
->name
->content
, HOSTLEN
);
44 wnp
->realname
= newrealname();
45 memset(wnp
->realname
, 0, sizeof(realname
));
46 wnp
->realname
->name
= getsstring(np
->realname
->name
->content
, REALLEN
);
47 wnp
->shident
= np
->shident
? getsstring(np
->shident
->content
, 512) : NULL
;
48 wnp
->sethost
= np
->sethost
? getsstring(np
->sethost
->content
, 512) : NULL
;
49 wnp
->opername
= np
->opername
? getsstring(np
->opername
->content
, 512) : NULL
;
50 wnp
->umodes
= np
->umodes
;
52 wnp
->auth
= newauthname();
53 memset(wnp
->auth
, 0, sizeof(authname
));
54 wnp
->auth
->userid
= np
->auth
->userid
;
55 strncpy(wnp
->auth
->name
, np
->auth
->name
, ACCOUNTLEN
+ 1);
56 wnp
->authname
= wnp
->auth
->name
;
58 wnp
->timestamp
= np
->timestamp
;
59 wnp
->accountts
= np
->accountts
;
60 wnp
->away
= np
->away
? getsstring(np
->away
->content
, 512) : NULL
;
62 memcpy(&wnp
->ipaddress
, &np
->ipaddress
, sizeof(struct irc_in_addr
));
64 ip_canonicalize_tunnel(&ipaddress_canonical
, &np
->ipaddress
);
65 wnp
->ipnode
= refnode(iptree
, &ipaddress_canonical
, PATRICIA_MAXBITS
);
67 wnp
->next
= (nick
*)ww
; /* Yuck. */
69 ww
->timestamp
= getnettime();
70 ww
->type
= WHOWAS_USED
;
74 triggerhook(HOOK_WHOWAS_NEWRECORD
, args
);
79 void whowas_clean(whowas
*ww
) {
82 if (!ww
|| ww
->type
== WHOWAS_UNUSED
)
85 triggerhook(HOOK_WHOWAS_LOSTRECORD
, ww
);
88 freesstring(np
->host
->name
);
90 freesstring(np
->realname
->name
);
91 freerealname(np
->realname
);
92 freesstring(np
->shident
);
93 freesstring(np
->sethost
);
94 freesstring(np
->opername
);
95 freeauthname(np
->auth
);
96 freesstring(np
->away
);
97 derefnode(iptree
, np
->ipnode
);
98 freesstring(ww
->reason
);
99 freesstring(ww
->newnick
);
100 ww
->type
= WHOWAS_UNUSED
;
103 void whowas_free(whowas
*ww
) {
108 static void whowas_handlequitorkill(int hooknum
, void *arg
) {
111 char *reason
= args
[1];
116 /* Create a new record. */
117 ww
= whowas_fromnick(np
, 0);
119 if (hooknum
== HOOK_NICK_KILL
) {
120 if ((rreason
= strchr(reason
, ' '))) {
121 sprintf(resbuf
, "Killed%s", rreason
);
125 ww
->type
= WHOWAS_KILL
;
127 if (strncmp(reason
, "G-lined", 7) == 0)
128 ww
->type
= WHOWAS_KILL
;
130 ww
->type
= WHOWAS_QUIT
;
133 ww
->reason
= getsstring(reason
, WW_REASONLEN
);
136 static void whowas_handlerename(int hooknum
, void *arg
) {
139 char *oldnick
= args
[1];
143 ww
= whowas_fromnick(np
, 0);
144 ww
->type
= WHOWAS_RENAME
;
146 ww
->newnick
= getsstring(wnp
->nick
, NICKLEN
);
147 strncpy(wnp
->nick
, oldnick
, NICKLEN
+ 1);
150 whowas
*whowas_chase(const char *target
, int maxage
) {
158 for (i
= whowasoffset
+ whowasmax
- 1; i
>= whowasoffset
; i
--) {
159 ww
= &whowasrecs
[i
% whowasmax
];
161 if (ww
->type
== WHOWAS_UNUSED
)
166 if (ww
->timestamp
< now
- maxage
)
167 break; /* records are in timestamp order, we're done */
169 if (ircd_strcmp(wnp
->nick
, target
) == 0)
176 const char *whowas_format(whowas
*ww
) {
177 nick
*np
= &ww
->nick
;
178 static char buf
[512];
182 snprintf(hostmask
, sizeof(hostmask
), "%s!%s@%s%s%s [%s] (%s)",
183 np
->nick
, np
->ident
, np
->host
->name
->content
,
184 np
->auth
? "/" : "", np
->auth
? np
->authname
: "",
185 IPtostr(np
->ipaddress
),
186 printflags(np
->umodes
, umodeflags
));
187 strftime(timebuf
, sizeof(timebuf
), "%d/%m/%y %H:%M:%S", localtime(&(ww
->timestamp
)));
189 if (ww
->type
== WHOWAS_RENAME
)
190 snprintf(buf
, sizeof(buf
), "[%s] NICK %s r(%s) -> %s", timebuf
, hostmask
, np
->realname
->name
->content
, ww
->newnick
->content
);
192 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
);
197 const char *whowas_formatchannels(whowas
*ww
) {
198 static char buf
[512];
201 strcpy(buf
, "Channels: ");
203 for (i
= 0; i
< WW_MAXCHANNELS
; i
++) {
204 if (!ww
->channels
[i
])
208 strncat(buf
, ", ", sizeof(buf
));
212 strncat(buf
, ww
->channels
[i
]->name
->content
, sizeof(buf
));
215 if (!ww
->channels
[0])
216 strncat(buf
, "(No channels.)", sizeof(buf
));
218 buf
[sizeof(buf
) - 1] = '\0';
223 unsigned int nextwhowasmarker() {
226 static unsigned int whowasmarker
=0;
231 /* If we wrapped to zero, zap the marker on all records */
232 for (i
= 0; i
< whowasmax
; i
++) {
233 ww
= &whowasrecs
[i
% whowasmax
];
245 sstring
*temp
= getcopyconfigitem("whowas", "maxentries", XStringify(WW_DEFAULT_MAXENTRIES
), 10);
246 whowasmax
= atoi(temp
->content
);
249 whowasrecs
= calloc(whowasmax
, sizeof(whowas
));
251 registerhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
252 registerhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
253 registerhook(HOOK_NICK_RENAME
, whowas_handlerename
);
260 deregisterhook(HOOK_NICK_QUIT
, whowas_handlequitorkill
);
261 deregisterhook(HOOK_NICK_KILL
, whowas_handlequitorkill
);
262 deregisterhook(HOOK_NICK_RENAME
, whowas_handlerename
);
264 for (i
= 0; i
< whowasmax
; i
++) {