]> jfr.im git - irc/quakenet/newserv.git/blame - newsearch/ns-modes.c
LUA: port luadb to dbapi2 to drop postgres dependency
[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 {
6d6b2cb9 32 flaglist=umodeflags;
c86edd1d
Q
33 }
34
9ce4f0be
IB
35 if (!(localdata=(struct modes_localdata *)malloc(sizeof(struct modes_localdata)))) {
36 parseError = "malloc: could not allocate memory for this search.";
37 return NULL;
38 }
c86edd1d 39
c86edd1d
Q
40 localdata->setmodes=0;
41 localdata->clearmodes = ~0;
42
31686847
CP
43 if (!(modestring=argtoconststr("modes", ctx, argv[0], &p))) {
44 free(localdata);
45 return NULL;
46 }
47
48 setflags(&(localdata->setmodes), 0xFFFF, p, flaglist, REJECT_NONE);
49 setflags(&(localdata->clearmodes), 0xFFFF, p, flaglist, REJECT_NONE);
50 (modestring->free)(ctx, modestring);
51
c86edd1d
Q
52 localdata->clearmodes = ~(localdata->clearmodes);
53
9ce4f0be
IB
54 if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
55 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
56 parseError = "malloc: could not allocate memory for this search.";
57 free(localdata);
58 return NULL;
59 }
c86edd1d
Q
60
61 thenode->returntype = RETURNTYPE_BOOL;
62 thenode->localdata = (void *)localdata;
63 thenode->exe = modes_exe;
64 thenode->free = modes_free;
65
66 return thenode;
67}
68
c8be5183 69void *modes_exe(searchCtx *ctx, struct searchNode *thenode, void *value) {
c86edd1d
Q
70 struct modes_localdata *localdata;
71 nick *np;
72 chanindex *cip;
73 flag_t flags;
74
75 localdata = (struct modes_localdata *)thenode->localdata;
76
a92bb8e1 77 if (ctx->searchcmd == reg_chansearch) {
c86edd1d
Q
78 cip=(chanindex *)value;
79 if (!cip->channel)
80 return NULL;
81 flags=cip->channel->flags;
6d6b2cb9 82 } else {
c86edd1d
Q
83 np=(nick *)value;
84 flags=np->umodes;
c86edd1d
Q
85 }
86
87 if (~flags & (localdata->setmodes))
c7f7a584 88 return (void *)0;
c86edd1d
Q
89
90 if (flags & (localdata->clearmodes))
c7f7a584 91 return (void *)0;
c86edd1d 92
c7f7a584 93 return (void *)1;
c86edd1d
Q
94}
95
c8be5183 96void modes_free(searchCtx *ctx, struct searchNode *thenode) {
c86edd1d
Q
97 free (thenode->localdata);
98 free (thenode);
99}