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"
12 #define NSMAX_KILL_LIMIT 500
13 #define NSMAX_GLINE_LIMIT 500
14 #define NSMAX_GLINE_CLONES 5
16 /* gline duration, in seconds */
17 #define NSGLINE_DURATION 3600
19 #define NSMAX_REASON_LEN 120
20 #define NSMAX_NOTICE_LEN 250
21 #define NSMAX_COMMAND_LEN 20
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
29 #define VARIABLE_LEN 10
30 #define MAX_VARIABLES 10
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
, ...);
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
);
49 struct searchNode
*child
;
56 typedef struct searchNode
{
63 struct searchVariable
{
64 char name
[VARIABLE_LEN
];
65 struct searchNode data
;
66 struct coercedata cdata
;
69 typedef struct searchCmd
{
70 void *defaultdisplayfunc
;
72 CommandHandler handler
;
73 struct CommandTree
*outputtree
;
74 struct CommandTree
*searchtree
;
77 typedef struct searchList
{
81 struct searchList
*next
;
84 typedef struct searchCtx
{
85 searchParseFunc parser
;
89 struct searchVariable vars
[MAX_VARIABLES
];
91 struct searchCmd
*searchcmd
;
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
);
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
);
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
);
112 /* Length (STRING -> INT) */
113 struct searchNode
*length_parse(searchCtx
*ctx
, int argc
, char **argv
);
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
);
119 /* notice action (BOOL) */
120 struct searchNode
*notice_parse(searchCtx
*ctx
, int argc
, char **argv
);
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
);
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
*authname_parse(searchCtx
*ctx
, int argc
, char **argv
);
130 struct searchNode
*authts_parse(searchCtx
*ctx
, int argc
, char **argv
);
131 struct searchNode
*ident_parse(searchCtx
*ctx
, int argc
, char **argv
);
132 struct searchNode
*host_parse(searchCtx
*ctx
, int argc
, char **argv
);
133 struct searchNode
*channel_parse(searchCtx
*ctx
, int argc
, char **argv
);
134 struct searchNode
*timestamp_parse(searchCtx
*ctx
, int argc
, char **argv
);
135 struct searchNode
*country_parse(searchCtx
*ctx
, int argc
, char **argv
);
136 struct searchNode
*ip_parse(searchCtx
*ctx
, int argc
, char **argv
);
137 struct searchNode
*channels_parse(searchCtx
*ctx
, int argc
, char **argv
);
138 struct searchNode
*server_parse(searchCtx
*ctx
, int argc
, char **argv
);
139 struct searchNode
*authid_parse(searchCtx
*ctx
, int argc
, char **argv
);
140 struct searchNode
*cidr_parse(searchCtx
*ctx
, int argc
, char **argv
);
142 /* Channel functions (various types) */
143 struct searchNode
*exists_parse(searchCtx
*ctx
, int argc
, char **argv
);
144 struct searchNode
*services_parse(searchCtx
*ctx
, int argc
, char **argv
);
145 struct searchNode
*size_parse(searchCtx
*ctx
, int argc
, char **argv
);
146 struct searchNode
*name_parse(searchCtx
*ctx
, int argc
, char **argv
);
147 struct searchNode
*topic_parse(searchCtx
*ctx
, int argc
, char **argv
);
148 struct searchNode
*oppct_parse(searchCtx
*ctx
, int argc
, char **argv
);
149 struct searchNode
*cumodecount_parse(searchCtx
*ctx
, int argc
, char **argv
);
150 struct searchNode
*cumodepct_parse(searchCtx
*ctx
, int argc
, char **argv
);
151 struct searchNode
*hostpct_parse(searchCtx
*ctx
, int argc
, char **argv
);
152 struct searchNode
*authedpct_parse(searchCtx
*ctx
, int argc
, char **argv
);
153 struct searchNode
*kick_parse(searchCtx
*ctx
, int argc
, char **argv
);
155 /* Interpret a string to give a node */
156 struct searchNode
*search_parse(searchCtx
*ctx
, char *input
);
158 /* Iteration functions */
159 struct searchNode
*any_parse(searchCtx
*ctx
, int argc
, char **argv
);
160 struct searchNode
*all_parse(searchCtx
*ctx
, int argc
, char **argv
);
161 struct searchNode
*var_parse(searchCtx
*ctx
, int argc
, char **argv
);
163 /* Iteraterable functions */
164 struct searchNode
*channeliter_parse(searchCtx
*ctx
, int argc
, char **argv
);
165 struct searchNode
*nickiter_parse(searchCtx
*ctx
, int argc
, char **argv
);
167 /* Functions that operate on strings */
168 struct searchNode
*cumodes_parse(searchCtx
*ctx
, int argc
, char **argv
);
170 /* Force a node to return the thing you want */
171 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
);
173 /* Registration functions */
174 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
);
175 void deregistersearchcommand(searchCmd
*scmd
);
176 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
);
177 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
);
179 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
);
180 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
);
182 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
);
183 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
);
185 /* Special nick* printf */
186 void nssnprintf(char *, size_t, const char *, nick
*);
188 extern const char *parseError
;
189 extern nick
*senderNSExtern
;
191 void printnick(searchCtx
*, nick
*, nick
*);
192 void printuser(searchCtx
*, nick
*, authname
*);
193 void printchannel(searchCtx
*, nick
*, chanindex
*);
195 void nicksearch_exe(struct searchNode
*search
, searchCtx
*sctx
);
196 void chansearch_exe(struct searchNode
*search
, searchCtx
*sctx
);
197 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
);
199 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
200 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
201 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
203 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
204 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
);
206 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
);
207 searchNode
*var_get(searchCtx
*ctx
, char *arg
);
208 void var_setstr(struct searchVariable
*v
, char *data
);
210 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*sender
, void *displayfn
, int limit
);
214 struct searchASTNode
;
216 #define AST_NODE_CHILD 1
217 #define AST_NODE_LITERAL 2
219 /* items to store in the ast lookup cache */
220 #define AST_RECENT 10
222 struct searchASTExpr
;
224 typedef struct searchASTNode
{
227 struct searchASTExpr
*argv
;
230 typedef struct searchASTExpr
{
234 /* this was originally a pointer, but having it as one means you can't memcpy directly */
235 struct searchASTNode child
;
239 #define __NSASTSizeOfArray(x) (sizeof(x) / sizeof(x[0]))
241 #define __NSASTExpr(x, y, ...) (searchASTExpr){.type = x, .u.y = __VA_ARGS__}
242 #define __NSASTRawNode(x, c, y) (searchASTNode){.fn = x, .argc = c, .argv = y}
244 #define __NSASTSizeNode(x, y) __NSASTRawNode(x, __NSASTSizeOfArray(y), y)
245 #define __NSASTNode(x, ...) __NSASTSizeNode(x, ((searchASTExpr []){__VA_ARGS__}))
247 #define __NSASTChild(...) __NSASTExpr(AST_NODE_CHILD, child, __VA_ARGS__)
249 #define NSASTLiteral(value) __NSASTExpr(AST_NODE_LITERAL, literal, value)
250 #define NSASTNode(fn, ...) __NSASTChild(__NSASTNode(fn, __VA_ARGS__))
251 #define NSASTManualNode(fn, count, children) __NSASTChild(__NSASTRawNode(fn, count, children))
253 searchNode
*search_astparse(searchCtx
*, char *);
255 int ast_nicksearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, NickDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
256 int ast_chansearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, ChanDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
257 int ast_usersearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, UserDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
259 char *ast_printtree(char *buf
, size_t bufsize
, searchASTExpr
*expr
, searchCmd
*cmd
);
261 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
);
263 typedef int (*ASTFunc
)(searchASTExpr
*, replyFunc
, void *, wallFunc
, void *, HeaderFunc
, void *, int limit
);
266 extern searchList
*globalterms
;
268 extern searchCmd
*reg_nicksearch
;
269 extern searchCmd
*reg_chansearch
;
270 extern searchCmd
*reg_usersearch
;
272 extern UserDisplayFunc defaultuserfn
;
273 extern NickDisplayFunc defaultnickfn
;
274 extern ChanDisplayFunc defaultchanfn
;
276 struct searchNode
*argtoconststr(char *command
, searchCtx
*ctx
, char *arg
, char **p
);