]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-modes.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-modes.c
CommitLineData
c86edd1d
Q
1/*
2 * MODES functionality
3 */
4
5#include "newsearch.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9
c86edd1d 10struct modes_localdata {
c86edd1d
Q
11 flag_t setmodes;
12 flag_t clearmodes;
13};
14
c8be5183
CP
15void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
16void modes_free(searchCtx *ctx, struct searchNode *thenode);
c86edd1d 17
f33f3f52 18struct searchNode *modes_parse(searchCtx *ctx, int argc, char **argv) {
c86edd1d 19 struct modes_localdata *localdata;
31686847 20 struct searchNode *thenode, *modestring;
c86edd1d 21 const flag *flaglist;
31686847
CP
22 char *p;
23
c86edd1d
Q
24 if (argc!=1) {
25 parseError="modes: usage: modes (mode string)";
26 return NULL;
27 }
28
a92bb8e1 29 if (ctx->searchcmd == reg_chansearch) {
c86edd1d 30 flaglist=cmodeflags;
a92bb8e1 31 } else if (ctx->searchcmd == reg_nicksearch) {
c86edd1d 32 flaglist=umodeflags;
a92bb8e1 33 } else {
c86edd1d
Q
34 parseError="modes: unsupported search type";
35 return NULL;
36 }
37
9ce4f0be
IB
38 if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
39 parseError = "malloc: could not allocate memory for this search.";
40 return NULL;
41 }
c86edd1d 42
c86edd1d
Q
43 localdata->setmodes=0;
44 localdata->clearmodes = ~0;
45
31686847
CP
46 if (!(modestring=argtoconststr("modes", ctx, argv[0], &p))) {
47 free(localdata);
48 return NULL;
49 }
50
51 setflags(&(localdata->setmodes), 0xFFFF, p, flaglist, REJECT_NONE);
52 setflags(&(localdata->clearmodes), 0xFFFF, p, flaglist, REJECT_NONE);
53 (modestring->free)(ctx, modestring);
54
c86edd1d
Q
55 localdata->clearmodes = ~(localdata->clearmodes);
56
9ce4f0be
IB
57 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
58 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
59 parseError = "malloc: could not allocate memory for this search.";
60 free(localdata);
61 return NULL;
62 }
c86edd1d
Q
63
64 thenode->returntype = RETURNTYPE_BOOL;
65 thenode->localdata = (void *)localdata;
66 thenode->exe = modes_exe;
67 thenode->free = modes_free;
68
69 return thenode;
70}
71
c8be5183 72void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
c86edd1d
Q
73 struct modes_localdata *localdata;
74 nick *np;
75 chanindex *cip;
76 flag_t flags;
77
78 localdata = (struct modes_localdata *)thenode->localdata;
79
a92bb8e1 80 if (ctx->searchcmd == reg_chansearch) {
c86edd1d
Q
81 cip=(chanindex *)value;
82 if (!cip->channel)
83 return NULL;
84 flags=cip->channel->flags;
a92bb8e1 85 } else if (ctx->searchcmd == reg_nicksearch) {
c86edd1d
Q
86 np=(nick *)value;
87 flags=np->umodes;
a92bb8e1 88 } else {
c86edd1d
Q
89 return NULL;
90 }
91
92 if (~flags & (localdata->setmodes))
c7f7a584 93 return (void *)0;
c86edd1d
Q
94
95 if (flags & (localdata->clearmodes))
c7f7a584 96 return (void *)0;
c86edd1d 97
c7f7a584 98 return (void *)1;
c86edd1d
Q
99}
100
c8be5183 101void modes_free(searchCtx *ctx, struct searchNode *thenode) {
c86edd1d
Q
102 free (thenode->localdata);
103 free (thenode);
104}