]>
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 | ||
31686847 | 33 | /* @argv usage OK */ |
cb857218 C |
34 | if(!(localdata->variable=var_register(ctx, argv[0], RETURNTYPE_STRING))) { |
35 | free(localdata); | |
36 | return NULL; | |
37 | } | |
38 | ||
39 | if(!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
40 | parseError = "malloc: could not allocate memory for this search."; | |
41 | free(localdata); | |
42 | return NULL; | |
43 | } | |
44 | ||
45 | localdata->currentnick = 0; | |
46 | localdata->lastchanindex = NULL; | |
47 | ||
48 | thenode->returntype = RETURNTYPE_BOOL; | |
49 | thenode->localdata = localdata; | |
50 | thenode->exe = nickiter_exe; | |
51 | thenode->free = nickiter_free; | |
52 | ||
53 | return thenode; | |
54 | } | |
55 | ||
56 | void nickiter_free(searchCtx *ctx, struct searchNode *thenode) { | |
57 | free(thenode->localdata); | |
58 | free(thenode); | |
59 | } | |
60 | ||
61 | void *nickiter_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput) { | |
62 | struct nickiter_localdata *localdata = thenode->localdata; | |
63 | chanindex *cip = (chanindex *)theinput; | |
64 | nick *np; | |
65 | ||
66 | if(cip != localdata->lastchanindex) { | |
67 | localdata->lastchanindex = cip; | |
68 | localdata->currentnick = 0; | |
69 | } | |
70 | ||
71 | if(!cip->channel || !cip->channel->users) | |
72 | return (void *)0; | |
73 | ||
74 | while(localdata->currentnick < cip->channel->users->hashsize) { | |
75 | if(cip->channel->users->content[localdata->currentnick] != nouser) { | |
76721b0b C |
76 | np = getnickbynumeric(cip->channel->users->content[localdata->currentnick]); |
77 | localdata->currentnick++; | |
cb857218 C |
78 | |
79 | if(!np) | |
80 | continue; | |
81 | ||
82 | var_setstr(localdata->variable, np->nick); | |
83 | return (void *)1; | |
84 | } | |
85 | localdata->currentnick++; | |
86 | } | |
87 | ||
88 | return (void *)0; | |
89 | } |