]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/newsearch.h
Add iterators to newsearch, along with an example channel iterator function.
[irc/quakenet/newserv.git] / newsearch / newsearch.h
1 #include "../nick/nick.h"
2 #include "../parser/parser.h"
3 #include "../channel/channel.h"
4 #include "../lib/flags.h"
5 #include "../authext/authext.h"
6
7 #define SEARCHTYPE_CHANNEL 1
8 #define SEARCHTYPE_NICK 2
9 #define SEARCHTYPE_USER 3
10
11
12 #define NSMAX_KILL_LIMIT 500
13 #define NSMAX_GLINE_LIMIT 500
14
15
16 #define NSMAX_GLINE_CLONES 5
17
18
19 /* gline duration, in seconds */
20 #define NSGLINE_DURATION 3600
21
22 #define NSMAX_REASON_LEN 120
23 #define NSMAX_NOTICE_LEN 250
24
25
26 #define RETURNTYPE_BOOL 0x01
27 #define RETURNTYPE_INT 0x02
28 #define RETURNTYPE_STRING 0x03
29 #define RETURNTYPE_TYPE 0xFF
30 #define RETURNTYPE_CONST 0x100
31
32 #define VARIABLE_LEN 10
33 #define MAX_VARIABLES 10
34
35 struct searchNode;
36 struct searchCtx;
37 struct coercedata;
38
39 typedef struct searchNode *(*searchParseFunc)(struct searchCtx *ctx, int type, char *input);
40 typedef void (*replyFunc)(nick *np, char *format, ...);
41 typedef void (*wallFunc)(int level, char *format, ...);
42
43 typedef struct searchNode *(*parseFunc)(struct searchCtx *, int, int, char **);
44 typedef void (*freeFunc)(struct searchCtx *, struct searchNode *);
45 typedef void *(*exeFunc)(struct searchCtx *, struct searchNode *, void *);
46 typedef void (*ChanDisplayFunc)(struct searchCtx *, nick *, chanindex *);
47 typedef void (*NickDisplayFunc)(struct searchCtx *, nick *, nick *);
48 typedef void (*UserDisplayFunc)(struct searchCtx *, nick *, authname *);
49 typedef void (*HeaderFunc)(void *sender, void *arg);
50
51 struct coercedata {
52 struct searchNode *child;
53 union {
54 char *stringbuf;
55 unsigned long val;
56 } u;
57 };
58
59 typedef struct searchNode {
60 int returntype;
61 exeFunc exe;
62 freeFunc free;
63 void *localdata;
64 } searchNode;
65
66 struct searchVariable {
67 char name[VARIABLE_LEN];
68 struct searchNode data;
69 struct coercedata cdata;
70 };
71
72 typedef struct searchCtx {
73 searchParseFunc parser;
74 replyFunc reply;
75 wallFunc wall;
76 void *arg;
77 struct searchVariable vars[MAX_VARIABLES];
78 int lastvar;
79 } searchCtx;
80
81 /* Core functions */
82 /* Logical (BOOL -> BOOL)*/
83 struct searchNode *and_parse(searchCtx *ctx, int type, int argc, char **argv);
84 struct searchNode *not_parse(searchCtx *ctx, int type, int argc, char **argv);
85 struct searchNode *or_parse(searchCtx *ctx, int type, int argc, char **argv);
86
87 /* Comparison (INT -> BOOL) */
88 struct searchNode *eq_parse(searchCtx *ctx, int type, int argc, char **argv);
89 struct searchNode *lt_parse(searchCtx *ctx, int type, int argc, char **argv);
90 struct searchNode *gt_parse(searchCtx *ctx, int type, int argc, char **argv);
91
92 /* String match (STRING -> BOOL) */
93 struct searchNode *match_parse(searchCtx *ctx, int type, int argc, char **argv);
94 struct searchNode *regex_parse(searchCtx *ctx, int type, int argc, char **argv);
95
96 /* Length (STRING -> INT) */
97 struct searchNode *length_parse(searchCtx *ctx, int type, int argc, char **argv);
98
99 /* kill/gline actions (BOOL) */
100 struct searchNode *kill_parse(searchCtx *ctx, int type, int argc, char **argv);
101 struct searchNode *gline_parse(searchCtx *ctx, int type, int argc, char **argv);
102
103 /* notice action (BOOL) */
104 struct searchNode *notice_parse(searchCtx *ctx, int type, int argc, char **argv);
105
106 /* Nick/Channel functions (various types) */
107 struct searchNode *nick_parse(searchCtx *ctx, int type, int argc, char **argv);
108 struct searchNode *modes_parse(searchCtx *ctx, int type, int argc, char **argv);
109
110 /* Nick functions (various types) */
111 struct searchNode *hostmask_parse(searchCtx *ctx, int type, int argc, char **argv);
112 struct searchNode *realname_parse(searchCtx *ctx, int type, int argc, char **argv);
113 struct searchNode *authname_parse(searchCtx *ctx, int type, int argc, char **argv);
114 struct searchNode *authts_parse(searchCtx *ctx, int type, int argc, char **argv);
115 struct searchNode *ident_parse(searchCtx *ctx, int type, int argc, char **argv);
116 struct searchNode *host_parse(searchCtx *ctx, int type, int argc, char **argv);
117 struct searchNode *channel_parse(searchCtx *ctx, int type, int argc, char **argv);
118 struct searchNode *timestamp_parse(searchCtx *ctx, int type, int argc, char **argv);
119 struct searchNode *country_parse(searchCtx *ctx, int type, int argc, char **argv);
120 struct searchNode *ip_parse(searchCtx *ctx, int type, int argc, char **argv);
121 struct searchNode *channels_parse(searchCtx *ctx, int type, int argc, char **argv);
122 struct searchNode *server_parse(searchCtx *ctx, int type, int argc, char **argv);
123 struct searchNode *authid_parse(searchCtx *ctx, int type, int argc, char **argv);
124
125 /* Channel functions (various types) */
126 struct searchNode *exists_parse(searchCtx *ctx, int type, int argc, char **argv);
127 struct searchNode *services_parse(searchCtx *ctx, int type, int argc, char **argv);
128 struct searchNode *size_parse(searchCtx *ctx, int type, int argc, char **argv);
129 struct searchNode *name_parse(searchCtx *ctx, int type, int argc, char **argv);
130 struct searchNode *topic_parse(searchCtx *ctx, int type, int argc, char **argv);
131 struct searchNode *oppct_parse(searchCtx *ctx, int type, int argc, char **argv);
132 struct searchNode *hostpct_parse(searchCtx *ctx, int type, int argc, char **argv);
133 struct searchNode *authedpct_parse(searchCtx *ctx, int type, int argc, char **argv);
134 struct searchNode *kick_parse(searchCtx *ctx, int type, int argc, char **argv);
135
136 /* Interpret a string to give a node */
137 struct searchNode *search_parse(searchCtx *ctx, int type, char *input);
138
139 /* Iteration functions */
140 struct searchNode *any_parse(searchCtx *ctx, int type, int argc, char **argv);
141 struct searchNode *all_parse(searchCtx *ctx, int type, int argc, char **argv);
142 struct searchNode *var_parse(searchCtx *ctx, int type, int argc, char **argv);
143
144 /* Iteraterable functions */
145 struct searchNode *channeliter_parse(searchCtx *ctx, int type, int argc, char **argv);
146
147 /* Force a node to return the thing you want */
148 struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type);
149
150 /* Registration functions */
151 void registersearchterm(char *term, parseFunc parsefunc);
152 void deregistersearchterm(char *term, parseFunc parsefunc);
153 void regchandisp(const char *name, ChanDisplayFunc handler);
154 void unregchandisp(const char *name, ChanDisplayFunc handler);
155 void regnickdisp(const char *name, NickDisplayFunc handler);
156 void unregnickdisp(const char *name, NickDisplayFunc handler);
157 void reguserdisp(const char *name, UserDisplayFunc handler);
158 void unreguserdisp(const char *name, UserDisplayFunc handler);
159
160 /* Special nick* printf */
161 void nssnprintf(char *, size_t, const char *, nick *);
162
163 extern const char *parseError;
164 extern nick *senderNSExtern;
165
166 void printnick(searchCtx *, nick *, nick *);
167 void printuser(searchCtx *, nick *, authname *);
168
169 void nicksearch_exe(struct searchNode *search, searchCtx *sctx, nick *sender, NickDisplayFunc display, int limit);
170 void chansearch_exe(struct searchNode *search, searchCtx *sctx, nick *sender, ChanDisplayFunc display, int limit);
171 void usersearch_exe(struct searchNode *search, searchCtx *ctx, nick *sender, UserDisplayFunc display, int limit);
172
173 int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv);
174 int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv);
175 int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv);
176
177 void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput);
178 void literal_free(searchCtx *ctx, struct searchNode *thenode);
179
180 struct searchVariable *var_register(searchCtx *ctx, int nstype, char *arg, int type);
181 searchNode *var_get(searchCtx *ctx, int nstype, char *arg);
182 void var_setstr(struct searchVariable *v, char *data);
183
184 void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg);
185
186 /* AST functions */
187
188 struct searchASTNode;
189
190 #define AST_NODE_CHILD 1
191 #define AST_NODE_LITERAL 2
192
193 /* items to store in the ast lookup cache */
194 #define AST_RECENT 10
195
196 typedef struct searchASTExpr {
197 int type;
198 union {
199 char *literal;
200 struct searchASTNode *child;
201 } u;
202 } searchASTExpr;
203
204 typedef struct searchASTNode {
205 parseFunc fn;
206 int argc;
207 struct searchASTExpr **argv;
208 } searchASTNode;
209
210 /*
211 *
212 * FEAR THE COMPOUND LITERALS
213 * MUHAHAHHAHAHAHAHAHAAH
214 *
215 */
216 #define __NSASTExpr(x, y, ...) &(searchASTExpr){.type = x, .u.y = __VA_ARGS__}
217 #define __NSASTList(...) (searchASTExpr *[]){__VA_ARGS__}
218 #define __NSASTNode(x, ...) &(searchASTNode){.fn = x, .argc = sizeof(__NSASTList(__VA_ARGS__)) / sizeof(__NSASTList(__VA_ARGS__)[0]), .argv = __NSASTList(__VA_ARGS__)}
219 #define __NSASTChild(...) __NSASTExpr(AST_NODE_CHILD, child, __VA_ARGS__)
220
221 #define NSASTLiteral(data) __NSASTExpr(AST_NODE_LITERAL, literal, data)
222 #define NSASTNode(fn, ...) __NSASTChild(__NSASTNode(fn, __VA_ARGS__))
223
224 searchNode *search_astparse(searchCtx *, int, char *);
225
226 int ast_nicksearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, NickDisplayFunc display, HeaderFunc header, void *headerarg, int limit);
227 int ast_chansearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, ChanDisplayFunc display, HeaderFunc header, void *headerarg, int limit);
228 int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, UserDisplayFunc display, HeaderFunc header, void *headerarg, int limit);
229
230 char *ast_printtree(char *buf, size_t bufsize, searchASTExpr *expr);
231
232 /* erk */
233 extern CommandTree *searchTree;
234
235 extern UserDisplayFunc defaultuserfn;
236 extern NickDisplayFunc defaultnickfn;
237 extern ChanDisplayFunc defaultchanfn;
238