]>
Commit | Line | Data |
---|---|---|
71ad10f8 IB |
1 | /* |
2 | * services functionality | |
3 | */ | |
4 | ||
5 | #include "newsearch.h" | |
6 | ||
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | ||
10 | struct services_localdata { | |
11 | long count; | |
12 | }; | |
13 | ||
14 | void *services_exe(struct searchNode *thenode, int type, void *theinput); | |
15 | void services_free(struct searchNode *thenode); | |
16 | ||
17 | struct searchNode *services_parse(int type, int argc, char **argv) { | |
18 | struct services_localdata *localdata; | |
19 | struct searchNode *thenode; | |
20 | ||
21 | if (type != SEARCHTYPE_CHANNEL) { | |
22 | parseError = "services: this function is only valid for channel searches."; | |
23 | return NULL; | |
24 | } | |
25 | ||
26 | if (argc!=1) { | |
27 | parseError="services: usage: (services number)"; | |
28 | return NULL; | |
29 | } | |
30 | ||
31 | if (!(localdata=(struct services_localdata *)malloc(sizeof(struct services_localdata)))) { | |
32 | parseError = "malloc: could not allocate memory for this search."; | |
33 | return NULL; | |
34 | } | |
35 | ||
36 | localdata->count = strtoul(argv[0],NULL,10); | |
37 | ||
38 | if (!(thenode=(struct searchNode *)malloc(sizeof(struct searchNode)))) { | |
39 | /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */ | |
40 | parseError = "malloc: could not allocate memory for this search."; | |
41 | free(localdata); | |
42 | return NULL; | |
43 | } | |
44 | ||
45 | thenode->returntype = RETURNTYPE_BOOL; | |
46 | thenode->localdata = localdata; | |
47 | thenode->exe = services_exe; | |
48 | thenode->free = services_free; | |
49 | ||
50 | return thenode; | |
51 | } | |
52 | ||
53 | void *services_exe(struct searchNode *thenode, int type, void *theinput) { | |
54 | chanindex *cip = (chanindex *)theinput; | |
55 | nick *np; | |
56 | struct services_localdata *localdata; | |
57 | int i; | |
58 | ||
59 | localdata = thenode->localdata; | |
60 | ||
61 | long count = localdata->count; | |
62 | ||
63 | if (cip->channel == NULL) | |
64 | return falseval(type); | |
65 | ||
66 | for(i=0;i<cip->channel->users->hashsize;i++) { | |
67 | if (cip->channel->users->content[i]==nouser) | |
68 | continue; | |
69 | ||
70 | if (!(np=getnickbynumeric(cip->channel->users->content[i]))) | |
71 | continue; | |
72 | if (IsService(np) && !--count) | |
73 | return trueval(type); /* channel found */ | |
74 | } | |
75 | ||
76 | return falseval(type); /* channel not found */ | |
77 | } | |
78 | ||
79 | void services_free(struct searchNode *thenode) { | |
80 | free(thenode->localdata); | |
81 | free(thenode); | |
82 | } | |
83 |