]> jfr.im git - irc/quakenet/newserv.git/blob - countusers/countusers.c
Initial Import
[irc/quakenet/newserv.git] / countusers / countusers.c
1 #include "../control/control.h"
2 #include "../nick/nick.h"
3 #include "../channel/channel.h"
4 #include "../lib/irc_string.h"
5 #include <string.h>
6
7 int do_countusers(void *source, int cargc, char **cargv) {
8 nick *sender=(nick *)source, *np;
9 host *hp;
10 unsigned int i=0,count=0;
11 char *chp,*host,*ident;
12
13 if (cargc<1) {
14 controlreply(sender, "Usage: countusers <user@host or host>");
15 return CMD_ERROR;
16 }
17
18 for (chp=cargv[0]; ;chp++) {
19 if (*chp=='@') {
20 /* found @, so it's user@host */
21 ident=cargv[0];
22 *chp='\0';
23 host=chp+1;
24 break;
25 } else if (*chp=='\0') {
26 /* found NULL, so just host */
27 host=cargv[0];
28 ident=NULL;
29 break;
30 }
31 }
32
33 if ((hp=findhost(host))==NULL) {
34 controlreply(sender,"0 users match %s%s%s",ident?ident:"",ident?"@":"",host);
35 return CMD_OK;
36 }
37
38 if (!ident) {
39 /* We want *@host, so it's just the clonecount we have for free */
40 controlreply(sender,"%d users match %s",hp->clonecount,host);
41 } else {
42 /* Do it the slow way */
43 for (np=hp->nicks;np;np=np->nextbyhost) {
44 if (!ircd_strcmp(np->ident,ident))
45 count++;
46 }
47 controlreply(sender,"%d users match %s@%s",count,ident,host);
48 }
49
50 return CMD_OK;
51 }
52
53 void _init() {
54 registercontrolcmd("countusers", 10, 1, do_countusers);
55 }
56
57 void _fini() {
58 deregistercontrolcmd("countusers", do_countusers);
59 }