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"
9 #define NSMAX_KILL_LIMIT 500
10 #define NSMAX_GLINE_LIMIT 500
11 #define NSMAX_GLINE_CLONES 5
13 /* gline duration, in seconds */
14 #define NSGLINE_DURATION 3600
16 #define NSMAX_REASON_LEN 120
17 #define NSMAX_NOTICE_LEN 250
18 #define NSMAX_COMMAND_LEN 20
20 #define RETURNTYPE_BOOL 0x01
21 #define RETURNTYPE_INT 0x02
22 #define RETURNTYPE_STRING 0x03
23 #define RETURNTYPE_TYPE 0xFF
24 #define RETURNTYPE_CONST 0x100
26 #define VARIABLE_LEN 10
27 #define MAX_VARIABLES 10
33 typedef struct searchNode
*(*searchParseFunc
)(struct searchCtx
*ctx
, char *input
);
34 typedef void (*replyFunc
)(nick
*np
, char *format
, ...);
35 typedef void (*wallFunc
)(int level
, char *format
, ...);
37 typedef struct searchNode
*(*parseFunc
)(struct searchCtx
*, int, char **);
38 typedef void (*freeFunc
)(struct searchCtx
*, struct searchNode
*);
39 typedef void *(*exeFunc
)(struct searchCtx
*, struct searchNode
*, void *);
40 typedef void (*ChanDisplayFunc
)(struct searchCtx
*, nick
*, chanindex
*);
41 typedef void (*NickDisplayFunc
)(struct searchCtx
*, nick
*, nick
*);
42 typedef void (*UserDisplayFunc
)(struct searchCtx
*, nick
*, authname
*);
43 typedef void (*HeaderFunc
)(void *sender
, void *arg
);
46 struct searchNode
*child
;
53 typedef struct searchNode
{
60 struct searchVariable
{
61 char name
[VARIABLE_LEN
];
62 struct searchNode data
;
63 struct coercedata cdata
;
66 typedef struct searchCmd
{
67 void *defaultdisplayfunc
;
69 CommandHandler handler
;
70 struct CommandTree
*outputtree
;
71 struct CommandTree
*searchtree
;
74 typedef struct searchList
{
78 struct searchList
*next
;
81 typedef struct searchCtx
{
82 searchParseFunc parser
;
86 struct searchVariable vars
[MAX_VARIABLES
];
88 struct searchCmd
*searchcmd
;
93 /* Logical (BOOL -> BOOL)*/
94 struct searchNode
*and_parse(searchCtx
*ctx
, int argc
, char **argv
);
95 struct searchNode
*not_parse(searchCtx
*ctx
, int argc
, char **argv
);
96 struct searchNode
*or_parse(searchCtx
*ctx
, int argc
, char **argv
);
98 /* Comparison (INT -> BOOL) */
99 struct searchNode
*eq_parse(searchCtx
*ctx
, int argc
, char **argv
);
100 struct searchNode
*lt_parse(searchCtx
*ctx
, int argc
, char **argv
);
101 struct searchNode
*gt_parse(searchCtx
*ctx
, int argc
, char **argv
);
103 /* String match (STRING -> BOOL) */
104 struct searchNode
*match_parse(searchCtx
*ctx
, int argc
, char **argv
);
105 struct searchNode
*regex_parse(searchCtx
*ctx
, int argc
, char **argv
);
107 /* Length (STRING -> INT) */
108 struct searchNode
*length_parse(searchCtx
*ctx
, int argc
, char **argv
);
110 /* kill/gline actions (BOOL) */
111 struct searchNode
*kill_parse(searchCtx
*ctx
, int argc
, char **argv
);
112 struct searchNode
*gline_parse(searchCtx
*ctx
, int argc
, char **argv
);
114 /* notice action (BOOL) */
115 struct searchNode
*notice_parse(searchCtx
*ctx
, int argc
, char **argv
);
117 /* Nick/Channel functions (various types) */
118 struct searchNode
*nick_parse(searchCtx
*ctx
, int argc
, char **argv
);
119 struct searchNode
*modes_parse(searchCtx
*ctx
, int argc
, char **argv
);
121 /* Nick functions (various types) */
122 struct searchNode
*hostmask_parse(searchCtx
*ctx
, int argc
, char **argv
);
123 struct searchNode
*realname_parse(searchCtx
*ctx
, int argc
, char **argv
);
124 struct searchNode
*authname_parse(searchCtx
*ctx
, int argc
, char **argv
);
125 struct searchNode
*authts_parse(searchCtx
*ctx
, int argc
, char **argv
);
126 struct searchNode
*ident_parse(searchCtx
*ctx
, int argc
, char **argv
);
127 struct searchNode
*host_parse(searchCtx
*ctx
, int argc
, char **argv
);
128 struct searchNode
*channel_parse(searchCtx
*ctx
, int argc
, char **argv
);
129 struct searchNode
*timestamp_parse(searchCtx
*ctx
, int argc
, char **argv
);
130 struct searchNode
*country_parse(searchCtx
*ctx
, int argc
, char **argv
);
131 struct searchNode
*ip_parse(searchCtx
*ctx
, int argc
, char **argv
);
132 struct searchNode
*channels_parse(searchCtx
*ctx
, int argc
, char **argv
);
133 struct searchNode
*server_parse(searchCtx
*ctx
, int argc
, char **argv
);
134 struct searchNode
*authid_parse(searchCtx
*ctx
, int argc
, char **argv
);
136 /* Channel functions (various types) */
137 struct searchNode
*exists_parse(searchCtx
*ctx
, int argc
, char **argv
);
138 struct searchNode
*services_parse(searchCtx
*ctx
, int argc
, char **argv
);
139 struct searchNode
*size_parse(searchCtx
*ctx
, int argc
, char **argv
);
140 struct searchNode
*name_parse(searchCtx
*ctx
, int argc
, char **argv
);
141 struct searchNode
*topic_parse(searchCtx
*ctx
, int argc
, char **argv
);
142 struct searchNode
*oppct_parse(searchCtx
*ctx
, int argc
, char **argv
);
143 struct searchNode
*hostpct_parse(searchCtx
*ctx
, int argc
, char **argv
);
144 struct searchNode
*authedpct_parse(searchCtx
*ctx
, int argc
, char **argv
);
145 struct searchNode
*kick_parse(searchCtx
*ctx
, int argc
, char **argv
);
147 /* Interpret a string to give a node */
148 struct searchNode
*search_parse(searchCtx
*ctx
, char *input
);
150 /* Iteration functions */
151 struct searchNode
*any_parse(searchCtx
*ctx
, int argc
, char **argv
);
152 struct searchNode
*all_parse(searchCtx
*ctx
, int argc
, char **argv
);
153 struct searchNode
*var_parse(searchCtx
*ctx
, int argc
, char **argv
);
155 /* Iteraterable functions */
156 struct searchNode
*channeliter_parse(searchCtx
*ctx
, int argc
, char **argv
);
158 /* Force a node to return the thing you want */
159 struct searchNode
*coerceNode(searchCtx
*ctx
, struct searchNode
*thenode
, int type
);
161 /* Registration functions */
162 searchCmd
*registersearchcommand(char *name
, int level
, CommandHandler cmd
, void *defaultdisplayfunc
);
163 void deregistersearchcommand(searchCmd
*scmd
);
164 void registersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
, int level
, char *help
);
165 void deregistersearchterm(searchCmd
*cmd
, char *term
, parseFunc parsefunc
);
167 void registerglobalsearchterm(char *term
, parseFunc parsefunc
, char *help
);
168 void deregisterglobalsearchterm(char *term
, parseFunc parsefunc
);
170 void regdisp( searchCmd
*cmd
, const char *name
, void *handler
, int level
, char *help
);
171 void unregdisp( searchCmd
*cmd
, const char *name
, void *handler
);
173 /* Special nick* printf */
174 void nssnprintf(char *, size_t, const char *, nick
*);
176 extern const char *parseError
;
177 extern nick
*senderNSExtern
;
179 void printnick(searchCtx
*, nick
*, nick
*);
180 void printuser(searchCtx
*, nick
*, authname
*);
181 void printchannel(searchCtx
*, nick
*, chanindex
*);
183 void nicksearch_exe(struct searchNode
*search
, searchCtx
*sctx
, nick
*sender
, NickDisplayFunc display
, int limit
);
184 void chansearch_exe(struct searchNode
*search
, searchCtx
*sctx
, nick
*sender
, ChanDisplayFunc display
, int limit
);
185 void usersearch_exe(struct searchNode
*search
, searchCtx
*ctx
, nick
*sender
, UserDisplayFunc display
, int limit
);
187 int do_nicksearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
188 int do_chansearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
189 int do_usersearch_real(replyFunc reply
, wallFunc wall
, void *source
, int cargc
, char **cargv
);
191 void *literal_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
192 void literal_free(searchCtx
*ctx
, struct searchNode
*thenode
);
194 struct searchVariable
*var_register(searchCtx
*ctx
, char *arg
, int type
);
195 searchNode
*var_get(searchCtx
*ctx
, char *arg
);
196 void var_setstr(struct searchVariable
*v
, char *data
);
198 void newsearch_ctxinit(searchCtx
*ctx
, searchParseFunc searchfn
, replyFunc replyfn
, wallFunc wallfn
, void *arg
, searchCmd
*cmd
, nick
*sender
);
202 struct searchASTNode
;
204 #define AST_NODE_CHILD 1
205 #define AST_NODE_LITERAL 2
207 /* items to store in the ast lookup cache */
208 #define AST_RECENT 10
210 typedef struct searchASTExpr
{
214 struct searchASTNode
*child
;
218 typedef struct searchASTNode
{
221 struct searchASTExpr
**argv
;
226 * FEAR THE COMPOUND LITERALS
227 * MUHAHAHHAHAHAHAHAHAAH
230 #define __NSASTExpr(x, y, ...) &(searchASTExpr){.type = x, .u.y = __VA_ARGS__}
231 #define __NSASTList(...) (searchASTExpr *[]){__VA_ARGS__}
232 #define __NSASTNode(x, ...) &(searchASTNode){.fn = x, .argc = sizeof(__NSASTList(__VA_ARGS__)) / sizeof(__NSASTList(__VA_ARGS__)[0]), .argv = __NSASTList(__VA_ARGS__)}
233 #define __NSASTChild(...) __NSASTExpr(AST_NODE_CHILD, child, __VA_ARGS__)
235 #define NSASTLiteral(data) __NSASTExpr(AST_NODE_LITERAL, literal, data)
236 #define NSASTNode(fn, ...) __NSASTChild(__NSASTNode(fn, __VA_ARGS__))
238 searchNode
*search_astparse(searchCtx
*, char *);
240 int ast_nicksearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, NickDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
241 int ast_chansearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, ChanDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
242 int ast_usersearch(searchASTExpr
*tree
, replyFunc reply
, void *sender
, wallFunc wall
, UserDisplayFunc display
, HeaderFunc header
, void *headerarg
, int limit
);
244 char *ast_printtree(char *buf
, size_t bufsize
, searchASTExpr
*expr
, searchCmd
*cmd
);
246 int parseopts(int cargc
, char **cargv
, int *arg
, int *limit
, void **subset
, void **display
, CommandTree
*sl
, replyFunc reply
, void *sender
);
249 extern searchList
*globalterms
;
251 extern searchCmd
*reg_nicksearch
;
252 extern searchCmd
*reg_chansearch
;
253 extern searchCmd
*reg_usersearch
;
255 extern UserDisplayFunc defaultuserfn
;
256 extern NickDisplayFunc defaultnickfn
;
257 extern ChanDisplayFunc defaultchanfn
;