]> jfr.im git - irc/quakenet/newserv.git/blame - patriciasearch/patriciasearch.c
nick->node
[irc/quakenet/newserv.git] / patriciasearch / patriciasearch.c
CommitLineData
3128667f
P
1#include <stdio.h>
2#include <stdarg.h>
3#include <string.h>
4
5#include "../control/control.h"
6#include "../irc/irc_config.h"
7#include "../lib/irc_string.h"
8#include "../parser/parser.h"
9#include "../lib/splitline.h"
10#include "../lib/version.h"
11#include "../lib/stringbuf.h"
12#include "../lib/strlfunc.h"
13#include "patriciasearch.h"
14
3128667f
P
15searchCmd *reg_nodesearch;
16
17int do_pnodesearch(void *source, int cargc, char **cargv);
18
19NodeDisplayFunc defaultpnodefn = printnode;
20
3128667f 21void _init() {
3128667f
P
22 reg_nodesearch = (searchCmd *)registersearchcommand("nodesearch",NO_OPER,do_pnodesearch, printnode);
23
ea634fc1
P
24 registersearchterm(reg_nodesearch, "users", ps_users_parse, 0, "");
25 registersearchterm(reg_nodesearch, "nick", ps_nick_parse, 0, "");
a4b2a5b9 26 registersearchterm(reg_nodesearch, "ipvsix", ps_ipv6_parse, 0, "");
3128667f
P
27}
28
29void _fini() {
3128667f
P
30 deregistersearchcommand( reg_nodesearch );
31}
32
33static void controlwallwrapper(int level, char *format, ...) {
34 char buf[1024];
35 va_list ap;
36
37 va_start(ap, format);
38 vsnprintf(buf, sizeof(buf), format, ap);
39 controlwall(NO_OPER, level, "%s", buf);
40 va_end(ap);
41}
42
43int do_pnodesearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv) {
a4b2a5b9 44 nick *sender = source;
3128667f
P
45 int limit=500;
46 int arg=0;
47 NodeDisplayFunc display=defaultpnodefn;
3128667f
P
48 int ret;
49 patricia_node_t *subset = iptree->head;
a4b2a5b9 50 parsertree *tree;
3128667f 51
a4b2a5b9
P
52 if (cargc<1) {
53 reply( sender, "Usage: [flags] <criteria>");
96135b6a 54 reply( sender, "For help, see help nodesearch");
a4b2a5b9
P
55 return CMD_OK;
56 }
3128667f 57
6cb1b086 58 ret = parseopts(cargc, cargv, &arg, &limit, (void *)&subset, (void *)&display, reg_nodesearch->outputtree, reply, sender);
3128667f
P
59 if(ret != CMD_OK)
60 return ret;
61
62 if (arg>=cargc) {
63 reply(sender,"No search terms - aborting.");
64 return CMD_ERROR;
65 }
66
67 if (arg<(cargc-1)) {
68 rejoinline(cargv[arg],cargc-arg);
69 }
70
a4b2a5b9
P
71 tree = parse_string(reg_nodesearch, cargv[arg]);
72 if(!tree) {
73 displaystrerror(reply, sender, cargv[arg]);
3128667f
P
74 return CMD_ERROR;
75 }
76
a4b2a5b9 77 ast_nodesearch(tree->root, reply, sender, wall, display, NULL, NULL, limit);
3128667f 78
a4b2a5b9 79 parse_free(tree);
3128667f
P
80
81 return CMD_OK;
82}
83
84int do_pnodesearch(void *source, int cargc, char **cargv) {
85 return do_pnodesearch_real(controlreply, controlwallwrapper, source, cargc, cargv);
86}
87
6b2202d0 88void pnodesearch_exe(struct searchNode *search, searchCtx *ctx, patricia_node_t *subset) {
3128667f
P
89 int matches = 0;
90 patricia_node_t *node;
6b2202d0 91 nick *sender = ctx->sender;
a4b2a5b9 92 senderNSExtern = sender;
6b2202d0
CP
93 int limit = ctx->limit;
94 NodeDisplayFunc display = ctx->displayfn;
3128667f
P
95
96 /* Get a marker value to mark "seen" channels for unique count */
97 //nmarker=nextnodemarker();
98
99 /* The top-level node needs to return a BOOL */
100 search=coerceNode(ctx, search, RETURNTYPE_BOOL);
101
102 PATRICIA_WALK(subset, node) {
103 if ((search->exe)(ctx, search, node)) {
104 if (matches<limit)
105 display(ctx, sender, node);
106
107 if (matches==limit)
108 ctx->reply(sender, "--- More than %d matches, skipping the rest",limit);
109 matches++;
110 }
111 }
112 PATRICIA_WALK_END;
113
114 ctx->reply(sender,"--- End of list: %d matches",
115 matches);
116}
117
118