]>
Commit | Line | Data |
---|---|---|
1 | #include "../nick/nick.h" | |
2 | #include "../lib/sstring.h" | |
3 | #include "../parser/parser.h" | |
4 | #include "../channel/channel.h" | |
5 | #include "../lib/flags.h" | |
6 | #include "../authext/authext.h" | |
7 | #include "../patricia/patricia.h" | |
8 | ||
9 | #ifndef __NEWSEARCH_H | |
10 | #define __NEWSEARCH_H | |
11 | ||
12 | #define NSMAX_KILL_LIMIT 500 | |
13 | #define NSMAX_GLINE_LIMIT 500 | |
14 | #define NSMAX_GLINE_CLONES 5 | |
15 | ||
16 | /* gline duration, in seconds */ | |
17 | #define NSGLINE_DURATION 3600 | |
18 | ||
19 | #define NSMAX_REASON_LEN 120 | |
20 | #define NSMAX_NOTICE_LEN 250 | |
21 | #define NSMAX_COMMAND_LEN 20 | |
22 | ||
23 | #define RETURNTYPE_BOOL 0x01 | |
24 | #define RETURNTYPE_INT 0x02 | |
25 | #define RETURNTYPE_STRING 0x03 | |
26 | #define RETURNTYPE_TYPE 0xFF | |
27 | #define RETURNTYPE_CONST 0x100 | |
28 | ||
29 | #define VARIABLE_LEN 10 | |
30 | #define MAX_VARIABLES 10 | |
31 | ||
32 | struct searchNode; | |
33 | struct searchCtx; | |
34 | struct coercedata; | |
35 | ||
36 | typedef struct searchNode *(*searchParseFunc)(struct searchCtx *ctx, char *input); | |
37 | typedef void (*replyFunc)(nick *np, char *format, ...); | |
38 | typedef void (*wallFunc)(int level, char *format, ...); | |
39 | ||
40 | typedef struct searchNode *(*parseFunc)(struct searchCtx *, int, char **); | |
41 | typedef void (*freeFunc)(struct searchCtx *, struct searchNode *); | |
42 | typedef void *(*exeFunc)(struct searchCtx *, struct searchNode *, void *); | |
43 | typedef void (*ChanDisplayFunc)(struct searchCtx *, nick *, chanindex *); | |
44 | typedef void (*NickDisplayFunc)(struct searchCtx *, nick *, nick *); | |
45 | typedef void (*UserDisplayFunc)(struct searchCtx *, nick *, authname *); | |
46 | typedef void (*HeaderFunc)(void *sender, void *arg); | |
47 | ||
48 | struct coercedata { | |
49 | struct searchNode *child; | |
50 | union { | |
51 | char *stringbuf; | |
52 | unsigned long val; | |
53 | } u; | |
54 | }; | |
55 | ||
56 | typedef struct searchNode { | |
57 | int returntype; | |
58 | exeFunc exe; | |
59 | freeFunc free; | |
60 | void *localdata; | |
61 | } searchNode; | |
62 | ||
63 | struct searchVariable { | |
64 | char name[VARIABLE_LEN]; | |
65 | struct searchNode data; | |
66 | struct coercedata cdata; | |
67 | }; | |
68 | ||
69 | typedef struct searchCmd { | |
70 | void *defaultdisplayfunc; | |
71 | sstring *name; | |
72 | CommandHandler handler; | |
73 | struct CommandTree *outputtree; | |
74 | struct CommandTree *searchtree; | |
75 | } searchCmd; | |
76 | ||
77 | typedef struct searchList { | |
78 | void *cmd; | |
79 | sstring *name; | |
80 | char *help; | |
81 | struct searchList *next; | |
82 | } searchList; | |
83 | ||
84 | typedef struct searchCtx { | |
85 | searchParseFunc parser; | |
86 | replyFunc reply; | |
87 | wallFunc wall; | |
88 | void *arg; | |
89 | struct searchVariable vars[MAX_VARIABLES]; | |
90 | int lastvar; | |
91 | struct searchCmd *searchcmd; | |
92 | nick *sender; | |
93 | int limit; | |
94 | void *displayfn; | |
95 | } searchCtx; | |
96 | ||
97 | /* Core functions */ | |
98 | /* Logical (BOOL -> BOOL)*/ | |
99 | struct searchNode *and_parse(searchCtx *ctx, int argc, char **argv); | |
100 | struct searchNode *not_parse(searchCtx *ctx, int argc, char **argv); | |
101 | struct searchNode *or_parse(searchCtx *ctx, int argc, char **argv); | |
102 | ||
103 | /* Comparison (INT -> BOOL) */ | |
104 | struct searchNode *eq_parse(searchCtx *ctx, int argc, char **argv); | |
105 | struct searchNode *lt_parse(searchCtx *ctx, int argc, char **argv); | |
106 | struct searchNode *gt_parse(searchCtx *ctx, int argc, char **argv); | |
107 | ||
108 | /* String match (STRING -> BOOL) */ | |
109 | struct searchNode *match_parse(searchCtx *ctx, int argc, char **argv); | |
110 | struct searchNode *regex_parse(searchCtx *ctx, int argc, char **argv); | |
111 | ||
112 | /* Length (STRING -> INT) */ | |
113 | struct searchNode *length_parse(searchCtx *ctx, int argc, char **argv); | |
114 | ||
115 | /* kill/gline actions (BOOL) */ | |
116 | struct searchNode *kill_parse(searchCtx *ctx, int argc, char **argv); | |
117 | struct searchNode *gline_parse(searchCtx *ctx, int argc, char **argv); | |
118 | ||
119 | /* notice action (BOOL) */ | |
120 | struct searchNode *notice_parse(searchCtx *ctx, int argc, char **argv); | |
121 | ||
122 | /* Nick/Channel functions (various types) */ | |
123 | struct searchNode *nick_parse(searchCtx *ctx, int argc, char **argv); | |
124 | struct searchNode *modes_parse(searchCtx *ctx, int argc, char **argv); | |
125 | ||
126 | /* Nick functions (various types) */ | |
127 | struct searchNode *hostmask_parse(searchCtx *ctx, int argc, char **argv); | |
128 | struct searchNode *realname_parse(searchCtx *ctx, int argc, char **argv); | |
129 | struct searchNode *away_parse(searchCtx *ctx, int argc, char **argv); | |
130 | struct searchNode *authname_parse(searchCtx *ctx, int argc, char **argv); | |
131 | struct searchNode *authts_parse(searchCtx *ctx, int argc, char **argv); | |
132 | struct searchNode *ident_parse(searchCtx *ctx, int argc, char **argv); | |
133 | struct searchNode *host_parse(searchCtx *ctx, int argc, char **argv); | |
134 | struct searchNode *channel_parse(searchCtx *ctx, int argc, char **argv); | |
135 | struct searchNode *timestamp_parse(searchCtx *ctx, int argc, char **argv); | |
136 | struct searchNode *country_parse(searchCtx *ctx, int argc, char **argv); | |
137 | struct searchNode *ip_parse(searchCtx *ctx, int argc, char **argv); | |
138 | struct searchNode *channels_parse(searchCtx *ctx, int argc, char **argv); | |
139 | struct searchNode *server_parse(searchCtx *ctx, int argc, char **argv); | |
140 | struct searchNode *authid_parse(searchCtx *ctx, int argc, char **argv); | |
141 | struct searchNode *cidr_parse(searchCtx *ctx, int argc, char **argv); | |
142 | struct searchNode *ipv6_parse(searchCtx *ctx, int argc, char **argv); | |
143 | ||
144 | /* Channel functions (various types) */ | |
145 | struct searchNode *exists_parse(searchCtx *ctx, int argc, char **argv); | |
146 | struct searchNode *services_parse(searchCtx *ctx, int argc, char **argv); | |
147 | struct searchNode *size_parse(searchCtx *ctx, int argc, char **argv); | |
148 | struct searchNode *name_parse(searchCtx *ctx, int argc, char **argv); | |
149 | struct searchNode *topic_parse(searchCtx *ctx, int argc, char **argv); | |
150 | struct searchNode *oppct_parse(searchCtx *ctx, int argc, char **argv); | |
151 | struct searchNode *cumodecount_parse(searchCtx *ctx, int argc, char **argv); | |
152 | struct searchNode *cumodepct_parse(searchCtx *ctx, int argc, char **argv); | |
153 | struct searchNode *hostpct_parse(searchCtx *ctx, int argc, char **argv); | |
154 | struct searchNode *authedpct_parse(searchCtx *ctx, int argc, char **argv); | |
155 | struct searchNode *kick_parse(searchCtx *ctx, int argc, char **argv); | |
156 | ||
157 | /* Interpret a string to give a node */ | |
158 | struct searchNode *search_parse(searchCtx *ctx, char *input); | |
159 | ||
160 | /* Iteration functions */ | |
161 | struct searchNode *any_parse(searchCtx *ctx, int argc, char **argv); | |
162 | struct searchNode *all_parse(searchCtx *ctx, int argc, char **argv); | |
163 | struct searchNode *var_parse(searchCtx *ctx, int argc, char **argv); | |
164 | ||
165 | /* Iteraterable functions */ | |
166 | struct searchNode *channeliter_parse(searchCtx *ctx, int argc, char **argv); | |
167 | struct searchNode *nickiter_parse(searchCtx *ctx, int argc, char **argv); | |
168 | ||
169 | /* Functions that operate on strings */ | |
170 | struct searchNode *cumodes_parse(searchCtx *ctx, int argc, char **argv); | |
171 | ||
172 | /* Force a node to return the thing you want */ | |
173 | struct searchNode *coerceNode(searchCtx *ctx, struct searchNode *thenode, int type); | |
174 | ||
175 | /* Registration functions */ | |
176 | searchCmd *registersearchcommand(char *name, int level, CommandHandler cmd, void *defaultdisplayfunc); | |
177 | void deregistersearchcommand(searchCmd *scmd); | |
178 | void registersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc, int level, char *help); | |
179 | void deregistersearchterm(searchCmd *cmd, char *term, parseFunc parsefunc); | |
180 | ||
181 | void registerglobalsearchterm(char *term, parseFunc parsefunc, char *help); | |
182 | void deregisterglobalsearchterm(char *term, parseFunc parsefunc); | |
183 | ||
184 | void regdisp( searchCmd *cmd, const char *name, void *handler, int level, char *help); | |
185 | void unregdisp( searchCmd *cmd, const char *name, void *handler); | |
186 | ||
187 | /* Special nick* printf */ | |
188 | void nssnprintf(char *, size_t, const char *, nick *); | |
189 | ||
190 | #ifdef NEWSEARCH_NEWPARSER | |
191 | void displaystrerror(replyFunc reply, nick *np, const char *input); | |
192 | #endif | |
193 | ||
194 | extern const char *parseError; | |
195 | extern nick *senderNSExtern; | |
196 | ||
197 | void printnick(searchCtx *, nick *, nick *); | |
198 | void printuser(searchCtx *, nick *, authname *); | |
199 | void printchannel(searchCtx *, nick *, chanindex *); | |
200 | ||
201 | void nicksearch_exe(struct searchNode *search, searchCtx *sctx); | |
202 | void chansearch_exe(struct searchNode *search, searchCtx *sctx); | |
203 | void usersearch_exe(struct searchNode *search, searchCtx *ctx); | |
204 | ||
205 | int do_nicksearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv); | |
206 | int do_chansearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv); | |
207 | int do_usersearch_real(replyFunc reply, wallFunc wall, void *source, int cargc, char **cargv); | |
208 | ||
209 | void *literal_exe(searchCtx *ctx, struct searchNode *thenode, void *theinput); | |
210 | void literal_free(searchCtx *ctx, struct searchNode *thenode); | |
211 | ||
212 | struct searchVariable *var_register(searchCtx *ctx, char *arg, int type); | |
213 | searchNode *var_get(searchCtx *ctx, char *arg); | |
214 | void var_setstr(struct searchVariable *v, char *data); | |
215 | ||
216 | void newsearch_ctxinit(searchCtx *ctx, searchParseFunc searchfn, replyFunc replyfn, wallFunc wallfn, void *arg, searchCmd *cmd, nick *sender, void *displayfn, int limit); | |
217 | ||
218 | /* AST functions */ | |
219 | ||
220 | struct searchASTNode; | |
221 | ||
222 | #define AST_NODE_CHILD 1 | |
223 | #define AST_NODE_LITERAL 2 | |
224 | ||
225 | /* items to store in the ast lookup cache */ | |
226 | #define AST_RECENT 10 | |
227 | ||
228 | struct searchASTExpr; | |
229 | ||
230 | typedef struct searchASTNode { | |
231 | parseFunc fn; | |
232 | int argc; | |
233 | struct searchASTExpr *argv; | |
234 | } searchASTNode; | |
235 | ||
236 | typedef struct searchASTExpr { | |
237 | int type; | |
238 | union { | |
239 | char *literal; | |
240 | /* this was originally a pointer, but having it as one means you can't memcpy directly */ | |
241 | struct searchASTNode child; | |
242 | } u; | |
243 | } searchASTExpr; | |
244 | ||
245 | typedef struct searchASTCache { | |
246 | searchASTExpr *tree; | |
247 | searchASTExpr *cache[AST_RECENT]; | |
248 | int nextpos; | |
249 | } searchASTCache; | |
250 | ||
251 | #define __NSASTSizeOfArray(x) (sizeof(x) / sizeof(x[0])) | |
252 | ||
253 | #define __NSASTExpr(x, y, ...) (searchASTExpr){.type = x, .u.y = __VA_ARGS__} | |
254 | #define __NSASTRawNode(x, c, y) (searchASTNode){.fn = x, .argc = c, .argv = y} | |
255 | ||
256 | #define __NSASTSizeNode(x, y) __NSASTRawNode(x, __NSASTSizeOfArray(y), y) | |
257 | #define __NSASTNode(x, ...) __NSASTSizeNode(x, ((searchASTExpr []){__VA_ARGS__})) | |
258 | ||
259 | #define __NSASTChild(...) __NSASTExpr(AST_NODE_CHILD, child, __VA_ARGS__) | |
260 | ||
261 | #define NSASTLiteral(value) __NSASTExpr(AST_NODE_LITERAL, literal, value) | |
262 | #define NSASTNode(fn, ...) __NSASTChild(__NSASTNode(fn, __VA_ARGS__)) | |
263 | #define NSASTManualNode(fn, count, children) __NSASTChild(__NSASTRawNode(fn, count, children)) | |
264 | ||
265 | searchNode *search_astparse(searchCtx *, char *); | |
266 | ||
267 | int ast_nicksearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, NickDisplayFunc display, HeaderFunc header, void *headerarg, int limit); | |
268 | int ast_chansearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, ChanDisplayFunc display, HeaderFunc header, void *headerarg, int limit); | |
269 | int ast_usersearch(searchASTExpr *tree, replyFunc reply, void *sender, wallFunc wall, UserDisplayFunc display, HeaderFunc header, void *headerarg, int limit); | |
270 | ||
271 | char *ast_printtree(char *buf, size_t bufsize, searchASTExpr *expr, searchCmd *cmd); | |
272 | ||
273 | int parseopts(int cargc, char **cargv, int *arg, int *limit, void **subset, void *display, CommandTree *sl, replyFunc reply, void *sender); | |
274 | ||
275 | typedef int (*ASTFunc)(searchASTExpr *, replyFunc, void *, wallFunc, void *, HeaderFunc, void *, int limit); | |
276 | ||
277 | /* erk */ | |
278 | extern searchList *globalterms; | |
279 | ||
280 | extern searchCmd *reg_nicksearch; | |
281 | extern searchCmd *reg_chansearch; | |
282 | extern searchCmd *reg_usersearch; | |
283 | ||
284 | extern UserDisplayFunc defaultuserfn; | |
285 | extern NickDisplayFunc defaultnickfn; | |
286 | extern ChanDisplayFunc defaultchanfn; | |
287 | ||
288 | struct searchNode *argtoconststr(char *command, searchCtx *ctx, char *arg, char **p); | |
289 | ||
290 | #endif |