]> jfr.im git - irc/quakenet/newserv.git/blame - whowas/whowas.c
TRUSTS: add whois handler to display basic trustgroup/host information in whois output
[irc/quakenet/newserv.git] / whowas / whowas.c
CommitLineData
fa6819a7
GB
1#include <stdio.h>
2#include <string.h>
3#include "../core/hooks.h"
4#include "../control/control.h"
5#include "../irc/irc.h"
6#include "../lib/irc_string.h"
7#include "whowas.h"
8
d6385de2
GB
9static whowas *wwhead, *wwtail;
10static int wwcount;
fa6819a7
GB
11
12int ww_cmdwhowas(void *source, int cargc, char **cargv) {
13 nick *np = source;
14 char *pattern;
15 whowas *ww;
16 char hostmask[WW_MASKLEN+1];
17 char timebuf[30];
d6385de2 18 int matches = 0, limit = 500;
fa6819a7
GB
19
20 if(cargc<1)
21 return CMD_USAGE;
22
23 pattern = cargv[0];
24
d6385de2
GB
25 if(cargc>1)
26 limit = strtol(cargv[1], NULL, 10);
27
28 for(ww=wwhead;ww;ww=ww->next) {
fa6819a7
GB
29 snprintf(hostmask, sizeof(hostmask), "%s!%s@%s", ww->nick, ww->ident, ww->host);
30
31 if (match2strings(pattern, hostmask)) {
fa6819a7
GB
32 matches++;
33
d6385de2
GB
34 if(matches<=limit) {
35 strftime(timebuf, 30, "%d/%m/%y %H:%M:%S", localtime(&(ww->seen)));
36
37 controlreply(np, "[%s] %s (%s): %s", timebuf, hostmask, ww->realname, ww->reason->content);
2cfb2042 38 } else if(matches==limit+1) {
d6385de2 39 controlreply(np, "--- More than %d matches, skipping the rest", limit);
dd980bc0 40 }
fa6819a7
GB
41 }
42 }
43
44 controlreply(np, "--- Found %d entries.", matches);
45
46 return CMD_OK;
47}
48
49void ww_handlequitorkill(int hooknum, void *arg) {
50 void **args=arg;
51 nick *np=args[0];
52 char *reason=args[1];
53 char *rreason;
54 char resbuf[512];
55 whowas *ww;
56 time_t now;
57
58 time(&now);
59
60 /* Clean up old records. */
d6385de2
GB
61 while((ww = wwtail) && (ww->seen < now - WW_MAXAGE || wwcount >= WW_MAXENTRIES)) {
62 wwtail = ww->prev;
63
64 if (ww->prev)
65 ww->prev->next = NULL;
66 else
67 wwhead = ww->prev;
68
69 wwcount--;
fa6819a7
GB
70 free(ww);
71 }
72
73 /* Create a new record. */
74 ww = malloc(sizeof(whowas));
75 strncpy(ww->nick, np->nick, NICKLEN);
76 strncpy(ww->ident, np->ident, USERLEN);
77 strncpy(ww->host, np->host->name->content, HOSTLEN);
78 strncpy(ww->realname, np->realname->name->content, REALLEN);
79 ww->seen = getnettime();
80
d6385de2 81 if(hooknum==HOOK_NICK_KILL && (rreason=strchr(reason,' '))) {
fa6819a7
GB
82 sprintf(resbuf,"Killed%s",rreason);
83 reason=resbuf;
84 }
85
d6385de2
GB
86 ww->reason = getsstring(reason, WW_REASONLEN);
87
88 if(wwhead)
89 wwhead->prev = ww;
90
91 ww->next = wwhead;
92 wwhead = ww;
93
94 ww->prev = NULL;
95
96 if(!wwtail)
97 wwtail = ww;
fa6819a7 98
d6385de2 99 wwcount++;
fa6819a7
GB
100}
101
102void _init(void) {
103 registerhook(HOOK_NICK_QUIT, ww_handlequitorkill);
104 registerhook(HOOK_NICK_KILL, ww_handlequitorkill);
105
d6385de2 106 registercontrolhelpcmd("whowas", NO_OPER, 2, &ww_cmdwhowas, "Usage: whowas <mask> ?limit?\nLooks up information about recently disconnected users.");
fa6819a7
GB
107}
108
109void _fini(void) {
110 whowas *ww;
111
112 deregisterhook(HOOK_NICK_QUIT, ww_handlequitorkill);
113 deregisterhook(HOOK_NICK_KILL, ww_handlequitorkill);
114
115 deregistercontrolcmd("whowas", &ww_cmdwhowas);
116
d6385de2
GB
117 while((ww = wwhead)) {
118 wwhead = ww->next;
fa6819a7
GB
119 free(ww);
120 }
121}