]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-server.c
merge
[irc/quakenet/newserv.git] / newsearch / ns-server.c
1 /*
2 * SERVER functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "../irc/irc_config.h"
12 #include "../lib/irc_string.h"
13 #include "../core/modules.h"
14 #include "../server/server.h"
15
16 void *server_exe_bool(searchCtx *ctx, struct searchNode *thenode, void *theinput);
17 void *server_exe_str(searchCtx *ctx, struct searchNode *thenode, void *theinput);
18 void server_free(searchCtx *ctx, struct searchNode *thenode);
19
20 int ext;
21
22 struct searchNode *server_parse(searchCtx *ctx, int argc, char **argv) {
23 struct searchNode *thenode;
24 int i;
25 long numeric;
26
27 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
28 parseError = "malloc: could not allocate memory for this search.";
29 return NULL;
30 }
31
32 if (argc>0) {
33 struct searchNode *servername;
34 char *p;
35
36 if (!(servername=argtoconststr("server", ctx, argv[0], &p))) {
37 free(thenode);
38 return NULL;
39 }
40
41 numeric = -1;
42 for(i=0;i<MAXSERVERS;i++) {
43 sstring *n = serverlist[i].name;
44 if(n && !strcmp(n->content, p)) {
45 numeric = i;
46 break;
47 }
48 }
49
50 (servername->free)(ctx, servername);
51
52 if(numeric == -1) {
53 parseError = "server: server not found.";
54 free(thenode);
55 return NULL;
56 }
57
58 thenode->returntype = RETURNTYPE_BOOL;
59 thenode->localdata = (void *)numeric;
60
61 thenode->exe = server_exe_bool;
62 } else {
63 thenode->returntype = RETURNTYPE_STRING;
64 thenode->localdata = NULL;
65
66 thenode->exe = server_exe_str;
67 }
68
69 thenode->free = server_free;
70
71 return thenode;
72 }
73
74 void *server_exe_bool(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
75 nick *np = (nick *)theinput;
76 long server = (long)thenode->localdata;
77
78 if(homeserver(np->numeric) == server)
79 return (void *)1;
80
81 return (void *)0;
82 }
83
84 void *server_exe_str(searchCtx *ctx, struct searchNode *thenode, void *theinput) {
85 nick *np = (nick *)theinput;
86 sstring *n = serverlist[homeserver(np->numeric)].name;
87
88 if(!n)
89 return NULL;
90
91 return n->content;
92 }
93
94 void server_free(searchCtx *ctx, struct searchNode *thenode) {
95 free(thenode);
96 }