]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-hostmask.c
Merge pull request #132 from retropc/lua_country
[irc/quakenet/newserv.git] / newsearch / ns-hostmask.c
CommitLineData
c86edd1d
Q
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"
c86edd1d
Q
11#include "../lib/irc_string.h"
12
c8be5183 13void *hostmask_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
371e8b02
CP
14void *hostmask_exe_rn(searchCtx *ctx, struct searchNode *thenode, void *theinput);
15void *hostmask_exe_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput);
16void *hostmask_exe_rn_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput);
c8be5183 17void hostmask_free(searchCtx *ctx, struct searchNode *thenode);
c86edd1d 18
f33f3f52 19struct searchNode *hostmask_parse(searchCtx *ctx, int argc, char **argv) {
c86edd1d 20 struct searchNode *thenode;
31686847 21 int realhost = 0, realname = 0, i, error = 0;
371e8b02
CP
22
23 for(i=0;i<argc;i++) {
31686847
CP
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")) {
371e8b02 31 realhost = 1;
31686847 32 } else if(!ircd_strcmp(p, "realname")) {
371e8b02
CP
33 realname = 1;
34 } else {
31686847
CP
35 error = 1;
36 }
37 convsn->free(ctx, convsn);
38
39 if(error) {
371e8b02
CP
40 parseError = "bad argument: use realhost and/or realname";
41 return NULL;
42 }
43 }
c86edd1d 44
9ce4f0be
IB
45 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
46 parseError = "malloc: could not allocate memory for this search.";
47 return NULL;
48 }
c86edd1d
Q
49
50 thenode->returntype = RETURNTYPE_STRING;
e39a7352 51 if (!(thenode->localdata = (void *)malloc(HOSTLEN+USERLEN+NICKLEN+REALLEN+10))) {
9ce4f0be
IB
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 }
c86edd1d
Q
57 thenode->free = hostmask_free;
58
371e8b02
CP
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 }
c86edd1d
Q
71 }
72
73 return thenode;
74}
75
c8be5183 76void *hostmask_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c86edd1d
Q
77 nick *np = (nick *)theinput;
78 char *buf = thenode->localdata;
79
c86edd1d
Q
80 return visiblehostmask(np, buf);
81}
82
371e8b02
CP
83void *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
92void *hostmask_exe_rn_rh(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
c86edd1d
Q
93 nick *np = (nick *)theinput;
94 char *buf = thenode->localdata;
95
e39a7352 96 sprintf(buf,"%s!%s@%s\r%s",np->nick,np->ident,np->host->name->content,np->realname->name->content);
c86edd1d
Q
97
98 return buf;
99}
100
371e8b02
CP
101void *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
c8be5183 110void hostmask_free(searchCtx *ctx, struct searchNode *thenode) {
c86edd1d
Q
111 free(thenode->localdata);
112 free(thenode);
113}
114