]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-channeliter.c
LUA: port luadb to dbapi2 to drop postgres dependency
[irc/quakenet/newserv.git] / newsearch / ns-channeliter.c
1 /*
2 * AND functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #define MAX_CHANS 500
11
12 void channeliter_free(searchCtx *ctx, struct searchNode *thenode);
13 void *channeliter_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
14
15 struct channeliter_localdata {
16 int currentchannel;
17 struct searchVariable *variable;
18 nick *lastnick;
19 };
20
21 struct searchNode *channeliter_parse(searchCtx *ctx, int argc, char **argv) {
22 searchNode *thenode;
23 struct channeliter_localdata *localdata;
24
25 if(argc < 1) {
26 parseError = "channeliter: usage: channeliter variable";
27 return NULL;
28 }
29
30 if(!(localdata=(struct channeliter_localdata *)malloc(sizeof(struct channeliter_localdata)))) {
31 parseError = "malloc: could not allocate memory for this search.";
32 return NULL;
33 }
34
35 /* @argv usage OK */
36 if(!(localdata->variable=var_register(ctx, argv[0], RETURNTYPE_STRING))) {
37 free(localdata);
38 return NULL;
39 }
40
41 if(!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) {
42 parseError = "malloc: could not allocate memory for this search.";
43 free(localdata);
44 return NULL;
45 }
46
47 localdata->currentchannel = 0;
48 localdata->lastnick = NULL;
49
50 thenode->returntype = RETURNTYPE_BOOL;
51 thenode->localdata = localdata;
52 thenode->exe = channeliter_exe;
53 thenode->free = channeliter_free;
54
55 return thenode;
56 }
57
58 void channeliter_free(searchCtx *ctx, struct searchNode *thenode) {
59 free(thenode->localdata);
60 free(thenode);
61 }
62
63 void *channeliter_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
64 struct channeliter_localdata *localdata = thenode->localdata;
65 nick *np = (nick *)theinput;
66
67 if(np != localdata->lastnick) {
68 localdata->lastnick = np;
69 localdata->currentchannel = 0;
70 }
71
72 /* if(np->channels->cursi > MAX_CHANS)
73 return (void *)0;
74 */
75
76 if(localdata->currentchannel >= np->channels->cursi)
77 return (void *)0;
78
79 var_setstr(localdata->variable, ((channel **)np->channels->content)[localdata->currentchannel++]->index->name->content);
80
81 return (void *)1;
82 }