]>
Commit | Line | Data |
---|---|---|
b697c21c CP |
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 type, 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(type != SEARCHTYPE_NICK) { | |
31 | parseError = "channeliter: this function is only valid for nick searches."; | |
32 | return NULL; | |
33 | } | |
34 | ||
35 | if(!(localdata=(struct channeliter_localdata *)malloc(sizeof(struct channeliter_localdata)))) { | |
36 | parseError = "malloc: could not allocate memory for this search."; | |
37 | return NULL; | |
38 | } | |
39 | ||
40 | if(!(localdata->variable=var_register(ctx, type, argv[0], RETURNTYPE_STRING))) { | |
41 | free(localdata); | |
42 | return NULL; | |
43 | } | |
44 | ||
45 | if(!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
46 | parseError = "malloc: could not allocate memory for this search."; | |
47 | free(localdata); | |
48 | return NULL; | |
49 | } | |
50 | ||
51 | localdata->currentchannel = 0; | |
52 | localdata->lastnick = NULL; | |
53 | ||
54 | thenode->returntype = RETURNTYPE_BOOL; | |
55 | thenode->localdata = localdata; | |
56 | thenode->exe = channeliter_exe; | |
57 | thenode->free = channeliter_free; | |
58 | ||
59 | return thenode; | |
60 | } | |
61 | ||
62 | void channeliter_free(searchCtx *ctx, struct searchNode *thenode) { | |
63 | free(thenode->localdata); | |
64 | free(thenode); | |
65 | } | |
66 | ||
67 | void *channeliter_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
68 | struct channeliter_localdata *localdata = thenode->localdata; | |
69 | nick *np = (nick *)theinput; | |
70 | ||
71 | if(np != localdata->lastnick) { | |
72 | localdata->lastnick = np; | |
73 | localdata->currentchannel = 0; | |
74 | } | |
75 | ||
76 | if(np->channels->cursi > MAX_CHANS) | |
77 | return (void *)0; | |
78 | ||
79 | if(localdata->currentchannel >= np->channels->cursi) | |
80 | return (void *)0; | |
81 | ||
82 | var_setstr(localdata->variable, ((channel **)np->channels->content)[localdata->currentchannel++]->index->name->content); | |
83 | ||
84 | return (void *)1; | |
85 | } |