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.
33 #include "ircd_alloc.h"
34 #include "ircd_auth.h"
35 #include "ircd_features.h"
37 #include "ircd_reply.h"
38 #include "ircd_snprintf.h"
39 #include "ircd_string.h"
46 #include "querycmds.h"
68 /** Array of English month names (0 = January). */
69 static char *months[] = {
70 "January", "February", "March", "April",
71 "May", "June", "July", "August",
72 "September", "October", "November", "December"
75 /** Array of English day names (0 = Sunday). */
76 static char *weekdays[] = {
77 "Sunday", "Monday", "Tuesday", "Wednesday",
78 "Thursday", "Friday", "Saturday"
84 /** Global statistics structure. */
85 static struct ServerStatistics ircst;
86 /** Public pointer to global statistics structure. */
87 struct ServerStatistics* ServerStats = &ircst;
89 /** Formats a Unix time as a readable string.
90 * @param clock Unix time to format (0 means #CurrentTime).
91 * @return Pointer to a static buffer containing something like
92 * "Sunday January 1 2000 -- 09:30 +01:00"
94 char *date(time_t clock)
96 static char buf[80], plus;
104 memcpy(&gmbuf, gm, sizeof(gmbuf));
106 lt = localtime(&clock);
108 /* There is unfortunately no clean portable way to extract time zone
109 * offset information, so do ugly things.
111 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
112 if (lt->tm_yday != gm->tm_yday)
114 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
115 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
121 plus = (minswest > 0) ? '-' : '+';
123 minswest = -minswest;
125 sprintf(buf, "%s %s %d %d -- %02d:%02d %c%02d:%02d",
126 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
127 1900 + lt->tm_year, lt->tm_hour, lt->tm_min,
128 plus, minswest / 60, minswest % 60);
133 /** Like ctime() but with no trailing newline. Also, it takes
134 * the time value as parameter, instead of pointer to it.
135 * @param value Unix time to format.
136 * @return Pointer to a static buffer containing formatted time.
138 char *myctime(time_t value)
140 /* Use a secondary buffer in case ctime() would not replace an
141 * overwritten newline.
146 strcpy(buf, ctime(&value));
147 if ((p = strchr(buf, '\n')) != NULL)
153 /** Return the name of the client for various tracking and admin
154 * purposes. The main purpose of this function is to return the
155 * "socket host" name of the client, if that differs from the
156 * advertised name (other than case). But, this can be used on any
158 * @param sptr Client to operate on.
159 * @param showip If non-zero, append [username\@text-ip] to name.
160 * @return Either cli_name(\a sptr) or a static buffer.
162 const char* get_client_name(const struct Client* sptr, int showip)
164 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
166 if (MyConnect(sptr)) {
168 ircd_snprintf(0, nbuf, sizeof(nbuf), "%s[%s@%s]", cli_name(sptr),
169 IsIdented(sptr) ? cli_username(sptr) : "unknown",
172 return cli_name(sptr);
175 return cli_name(sptr);
178 /** Set cli_sockhost(cptr) from \a host.
179 * If \a host contains an '@', copy starting after that byte.
180 * Otherwise copy all of \a host.
181 * @param cptr Client to operate on.
182 * @param host hostname or user\@hostname string.
184 void get_sockhost(struct Client *cptr, char *host)
187 if ((s = strchr(host, '@')))
191 ircd_strncpy(cli_sockhost(cptr), s, HOSTLEN);
195 * Exit one client, local or remote. Assuming for local client that
196 * all dependants already have been removed, and socket is closed.
197 * @param bcptr Client being (s)quitted.
198 * @param comment The QUIT comment to send.
200 /* Rewritten by Run - 24 sept 94 */
201 static void exit_one_client(struct Client* bcptr, const char* comment)
205 if (cli_serv(bcptr) && cli_serv(bcptr)->client_list) /* Was SetServerYXX called ? */
206 ClearServerYXX(bcptr); /* Removes server from server_list[] */
209 * clear out uping requests
212 uping_cancel(bcptr, 0);
214 * Stop a running /LIST clean
216 if (MyUser(bcptr) && cli_listing(bcptr)) {
217 cli_listing(bcptr)->chptr->mode.mode &= ~MODE_LISTED;
218 MyFree(cli_listing(bcptr));
219 cli_listing(bcptr) = NULL;
222 * If a person is on a channel, send a QUIT notice
223 * to every client (person) on the same channel (so
224 * that the client can show the "**signoff" message).
225 * (Note: The notice is to the local clients *only*)
227 sendcmdto_common_channels_butone(bcptr, CMD_QUIT, NULL, ":%s", comment);
229 remove_user_from_all_channels(bcptr);
231 /* Clean up invitefield */
232 while ((lp = cli_user(bcptr)->invited))
233 del_invite(bcptr, lp->value.chptr);
235 /* Clean up silencefield */
236 while ((lp = cli_user(bcptr)->silence))
237 del_silence(bcptr, lp->value.cp);
239 /* Clean up snotice lists */
241 set_snomask(bcptr, ~0, SNO_DEL);
243 if (IsInvisible(bcptr))
244 --UserStats.inv_clients;
247 if (MyConnect(bcptr))
248 Count_clientdisconnects(bcptr, UserStats);
250 Count_remoteclientquits(UserStats, bcptr);
253 else if (IsServer(bcptr))
255 /* Remove downlink list node of uplink */
256 remove_dlink(&(cli_serv(cli_serv(bcptr)->up))->down, cli_serv(bcptr)->updown);
257 cli_serv(bcptr)->updown = 0;
259 if (MyConnect(bcptr))
260 Count_serverdisconnects(UserStats);
262 Count_remoteserverquits(UserStats);
264 else if (IsMe(bcptr))
266 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
268 return; /* ...must *never* exit self! */
270 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
271 Count_unknowndisconnects(UserStats);
276 if (IsIPChecked(bcptr))
277 IPcheck_disconnect(bcptr);
280 * Remove from serv->client_list
281 * NOTE: user is *always* NULL if this is a server
283 if (cli_user(bcptr)) {
284 assert(!IsServer(bcptr));
285 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
286 RemoveYXXClient(cli_user(bcptr)->server, cli_yxx(bcptr));
287 if (IsIAuthed(bcptr) || cli_iauth(bcptr))
288 iauth_exit_client(bcptr);
291 /* Remove bcptr from the client list */
293 if (hRemClient(bcptr) != 0)
294 Debug((DEBUG_ERROR, "%p !in tab %s[%s] %p %p %p %d %d %p",
295 bcptr, cli_name(bcptr), cli_from(bcptr) ? cli_sockhost(cli_from(bcptr)) : "??host",
296 cli_from(bcptr), cli_next(bcptr), cli_prev(bcptr), cli_fd(bcptr),
297 cli_status(bcptr), cli_user(bcptr)));
301 remove_client_from_list(bcptr);
304 /* exit_downlinks - added by Run 25-9-94 */
306 * Removes all clients and downlinks (+clients) of any server
307 * QUITs are generated and sent to local users.
308 * @param cptr server that must have all dependents removed
309 * @param sptr source who thought that this was a good idea
310 * @param comment comment sent as sign off message to local clients
312 static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
314 struct Client *acptr;
317 struct Client **acptrp;
320 /* Run over all its downlinks */
321 for (lp = cli_serv(cptr)->down; lp; lp = next)
324 acptr = lp->value.cptr;
325 /* Remove the downlinks and client of the downlink */
326 exit_downlinks(acptr, sptr, comment);
327 /* Remove the downlink itself */
328 exit_one_client(acptr, cli_name(&me));
330 /* Remove all clients of this server */
331 acptrp = cli_serv(cptr)->client_list;
332 for (i = 0; i <= cli_serv(cptr)->nn_mask; ++acptrp, ++i) {
334 exit_one_client(*acptrp, comment);
338 /* exit_client, rewritten 25-9-94 by Run */
340 * Eexits a client of *any* type (user, server, etc)
341 * from this server. Also, this generates all necessary prototol
342 * messages that this exit may cause.
344 * This function implicitly exits all other clients depending on
347 * For convenience, this function returns a suitable value for
348 * m_funtion return value:
350 * CPTR_KILLED if (cptr == bcptr)
351 * 0 if (cptr != bcptr)
353 * This function can be called in two ways:
354 * 1) From before or in parse(), exitting the 'cptr', in which case it was
355 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
356 * return CPTR_KILLED.
357 * 2) Via parse from a m_function call, in which case it was invoked as
358 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
359 * that generated the message in a way that we can assume he already
360 * did remove acptr from memory himself (or in other cases we don't mind
361 * because he will be delinked.) Or invoked as:
362 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
364 * In general: No generated SQUIT or QUIT should be sent to source link
365 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
368 * @param cptr Connection currently being handled by read_message.
369 * @param victim Client being killed.
370 * @param killer Client that made the decision to remove \a victim.
371 * @param comment Reason for the exit.
372 * @return CPTR_KILLED if cptr == bcptr, else 0.
374 int exit_client(struct Client *cptr,
375 struct Client* victim,
376 struct Client* killer,
379 struct Client* acptr = 0;
383 char comment1[HOSTLEN + HOSTLEN + 2];
385 if (MyConnect(victim))
387 SetFlag(victim, FLAG_CLOSING);
389 if (feature_bool(FEAT_CONNEXIT_NOTICES) && IsUser(victim))
390 sendto_opmask_butone(0, SNO_CONNEXIT,
391 "Client exiting: %s (%s@%s) [%s] [%s] <%s%s>",
392 cli_name(victim), cli_user(victim)->username,
393 cli_user(victim)->host, comment,
394 ircd_ntoa(&cli_ip(victim)),
395 NumNick(victim) /* two %s's */);
398 on_for = CurrentTime - cli_firsttime(victim);
401 log_write(LS_USER, L_TRACE, 0, "%Tu %i %s@%s %s %s %s%s %s :%s",
402 cli_firsttime(victim), on_for,
403 cli_user(victim)->username, cli_sockhost(victim),
404 ircd_ntoa(&cli_ip(victim)),
405 IsAccount(victim) ? cli_username(victim) : "0",
406 NumNick(victim), /* two %s's */
407 cli_name(victim), cli_info(victim));
409 if (victim != cli_from(killer) /* The source knows already */
410 && IsClient(victim)) /* Not a Ping struct or Log file */
412 if (IsServer(victim) || IsHandshake(victim))
413 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", cli_name(&me), comment);
414 else if (!IsConnecting(victim)) {
415 if (!IsDead(victim)) {
416 if (IsServer(victim))
417 sendcmdto_one(killer, CMD_ERROR, victim,
418 ":Closing Link: %s by %s (%s)", cli_name(victim),
419 cli_name(killer), comment);
421 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
422 cli_name(victim), IsServer(killer) ? cli_name(&me) :
423 cli_name(killer), comment);
426 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
427 (killer == &me || (IsServer(killer) &&
428 (strncmp(comment, "Leaf-only link", 14) ||
429 strncmp(comment, "Non-Hub link", 12)))))
432 * Note: check user == user needed to make sure we have the same
435 if (cli_serv(victim)->user && *(cli_serv(victim))->by &&
436 (acptr = findNUser(cli_serv(victim)->by))) {
437 if (cli_user(acptr) == cli_serv(victim)->user) {
438 sendcmdto_one(&me, CMD_NOTICE, acptr,
439 "%C :Link with %s cancelled: %s", acptr,
440 cli_name(victim), comment);
444 * not right client, set by to empty string
447 *(cli_serv(victim))->by = '\0';
451 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s cancelled: %s",
452 cli_name(victim), comment);
456 * Close the Client connection first.
458 close_connection(victim);
461 if (IsServer(victim))
463 if (feature_bool(FEAT_HIS_NETSPLIT))
464 strcpy(comment1, "*.net *.split");
467 strcpy(comment1, cli_name(cli_serv(victim)->up));
468 strcat(comment1, " ");
469 strcat(comment1, cli_name(victim));
473 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
474 (cli_user(killer)->server == victim ||
475 cli_user(killer)->server == cli_serv(victim)->up) ?
477 get_client_name(killer, HIDE_IP),
478 cli_name(cli_user(killer)->server));
479 else if (killer != &me && cli_serv(victim)->up != killer)
480 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
481 cli_name(victim), IsServer(killer) ? cli_name(killer) :
482 get_client_name(killer, HIDE_IP));
483 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
484 cli_serv(victim)->up, victim, comment);
488 * First generate the needed protocol for the other server links
491 for (dlp = cli_serv(&me)->down; dlp; dlp = dlp->next) {
492 if (dlp->value.cptr != cli_from(killer) && dlp->value.cptr != victim)
494 if (IsServer(victim))
495 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
496 cli_name(victim), cli_serv(victim)->timestamp, comment);
497 else if (IsUser(victim) && !HasFlag(victim, FLAG_KILLED))
498 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
501 /* Then remove the client structures */
502 if (IsServer(victim))
503 exit_downlinks(victim, killer, comment1);
504 exit_one_client(victim, comment);
507 * cptr can only have been killed if it was cptr itself that got killed here,
508 * because cptr can never have been a dependant of victim --Run
510 return (cptr == victim) ? CPTR_KILLED : 0;
514 * Exit client with formatted va_list message.
515 * Thin wrapper around exit_client().
516 * @param cptr Connection being processed.
517 * @param bcptr Connection being closed.
518 * @param sptr Connection who asked to close the victim.
519 * @param pattern Format string for message.
520 * @param vl Stdargs argument list.
521 * @return Has a tail call to exit_client().
523 /* added 25-9-94 by Run */
524 int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
525 const char *pattern, va_list vl)
528 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
529 return exit_client(cptr, bcptr, sptr, msgbuf);
533 * Exit client with formatted message using a variable-length argument list.
534 * Thin wrapper around exit_client().
535 * @param cptr Connection being processed.
536 * @param bcptr Connection being closed.
537 * @param sptr Connection who asked to close the victim.
538 * @param pattern Format string for message.
539 * @return Has a tail call to exit_client().
541 int exit_client_msg(struct Client *cptr, struct Client *bcptr,
542 struct Client *sptr, const char *pattern, ...)
547 va_start(vl, pattern);
548 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
551 return exit_client(cptr, bcptr, sptr, msgbuf);
554 /** Initialize global server statistics. */
555 /* (Kind of pointless since C guarantees it's already zero'ed, but... */
558 memset(&ircst, 0, sizeof(ircst));
561 /** Report server statistics to a client.
562 * @param cptr Client who wants statistics.
563 * @param sd StatDesc structure being looked up (unused).
564 * @param param Extra parameter passed by user (unused).
566 void tstats(struct Client *cptr, const struct StatDesc *sd, char *param)
568 struct Client *acptr;
570 struct ServerStatistics *sp;
571 struct ServerStatistics tmp;
574 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
575 for (i = 0; i < MAXCONNECTIONS; i++)
577 if (!(acptr = LocalClientArray[i]))
581 sp->is_sbs += cli_sendB(acptr);
582 sp->is_sbr += cli_receiveB(acptr);
583 sp->is_sks += cli_sendK(acptr);
584 sp->is_skr += cli_receiveK(acptr);
585 sp->is_sti += CurrentTime - cli_firsttime(acptr);
587 if (sp->is_sbs > 1023)
589 sp->is_sks += (sp->is_sbs >> 10);
592 if (sp->is_sbr > 1023)
594 sp->is_skr += (sp->is_sbr >> 10);
598 else if (IsUser(acptr))
600 sp->is_cbs += cli_sendB(acptr);
601 sp->is_cbr += cli_receiveB(acptr);
602 sp->is_cks += cli_sendK(acptr);
603 sp->is_ckr += cli_receiveK(acptr);
604 sp->is_cti += CurrentTime - cli_firsttime(acptr);
606 if (sp->is_cbs > 1023)
608 sp->is_cks += (sp->is_cbs >> 10);
611 if (sp->is_cbr > 1023)
613 sp->is_ckr += (sp->is_cbr >> 10);
617 else if (IsUnknown(acptr))
621 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
622 sp->is_ac, sp->is_ref);
623 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
624 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
625 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
626 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
627 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
628 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
629 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
630 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
631 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
632 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
633 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
635 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
636 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
637 sp->is_cl, sp->is_sv);
638 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %u.%uK %u.%uK",
639 sp->is_cks, sp->is_cbs, sp->is_sks, sp->is_sbs);
640 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %u.%uK %u.%uK",
641 sp->is_ckr, sp->is_cbr, sp->is_skr, sp->is_sbr);
642 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Tu %Tu",
643 sp->is_cti, sp->is_sti);