]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-hostmask.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-hostmask.c
1 /*
2 * HOSTMASK functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "../irc/irc_config.h"
11 #include "../lib/irc_string.h"
12
13 void *hostmask_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
14 void *hostmask_exe_rn(searchCtx *ctx, struct searchNode *thenode, void *theinput);
15 void *hostmask_exe_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput);
16 void *hostmask_exe_rn_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput);
17 void hostmask_free(searchCtx *ctx, struct searchNode *thenode);
18
19 struct searchNode *hostmask_parse(searchCtx *ctx, int argc, char **argv) {
20 struct searchNode *thenode;
21 int realhost = 0, realname = 0, i, error = 0;
22
23 for(i=0;i<argc;i++) {
24 struct searchNode *convsn;
25 char *p;
26
27 if (!(convsn=argtoconststr("hostmask", ctx, argv[i], &p)))
28 return NULL;
29
30 if(!ircd_strcmp(p, "realhost")) {
31 realhost = 1;
32 } else if(!ircd_strcmp(p, "realname")) {
33 realname = 1;
34 } else {
35 error = 1;
36 }
37 convsn->free(ctx, convsn);
38
39 if(error) {
40 parseError = "bad argument: use realhost and/or realname";
41 return NULL;
42 }
43 }
44
45 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
46 parseError = "malloc: could not allocate memory for this search.";
47 return NULL;
48 }
49
50 thenode->returntype = RETURNTYPE_STRING;
51 if (!(thenode->localdata = (void *)malloc(HOSTLEN+USERLEN+NICKLEN+REALLEN+10))) {
52 /* couldn't malloc() memory for thenode->localdata, so free thenode to avoid leakage */
53 parseError = "malloc: could not allocate memory for this search.";
54 free(thenode);
55 return NULL;
56 }
57 thenode->free = hostmask_free;
58
59 if(realname) {
60 if(realhost) {
61 thenode->exe = hostmask_exe_rn_rh;
62 } else {
63 thenode->exe = hostmask_exe_rn;
64 }
65 } else {
66 if(realhost) {
67 thenode->exe = hostmask_exe_rh;
68 } else {
69 thenode->exe = hostmask_exe;
70 }
71 }
72
73 return thenode;
74 }
75
76 void *hostmask_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
77 nick *np = (nick *)theinput;
78 char *buf = thenode->localdata;
79
80 return visiblehostmask(np, buf);
81 }
82
83 void *hostmask_exe_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
84 nick *np = (nick *)theinput;
85 char *buf = thenode->localdata;
86
87 sprintf(buf,"%s!%s@%s",np->nick,np->ident,np->host->name->content);
88
89 return buf;
90 }
91
92 void *hostmask_exe_rn_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
93 nick *np = (nick *)theinput;
94 char *buf = thenode->localdata;
95
96 sprintf(buf,"%s!%s@%s\r%s",np->nick,np->ident,np->host->name->content,np->realname->name->content);
97
98 return buf;
99 }
100
101 void *hostmask_exe_rn(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
102 nick *np = (nick *)theinput;
103 char *buf = thenode->localdata;
104
105 sprintf(buf,"%s\r%s",visiblehostmask(np, buf),np->realname->name->content);
106
107 return buf;
108 }
109
110 void hostmask_free(searchCtx *ctx, struct searchNode *thenode) {
111 free(thenode->localdata);
112 free(thenode);
113 }
114