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.
35 #include "ircd_alloc.h"
36 #include "ircd_auth.h"
37 #include "ircd_features.h"
39 #include "ircd_reply.h"
40 #include "ircd_snprintf.h"
41 #include "ircd_string.h"
48 #include "querycmds.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)) {
169 ircd_snprintf(0, nbuf, sizeof(nbuf), "%s[%s@%s]", cli_name(sptr),
170 IsIdented(sptr) ? cli_username(sptr) : "unknown",
173 return cli_name(sptr);
176 return cli_name(sptr);
179 /** Set cli_sockhost(cptr) from \a host.
180 * If \a host contains an '@', copy starting after that byte.
181 * Otherwise copy all of \a host.
182 * @param cptr Client to operate on.
183 * @param host hostname or user\@hostname string.
185 void get_sockhost(struct Client *cptr, char *host)
188 if ((s = strchr(host, '@')))
192 ircd_strncpy(cli_sockhost(cptr), s, HOSTLEN);
196 * Exit one client, local or remote. Assuming for local client that
197 * all dependants already have been removed, and socket is closed.
198 * @param bcptr Client being (s)quitted.
199 * @param comment The QUIT comment to send.
201 /* Rewritten by Run - 24 sept 94 */
202 static void exit_one_client(struct Client* bcptr, const char* comment)
207 if (cli_serv(bcptr) && cli_serv(bcptr)->client_list) /* Was SetServerYXX called ? */
208 ClearServerYXX(bcptr); /* Removes server from server_list[] */
211 * clear out uping requests
214 uping_cancel(bcptr, 0);
216 * Stop a running /LIST clean
218 if (MyUser(bcptr) && cli_listing(bcptr)) {
219 MyFree(cli_listing(bcptr));
220 cli_listing(bcptr) = NULL;
223 * If a person is on a channel, send a QUIT notice
224 * to every client (person) on the same channel (so
225 * that the client can show the "**signoff" message).
226 * (Note: The notice is to the local clients *only*)
228 sendcmdto_common_channels_butone(bcptr, CMD_QUIT, NULL, ":%s", comment);
230 remove_user_from_all_channels(bcptr);
232 /* Clean up invitefield */
233 while ((lp = cli_user(bcptr)->invited))
234 del_invite(bcptr, lp->value.chptr);
236 /* Clean up silencefield */
237 while ((bp = cli_user(bcptr)->silence)) {
238 cli_user(bcptr)->silence = bp->next;
242 /* Clean up snotice lists */
244 set_snomask(bcptr, ~0, SNO_DEL);
246 if (IsInvisible(bcptr))
247 --UserStats.inv_clients;
250 if (MyConnect(bcptr))
251 Count_clientdisconnects(bcptr, UserStats);
253 Count_remoteclientquits(UserStats, bcptr);
256 else if (IsServer(bcptr))
258 /* Remove downlink list node of uplink */
259 remove_dlink(&(cli_serv(cli_serv(bcptr)->up))->down, cli_serv(bcptr)->updown);
260 cli_serv(bcptr)->updown = 0;
262 if (MyConnect(bcptr))
263 Count_serverdisconnects(UserStats);
265 Count_remoteserverquits(UserStats);
267 else if (IsMe(bcptr))
269 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
271 return; /* ...must *never* exit self! */
273 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
274 Count_unknowndisconnects(UserStats);
279 if (IsIPChecked(bcptr))
280 IPcheck_disconnect(bcptr);
283 * Remove from serv->client_list
284 * NOTE: user is *always* NULL if this is a server
286 if (cli_user(bcptr)) {
287 assert(!IsServer(bcptr));
288 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
289 RemoveYXXClient(cli_user(bcptr)->server, cli_yxx(bcptr));
290 if (IsIAuthed(bcptr) || cli_iauth(bcptr))
291 iauth_exit_client(bcptr);
294 /* Remove bcptr from the client list */
296 if (hRemClient(bcptr) != 0)
297 Debug((DEBUG_ERROR, "%p !in tab %s[%s] %p %p %p %d %d %p",
298 bcptr, cli_name(bcptr), cli_from(bcptr) ? cli_sockhost(cli_from(bcptr)) : "??host",
299 cli_from(bcptr), cli_next(bcptr), cli_prev(bcptr), cli_fd(bcptr),
300 cli_status(bcptr), cli_user(bcptr)));
304 remove_client_from_list(bcptr);
307 /* exit_downlinks - added by Run 25-9-94 */
309 * Removes all clients and downlinks (+clients) of any server
310 * QUITs are generated and sent to local users.
311 * @param cptr server that must have all dependents removed
312 * @param sptr source who thought that this was a good idea
313 * @param comment comment sent as sign off message to local clients
315 static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
317 struct Client *acptr;
320 struct Client **acptrp;
323 /* Run over all its downlinks */
324 for (lp = cli_serv(cptr)->down; lp; lp = next)
327 acptr = lp->value.cptr;
328 /* Remove the downlinks and client of the downlink */
329 exit_downlinks(acptr, sptr, comment);
330 /* Remove the downlink itself */
331 exit_one_client(acptr, cli_name(&me));
333 /* Remove all clients of this server */
334 acptrp = cli_serv(cptr)->client_list;
335 for (i = 0; i <= cli_serv(cptr)->nn_mask; ++acptrp, ++i) {
337 exit_one_client(*acptrp, comment);
341 /* exit_client, rewritten 25-9-94 by Run */
343 * Eexits a client of *any* type (user, server, etc)
344 * from this server. Also, this generates all necessary prototol
345 * messages that this exit may cause.
347 * This function implicitly exits all other clients depending on
350 * For convenience, this function returns a suitable value for
351 * m_funtion return value:
353 * CPTR_KILLED if (cptr == bcptr)
354 * 0 if (cptr != bcptr)
356 * This function can be called in two ways:
357 * 1) From before or in parse(), exitting the 'cptr', in which case it was
358 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
359 * return CPTR_KILLED.
360 * 2) Via parse from a m_function call, in which case it was invoked as
361 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
362 * that generated the message in a way that we can assume he already
363 * did remove acptr from memory himself (or in other cases we don't mind
364 * because he will be delinked.) Or invoked as:
365 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
367 * In general: No generated SQUIT or QUIT should be sent to source link
368 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
371 * @param cptr Connection currently being handled by read_message.
372 * @param victim Client being killed.
373 * @param killer Client that made the decision to remove \a victim.
374 * @param comment Reason for the exit.
375 * @return CPTR_KILLED if cptr == bcptr, else 0.
377 int exit_client(struct Client *cptr,
378 struct Client* victim,
379 struct Client* killer,
382 struct Client* acptr = 0;
386 char comment1[HOSTLEN + HOSTLEN + 2];
388 if (MyConnect(victim))
390 SetFlag(victim, FLAG_CLOSING);
392 if (feature_bool(FEAT_CONNEXIT_NOTICES) && IsUser(victim))
393 sendto_opmask_butone(0, SNO_CONNEXIT,
394 "Client exiting: %s (%s@%s) [%s] [%s] <%s%s>",
395 cli_name(victim), cli_user(victim)->username,
396 cli_user(victim)->host, comment,
397 ircd_ntoa(&cli_ip(victim)),
398 NumNick(victim) /* two %s's */);
401 on_for = CurrentTime - cli_firsttime(victim);
404 log_write(LS_USER, L_TRACE, 0, "%Tu %i %s@%s %s %s %s%s %s :%s",
405 cli_firsttime(victim), on_for,
406 cli_user(victim)->username, cli_sockhost(victim),
407 ircd_ntoa(&cli_ip(victim)),
408 IsAccount(victim) ? cli_username(victim) : "0",
409 NumNick(victim), /* two %s's */
410 cli_name(victim), cli_info(victim));
412 if (victim != cli_from(killer) /* The source knows already */
413 && IsClient(victim)) /* Not a Ping struct or Log file */
415 if (IsServer(victim) || IsHandshake(victim))
416 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", cli_name(&me), comment);
417 else if (!IsConnecting(victim)) {
418 if (!IsDead(victim)) {
419 if (IsServer(victim))
420 sendcmdto_one(killer, CMD_ERROR, victim,
421 ":Closing Link: %s by %s (%s)", cli_name(victim),
422 cli_name(killer), comment);
424 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
425 cli_name(victim), IsServer(killer) ? cli_name(&me) :
426 cli_name(killer), comment);
429 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
430 (killer == &me || (IsServer(killer) &&
431 (strncmp(comment, "Leaf-only link", 14) ||
432 strncmp(comment, "Non-Hub link", 12)))))
435 * Note: check user == user needed to make sure we have the same
438 if (cli_serv(victim)->user && *(cli_serv(victim))->by &&
439 (acptr = findNUser(cli_serv(victim)->by))) {
440 if (cli_user(acptr) == cli_serv(victim)->user) {
441 sendcmdto_one(&me, CMD_NOTICE, acptr,
442 "%C :Link with %s cancelled: %s", acptr,
443 cli_name(victim), comment);
447 * not right client, set by to empty string
450 *(cli_serv(victim))->by = '\0';
454 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s cancelled: %s",
455 cli_name(victim), comment);
459 * Close the Client connection first.
461 close_connection(victim);
464 if (IsServer(victim))
466 if (feature_bool(FEAT_HIS_NETSPLIT))
467 strcpy(comment1, "*.net *.split");
470 strcpy(comment1, cli_name(cli_serv(victim)->up));
471 strcat(comment1, " ");
472 strcat(comment1, cli_name(victim));
476 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
477 (cli_user(killer)->server == victim ||
478 cli_user(killer)->server == cli_serv(victim)->up) ?
480 get_client_name(killer, HIDE_IP),
481 cli_name(cli_user(killer)->server));
482 else if (killer != &me && cli_serv(victim)->up != killer)
483 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
484 cli_name(victim), IsServer(killer) ? cli_name(killer) :
485 get_client_name(killer, HIDE_IP));
486 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
487 cli_serv(victim)->up, victim, comment);
491 * First generate the needed protocol for the other server links
494 for (dlp = cli_serv(&me)->down; dlp; dlp = dlp->next) {
495 if (dlp->value.cptr != cli_from(killer) && dlp->value.cptr != victim)
497 if (IsServer(victim))
498 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
499 cli_name(victim), cli_serv(victim)->timestamp, comment);
500 else if (IsUser(victim) && !HasFlag(victim, FLAG_KILLED))
501 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
504 /* Then remove the client structures */
505 if (IsServer(victim))
506 exit_downlinks(victim, killer, comment1);
507 exit_one_client(victim, comment);
510 * cptr can only have been killed if it was cptr itself that got killed here,
511 * because cptr can never have been a dependant of victim --Run
513 return (cptr == victim) ? CPTR_KILLED : 0;
517 * Exit client with formatted va_list message.
518 * Thin wrapper around exit_client().
519 * @param cptr Connection being processed.
520 * @param bcptr Connection being closed.
521 * @param sptr Connection who asked to close the victim.
522 * @param pattern Format string for message.
523 * @param vl Stdargs argument list.
524 * @return Has a tail call to exit_client().
526 /* added 25-9-94 by Run */
527 int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
528 const char *pattern, va_list vl)
531 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
532 return exit_client(cptr, bcptr, sptr, msgbuf);
536 * Exit client with formatted message using a variable-length argument list.
537 * Thin wrapper around exit_client().
538 * @param cptr Connection being processed.
539 * @param bcptr Connection being closed.
540 * @param sptr Connection who asked to close the victim.
541 * @param pattern Format string for message.
542 * @return Has a tail call to exit_client().
544 int exit_client_msg(struct Client *cptr, struct Client *bcptr,
545 struct Client *sptr, const char *pattern, ...)
550 va_start(vl, pattern);
551 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
554 return exit_client(cptr, bcptr, sptr, msgbuf);
557 /** Initialize global server statistics. */
558 /* (Kind of pointless since C guarantees it's already zero'ed, but... */
561 memset(&ircst, 0, sizeof(ircst));
564 /** Report server statistics to a client.
565 * @param cptr Client who wants statistics.
566 * @param sd StatDesc structure being looked up (unused).
567 * @param param Extra parameter passed by user (unused).
569 void tstats(struct Client *cptr, const struct StatDesc *sd, char *param)
571 struct Client *acptr;
573 struct ServerStatistics *sp;
574 struct ServerStatistics tmp;
577 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
578 for (i = 0; i < MAXCONNECTIONS; i++)
580 if (!(acptr = LocalClientArray[i]))
584 sp->is_sbs += cli_sendB(acptr);
585 sp->is_sbr += cli_receiveB(acptr);
586 sp->is_sti += CurrentTime - cli_firsttime(acptr);
589 else if (IsUser(acptr))
591 sp->is_cbs += cli_sendB(acptr);
592 sp->is_cbr += cli_receiveB(acptr);
593 sp->is_cti += CurrentTime - cli_firsttime(acptr);
596 else if (IsUnknown(acptr))
600 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
601 sp->is_ac, sp->is_ref);
602 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
603 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
604 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
605 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
606 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
607 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
608 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
609 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
610 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
611 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
612 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
614 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
615 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
616 sp->is_cl, sp->is_sv);
617 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %Lu %Lu",
618 sp->is_cbs, sp->is_sbs);
619 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %Lu %Lu",
620 sp->is_cbr, sp->is_sbr);
621 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Lu %Lu",
622 sp->is_cti, sp->is_sti);