2 * IRC - Internet Relay Chat, ircd/s_misc.c (formerly ircd/date.c)
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * @brief Miscellaneous support functions.
25 * @version $Id: s_misc.c 1818 2007-07-14 02:40:01Z isomer $
35 #include "ircd_alloc.h"
36 #include "ircd_features.h"
38 #include "ircd_reply.h"
39 #include "ircd_snprintf.h"
40 #include "ircd_string.h"
47 #include "querycmds.h"
61 /* #include <assert.h> -- Now using assert in ircd_log.h */
69 /** Array of English month names (0 = January). */
70 static char *months[] = {
71 "January", "February", "March", "April",
72 "May", "June", "July", "August",
73 "September", "October", "November", "December"
76 /** Array of English day names (0 = Sunday). */
77 static char *weekdays[] = {
78 "Sunday", "Monday", "Tuesday", "Wednesday",
79 "Thursday", "Friday", "Saturday"
85 /** Global statistics structure. */
86 static struct ServerStatistics ircst;
87 /** Public pointer to global statistics structure. */
88 struct ServerStatistics* ServerStats = &ircst;
90 /** Formats a Unix time as a readable string.
91 * @param clock Unix time to format (0 means #CurrentTime).
92 * @return Pointer to a static buffer containing something like
93 * "Sunday January 1 2000 -- 09:30 +01:00"
95 char *date(time_t clock)
97 static char buf[80], plus;
105 memcpy(&gmbuf, gm, sizeof(gmbuf));
107 lt = localtime(&clock);
109 /* There is unfortunately no clean portable way to extract time zone
110 * offset information, so do ugly things.
112 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
113 if (lt->tm_yday != gm->tm_yday)
115 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
116 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
122 plus = (minswest > 0) ? '-' : '+';
124 minswest = -minswest;
126 sprintf(buf, "%s %s %d %d -- %02d:%02d %c%02d:%02d",
127 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
128 1900 + lt->tm_year, lt->tm_hour, lt->tm_min,
129 plus, minswest / 60, minswest % 60);
134 /** Like ctime() but with no trailing newline. Also, it takes
135 * the time value as parameter, instead of pointer to it.
136 * @param value Unix time to format.
137 * @return Pointer to a static buffer containing formatted time.
139 char *myctime(time_t value)
141 /* Use a secondary buffer in case ctime() would not replace an
142 * overwritten newline.
147 strcpy(buf, ctime(&value));
148 if ((p = strchr(buf, '\n')) != NULL)
154 /** Return the name of the client for various tracking and admin
155 * purposes. The main purpose of this function is to return the
156 * "socket host" name of the client, if that differs from the
157 * advertised name (other than case). But, this can be used on any
159 * @param sptr Client to operate on.
160 * @param showip If non-zero, append [username\@text-ip] to name.
161 * @return Either cli_name(\a sptr) or a static buffer.
163 const char* get_client_name(const struct Client* sptr, int showip)
165 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
167 if (!MyConnect(sptr) || !showip)
168 return cli_name(sptr);
169 ircd_snprintf(0, nbuf, sizeof(nbuf), "%s[%s@%s]", cli_name(sptr),
170 IsIdented(sptr) ? cli_username(sptr) : "",
176 * Exit one client, local or remote. Assuming for local client that
177 * all dependents already have been removed, and socket is closed.
178 * @param bcptr Client being (s)quitted.
179 * @param comment The QUIT comment to send.
181 /* Rewritten by Run - 24 sept 94 */
182 static void exit_one_client(struct Client* bcptr, const char* comment)
187 if (cli_serv(bcptr) && cli_serv(bcptr)->client_list) /* Was SetServerYXX called ? */
188 ClearServerYXX(bcptr); /* Removes server from server_list[] */
191 * clear out uping requests
194 uping_cancel(bcptr, 0);
196 * Stop a running /LIST clean
198 if (MyUser(bcptr) && cli_listing(bcptr)) {
199 MyFree(cli_listing(bcptr));
200 cli_listing(bcptr) = NULL;
203 * If a person is on a channel, send a QUIT notice
204 * to every client (person) on the same channel (so
205 * that the client can show the "**signoff" message).
206 * (Note: The notice is to the local clients *only*)
209 sendcmdto_common_channels_butone_audit(bcptr, CMD_QUIT, NULL, ":%s", comment);
211 remove_user_from_all_channels(bcptr);
213 /* Clean up invitefield */
214 while ((lp = cli_user(bcptr)->invited))
215 del_invite(bcptr, lp->value.chptr);
217 /* Clean up silencefield */
218 while ((bp = cli_user(bcptr)->silence)) {
219 cli_user(bcptr)->silence = bp->next;
223 /* Clean up snotice lists */
225 set_snomask(bcptr, ~0, SNO_DEL);
227 if (IsInvisible(bcptr)) {
228 assert(UserStats.inv_clients > 0);
229 --UserStats.inv_clients;
232 assert(UserStats.opers > 0);
235 if (MyConnect(bcptr))
236 Count_clientdisconnects(bcptr, UserStats);
238 Count_remoteclientquits(UserStats, bcptr);
240 else if (IsServer(bcptr))
242 /* Remove downlink list node of uplink */
243 remove_dlink(&(cli_serv(cli_serv(bcptr)->up))->down, cli_serv(bcptr)->updown);
244 cli_serv(bcptr)->updown = 0;
246 if (MyConnect(bcptr))
247 Count_serverdisconnects(UserStats);
249 Count_remoteserverquits(UserStats);
251 else if (IsMe(bcptr))
253 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
255 return; /* ...must *never* exit self! */
257 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
258 Count_unknowndisconnects(UserStats);
263 if (IsIPChecked(bcptr))
264 IPcheck_disconnect(bcptr);
267 * Remove from serv->client_list
268 * NOTE: user is *always* NULL if this is a server
270 if (cli_user(bcptr)) {
271 assert(!IsServer(bcptr));
272 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
273 RemoveYXXClient(cli_user(bcptr)->server, cli_yxx(bcptr));
276 /* Remove bcptr from the client list */
278 if (hRemClient(bcptr) != 0)
279 Debug((DEBUG_ERROR, "%p !in tab %s[%s] %p %p %p %d %d %p",
280 bcptr, cli_name(bcptr), cli_from(bcptr) ? cli_sockhost(cli_from(bcptr)) : "??host",
281 cli_from(bcptr), cli_next(bcptr), cli_prev(bcptr), cli_fd(bcptr),
282 cli_status(bcptr), cli_user(bcptr)));
286 remove_client_from_list(bcptr);
289 /* exit_downlinks - added by Run 25-9-94 */
291 * Removes all clients and downlinks (+clients) of any server
292 * QUITs are generated and sent to local users.
293 * @param cptr server that must have all dependents removed
294 * @param sptr source who thought that this was a good idea
295 * @param comment comment sent as sign off message to local clients
297 static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
299 struct Client *acptr;
302 struct Client **acptrp;
305 /* Run over all its downlinks */
306 for (lp = cli_serv(cptr)->down; lp; lp = next)
309 acptr = lp->value.cptr;
310 /* Remove the downlinks and client of the downlink */
311 exit_downlinks(acptr, sptr, comment);
312 /* Remove the downlink itself */
313 exit_one_client(acptr, cli_name(&me));
315 /* Remove all clients of this server */
316 acptrp = cli_serv(cptr)->client_list;
317 for (i = 0; i <= cli_serv(cptr)->nn_mask; ++acptrp, ++i) {
319 exit_one_client(*acptrp, comment);
323 /* exit_client, rewritten 25-9-94 by Run */
325 * Exits a client of *any* type (user, server, etc)
326 * from this server. Also, this generates all necessary prototol
327 * messages that this exit may cause.
329 * This function implicitly exits all other clients depending on
332 * For convenience, this function returns a suitable value for
333 * m_function return value:
335 * CPTR_KILLED if (cptr == bcptr)
336 * 0 if (cptr != bcptr)
338 * This function can be called in two ways:
339 * 1) From before or in parse(), exiting the 'cptr', in which case it was
340 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
341 * return CPTR_KILLED.
342 * 2) Via parse from a m_function call, in which case it was invoked as
343 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
344 * that generated the message in a way that we can assume he already
345 * did remove acptr from memory himself (or in other cases we don't mind
346 * because he will be delinked.) Or invoked as:
347 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
349 * In general: No generated SQUIT or QUIT should be sent to source link
350 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
353 * @param cptr Connection currently being handled by read_message.
354 * @param victim Client being killed.
355 * @param killer Client that made the decision to remove \a victim.
356 * @param comment Reason for the exit.
357 * @return CPTR_KILLED if cptr == bcptr, else 0.
359 int exit_client(struct Client *cptr,
360 struct Client* victim,
361 struct Client* killer,
364 struct Client* acptr = 0;
368 char comment1[HOSTLEN + HOSTLEN + 2];
370 if (MyConnect(victim))
372 SetFlag(victim, FLAG_CLOSING);
374 if (feature_bool(FEAT_CONNEXIT_NOTICES) && IsUser(victim))
375 sendto_opmask_butone(0, SNO_CONNEXIT,
376 "Client exiting: %s (%s@%s) [%s] [%s] <%s%s>",
377 cli_name(victim), cli_user(victim)->username,
378 cli_user(victim)->host, comment,
379 ircd_ntoa(&cli_ip(victim)),
380 NumNick(victim) /* two %s's */);
383 on_for = CurrentTime - cli_firsttime(victim);
385 if (IsUser(victim) || IsUserPort(victim))
386 auth_send_exit(victim);
389 log_write(LS_USER, L_TRACE, 0, "%Tu %i %s@%s %s %s %s%s %s :%s",
390 cli_firsttime(victim), on_for,
391 cli_user(victim)->username, cli_sockhost(victim),
392 ircd_ntoa(&cli_ip(victim)),
393 IsAccount(victim) ? cli_username(victim) : "0",
394 NumNick(victim), /* two %s's */
395 cli_name(victim), cli_info(victim));
397 if (victim != cli_from(killer) /* The source knows already */
398 && IsClient(victim)) /* Not a Ping struct or Log file */
400 if (IsServer(victim) || IsHandshake(victim))
401 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", cli_name(&me), comment);
402 else if (!IsConnecting(victim)) {
403 if (!IsDead(victim)) {
404 if (IsServer(victim))
405 sendcmdto_one(killer, CMD_ERROR, victim,
406 ":Closing Link: %s by %s (%s)", cli_name(victim),
407 cli_name(killer), comment);
409 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
411 cli_name(IsServer(killer) ? &his : killer),
415 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
416 (killer == &me || (IsServer(killer) &&
417 (strncmp(comment, "Leaf-only link", 14) ||
418 strncmp(comment, "Non-Hub link", 12)))))
421 * Note: check user == user needed to make sure we have the same
424 if (cli_serv(victim)->user && *(cli_serv(victim))->by &&
425 (acptr = findNUser(cli_serv(victim)->by))) {
426 if (cli_user(acptr) == cli_serv(victim)->user) {
427 sendcmdto_one(&me, CMD_NOTICE, acptr,
428 "%C :Link with %s canceled: %s", acptr,
429 cli_name(victim), comment);
433 * not right client, set by to empty string
436 *(cli_serv(victim))->by = '\0';
440 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s canceled: %s",
441 cli_name(victim), comment);
445 * Close the Client connection first.
447 close_connection(victim);
450 if (IsServer(victim))
452 if (feature_bool(FEAT_HIS_NETSPLIT))
453 strcpy(comment1, "*.net *.split");
456 strcpy(comment1, cli_name(cli_serv(victim)->up));
457 strcat(comment1, " ");
458 strcat(comment1, cli_name(victim));
462 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
463 (cli_user(killer)->server == victim ||
464 cli_user(killer)->server == cli_serv(victim)->up) ?
466 get_client_name(killer, HIDE_IP),
467 cli_name(cli_user(killer)->server));
468 else if (killer != &me && cli_serv(victim)->up != killer)
469 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
470 cli_name(victim), IsServer(killer) ? cli_name(killer) :
471 get_client_name(killer, HIDE_IP));
472 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
473 cli_serv(victim)->up, victim, comment);
477 * First generate the needed protocol for the other server links
480 for (dlp = cli_serv(&me)->down; dlp; dlp = dlp->next) {
481 if (dlp->value.cptr != cli_from(killer) && dlp->value.cptr != victim)
483 if (IsServer(victim))
484 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
485 cli_name(victim), cli_serv(victim)->timestamp, comment);
486 else if (IsUser(victim) && !HasFlag(victim, FLAG_KILLED))
487 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
490 /* Then remove the client structures */
491 if (IsServer(victim))
492 exit_downlinks(victim, killer, comment1);
493 exit_one_client(victim, comment);
496 * cptr can only have been killed if it was cptr itself that got killed here,
497 * because cptr can never have been a dependent of victim --Run
499 return (cptr == victim) ? CPTR_KILLED : 0;
503 * Exit client with formatted va_list message.
504 * Thin wrapper around exit_client().
505 * @param cptr Connection being processed.
506 * @param bcptr Connection being closed.
507 * @param sptr Connection who asked to close the victim.
508 * @param pattern Format string for message.
509 * @param vl Stdargs argument list.
510 * @return Has a tail call to exit_client().
512 /* added 25-9-94 by Run */
513 int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
514 const char *pattern, va_list vl)
517 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
518 return exit_client(cptr, bcptr, sptr, msgbuf);
522 * Exit client with formatted message using a variable-length argument list.
523 * Thin wrapper around exit_client().
524 * @param cptr Connection being processed.
525 * @param bcptr Connection being closed.
526 * @param sptr Connection who asked to close the victim.
527 * @param pattern Format string for message.
528 * @return Has a tail call to exit_client().
530 int exit_client_msg(struct Client *cptr, struct Client *bcptr,
531 struct Client *sptr, const char *pattern, ...)
536 va_start(vl, pattern);
537 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
540 return exit_client(cptr, bcptr, sptr, msgbuf);
543 /** Initialize global server statistics. */
544 /* (Kind of pointless since C guarantees it's already zero'ed, but... */
547 memset(&ircst, 0, sizeof(ircst));
550 /** Report server statistics to a client.
551 * @param cptr Client who wants statistics.
552 * @param sd StatDesc structure being looked up (unused).
553 * @param param Extra parameter passed by user (unused).
555 void tstats(struct Client *cptr, const struct StatDesc *sd, char *param)
557 struct Client *acptr;
559 struct ServerStatistics *sp;
560 struct ServerStatistics tmp;
563 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
564 for (i = 0; i < MAXCONNECTIONS; i++)
566 if (!(acptr = LocalClientArray[i]))
570 sp->is_sbs += cli_sendB(acptr);
571 sp->is_sbr += cli_receiveB(acptr);
572 sp->is_sti += CurrentTime - cli_firsttime(acptr);
575 else if (IsUser(acptr))
577 sp->is_cbs += cli_sendB(acptr);
578 sp->is_cbr += cli_receiveB(acptr);
579 sp->is_cti += CurrentTime - cli_firsttime(acptr);
582 else if (IsUnknown(acptr))
586 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
587 sp->is_ac, sp->is_ref);
588 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
589 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
590 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
591 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
592 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
593 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
594 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
595 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
596 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
597 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
598 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
600 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
601 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
602 sp->is_cl, sp->is_sv);
603 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %Lu %Lu",
604 sp->is_cbs, sp->is_sbs);
605 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %Lu %Lu",
606 sp->is_cbr, sp->is_sbr);
607 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Lu %Lu",
608 sp->is_cti, sp->is_sti);