]>
Commit | Line | Data |
---|---|---|
cb857218 C |
1 | /* |
2 | * NICKITER functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | void nickiter_free(searchCtx *ctx, struct searchNode *thenode); | |
11 | void *nickiter_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
12 | ||
13 | struct nickiter_localdata { | |
14 | int currentnick; | |
15 | struct searchVariable *variable; | |
16 | chanindex *lastchanindex; | |
17 | }; | |
18 | ||
19 | struct searchNode *nickiter_parse(searchCtx *ctx, int argc, char **argv) { | |
20 | searchNode *thenode; | |
21 | struct nickiter_localdata *localdata; | |
22 | ||
23 | if(argc < 1) { | |
24 | parseError = "nickiter: usage: nickiter variable"; | |
25 | return NULL; | |
26 | } | |
27 | ||
28 | if(!(localdata=(struct nickiter_localdata *)malloc(sizeof(struct nickiter_localdata)))) { | |
29 | parseError = "malloc: could not allocate memory for this search."; | |
30 | return NULL; | |
31 | } | |
32 | ||
33 | if(!(localdata->variable=var_register(ctx, argv[0], RETURNTYPE_STRING))) { | |
34 | free(localdata); | |
35 | return NULL; | |
36 | } | |
37 | ||
38 | if(!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
39 | parseError = "malloc: could not allocate memory for this search."; | |
40 | free(localdata); | |
41 | return NULL; | |
42 | } | |
43 | ||
44 | localdata->currentnick = 0; | |
45 | localdata->lastchanindex = NULL; | |
46 | ||
47 | thenode->returntype = RETURNTYPE_BOOL; | |
48 | thenode->localdata = localdata; | |
49 | thenode->exe = nickiter_exe; | |
50 | thenode->free = nickiter_free; | |
51 | ||
52 | return thenode; | |
53 | } | |
54 | ||
55 | void nickiter_free(searchCtx *ctx, struct searchNode *thenode) { | |
56 | free(thenode->localdata); | |
57 | free(thenode); | |
58 | } | |
59 | ||
60 | void *nickiter_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
61 | struct nickiter_localdata *localdata = thenode->localdata; | |
62 | chanindex *cip = (chanindex *)theinput; | |
63 | nick *np; | |
64 | ||
65 | if(cip != localdata->lastchanindex) { | |
66 | localdata->lastchanindex = cip; | |
67 | localdata->currentnick = 0; | |
68 | } | |
69 | ||
70 | if(!cip->channel || !cip->channel->users) | |
71 | return (void *)0; | |
72 | ||
73 | while(localdata->currentnick < cip->channel->users->hashsize) { | |
74 | if(cip->channel->users->content[localdata->currentnick] != nouser) { | |
76721b0b C |
75 | np = getnickbynumeric(cip->channel->users->content[localdata->currentnick]); |
76 | localdata->currentnick++; | |
cb857218 C |
77 | |
78 | if(!np) | |
79 | continue; | |
80 | ||
81 | var_setstr(localdata->variable, np->nick); | |
82 | return (void *)1; | |
83 | } | |
84 | localdata->currentnick++; | |
85 | } | |
86 | ||
87 | return (void *)0; | |
88 | } |