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
;
96 /* Logical (BOOL -> BOOL)*/
97 struct searchNode
*and_parse(searchCtx
*ctx
, int argc
, char **argv
);
98 struct searchNode
*not_parse(searchCtx
*ctx
, int argc
, char **argv
);
99 struct searchNode
*or_parse(searchCtx
*ctx
, int argc
, char **argv
);
101 /* Comparison (INT -> BOOL) */
102 struct searchNode
*eq_parse(searchCtx
*ctx
, int argc
, char **argv
);
103 struct searchNode
*lt_parse(searchCtx
*ctx
, int argc
, char **argv
);
104 struct searchNode
*gt_parse(searchCtx
*ctx
, int argc
, char **argv
);
106 /* String match (STRING -> BOOL) */
107 struct searchNode
*match_parse(searchCtx
*ctx
, int argc
, char **argv
);
108 struct searchNode
*regex_parse(searchCtx
*ctx
, int argc
, char **argv
);
110 /* Length (STRING -> INT) */
111 struct searchNode
*length_parse(searchCtx
*ctx
, int argc
, char **argv
);
113 /* kill/gline actions (BOOL) */
114 struct searchNode
*kill_parse(searchCtx
*ctx
, int argc
, char **argv
);
115 struct searchNode
*gline_parse(searchCtx
*ctx
, int argc
, char **argv
);
117 /* notice action (BOOL) */
118 struct searchNode
*notice_parse(searchCtx
*ctx
, int argc
, char **argv
);
120 /* Nick/Channel functions (various types) */
121 struct searchNode
*nick_parse(searchCtx
*ctx
, int argc
, char **argv
);
122 struct searchNode
*modes_parse(searchCtx
*ctx
, int argc
, char **argv
);
124 /* Nick functions (various types) */
125 struct searchNode
*hostmask_parse(searchCtx
*ctx
, int argc
, char **argv
);
126 struct searchNode
*realname_parse(searchCtx
*ctx
, int argc
, char **argv
);
127 struct searchNode
*authname_parse(searchCtx
*ctx
, int argc
, char **argv
);
128 struct searchNode
*authts_parse(searchCtx
*ctx
, int argc
, char **argv
);
129 struct searchNode
*ident_parse(searchCtx
*ctx
, int argc
, char **argv
);
130 struct searchNode
*host_parse(searchCtx
*ctx
, int argc
, char **argv
);
131 struct searchNode
*channel_parse(searchCtx
*ctx
, int argc
, char **argv
);
132 struct searchNode
*timestamp_parse(searchCtx
*ctx
, int argc
, char **argv
);
133 struct searchNode
*country_parse(searchCtx
*ctx
, int argc
, char **argv
);
134 struct searchNode
*ip_parse(searchCtx
*ctx
, int argc
, char **argv
);
135 struct searchNode
*channels_parse(searchCtx
*ctx
, int argc
, char **argv
);
136 struct searchNode
*server_parse(searchCtx
*ctx
, int argc
, char **argv
);
137 struct searchNode
*authid_parse(searchCtx
*ctx
, int argc
, char **argv
);
138 struct searchNode
*cidr_parse(searchCtx
*ctx
, int argc
, char **argv
);
140 /* Channel functions (various types) */
141 struct searchNode
*exists_parse(searchCtx
*ctx
, int argc
, char **argv
);
142 struct searchNode
*services_parse(searchCtx
*ctx
, int argc
, char **argv
);
143 struct searchNode
*size_parse(searchCtx
*ctx
, int argc
, char **argv
);
144 struct searchNode
*name_parse(searchCtx
*ctx
, int argc
, char **argv
);
145 struct searchNode
*topic_parse(searchCtx
*ctx
, int argc
, char **argv
);
146 struct searchNode
*oppct_parse(searchCtx
*ctx
, int argc
, char **argv
);
147 struct searchNode
*hostpct_parse(searchCtx
*ctx
, int argc
, char **argv
);
148 struct searchNode
*authedpct_parse(searchCtx
*ctx
, int argc
, char **argv
);
149 struct searchNode
*kick_parse(searchCtx
*ctx
, int argc
, char **argv
);
151 /* Interpret a string to give a node */
152 struct searchNode
*search_parse(searchCtx
*ctx
, char *input
);
154 /* Iteration functions */
155 struct searchNode
*any_parse(searchCtx
*ctx
, int argc
, char **argv
);
156 struct searchNode
*all_parse(searchCtx
*ctx
, int argc
, char **argv
);
157 struct searchNode
*var_parse(searchCtx
*ctx
, int argc
, char **argv
);
159 /* Iteraterable functions */
160 struct searchNode
*channeliter_parse(searchCtx
*ctx
, int argc
, char **argv
);
162 /* Functions that operate on strings */
163 struct searchNode
*cumodes_parse(searchCtx
*ctx
, int argc
, char **argv
);
165 /* Force a node to return the thing you want */
166 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
);
168 /* Registration functions */
169 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
);
170 void deregistersearchcommand(searchCmd
*scmd
);
171 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
);
172 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
);
174 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
);
175 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
);
177 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
);
178 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
);
180 /* Special nick* printf */
181 void nssnprintf(char *, size_t, const char *, nick
*);
183 extern const char *parseError
;
184 extern nick
*senderNSExtern
;
186 void printnick(searchCtx
*, nick
*, nick
*);
187 void printuser(searchCtx
*, nick
*, authname
*);
188 void printchannel(searchCtx
*, nick
*, chanindex
*);
190 void nicksearch_exe(struct searchNode
*search
, searchCtx
*sctx
, nick
*sender
, NickDisplayFunc display
, int limit
);
191 void chansearch_exe(struct searchNode
*search
, searchCtx
*sctx
, nick
*sender
, ChanDisplayFunc display
, int limit
);
192 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, UserDisplayFunc display
, int limit
);
194 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
195 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
196 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
198 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
199 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
);
201 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
);
202 searchNode
*var_get(searchCtx
*ctx
, char *arg
);
203 void var_setstr(struct searchVariable
*v
, char *data
);
205 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*sender
);
209 struct searchASTNode
;
211 #define AST_NODE_CHILD 1
212 #define AST_NODE_LITERAL 2
214 /* items to store in the ast lookup cache */
215 #define AST_RECENT 10
217 struct searchASTExpr
;
219 typedef struct searchASTNode
{
222 struct searchASTExpr
*argv
;
225 typedef struct searchASTExpr
{
229 /* this was originally a pointer, but having it as one means you can't memcpy directly */
230 struct searchASTNode child
;
234 #define __NSASTSizeOfArray(x) (sizeof(x) / sizeof(x[0]))
236 #define __NSASTExpr(x, y, ...) (searchASTExpr){.type = x, .u.y = __VA_ARGS__}
237 #define __NSASTRawNode(x, c, y) (searchASTNode){.fn = x, .argc = c, .argv = y}
239 #define __NSASTSizeNode(x, y) __NSASTRawNode(x, __NSASTSizeOfArray(y), y)
240 #define __NSASTNode(x, ...) __NSASTSizeNode(x, ((searchASTExpr []){__VA_ARGS__}))
242 #define __NSASTChild(...) __NSASTExpr(AST_NODE_CHILD, child, __VA_ARGS__)
244 #define NSASTLiteral(value) __NSASTExpr(AST_NODE_LITERAL, literal, value)
245 #define NSASTNode(fn, ...) __NSASTChild(__NSASTNode(fn, __VA_ARGS__))
246 #define NSASTManualNode(fn, count, children) __NSASTChild(__NSASTRawNode(fn, count, children))
248 searchNode
*search_astparse(searchCtx
*, char *);
250 int ast_nicksearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, NickDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
251 int ast_chansearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, ChanDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
252 int ast_usersearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, UserDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
254 char *ast_printtree(char *buf
, size_t bufsize
, searchASTExpr
*expr
, searchCmd
*cmd
);
256 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void *display
, CommandTree
*sl
, replyFunc reply
, void *sender
);
258 typedef int (*ASTFunc
)(searchASTExpr
*, replyFunc
, void *, wallFunc
, void *, HeaderFunc
, void *, int limit
);
261 extern searchList
*globalterms
;
263 extern searchCmd
*reg_nicksearch
;
264 extern searchCmd
*reg_chansearch
;
265 extern searchCmd
*reg_usersearch
;
267 extern UserDisplayFunc defaultuserfn
;
268 extern NickDisplayFunc defaultnickfn
;
269 extern ChanDisplayFunc defaultchanfn
;