]> jfr.im git - irc/quakenet/snircd-patchqueue.git/blame - split.patch
split.patch: add SPLIT_BURST state flag, added wildcard support for mask in /SPLIT...
[irc/quakenet/snircd-patchqueue.git] / split.patch
CommitLineData
12e82c05 1work in progress
2Add split functionality into ircd
3Add /split command
4Add /stats S/split (make /STATS s/S case sensitive, s spoofhosts)
5Add feature SPLIT
6Add split.c split.h m_split.c
7
7efbed3b 8NOTE: feature SPLIT is default enabled as that is just easier when working on this patch.
9
0c63b280 10diff -r bcffa56c93d4 doc/api/log.txt
11--- a/doc/api/log.txt Fri Jan 23 18:45:25 2009 +0100
12+++ b/doc/api/log.txt Fri Jan 23 23:04:58 2009 +0100
13@@ -41,7 +41,7 @@
14
15 <enum>
16 enum LogSys {
17- LS_SYSTEM, LS_CONFIG, LS_OPERMODE, LS_GLINE, LS_JUPE, LS_WHO, LS_NETWORK,
18+ LS_SYSTEM, LS_CONFIG, LS_OPERMODE, LS_GLINE, LS_JUPE, LS_SPLIT, LS_WHO, LS_NETWORK,
19 LS_OPERKILL, LS_SERVKILL, LS_USER, LS_OPER, LS_RESOLVER, LS_SOCKET,
20 LS_DEBUG, LS_OLDLOG,
21 LS_LAST_SYSTEM
22diff -r bcffa56c93d4 include/handlers.h
23--- a/include/handlers.h Fri Jan 23 18:45:25 2009 +0100
24+++ b/include/handlers.h Fri Jan 23 23:04:58 2009 +0100
12e82c05 25@@ -139,6 +139,7 @@
26 extern int m_registered(struct Client*, struct Client*, int, char*[]);
27 extern int m_sethost(struct Client*, struct Client*, int, char*[]);
28 extern int m_silence(struct Client*, struct Client*, int, char*[]);
29+extern int m_split(struct Client*, struct Client*, int, char*[]);
30 extern int m_stats(struct Client*, struct Client*, int, char*[]);
31 extern int m_time(struct Client*, struct Client*, int, char*[]);
32 extern int m_topic(struct Client*, struct Client*, int, char*[]);
33@@ -178,6 +179,7 @@
34 extern int mo_rping(struct Client*, struct Client*, int, char*[]);
35 extern int mo_set(struct Client*, struct Client*, int, char*[]);
36 extern int mo_settime(struct Client*, struct Client*, int, char*[]);
37+extern int mo_split(struct Client*, struct Client*, int, char*[]);
38 extern int mo_squit(struct Client*, struct Client*, int, char*[]);
39 extern int mo_stats(struct Client*, struct Client*, int, char*[]);
40 extern int mo_trace(struct Client*, struct Client*, int, char*[]);
41@@ -232,6 +234,7 @@
42 extern int ms_sethost(struct Client*, struct Client*, int, char*[]);
43 extern int ms_settime(struct Client*, struct Client*, int, char*[]);
44 extern int ms_silence(struct Client*, struct Client*, int, char*[]);
45+extern int ms_split(struct Client*, struct Client*, int, char*[]);
46 extern int ms_squit(struct Client*, struct Client*, int, char*[]);
47 extern int ms_stats(struct Client*, struct Client*, int, char*[]);
48 extern int ms_topic(struct Client*, struct Client*, int, char*[]);
0c63b280 49diff -r bcffa56c93d4 include/ircd_features.h
50--- a/include/ircd_features.h Fri Jan 23 18:45:25 2009 +0100
51+++ b/include/ircd_features.h Fri Jan 23 23:04:58 2009 +0100
52@@ -111,6 +111,10 @@
12e82c05 53 FEAT_SETHOST_USER,
54 FEAT_SETHOST_AUTO,
12e82c05 55
65b70298 56+ /* SPLIT */
57+ FEAT_SPLIT,
58+ FEAT_SPLIT_AUTO_EXPIRE,
59+
12e82c05 60 /* HEAD_IN_SAND Features */
61 FEAT_HIS_SNOTICES,
65b70298 62 FEAT_HIS_SNOTICES_OPER_ONLY,
0c63b280 63@@ -138,6 +142,7 @@
12e82c05 64 FEAT_HIS_STATS_q,
65 FEAT_HIS_STATS_R,
66 FEAT_HIS_STATS_r,
67+ FEAT_HIS_STATS_S,
68 FEAT_HIS_STATS_s,
69 FEAT_HIS_STATS_t,
70 FEAT_HIS_STATS_T,
0c63b280 71diff -r bcffa56c93d4 include/ircd_log.h
72--- a/include/ircd_log.h Fri Jan 23 18:45:25 2009 +0100
73+++ b/include/ircd_log.h Fri Jan 23 23:04:58 2009 +0100
74@@ -55,6 +55,7 @@
75 LS_OPERMODE, /**< Uses of OPMODE, CLEARMODE< etc. */
76 LS_GLINE, /**< Adding, (de-)activating or removing GLINEs. */
77 LS_JUPE, /**< Adding, (de-)activating or removing JUPEs. */
78+ LS_SPLIT, /**< Adding, (de-)activating or removing SPLITs. */
79 LS_WHO, /**< Use of extended WHO privileges. */
80 LS_NETWORK, /**< New server connections. */
81 LS_OPERKILL, /**< Kills by IRC operators. */
82diff -r bcffa56c93d4 include/msg.h
83--- a/include/msg.h Fri Jan 23 18:45:25 2009 +0100
84+++ b/include/msg.h Fri Jan 23 23:04:58 2009 +0100
12e82c05 85@@ -332,6 +332,10 @@
86 #define TOK_JUPE "JU"
87 #define CMD_JUPE MSG_JUPE, TOK_JUPE
88
89+#define MSG_SPLIT "SPLIT" /* SPLIT */
90+#define TOK_SPLIT "SP"
91+#define CMD_SPLIT MSG_SPLIT, TOK_SPLIT
92+
93 #define MSG_OPMODE "OPMODE" /* OPMO */
94 #define TOK_OPMODE "OM"
95 #define CMD_OPMODE MSG_OPMODE, TOK_OPMODE
0c63b280 96diff -r bcffa56c93d4 include/numeric.h
97--- a/include/numeric.h Fri Jan 23 18:45:25 2009 +0100
98+++ b/include/numeric.h Fri Jan 23 23:04:58 2009 +0100
12e82c05 99@@ -117,6 +117,7 @@
100 RPL_STATSVLINE 227 unreal */
101 #define RPL_STATSALINE 226 /* Hybrid, Undernet */
102 #define RPL_STATSQLINE 228 /* Undernet extension */
103+#define RPL_STATSSPLIT 229 /* QuakeNet extension */
65b70298 104 #define RPL_STATSHEADER 230 /* QuakeNet extension */
12e82c05 105
106 /* RPL_SERVICEINFO 231 unused */
65b70298 107@@ -178,6 +179,8 @@
12e82c05 108 #define RPL_STATSDLINE 275 /* Undernet extension */
109 #define RPL_STATSRLINE 276 /* Undernet extension */
110
111+#define RPL_SPLITLIST 278 /* QuakeNet extension */
112+#define RPL_ENDOFSPLITLIST 279 /* QuakeNet extension */
113 #define RPL_GLIST 280 /* Undernet extension */
114 #define RPL_ENDOFGLIST 281 /* Undernet extension */
115 #define RPL_JUPELIST 282 /* Undernet extension - jupe -Kev */
0c63b280 116@@ -442,6 +445,7 @@
12e82c05 117 /* ERR_GHOSTEDCLIENT 503 efnet */
118 /* ERR_VWORLDWARN 503 austnet */
119
120+#define ERR_NOSUCHSPLIT 510 /* QuakeNet extension */
121 #define ERR_SILELISTFULL 511 /* Undernet extension */
122 /* ERR_NOTIFYFULL 512 aircd */
123 /* ERR_TOOMANYWATCH 512 Numeric List: Dalnet */
0c63b280 124diff -r bcffa56c93d4 include/split.h
12e82c05 125--- /dev/null Thu Jan 01 00:00:00 1970 +0000
0c63b280 126+++ b/include/split.h Fri Jan 23 23:04:58 2009 +0100
127@@ -0,0 +1,106 @@
128+#ifndef INCLUDED_split_h
129+#define INCLUDED_split_h
12e82c05 130+/*
131+ * IRC - Internet Relay Chat, include/split.h
132+ * Copyright (C) 1990 Jarkko Oikarinen and
133+ * University of Oulu, Computing Center
134+ * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
135+ *
136+ * This program is free software; you can redistribute it and/or modify
137+ * it under the terms of the GNU General Public License as published by
138+ * the Free Software Foundation; either version 2, or (at your option)
139+ * any later version.
140+ *
141+ * This program is distributed in the hope that it will be useful,
142+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
143+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
144+ * GNU General Public License for more details.
145+ *
146+ * You should have received a copy of the GNU General Public License
147+ * along with this program; if not, write to the Free Software
148+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
149+ */
150+/** @file
151+ * @brief Interface and declarations for split server handling.
152+ * @version $Id: jupe.h 1208 2004-10-03 14:12:35Z entrope $
153+ */
12e82c05 154+#ifndef INCLUDED_sys_types_h
155+#include <sys/types.h>
156+#define INCLUDED_sys_types_h
157+#endif
158+
159+
160+struct Client;
161+struct StatDesc;
162+
65b70298 163+#define SPLIT_MAX_EXPIRE 2419200 /**< Maximum split expiration time (4 weeks). */
12e82c05 164+
165+/* Describes a SPLIT server entry. */
166+struct Split {
167+ struct Split* sp_next; /**< Pointer to next Split. */
168+ struct Split** sp_prev_p; /**< Pointer to previous next pointer. */
169+ char* sp_server; /**< Name of server. */
170+ char* sp_reason; /**< Reason. */
171+ time_t sp_creation; /**< TODO: Creation time. What are we using this for then? */
172+ time_t sp_expire; /**< Expiration time. */
173+ time_t sp_lastmod; /**< Last modification time. */
174+ time_t sp_lifetime; /**< Life time. */
175+ unsigned int sp_flags; /**< Status flags. */
176+};
177+
178+/** Split state flags. */
0c63b280 179+#define SPLIT_ACTIVE 0x01 /**< Split is active. */
180+#define SPLIT_REMOVING 0x02 /**< Split is being force removed (instead of deactivated). */
181+#define SPLIT_BURST 0x04 /**< Split is for a server that is in progress of linking,
182+ and will be destroyed at the end of burst */
12e82c05 183+
12e82c05 184+/* Actions to perform on a SPLIT. */
185+enum SplitAction {
186+ SPLIT_ACTIVATE, /**< SPLIT should be activated. */
187+ SPLIT_DEACTIVATE, /**< SPLIT should be deactivated. */
188+ SPLIT_MODIFY, /**< SPLIT should be modified. */
189+ SPLIT_REMOVE /**< SPLIT should be removed. */
190+};
191+
12e82c05 192+/* Split update flags. */
0c63b280 193+#define SPLIT_EXPIRE 0x02 /**< Expiration time update. */
194+#define SPLIT_LIFETIME 0x04 /**< Record lifetime update. */
195+#define SPLIT_REASON 0x08 /**< Reason update. */
196+#define SPLIT_CREATION 0x10 /**< TODO: Creation time update - unused. */
197+#define SPLIT_MODIFY 0x20 /**< No state change. */
12e82c05 198+
199+/* mask for Split update flags. */
200+#define SPLIT_UPDATE (SPLIT_EXPIRE | SPLIT_LIFETIME | SPLIT_REASON)
201+
202+/* test whether a split entry is active. */
203+#define SplitIsActive(s) ((s)->sp_flags & SPLIT_ACTIVE)
0c63b280 204+/* test whether a split entry is marked for forced removal. */
205+#define SplitIsRemoving(s) ((s)->sp_flags & SPLIT_REMOVING)
206+/* test whether a split entry is part of a netmerge. */
207+#define SplitIsBurst(s) ((s)->sp_flags & SPLIT_BURST)
12e82c05 208+
209+extern int split_add(struct Client *cptr, struct Client *sptr,
210+ char *server, const char *reason,
211+ time_t creation, time_t expire, time_t lastmod, time_t lifetime,
212+ unsigned int flags);
213+extern int split_modify(struct Client *cptr, struct Client *sptr,
214+ struct Split *split, enum SplitAction action, const char *reason,
215+ time_t creation, time_t expire, time_t lastmod, time_t lifetime,
216+ unsigned int flags);
217+extern int split_remove(struct Client *cptr, struct Client *sptr,
218+ struct Split *split, const char *reason);
219+
220+extern struct Split* split_find(char *server);
221+extern void split_free(struct Split *split);
222+extern int split_expire(struct Split* split);
223+extern void split_burst(struct Client *cptr);
224+extern int split_resend(struct Client *cptr, struct Split *split);
225+extern int split_list(struct Client *sptr, char *server);
0c63b280 226+extern void split_newserver(struct Client *acptr);
227+extern int split_netmerge(struct Client *sptr);
228+extern int split_netbreak(struct Client *victim, const char *reason);
12e82c05 229+extern void split_conf();
230+extern void split_stats(struct Client *sptr, const struct StatDesc *sd, char *param);
231+extern int split_memory_count(size_t *sp_size);
232+
0c63b280 233+#endif /* INCLUDED_split_h */
234diff -r bcffa56c93d4 ircd/Makefile.in
235--- a/ircd/Makefile.in Fri Jan 23 18:45:25 2009 +0100
236+++ b/ircd/Makefile.in Fri Jan 23 23:04:58 2009 +0100
12e82c05 237@@ -173,6 +173,7 @@
238 m_sethost.c \
239 m_settime.c \
240 m_silence.c \
241+ m_split.c \
242 m_squit.c \
243 m_stats.c \
244 m_time.c \
245@@ -212,6 +213,7 @@
246 s_stats.c \
247 s_user.c \
248 send.c \
249+ split.c \
250 uping.c \
251 userload.c \
252 whocmds.c \
253@@ -1052,6 +1054,15 @@
254 ../include/ircd_chattr.h ../include/list.h ../include/msg.h \
255 ../include/numeric.h ../include/numnicks.h ../include/s_user.h \
256 ../include/send.h ../include/struct.h
257+m_split.o: m_split.c ../config.h ../include/client.h ../include/ircd_defs.h \
258+ ../include/dbuf.h ../include/msgq.h ../include/ircd_events.h \
259+ ../config.h ../include/ircd_handler.h ../include/res.h \
260+ ../include/capab.h ../include/split.h ../include/hash.h \
261+ ../include/ircd.h ../include/struct.h ../include/ircd_features.h \
262+ ../include/ircd_log.h ../include/ircd_reply.h ../include/ircd_string.h \
263+ ../include/ircd_chattr.h ../include/match.h ../include/msg.h \
264+ ../include/numeric.h ../include/numnicks.h ../include/s_conf.h \
265+ ../include/client.h ../include/s_misc.h ../include/send.h
266 m_squit.o: m_squit.c ../config.h ../include/client.h \
267 ../include/ircd_defs.h ../include/dbuf.h ../include/msgq.h \
268 ../include/ircd_events.h ../config.h ../include/ircd_handler.h \
269@@ -1325,6 +1336,7 @@
270 ../include/msgq.h ../include/numeric.h ../include/numnicks.h \
271 ../include/res.h ../include/s_bsd.h ../include/s_conf.h \
272 ../include/s_user.h ../include/s_stats.h ../include/send.h \
273+ ../include/split.h \
274 ../include/struct.h ../include/sys.h ../include/whowas.h
275 s_err.o: s_err.c ../config.h ../include/numeric.h ../include/ircd_log.h \
276 ../include/s_debug.h ../config.h ../include/ircd_defs.h
277@@ -1410,6 +1422,16 @@
278 ../include/msg.h ../include/numnicks.h ../include/parse.h \
279 ../include/s_bsd.h ../include/s_debug.h ../include/s_misc.h \
280 ../include/s_user.h ../include/struct.h ../include/sys.h
281+split.o: split.c ../config.h ../include/split.h ../include/client.h \
282+ ../include/ircd_defs.h ../include/dbuf.h ../include/msgq.h \
283+ ../include/ircd_events.h ../config.h ../include/ircd_handler.h \
284+ ../include/res.h ../include/capab.h ../include/hash.h ../include/ircd.h \
285+ ../include/struct.h ../include/ircd_alloc.h ../include/ircd_features.h \
286+ ../include/ircd_log.h ../include/ircd_reply.h ../include/ircd_string.h \
287+ ../include/ircd_chattr.h ../include/match.h ../include/msg.h \
288+ ../include/numeric.h ../include/numnicks.h ../include/s_bsd.h \
289+ ../include/s_misc.h ../include/send.h ../include/struct.h \
290+ ../include/sys.h
291 uping.o: uping.c ../config.h ../include/uping.h ../include/ircd_defs.h \
292 ../include/ircd_events.h ../config.h ../include/res.h \
293 ../include/client.h ../include/dbuf.h ../include/msgq.h \
0c63b280 294diff -r bcffa56c93d4 ircd/ircd.c
295--- a/ircd/ircd.c Fri Jan 23 18:45:25 2009 +0100
296+++ b/ircd/ircd.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 297@@ -55,6 +55,7 @@
298 #include "s_misc.h"
299 #include "s_stats.h"
300 #include "send.h"
301+#include "split.h"
302 #include "sys.h"
303 #include "uping.h"
304 #include "userload.h"
305@@ -763,6 +764,9 @@
306 Debug((DEBUG_NOTICE, "Server ready..."));
307 log_write(LS_SYSTEM, L_NOTICE, 0, "Server Ready");
308
309+ /* create SPLITs */
310+ split_conf();
311+
312 event_loop();
313
314 return 0;
0c63b280 315diff -r bcffa56c93d4 ircd/ircd_features.c
316--- a/ircd/ircd_features.c Fri Jan 23 18:45:25 2009 +0100
317+++ b/ircd/ircd_features.c Fri Jan 23 23:04:58 2009 +0100
318@@ -365,6 +365,10 @@
12e82c05 319 F_B(SETHOST_USER, 0, 0, 0),
320 F_B(SETHOST_AUTO, 0, 0, 0),
12e82c05 321
65b70298 322+ /* SPLIT */
323+ F_B(SPLIT, 0, 1, 0),
324+ F_I(SPLIT_AUTO_EXPIRE, 0, 604800, 0),
325+
12e82c05 326 /* HEAD_IN_SAND Features */
327 F_B(HIS_SNOTICES, 0, 1, 0),
65b70298 328 F_B(HIS_SNOTICES_OPER_ONLY, 0, 1, 0),
0c63b280 329@@ -392,6 +396,7 @@
12e82c05 330 F_B(HIS_STATS_q, 0, 1, 0),
331 F_B(HIS_STATS_R, 0, 1, 0),
332 F_B(HIS_STATS_r, 0, 1, 0),
333+ F_B(HIS_STATS_S, 0, 1, 0),
334 F_B(HIS_STATS_s, 0, 1, 0),
335 F_B(HIS_STATS_t, 0, 1, 0),
336 F_B(HIS_STATS_T, 0, 1, 0),
0c63b280 337diff -r bcffa56c93d4 ircd/ircd_log.c
338--- a/ircd/ircd_log.c Fri Jan 23 18:45:25 2009 +0100
339+++ b/ircd/ircd_log.c Fri Jan 23 23:04:58 2009 +0100
340@@ -154,6 +154,7 @@
341 S(OPERMODE, -1, SNO_HACK4),
342 S(GLINE, -1, SNO_GLINE),
343 S(JUPE, -1, SNO_NETWORK),
344+ S(SPLIT, -1, SNO_NETWORK),
345 S(WHO, -1, 0),
346 S(NETWORK, -1, SNO_NETWORK),
347 S(OPERKILL, -1, 0),
348diff -r bcffa56c93d4 ircd/m_endburst.c
349--- a/ircd/m_endburst.c Fri Jan 23 18:45:25 2009 +0100
350+++ b/ircd/m_endburst.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 351@@ -85,6 +85,7 @@
352 #include "client.h"
353 #include "hash.h"
354 #include "ircd.h"
355+#include "ircd_features.h"
356 #include "ircd_log.h"
357 #include "ircd_reply.h"
358 #include "ircd_string.h"
359@@ -92,6 +93,7 @@
360 #include "numeric.h"
361 #include "numnicks.h"
362 #include "send.h"
363+#include "split.h"
364
365 /* #include <assert.h> -- Now using assert in ircd_log.h */
366
367@@ -125,6 +127,7 @@
368 int ms_end_of_burst(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
369 {
370 struct Channel *chan, *next_chan;
371+ int split;
372
373 assert(0 != cptr);
374 assert(0 != sptr);
375@@ -135,6 +138,10 @@
376 dump_map(sptr, "*", 0, report_new_links, 0);
377 sendto_opmask_butone(0, SNO_NETWORK, "Completed net.burst from %C.",
378 sptr);
379+ /* cleanup SPLITs, but only bother ops when FEAT_SPLIT is enabled. */
0c63b280 380+ split = split_netmerge(sptr);
12e82c05 381+ if (feature_bool(FEAT_SPLIT))
382+ sendto_opmask_butone(0, SNO_NETWORK, "Removed %d SPLIT entries.", split);
383 if (MyConnect(sptr))
384 sendcmdto_one(&me, CMD_END_OF_BURST_ACK, sptr, "");
385
0c63b280 386diff -r bcffa56c93d4 ircd/m_reburst.c
387--- a/ircd/m_reburst.c Fri Jan 23 18:45:25 2009 +0100
388+++ b/ircd/m_reburst.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 389@@ -102,6 +102,7 @@
390 #include "ircd_snprintf.h"
391 #include "gline.h"
392 #include "jupe.h"
393+#include "split.h"
394
395 /* #include <assert.h> -- Now using assert in ircd_log.h */
396 #include <stdlib.h>
397@@ -130,6 +131,10 @@
398 case 'J':
399 jupe_burst(sptr);
400 break;
401+ case 's':
402+ case 'S':
403+ split_burst(sptr);
404+ break;
405 default:
406 break;
407 }
0c63b280 408diff -r bcffa56c93d4 ircd/m_server.c
409--- a/ircd/m_server.c Fri Jan 23 18:45:25 2009 +0100
410+++ b/ircd/m_server.c Fri Jan 23 23:04:58 2009 +0100
411@@ -47,6 +47,7 @@
412 #include "s_misc.h"
413 #include "s_serv.h"
414 #include "send.h"
415+#include "split.h"
416 #include "userload.h"
417
418 /* #include <assert.h> -- Now using assert in ircd_log.h */
419@@ -764,6 +765,9 @@
420 sendto_opmask_butone(0, SNO_NETWORK, "Net junction: %s %s",
421 cli_name(sptr), cli_name(acptr));
422 }
423+ /* new server - mark SPLIT for this server */
424+ split_newserver(acptr);
425+
426 /*
427 * Old sendto_serv_but_one() call removed because we now need to send
428 * different names to different servers (domain name matching).
429diff -r bcffa56c93d4 ircd/m_split.c
12e82c05 430--- /dev/null Thu Jan 01 00:00:00 1970 +0000
0c63b280 431+++ b/ircd/m_split.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 432@@ -0,0 +1,374 @@
433+/*
434+ * IRC - Internet Relay Chat, ircd/m_split.c
435+ * Copyright (C) 1990 Jarkko Oikarinen and
436+ * University of Oulu, Computing Center
437+ * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
438+ *
439+ * See file AUTHORS in IRC package for additional names of
440+ * the programmers.
441+ *
442+ * This program is free software; you can redistribute it and/or modify
443+ * it under the terms of the GNU General Public License as published by
444+ * the Free Software Foundation; either version 1, or (at your option)
445+ * any later version.
446+ *
447+ * This program is distributed in the hope that it will be useful,
448+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
449+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
450+ * GNU General Public License for more details.
451+ *
452+ * You should have received a copy of the GNU General Public License
453+ * along with this program; if not, write to the Free Software
454+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
455+ *
456+ * $Id: m_split.c 1737 2006-12-19 05:20:48Z entrope $
457+ */
458+
459+/*
460+ * m_functions execute protocol messages on this server:
461+ *
462+ * cptr is always NON-NULL, pointing to a *LOCAL* client
463+ * structure (with an open socket connected!). This
464+ * identifies the physical socket where the message
465+ * originated (or which caused the m_function to be
466+ * executed--some m_functions may call others...).
467+ *
468+ * sptr is the source of the message, defined by the
469+ * prefix part of the message if present. If not
470+ * or prefix not found, then sptr==cptr.
471+ *
472+ * (!IsServer(cptr)) => (cptr == sptr), because
473+ * prefixes are taken *only* from servers...
474+ *
475+ * (IsServer(cptr))
476+ * (sptr == cptr) => the message didn't
477+ * have the prefix.
478+ *
479+ * (sptr != cptr && IsServer(sptr) means
480+ * the prefix specified servername. (?)
481+ *
482+ * (sptr != cptr && !IsServer(sptr) means
483+ * that message originated from a remote
484+ * user (not local).
485+ *
486+ * combining
487+ *
488+ * (!IsServer(sptr)) means that, sptr can safely
489+ * taken as defining the target structure of the
490+ * message in this server.
491+ *
492+ * *Always* true (if 'parse' and others are working correct):
493+ *
494+ * 1) sptr->from == cptr (note: cptr->from == cptr)
495+ *
496+ * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
497+ * *cannot* be a local connection, unless it's
498+ * actually cptr!). [MyConnect(x) should probably
499+ * be defined as (x == x->from) --msa ]
500+ *
501+ * parc number of variable parameter strings (if zero,
502+ * parv is allowed to be NULL)
503+ *
504+ * parv a NULL terminated list of parameter pointers,
505+ *
506+ * parv[0], sender (prefix string), if not present
507+ * this points to an empty string.
508+ * parv[1]...parv[parc-1]
509+ * pointers to additional parameters
510+ * parv[parc] == NULL, *always*
511+ *
512+ * note: it is guaranteed that parv[0]..parv[parc-1] are all
513+ * non-NULL pointers.
514+ */
515+#include "config.h"
516+
517+#include "client.h"
518+#include "split.h"
519+#include "hash.h"
520+#include "ircd.h"
521+#include "ircd_features.h"
522+#include "ircd_log.h"
523+#include "ircd_reply.h"
524+#include "ircd_string.h"
525+#include "match.h"
526+#include "msg.h"
527+#include "numeric.h"
528+#include "numnicks.h"
529+#include "s_conf.h"
530+#include "s_debug.h"
531+#include "s_misc.h"
532+#include "send.h"
533+
534+/* #include <assert.h> -- Now using assert in ircd_log.h */
535+#include <stdlib.h>
536+#include <string.h>
537+
538+/*
539+ * ms_split - server message handler
540+ *
541+ * parv[0] = Send prefix
542+ * parv[1] = (+|-)<server name>
543+ * parv[2] = Creation time
544+ * parv[3] = Expiration time
545+ * parv[4] = Last modification time
546+ * parv[5] = Life time
547+ * parv[6] = Comment
548+ *
549+ */
550+int ms_split(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
551+{
552+ struct Client *acptr = 0;
553+ struct Split *asplit = 0;
554+ unsigned int flags = 0;
555+ enum SplitAction action = SPLIT_MODIFY;
556+ time_t creation = 0, expire = 0, lastmod = 0, lifetime = 0;
557+ char *server = parv[1], *tmp = 0;
558+ const char *reason = "No reason";
559+
560+ /* TODO: perhaps make some fields optional? reason? */
561+ if (parc < 6)
562+ return need_more_params(sptr, "SPLIT");
563+
564+ if (*server == '!') {
565+ server++;
566+ action = SPLIT_REMOVE; /* removing entry */
567+ }
568+
569+ switch (*server) { /* handle + and - */
570+ case '+': /* activate the split entry */
571+ if (action != SPLIT_REMOVE)
572+ action = SPLIT_ACTIVATE;
573+ server++;
574+ break;
575+
576+ case '-': /* deactivate the entry */
577+ if (action != SPLIT_REMOVE)
578+ action = SPLIT_DEACTIVATE;
579+ server++;
580+ break;
581+ }
582+
583+ /* Next, try to find the split entry... */
584+ asplit = split_find(server);
585+
586+ /* We now have all the pieces to tell us what we've got; let's put
587+ * it all together and convert the rest of the arguments.
588+ */
589+
590+ /* can't modify a split entry that doesn't exist, so remap to activate */
591+ if (!asplit && action == SPLIT_MODIFY)
592+ action = SPLIT_ACTIVATE;
593+
594+ /* OK, let's figure out what other parameters we may have... */
595+ creation = atoi(parv[2]);
596+ expire = atoi(parv[3]);
597+ lastmod = atoi(parv[4]);
598+ lifetime = atoi(parv[5]);
599+ reason = parv[parc - 1];
600+
601+ Debug((DEBUG_DEBUG, "I have a SPLIT I am acting upon now; "
602+ "server %s, action %s, creation %Tu, expire %Tu, "
603+ "lastmod %Tu, lifetime %Tu, reason: %s; split %s!", server,
604+ action == SPLIT_REMOVE ? "!-" :
605+ (action == SPLIT_ACTIVATE ? "+" :
606+ (action == SPLIT_DEACTIVATE ? "-" : "(MODIFY)")),
607+ creation, expire, lastmod, lifetime, reason,
608+ asplit ? "EXISTS" : "does not exist"));
609+
610+ /* OK, at this point, we have converted all available parameters.
611+ * Let's actually do the action!
612+ */
613+ if (asplit) {
614+ if (action == SPLIT_REMOVE)
615+ return split_remove(cptr, sptr, asplit, reason);
616+ return split_modify(cptr, sptr, asplit, action, reason, creation, expire,
617+ lastmod, lifetime, flags);
618+ }
619+
620+ assert(action != SPLIT_MODIFY);
621+
622+ return split_add(cptr, sptr, server, reason, creation, expire, lastmod, lifetime,
623+ flags | ((action == SPLIT_ACTIVATE) ? SPLIT_ACTIVE : 0));
624+}
625+
626+
627+/*
628+ * mo_split - oper message handler
629+ *
630+ * Local listing: 1 or 2 params
631+ * parv[0] = Send prefix
632+ * parv[1] = [Server or mask to match]
633+ *
634+ * Add or modify entry: 3, 4 or 5 params (3 is * then)
635+ * parv[0] = Send prefix
636+ * parv[1] = [+|-]<server name>
637+ * parv[2] = [Expiration offset] (required for new)
638+ * parv[3] = [Comment] (required for new)
639+ *
640+ */
641+int mo_split(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
642+{
643+ struct Client *acptr = 0;
644+ struct Split *asplit = 0;
645+ unsigned int flags = 0;
646+ enum SplitAction action = SPLIT_MODIFY;
0c63b280 647+ time_t expire = 0, lastmod = TStime(), creation = TStime();
12e82c05 648+ char *server = parv[1], *end;
649+ const char *reason = NULL;
650+
651+ if (parc < 2) {
652+ if (!HasPriv(sptr, PRIV_ROUTEINFO))
653+ return send_reply(sptr, ERR_NOPRIVILEGES);
654+ else
655+ return split_list(sptr, 0);
656+ }
657+
658+ if (*server == '!') {
659+ server++;
660+ action = SPLIT_REMOVE; /* force removal */
661+ }
662+
663+ switch (*server) { /* handle + and - */
664+ case '+': /* activate the split entry */
665+ if (action != SPLIT_REMOVE)
666+ action = SPLIT_ACTIVATE;
667+ server++;
668+ break;
669+
670+ case '-': /* deactivate the split entry */
671+ if (action != SPLIT_REMOVE)
672+ action = SPLIT_DEACTIVATE;
673+ server++;
674+ break;
675+ }
676+
677+ /* OK, let's figure out the parameters... */
678+ switch (action) {
679+ /* no specific action */
680+ case SPLIT_MODIFY:
681+ /* user wants a listing of a specific SPLIT */
682+ if (parc == 2)
683+ return split_list(sptr, server);
0c63b280 684+ expire = strtol(parv[2], &end, 10) + TStime(); /* and the expiration */
12e82c05 685+ if (*end != '\0')
686+ return send_reply(sptr, SND_EXPLICIT | ERR_BADEXPIRE,
687+ "%s :Bad expire time", parv[2]);
688+
689+ flags |= SPLIT_EXPIRE; /* remember that we got an expire time */
690+
691+ if (parc > 3) { /* also got a reason... */
692+ reason = parv[parc - 1];
693+ flags |= SPLIT_REASON;
694+ }
695+ break;
696+
697+ case SPLIT_REMOVE: /* TODO: require reason for this, but not expire? */
698+ if (parc < 4)
699+ return need_more_params(sptr, "SPLIT");
700+ reason = parv[parc - 1];
701+ flags |= SPLIT_REASON;
702+ break;
703+
704+ case SPLIT_ACTIVATE: /* TODO: require expire and reason when new */
705+ if (parc > 2) {
0c63b280 706+ expire = strtol(parv[2], &end, 10) + TStime(); /* and the expiration */
12e82c05 707+ if (*end != '\0')
708+ return send_reply(sptr, SND_EXPLICIT | ERR_BADEXPIRE,
709+ "%s :Bad expire time", parv[2]);
710+ flags |= SPLIT_EXPIRE; /* remember that we got an expire time */
711+ }
712+
713+ if (parc > 3) { /* also got a reason... */
714+ reason = parv[parc - 1];
715+ flags |= SPLIT_REASON;
716+ }
717+ break;
718+
719+ case SPLIT_DEACTIVATE: /* TODO: duplicate code? must be a cleaner way */
720+ if (parc > 2) {
0c63b280 721+ expire = strtol(parv[2], &end, 10) + TStime(); /* and the expiration */
12e82c05 722+ if (*end != '\0')
723+ return send_reply(sptr, SND_EXPLICIT | ERR_BADEXPIRE,
724+ "%s :Bad expire time", parv[2]);
725+ flags |= SPLIT_EXPIRE; /* remember that we got an expire time */
726+ }
727+
728+ if (parc > 3) { /* also got a reason... */
729+ reason = parv[parc - 1];
730+ flags |= SPLIT_REASON;
731+ }
732+ break;
733+ }
734+
735+
736+ /* check for permissions... */
737+ if (!feature_bool(FEAT_SPLIT)) /* TODO: but allow force removal? */
738+ return send_reply(sptr, ERR_DISABLED, "SPLIT");
739+ else if (!HasPriv(sptr, PRIV_SERVERINFO)) /* TODO: create PRIV_SPLIT - need help there */
740+ return send_reply(sptr, ERR_NOPRIVILEGES);
741+
742+ /* Next, try to find the SPLIT... */
743+ asplit = split_find(server);
744+
745+ /* We now have all the pieces to tell us what we've got; let's put
746+ * it all together and convert the rest of the arguments.
747+ */
748+
749+ /* SPLIT not found and thus we:
750+ * cannot remove SPLIT we do not have
751+ * cannot add new SPLIT without expire and reason
752+ */
753+ if (!asplit &&
754+ ((action == SPLIT_REMOVE) ||
755+ (action == SPLIT_ACTIVATE && !reason) ||
756+ (action == SPLIT_DEACTIVATE && !reason) ||
757+ (action == SPLIT_MODIFY && !reason)))
758+ return send_reply(sptr, ERR_NOSUCHSPLIT, server);
759+
760+ /* can't modify a split entry that doesn't exist, so remap to activate */
761+ if (!asplit && action == SPLIT_MODIFY)
762+ action = SPLIT_ACTIVATE;
763+
764+ Debug((DEBUG_DEBUG, "I have a SPLIT I am acting upon now; "
765+ "server %s, action %s, expire %Tu, "
766+ "reason: %s; split %s! (fields present: %s %s)", server,
767+ action == SPLIT_REMOVE ? "!-" :
768+ (action == SPLIT_ACTIVATE ? "+" :
769+ (action == SPLIT_DEACTIVATE ? "-" : "(MODIFY)")),
770+ expire, reason, asplit ? "EXISTS" : "does not exist",
771+ flags & SPLIT_EXPIRE ? "expire" : "",
772+ flags & SPLIT_REASON ? "reason" : ""));
773+
774+ if (asplit) { /* modifying an existing SPLIT */
775+ if (action == SPLIT_REMOVE)
776+ return split_remove(cptr, sptr, asplit, reason);
777+ return split_modify(cptr, sptr, asplit, action, reason,
778+ asplit->sp_creation, expire, lastmod, asplit->sp_lifetime, flags);
779+ }
780+
781+ assert(action != SPLIT_MODIFY);
782+ assert(action != SPLIT_REMOVE);
783+
784+ /* create a new SPLIT */
785+ return split_add(cptr, sptr, server, reason,
786+ creation, expire, lastmod, expire,
787+ flags | ((action == SPLIT_ACTIVATE) ? SPLIT_ACTIVE : 0));
788+}
789+
790+/*
791+ * m_split - user message handler
792+ *
793+ * parv[0] = Send prefix
794+ *
795+ * From user:
796+ *
797+ * parv[1] = [<server name>]
798+ *
799+ */
800+int m_split(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
801+{
802+ if (parc < 2)
803+ return split_list(sptr, 0);
804+
805+ return split_list(sptr, parv[1]);
806+}
0c63b280 807diff -r bcffa56c93d4 ircd/parse.c
808--- a/ircd/parse.c Fri Jan 23 18:45:25 2009 +0100
809+++ b/ircd/parse.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 810@@ -505,6 +505,13 @@
811 0, MAXPARA, MFLG_SLOW, 0, NULL,
812 /* UNREG, CLIENT, SERVER, OPER, SERVICE */
813 { m_unregistered, m_not_oper, ms_jupe, mo_jupe, m_ignore }
814+ },
815+ {
816+ MSG_SPLIT,
817+ TOK_SPLIT,
818+ 0, MAXPARA, MFLG_SLOW, 0, NULL,
819+ /* UNREG, CLIENT, SERVER, OPER, SERVICE */
820+ { m_unregistered, m_not_oper, ms_split, mo_split, m_ignore }
821 },
822 {
823 MSG_OPMODE,
0c63b280 824diff -r bcffa56c93d4 ircd/s_conf.c
825--- a/ircd/s_conf.c Fri Jan 23 18:45:25 2009 +0100
826+++ b/ircd/s_conf.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 827@@ -53,6 +53,7 @@
828 #include "s_debug.h"
829 #include "s_misc.h"
830 #include "send.h"
831+#include "split.h"
832 #include "struct.h"
833 #include "sys.h"
834
835@@ -1005,6 +1006,7 @@
836 }
837
838 attach_conf_uworld(&me);
839+ split_conf();
840
841 return ret;
842 }
0c63b280 843diff -r bcffa56c93d4 ircd/s_debug.c
844--- a/ircd/s_debug.c Fri Jan 23 18:45:25 2009 +0100
845+++ b/ircd/s_debug.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 846@@ -48,6 +48,7 @@
847 #include "s_user.h"
848 #include "s_stats.h"
849 #include "send.h"
850+#include "split.h"
851 #include "struct.h"
852 #include "sys.h"
853 #include "whowas.h"
854@@ -231,7 +232,8 @@
855 aw = 0, /* aways set */
856 wwa = 0, /* whowas aways */
857 gl = 0, /* glines */
858- ju = 0; /* jupes */
859+ ju = 0, /* jupes */
860+ sp = 0; /* split entries */
861
862 size_t chm = 0, /* memory used by channels */
863 chbm = 0, /* memory used by channel bans */
864@@ -244,6 +246,7 @@
865 wwm = 0, /* whowas array memory used */
866 glm = 0, /* memory used by glines */
867 jum = 0, /* memory used by jupes */
868+ spm = 0, /* memory used by split entries */
869 com = 0, /* memory used by conf lines */
870 dbufs_allocated = 0, /* memory used by dbufs */
871 dbufs_used = 0, /* memory used by dbufs */
872@@ -348,8 +351,9 @@
873
874 gl = gline_memory_count(&glm);
875 ju = jupe_memory_count(&jum);
876+ sp = split_memory_count(&spm);
877 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
878- ":Glines %d(%zu) Jupes %d(%zu)", gl, glm, ju, jum);
879+ ":Glines %d(%zu) Jupes %d(%zu) Splits %d(%zu)", gl, glm, ju, jum, sp, spm);
880
881 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
882 ":Hash: client %d(%zu), chan is the same", HASHSIZE,
0c63b280 883diff -r bcffa56c93d4 ircd/s_err.c
884--- a/ircd/s_err.c Fri Jan 23 18:45:25 2009 +0100
885+++ b/ircd/s_err.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 886@@ -490,7 +490,7 @@
887 /* 228 */
888 { RPL_STATSQLINE, "Q %s :%s", "228" },
889 /* 229 */
890- { 0 },
891+ { RPL_STATSSPLIT, "S %s %d %d %d %d %c :%s", "229" },
892 /* 230 */
65b70298 893 { RPL_STATSHEADER, 0, "230" },
12e82c05 894 /* 231 */
895@@ -588,9 +588,9 @@
896 /* 277 */
897 { 0 },
898 /* 278 */
899- { 0 },
900+ { RPL_SPLITLIST, "%s %Tu %Tu %Tu %Tu %c :%s", "278" },
901 /* 279 */
902- { 0 },
903+ { RPL_ENDOFSPLITLIST, ":End of Split List", "279" },
904 /* 280 */
65b70298 905 { RPL_GLIST, "%s%s%s%s%s %Tu %Tu %Tu %s %s%s%c :%s", "280" },
12e82c05 906 /* 281 */
907@@ -1052,7 +1052,7 @@
908 /* 509 */
909 { 0 },
910 /* 510 */
911- { 0 },
912+ { ERR_NOSUCHSPLIT, "%s :No such split", "510" },
913 /* 511 */
914 { ERR_SILELISTFULL, "%s :Your silence list is full", "511" },
915 /* 512 */
0c63b280 916diff -r bcffa56c93d4 ircd/s_misc.c
917--- a/ircd/s_misc.c Fri Jan 23 18:45:25 2009 +0100
918+++ b/ircd/s_misc.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 919@@ -53,6 +53,7 @@
920 #include "s_stats.h"
921 #include "s_user.h"
922 #include "send.h"
923+#include "split.h"
924 #include "struct.h"
925 #include "sys.h"
926 #include "uping.h"
927@@ -391,6 +392,7 @@
928 time_t on_for;
929
930 char comment1[HOSTLEN + HOSTLEN + 2];
931+ char splitreason[BUFSIZE];
932 assert(killer);
933 if (MyConnect(victim))
934 {
7efbed3b 935@@ -497,6 +499,15 @@
12e82c05 936 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
937 cli_serv(victim)->up, victim, comment);
938 dump_map(victim, "*", 0, report_lost_links, 0);
939+ if (feature_bool(FEAT_SPLIT)) {
7efbed3b 940+ /* TODO: remote & local SQUIT ? servername oper is on? */
12e82c05 941+ ircd_snprintf(0, splitreason, sizeof(splitreason),
942+ "Net break: %C %C (%s%s%s%s)", cli_serv(victim)->up, victim,
943+ IsUser(killer) ? "SQUIT by " : "", IsUser(killer) ? cli_name(killer) : "",
944+ IsUser(killer) ? ": " : "", comment);
945+ sendto_opmask_butone(0, SNO_NETWORK, "Created %d SPLIT entries.",
0c63b280 946+ split_netbreak(victim, splitreason));
12e82c05 947+ }
948 }
949
950 /*
0c63b280 951diff -r bcffa56c93d4 ircd/s_serv.c
952--- a/ircd/s_serv.c Fri Jan 23 18:45:25 2009 +0100
953+++ b/ircd/s_serv.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 954@@ -54,6 +54,7 @@
955 #include "s_misc.h"
956 #include "s_user.h"
957 #include "send.h"
958+#include "split.h"
959 #include "struct.h"
960 #include "sys.h"
961 #include "userload.h"
962@@ -196,6 +197,7 @@
963 */
964 gline_burst(cptr);
965 jupe_burst(cptr);
966+ split_burst(cptr);
967
968 /*
969 * Pass on my client information to the new server
0c63b280 970diff -r bcffa56c93d4 ircd/s_stats.c
971--- a/ircd/s_stats.c Fri Jan 23 18:45:25 2009 +0100
972+++ b/ircd/s_stats.c Fri Jan 23 23:04:58 2009 +0100
12e82c05 973@@ -52,6 +52,7 @@
974 #include "s_stats.h"
975 #include "s_user.h"
976 #include "send.h"
977+#include "split.h"
978 #include "struct.h"
979 #include "userload.h"
980
65b70298 981@@ -668,7 +669,10 @@
12e82c05 982 send_usage, 0,
983 "System resource usage (Debug only)." },
984 #endif
985- { 's', "spoofhosts", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM), FEAT_HIS_STATS_s,
986+ { 'S', "splits", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_S,
987+ split_stats, 0,
988+ "Server SPLITs information."},
989+ { 's', "spoofhosts", (STAT_FLAG_OPERFEAT | STAT_FLAG_VARPARAM | STAT_FLAG_CASESENS), FEAT_HIS_STATS_s,
990 stats_sline, 0,
991 "Spoofed hosts information." },
992 { 'T', "motds", (STAT_FLAG_OPERFEAT | STAT_FLAG_CASESENS), FEAT_HIS_STATS_T,
0c63b280 993diff -r bcffa56c93d4 ircd/split.c
12e82c05 994--- /dev/null Thu Jan 01 00:00:00 1970 +0000
0c63b280 995+++ b/ircd/split.c Fri Jan 23 23:04:58 2009 +0100
996@@ -0,0 +1,773 @@
12e82c05 997+/*
998+ * IRC - Internet Relay Chat, ircd/split.c
999+ * Copyright (C) 1990 Jarkko Oikarinen and
1000+ * University of Oulu, Finland
1001+ * Copyright (C) 2000 Kevin L. Mitchell <klmitch@mit.edu>
1002+ *
1003+ * This program is free software; you can redistribute it and/or modify
1004+ * it under the terms of the GNU General Public License as published by
1005+ * the Free Software Foundation; either version 1, or (at your option)
1006+ * any later version.
1007+ *
1008+ * This program is distributed in the hope that it will be useful,
1009+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
1010+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1011+ * GNU General Public License for more details.
1012+ *
1013+ * You should have received a copy of the GNU General Public License
1014+ * along with this program; if not, write to the Free Software
1015+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1016+ */
1017+/** @file
1018+ * @brief Implementation of split server handling functions.
1019+ * @version $Id: split.c 1633 2006-03-25 03:46:56Z entrope $
1020+ */
1021+#include "config.h"
1022+
1023+#include "split.h"
1024+#include "client.h"
1025+#include "hash.h"
1026+#include "ircd.h"
1027+#include "ircd_alloc.h"
1028+#include "ircd_features.h"
1029+#include "ircd_log.h"
1030+#include "ircd_reply.h"
1031+#include "ircd_string.h"
1032+#include "list.h"
1033+#include "match.h"
1034+#include "msg.h"
1035+#include "numeric.h"
1036+#include "numnicks.h"
1037+#include "s_conf.h"
1038+#include "s_bsd.h"
1039+#include "s_debug.h"
1040+#include "s_misc.h"
1041+#include "send.h"
1042+#include "struct.h"
1043+#include "sys.h" /* FALSE bleah */
1044+
1045+/* #include <assert.h> -- Now using assert in ircd_log.h */
1046+#include <string.h>
1047+
1048+/** List of split entries. */
1049+static struct Split *GlobalSplitList = 0;
1050+
1051+/** Allocate a new Split entry with the given parameters.
1052+ * @param[in] server Server name for split entry.
1053+ * @param[in] reason Reason for the split entry.
1054+ * @param[in] creation Creation time for split entry.
1055+ * @param[in] expire Expiration time for split entry.
1056+ * @param[in] lastmod Last modification time for split entry.
1057+ * @param[in] lifetime Life time for split entry.
1058+ * @param[in] flags Flags to set for the split entry.
1059+ */
1060+static struct Split *
1061+split_make(char *server, const char *reason,
1062+ time_t creation, time_t expire, time_t lastmod, time_t lifetime,
1063+ unsigned int flags)
1064+{
1065+ struct Split *asplit;
1066+
1067+ asplit = (struct Split*) MyMalloc(sizeof(struct Split)); /* alloc memory */
1068+ assert(0 != asplit);
1069+
1070+ memset(asplit, 0, sizeof(*asplit));
1071+ /* TODO: some limit for servername length? HOSTLEN? */
1072+ DupString(asplit->sp_server, server); /* copy vital information */
0c63b280 1073+ /* TODO: some limit for reason length? QUITLEN TOPICLEN? */
65b70298 1074+ /* TODO: prefix the reason here with the opername/ID? */
12e82c05 1075+ DupString(asplit->sp_reason, reason);
1076+ /* TODO: what we use creation for and do we need it? */
1077+ asplit->sp_creation = creation;
1078+ asplit->sp_expire = expire;
1079+ /* TODO: are we using nettime etc.? CurrentTime is used. */
65b70298 1080+ /* TODO: TStime() is CurrentTime + TSoffset */
12e82c05 1081+ asplit->sp_lastmod = lastmod;
1082+ asplit->sp_lifetime = lifetime;
12e82c05 1083+ asplit->sp_flags = flags & SPLIT_ACTIVE; /* set split flags */
1084+
1085+ asplit->sp_next = GlobalSplitList; /* link it into the list */
1086+ asplit->sp_prev_p = &GlobalSplitList;
1087+ if (GlobalSplitList)
1088+ GlobalSplitList->sp_prev_p = &asplit->sp_next;
1089+ GlobalSplitList = asplit;
1090+
1091+ return asplit;
1092+}
1093+
1094+/** Forward a split entry to another server.
1095+ * @param[in] cptr Local client that sent us the split entry.
1096+ * @param[in] sptr Originator of the split entry.
1097+ * @param[in] split Split entry to forward.
1098+ * @param[in] reason Reason to send upstream (used by split_remove)
1099+ */
1100+static void
1101+split_propagate(struct Client *cptr, struct Client *sptr,
1102+ struct Split *split, const char *reason)
1103+{
1104+ sendcmdto_serv_butone(sptr, CMD_SPLIT, cptr, "%s%c%s %Tu %Tu %Tu %Tu :%s",
1105+ SplitIsRemoving(split) ? "!" : "",
1106+ SplitIsActive(split) && !SplitIsRemoving(split) ? '+' : '-', /* always !- not !+ */
1107+ split->sp_server, split->sp_creation, split->sp_expire,
1108+ split->sp_lastmod, split->sp_lifetime,
65b70298 1109+ /* TODO: does reason ? reason : split->sp_reason work too? */
12e82c05 1110+ reason != NULL ? reason : split->sp_reason);
1111+}
1112+
1113+/** Add a new server split entry.
1114+ * @param[in] cptr Local client that sent us the split entry.
1115+ * @param[in] sptr Originator of the split entry.
1116+ * @param[in] server Server name to split entry.
1117+ * @param[in] reason Reason for the split entry.
1118+ * @param[in] expire Expiration timestamp.
1119+ * @param[in] lastmod Last modification timestamp.
1120+ * @param[in] flags Flags to set on the split entry.
1121+ * @return Zero
1122+ */
1123+int
1124+split_add(struct Client *cptr, struct Client *sptr, char *server, const char *reason,
1125+ time_t creation, time_t expire, time_t lastmod, time_t lifetime,
1126+ unsigned int flags)
1127+{
1128+
12e82c05 1129+ struct Split *asplit;
1130+ struct Client *acptr;
1131+
1132+ assert(0 != server);
1133+ assert(0 != reason);
1134+ assert(NULL != cptr);
1135+ assert(NULL != sptr);
1136+
1137+ Debug((DEBUG_DEBUG, "split_add(\"%s\", \"%s\", \"%s\", \"%s\", %Tu, %Tu, %Tu, "
1138+ "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), server, reason,
1139+ creation, expire, lastmod, lifetime, flags));
1140+
1141+ /* not adding SPLIT for server that is linked
1142+ * if sptr is my user throw error
1143+ * otherwise ignore - SERVER and SPLIT messages can cross,
1144+ * or a server is bursting and it will see our end and destroy the SPLITs
1145+ */
1146+ if ((acptr = FindServer(server))) {
1147+ if (MyUser(sptr))
1148+ sendcmdto_one(&me, CMD_NOTICE, sptr,
1149+ "%C :Cannot add SPLIT %s - server is linked.", sptr, server);
1150+ return 0;
1151+ }
1152+
1153+ /*
1154+ * You cannot set a negative (or zero) duration, nor can you set an
1155+ * duration greater than SPLIT_MAX_EXPIRE.
1156+ */
0c63b280 1157+ if (expire - TStime() <= 0 || expire - TStime() > SPLIT_MAX_EXPIRE) {
12e82c05 1158+ if (!IsServer(cptr) && MyConnect(cptr))
0c63b280 1159+ return send_reply(cptr, ERR_BADEXPIRE, expire - TStime());
1160+ if (lifetime <= TStime()) /* no point going further */
12e82c05 1161+ /* CHECK: sptr may have the wrong idea about the nettime?
1162+ * or we could be wrong?
12e82c05 1163+ * perhaps raise some sort of warning to ops
65b70298 1164+ */
12e82c05 1165+ return 0;
1166+ }
1167+
65b70298 1168+ /* TODO: add opername */
12e82c05 1169+ /* inform ops and log it */
1170+ sendto_opmask_butone(0, SNO_NETWORK, "%s adding%sSPLIT for %s, expiring at %Tu: %s",
1171+ (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
1172+ cli_name(sptr) : cli_name((cli_user(sptr))->server),
1173+ !(flags & SPLIT_ACTIVE) ? " deactivated " : " ",
1174+ server, expire, reason);
1175+
0c63b280 1176+ log_write(LS_SPLIT, L_INFO, LOG_NOSNOTICE,
12e82c05 1177+ "%#C adding%sSPLIT for %s, expiring at %Tu: %s",
1178+ sptr, !(flags & SPLIT_ACTIVE) ? " deactivated " : " ",
1179+ server, expire, reason);
1180+
1181+ /* make the split entry */
1182+ asplit = split_make(server, reason, creation, expire, lastmod, lifetime, flags);
1183+
1184+ assert(asplit);
1185+
1186+ /* and propagate it */
1187+ split_propagate(cptr, sptr, asplit, NULL);
1188+
1189+ return 0;
1190+}
1191+
1192+
1193+/** Modify a split entry.
1194+ * @param[in] cptr Client that sent us the split modification.
1195+ * @param[in] sptr Client that originated the split modification.
1196+ * @param[in] split Split entry being modified.
1197+ * @param[in] action Resultant status of the G-line.
1198+ * @param[in] reason Reason.
1199+ * @param[in] expire Expiration time.
1200+ * @param[in] lastmod Last modification time.
1201+ * @param[in] lifetime Lifetime.
1202+ * @param[in] flags Bitwise combination of SPLIT_* flags.
1203+ * @return Zero.
1204+ */
1205+int
1206+split_modify(struct Client *cptr, struct Client *sptr, struct Split *split,
1207+ enum SplitAction action, const char *reason,
1208+ time_t creation, time_t expire, time_t lastmod, time_t lifetime,
1209+ unsigned int flags)
1210+{
1211+ struct Client* acptr;
1212+ char buf[BUFSIZE];
1213+ int pos = 0;
1214+
0c63b280 1215+ /* TODO: assert() on other inputs? */
12e82c05 1216+ assert(split);
1217+ assert(NULL != cptr);
1218+ assert(NULL != sptr);
1219+
0c63b280 1220+ Debug((DEBUG_DEBUG, "split_modify(\"%s\", \"%s\", \"%s\", \"%s\", %d, %Tu, %Tu, %Tu, "
1221+ "%Tu, 0x%04x)", cli_name(cptr), cli_name(sptr), split->sp_server, reason, action,
12e82c05 1222+ creation, expire, lastmod, lifetime, flags));
1223+
1224+ /* not modifying SPLIT for server that is linked
1225+ * if sptr is my user throw error
1226+ * otherwise ignore - SERVER and SPLIT messages can cross.
1227+ *
1228+ * note: we cleanup SPLIT entries at end of burst,
1229+ * and not when a server is introduced.
1230+ * so between net junction and end of burst,
1231+ * we can get SPLITs for a linked server.
65b70298 1232+ * also more than one server can be bursting at a time.
12e82c05 1233+ */
12e82c05 1234+ if ((acptr = FindServer(split->sp_server))) {
1235+ if (MyUser(sptr))
1236+ sendcmdto_one(&me, CMD_NOTICE, sptr,
1237+ "%C :Cannot modify SPLIT %s - server is linked.", sptr, split->sp_server);
0c63b280 1238+ /* SPLIT is not for a server part of a netmerge */
1239+ if (!SplitIsBurst(split))
1240+ split_free(split); /* and free it */
12e82c05 1241+ return 0;
1242+ }
1243+
1244+ /* First, let's check lastmod... */
0c63b280 1245+ if (split->sp_lastmod > lastmod) { /* we have a more recent version */
12e82c05 1246+ if (IsBurstOrBurstAck(cptr))
1247+ return 0; /* middle of a burst, it'll resync on its own */
1248+ return split_resend(cptr, split); /* resync the server */
0c63b280 1249+ } else if (split->sp_lastmod == lastmod)
12e82c05 1250+ return 0; /* we have that version of the split entry... */
1251+
1252+ /* All right, we know that there's a change of some sort. What is it? */
1253+ /* first, check out the expiration time... */
1254+ /* TODO: expire != 0 or NULL - check that we have something? */
1255+ if (expire != 0 && expire != split->sp_expire) {
0c63b280 1256+ if (expire - TStime() <= 0 || expire - TStime() > SPLIT_MAX_EXPIRE) {
12e82c05 1257+ if (!IsServer(sptr) && MyConnect(sptr)) /* bad expiration time */
0c63b280 1258+ return send_reply(sptr, ERR_BADEXPIRE, expire - TStime());
12e82c05 1259+ }
1260+ flags |= SPLIT_EXPIRE;
1261+ } else
1262+ flags &= ~SPLIT_EXPIRE;
0c63b280 1263+
12e82c05 1264+ /* Next, check out lifetime */
1265+ if (!(flags & SPLIT_LIFETIME) || !lifetime)
1266+ lifetime = split->sp_lifetime; /* use Split lifetime */
1267+
1268+ lifetime = IRCD_MAX(lifetime, expire); /* set lifetime to the max */
1269+
0c63b280 1270+ /* now that we have the correct lifetime, check it */
1271+ if (lifetime - TStime() <= 0) /* no point in going further */
1272+ return 0;
1273+
12e82c05 1274+ /* OK, let's see which is greater... */
1275+ if (lifetime > split->sp_lifetime)
1276+ flags |= SPLIT_LIFETIME; /* have to update lifetime */
1277+ else {
1278+ flags &= ~SPLIT_LIFETIME; /* no change to lifetime */
1279+ lifetime = 0;
1280+ }
1281+
1282+ /* Finally, let's see if the reason needs to be updated */
1283+ /* TODO: (reason) or use != NULL / 0 ? */
1284+ if ((flags & SPLIT_REASON) && reason &&
1285+ ircd_strcmp(split->sp_reason, reason) != 0)
1286+ flags &= ~SPLIT_REASON; /* no changes to the reason */
1287+
1288+ /* OK, now let's take a look at the action... */
1289+ if ((action == SPLIT_ACTIVATE && SplitIsActive(split)) ||
1290+ (action == SPLIT_DEACTIVATE && !SplitIsActive(split)) ||
1291+ /* can't activate an expired split entry */
0c63b280 1292+ (IRCD_MAX(split->sp_expire, expire) <= TStime()))
12e82c05 1293+ action = SPLIT_MODIFY; /* no activity state modifications */
1294+
1295+ Debug((DEBUG_DEBUG, "About to perform changes; flags 0x%04x, action %s",
1296+ flags, action == SPLIT_ACTIVATE ? "SPLIT_ACTIVATE" :
1297+ (action == SPLIT_DEACTIVATE ? "SPLIT_DEACTIVATE" :
1298+ (action == SPLIT_MODIFY ? "SPLIT_MODIFY" : "<UNKNOWN>"))));
1299+
1300+ /* If there are no changes to perform, do no changes */
1301+ if (!(flags & SPLIT_UPDATE) && action == SPLIT_MODIFY)
1302+ return 0;
1303+
1304+ /* Start by updating lastmod, if indicated... */
1305+ split->sp_lastmod = lastmod;
1306+
1307+ /* Then move on to activity status changes... */
1308+ switch (action) {
1309+ case SPLIT_ACTIVATE: /* activating split entry */
1310+ split->sp_flags |= SPLIT_ACTIVE; /* make it active... */
1311+ pos += ircd_snprintf(0, buf, sizeof(buf), " activating SPLIT");
1312+ break;
1313+
1314+ case SPLIT_DEACTIVATE: /* deactivating split entry */
1315+ split->sp_flags &= ~SPLIT_ACTIVE; /* make it inactive... */
1316+ pos += ircd_snprintf(0, buf, sizeof(buf), " deactivating SPLIT");
1317+ break;
1318+
1319+ case SPLIT_MODIFY: /* no change to activity status */
1320+ break;
1321+ }
1322+
1323+ /* Handle expiration changes... */
1324+ if (flags & SPLIT_EXPIRE) {
1325+ split->sp_expire = expire; /* save new expiration time */
1326+ if (pos < BUFSIZE)
1327+ pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
1328+ "%s%s changing expiration time to %Tu",
1329+ pos ? ";" : "",
1330+ pos && !(flags & (SPLIT_LIFETIME | SPLIT_REASON)) ?
1331+ " and" : "", expire);
1332+ }
1333+
1334+ /* Next, handle lifetime changes... */
1335+ if (flags & SPLIT_LIFETIME) {
1336+ split->sp_lifetime = lifetime; /* save new lifetime */
1337+ if (pos < BUFSIZE)
1338+ pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
1339+ "%s%s extending record lifetime to %Tu",
1340+ pos ? ";" : "", pos && !(flags & SPLIT_REASON) ?
1341+ " and" : "", lifetime);
1342+ }
1343+
1344+ /* Now, handle reason changes... */
1345+ if (flags & SPLIT_REASON) {
1346+ MyFree(split->sp_reason); /* release old reason */
1347+ DupString(split->sp_reason, reason); /* store new reason */
1348+ if (pos < BUFSIZE)
1349+ pos += ircd_snprintf(0, buf + pos, sizeof(buf) - pos,
1350+ "%s%s changing reason to \"%s\"",
1351+ pos ? ";" : "", pos ? " and" : "", reason);
1352+ }
1353+
65b70298 1354+ /* TODO: add opername */
12e82c05 1355+ /* All right, inform ops... */
1356+ sendto_opmask_butone(0, SNO_NETWORK, "%s modifying SPLIT for %s:%s",
1357+ (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
1358+ cli_name(sptr) : cli_name((cli_user(sptr))->server),
1359+ split->sp_server, buf);
1360+
12e82c05 1361+ /* and log the change */
0c63b280 1362+ log_write(LS_SPLIT, L_INFO, LOG_NOSNOTICE,
12e82c05 1363+ "%#C modifying SPLIT for %s:%s",
1364+ sptr, split->sp_server, buf);
1365+
1366+ /* and propagate it */
1367+ split_propagate(cptr, sptr, split, NULL);
1368+
1369+ return 0;
1370+}
1371+
1372+
1373+/** Remove a split entry.
1374+ * @param[in] cptr Local client that sent us the split entry.
1375+ * @param[in] sptr Originator of the split entry.
1376+ * @param[in] split Split entry to remove.
1377+ * @param[in] reason Reason for removing this split entry.
1378+ * @return Zero.
1379+ */
1380+int
1381+split_remove(struct Client *cptr, struct Client *sptr, struct Split *split, const char *reason)
1382+{
1383+ unsigned int saveflags = 0;
1384+
1385+ assert(0 != split);
1386+ assert(NULL != cptr);
1387+ assert(NULL != sptr);
1388+
1389+ Debug((DEBUG_DEBUG, "split_remove(\"%s\", \"%s\", \"%s\", \"%s\")",
1390+ cli_name(cptr), cli_name(sptr), split->sp_server, reason));
1391+
1392+ /* deactivate entry and mark it for removal (used in split_propagate) */
1393+ split->sp_flags |= SPLIT_REMOVING;
0c63b280 1394+ split->sp_flags &= ~SPLIT_ACTIVE;
12e82c05 1395+
65b70298 1396+ /* TODO: add opername? */
12e82c05 1397+ /* inform ops and log it */
1398+ sendto_opmask_butone(0, SNO_NETWORK, "%s removing SPLIT for %s, expiring at %Tu: %s (%s)",
1399+ (feature_bool(FEAT_HIS_SNOTICES) || IsServer(sptr)) ?
1400+ cli_name(sptr) : cli_name((cli_user(sptr))->server),
1401+ split->sp_server, split->sp_expire, split->sp_reason, reason);
1402+
0c63b280 1403+ log_write(LS_SPLIT, L_INFO, LOG_NOSNOTICE,
12e82c05 1404+ "%#C removing SPLIT for %s, expiring at %Tu: %s (%s)",
1405+ sptr, split->sp_server, split->sp_expire, split->sp_reason, reason);
1406+
12e82c05 1407+ /* propagate it */
1408+ split_propagate(cptr, sptr, split, reason);
1409+
1410+ /* destroy it */
1411+ split_free(split);
1412+
1413+ return 0;
1414+}
1415+
1416+
1417+/** Find a split entry by name.
1418+ * @param[in] server Split entry name to search for.
1419+ * @return Matching split entry (or NULL if none match).
1420+ */
1421+struct Split *
1422+split_find(char *server)
1423+{
1424+ struct Split* split;
1425+ struct Split* ssplit;
1426+
1427+ for (split = GlobalSplitList; split; split = ssplit) { /* go through splits */
1428+ ssplit = split->sp_next;
1429+
1430+ if (split_expire(split)) /* expire any that need expiring */
1431+ split_free(split);
1432+ else if (0 == ircd_strcmp(server, split->sp_server)) /* found it yet? */
1433+ return split;
1434+ }
1435+ /* TODO: we return 0 not NULL? */
1436+ return 0;
1437+}
1438+
1439+/** Unlink and free an unused split entry.
1440+ * @param[in] split Server split entry to free.
1441+ */
1442+void
1443+split_free(struct Split* split)
1444+{
1445+ /* TODO: use 0 or NULL ? */
1446+ assert(0 != split);
1447+
1448+ *split->sp_prev_p = split->sp_next; /* squeeze this split entry out */
1449+ if (split->sp_next)
1450+ split->sp_next->sp_prev_p = split->sp_prev_p;
1451+
1452+ /* CHECK: the other fields in this struct are destroyed with MyFree() call? */
1453+ MyFree(split->sp_server); /* and free up the memory */
1454+ MyFree(split->sp_reason);
1455+ MyFree(split);
1456+}
1457+
1458+
1459+/** Check whether a split entry has past its life time.
1460+ * when entry is active and past expire time, but not life time, deactivate it
1461+ * @param[in] split Server split entry to check.
1462+ * @return 1 when entry can be free'd, 0 otherwise.
1463+ */
1464+int
1465+split_expire(struct Split* split)
1466+{
1467+ assert(0 != split);
1468+
65b70298 1469+ /* TODO: also check if the server in SPLIT entry is linked atm? */
12e82c05 1470+ /* past lifetime */
0c63b280 1471+ if (split->sp_lifetime <= TStime())
12e82c05 1472+ return 1;
1473+
1474+ /* past expire time, deactivate entry if it is active */
0c63b280 1475+ if ((split->sp_expire <= TStime()) && SplitIsActive(split))
1476+ split->sp_flags &= ~SPLIT_ACTIVE;
12e82c05 1477+
1478+ return 0;
1479+}
1480+
1481+/** Send the full list of split entries to \a cptr.
1482+ * @param[in] cptr Local server to send split entries to.
1483+ */
1484+void
1485+split_burst(struct Client *cptr)
1486+{
1487+ struct Split *split;
1488+ struct Split *ssplit;
1489+
1490+ assert(NULL != cptr);
1491+
1492+ for (split = GlobalSplitList; split; split = ssplit) { /* go through splits */
1493+ ssplit = split->sp_next;
1494+
1495+ /* expire any that need expiring */
1496+ if (split_expire(split))
1497+ split_free(split);
1498+ /* if we have an entry for cptr, dont send it - but do not free here yet
1499+ * free it at end of burst, to get the correct count for SPLITs removed.
1500+ */
1501+ else if (ircd_strcmp(split->sp_server, cli_name(cptr)) == 0)
1502+ continue;
1503+ else
1504+ sendcmdto_one(&me, CMD_SPLIT, cptr, "%c%s %Tu %Tu %Tu %Tu :%s",
1505+ SplitIsActive(split) ? '+' : '-', split->sp_server,
1506+ split->sp_creation, split->sp_expire,
1507+ split->sp_lastmod, split->sp_lifetime,
1508+ split->sp_reason);
1509+ }
1510+}
1511+
1512+/** Forward a split to another server.
1513+ * @param[in] cptr Server to send split entries to.
1514+ * @param[in] split Split to forward.
1515+ * @return Zero.
1516+ */
1517+int
1518+split_resend(struct Client *cptr, struct Split *split)
1519+{
1520+ sendcmdto_one(&me, CMD_SPLIT, cptr, "%c%s %Tu %Tu %Tu %Tu :%s",
1521+ SplitIsActive(split) ? '+' : '-', split->sp_server,
1522+ split->sp_creation, split->sp_expire,
1523+ split->sp_lastmod, split->sp_lifetime,
1524+ split->sp_reason);
1525+
1526+ return 0;
1527+}
1528+
1529+/** Send a split entry (or a list of split entries) to a server.
1530+ * @param[in] sptr Client searching for split entries.
1531+ * @param[in] server Name of split entry to search for (if NULL, list all).
1532+ * @return Zero.
1533+ */
1534+int
1535+split_list(struct Client *sptr, char *server)
1536+{
1537+ struct Split *split;
0c63b280 1538+ int count = 0;
12e82c05 1539+
1540+ assert(NULL != sptr);
1541+
0c63b280 1542+ /* go through SPLITs */
1543+ for (split = GlobalSplitList; split; split = split->sp_next) {
1544+ if (split_expire(split)) { /* expire any that need expiring */
1545+ split_free(split);
1546+ continue;
1547+ }
1548+ if (server && match(server, split->sp_server))
1549+ continue;
1550+ count++;
12e82c05 1551+ send_reply(sptr, RPL_SPLITLIST, split->sp_server,
0c63b280 1552+ split->sp_creation, split->sp_expire, split->sp_lastmod, split->sp_lifetime,
12e82c05 1553+ SplitIsActive(split) ? '+' : '-', split->sp_reason);
12e82c05 1554+ }
1555+
0c63b280 1556+ /* server given and nothing found */
1557+ if (server && !count)
1558+ return send_reply(sptr, ERR_NOSUCHSPLIT, server);
1559+
12e82c05 1560+ /* end of splitlist information */
1561+ return send_reply(sptr, RPL_ENDOFSPLITLIST);
1562+}
1563+
1564+
0c63b280 1565+/** Mark SPLIT for new server with SPLIT_BURST
1566+ * @param[in] acptr Server introduced to the network
1567+ */
1568+void
1569+split_newserver(struct Client *acptr)
1570+{
1571+ struct Split *split;
1572+
1573+ assert(NULL != acptr);
1574+
1575+ Debug((DEBUG_DEBUG, "split_newserver(\"%s\")", cli_name(acptr)));
1576+
1577+ /* mark the SPLIT with SPLIT_BURST
1578+ * so we know this SPLIT is marked for removal already
1579+ * but not remove it just yet to get the correct count
1580+ * of SPLITs removed at the end of burst.
1581+ * also we need the SPLIT in case the net breaks again
1582+ * before end of burst?
1583+ */
1584+ if ((split = split_find(cli_name(acptr))))
1585+ split->sp_flags |= SPLIT_BURST;
1586+ return;
1587+}
1588+
1589+
12e82c05 1590+/** Auto destroy SPLITs for servers gained in a netmerge
0c63b280 1591+ * @param[in] sptr Server that has linked to the network
12e82c05 1592+ * @return Number of destroyed SPLITs
1593+ */
1594+int
0c63b280 1595+split_netmerge(struct Client *sptr)
12e82c05 1596+{
1597+ struct DLink *lp;
1598+ struct Split *split;
1599+ int count = 0;
1600+
0c63b280 1601+ assert(NULL != sptr);
12e82c05 1602+
0c63b280 1603+ Debug((DEBUG_DEBUG, "split_netmerge(\"%s\")", cli_name(sptr)));
12e82c05 1604+
1605+ /* find the SPLIT for this server */
0c63b280 1606+ if ((split = split_find(cli_name(sptr)))) {
12e82c05 1607+ split_free(split);
1608+ count++;
1609+ }
1610+
1611+ /* go over its downlinks */
0c63b280 1612+ for (lp = cli_serv(sptr)->down; lp; lp = lp->next)
1613+ count += split_netmerge(lp->value.cptr);
12e82c05 1614+ return count;
1615+}
1616+
1617+
1618+/** Auto create SPLITs for servers lost in a netbreak
0c63b280 1619+ * @param[in] victim Server that lost link to network
12e82c05 1620+ * @param[in] reason Reason to add to SPLITs
1621+ * @return Number of created SPLITs
1622+ */
1623+int
0c63b280 1624+split_netbreak(struct Client *victim, const char *reason)
12e82c05 1625+{
1626+ struct DLink *lp;
1627+ struct Split *split;
1628+ int count = 0;
0c63b280 1629+ time_t creation = TStime(), expire = TStime() + feature_int(FEAT_SPLIT_AUTO_EXPIRE),
1630+ lastmod = TStime(), lifetime = expire;
12e82c05 1631+ unsigned int flags = SPLIT_ACTIVE;
1632+
0c63b280 1633+ assert(NULL != victim);
12e82c05 1634+
0c63b280 1635+ Debug((DEBUG_DEBUG, "split_netbreak(\"%s\", \"%s\")", cli_name(victim), reason));
12e82c05 1636+
65b70298 1637+ /* bad auto expire value */
1638+ if (feature_int(FEAT_SPLIT_AUTO_EXPIRE) <= 0)
1639+ return 0;
1640+
0c63b280 1641+ /* TODO: what if we do have a SPLIT for this server?
1642+ * we remove splits in end of burst
1643+ * and if the netbreaks again before that
1644+ * free the SPLIT and create a new one?
1645+ * or simply update lastmod/lifetime/reason but leave the status?
1646+ */
12e82c05 1647+ /* find the SPLIT for this server */
0c63b280 1648+ if (!(split = split_find(cli_name(victim)))) {
1649+ split_make(cli_name(victim), reason, creation, expire, lastmod, lifetime, flags);
12e82c05 1650+ count++;
0c63b280 1651+ } else
1652+ split->sp_flags &= SPLIT_BURST; /* clear the burst flag for it */
12e82c05 1653+
1654+ /* go over its downlinks */
0c63b280 1655+ for (lp = cli_serv(victim)->down; lp; lp = lp->next)
1656+ count += split_netbreak(lp->value.cptr, reason);
12e82c05 1657+ return count;
1658+}
1659+
1660+
1661+/** Auto create SPLITs for servers we have a Connect Block for
1662+ *
1663+ */
1664+void
1665+split_conf()
1666+{
1667+ struct ConfItem *conf;
1668+ struct Client *server;
1669+ struct Split *split;
1670+ struct Split *asplit;
0c63b280 1671+ time_t creation = TStime(), expire = TStime() + feature_int(FEAT_SPLIT_AUTO_EXPIRE),
1672+ lastmod = TStime(), lifetime = expire;
12e82c05 1673+ unsigned int flags = SPLIT_ACTIVE;
1674+ char reason[BUFSIZE];
1675+
1676+ Debug((DEBUG_DEBUG, "split_conf()"));
1677+
1678+ /* we are not set to generate SPLITs */
1679+ if (!feature_bool(FEAT_SPLIT))
1680+ return;
1681+
0c63b280 1682+ /* TODO: log this problem? */
65b70298 1683+ /* bad auto expire value */
1684+ if (feature_int(FEAT_SPLIT_AUTO_EXPIRE) <= 0)
1685+ return;
1686+
12e82c05 1687+ ircd_snprintf(0, reason, sizeof(reason),
0c63b280 1688+ "Generated upon loading conf file on %C", &me);
12e82c05 1689+
1690+ /* go over the conf contents */
1691+ for (conf = GlobalConfList; conf; conf = conf->next) {
1692+ /* not a Connect Block */
1693+ if (CONF_SERVER != conf->status)
1694+ continue;
1695+ /* server is linked */
1696+ if (server = FindServer(conf->name))
1697+ continue;
1698+ /* we have a SPLIT for this server already */
1699+ if (split = split_find(conf->name))
1700+ continue;
65b70298 1701+ /* TODO: use SNO_OLDSNO here?
1702+ * just like nickcollisions on local users go there?
1703+ * or at least change the source / message format?
1704+ * /REHASH goes to SNO_OLDSNO? so this too?
1705+ * if oper did rehash, add his name here?
1706+ */
12e82c05 1707+ /* inform ops and log it */
1708+ sendto_opmask_butone(0, SNO_NETWORK, "%C adding SPLIT for %s, expiring at %Tu: %s",
1709+ &me, conf->name, expire, reason);
1710+
0c63b280 1711+ log_write(LS_SPLIT, L_INFO, LOG_NOSNOTICE,
12e82c05 1712+ "%C adding SPLIT for %s, expiring at %Tu: %s",
1713+ &me, conf->name, expire, reason);
1714+
1715+ /* make the split entry */
1716+ asplit = split_make(conf->name, reason, creation, expire, lastmod, lifetime, flags);
1717+
1718+ assert(asplit);
1719+
1720+ /* and propagate it */
1721+ split_propagate(&me, &me, asplit, NULL);
1722+ }
1723+}
1724+
1725+
1726+/** Statistics callback to list SPLITs.
1727+ * @param[in] sptr Client requesting statistics.
1728+ * @param[in] sd Stats descriptor for request (ignored).
1729+ * @param[in] param Extra parameter from user (ignored).
1730+ */
1731+void
1732+split_stats(struct Client *sptr, const struct StatDesc *sd,
1733+ char *param)
1734+{
1735+ struct Split *split;
12e82c05 1736+
65b70298 1737+ /* send header so the client knows what we are showing */
1738+ send_reply(sptr, SND_EXPLICIT | RPL_STATSHEADER,
1739+ "S Server Creation Expire Lastmod Lifetime Status :Reason");
1740+
0c63b280 1741+ for (split = GlobalSplitList; split; split = split->sp_next) {
1742+ if (param && match(param, split->sp_server))
1743+ continue;
12e82c05 1744+ send_reply(sptr, RPL_STATSSPLIT, split->sp_server,
1745+ split->sp_creation, split->sp_expire, split->sp_lastmod, split->sp_lifetime,
1746+ SplitIsActive(split) ? '+' : '-', split->sp_reason);
0c63b280 1747+ }
12e82c05 1748+}
1749+
1750+
1751+/** Count split entries and memory used by them.
1752+ * @param[out] sp_size Receives total number of bytes allocated for split entries.
1753+ * @return Number of split entries currently allocated.
1754+ */
1755+int
1756+split_memory_count(size_t *sp_size)
1757+{
1758+ struct Split *split;
1759+ unsigned int sp = 0;
1760+
0c63b280 1761+ for (split = GlobalSplitList; split; split = split->sp_next) {
12e82c05 1762+ sp++;
1763+ *sp_size += sizeof(struct Split);
1764+ *sp_size += split->sp_server ? (strlen(split->sp_server) + 1) : 0;
1765+ *sp_size += split->sp_reason ? (strlen(split->sp_reason) + 1) : 0;
1766+ }
1767+ return sp;
1768+}
1769+