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"
35 #include "ircd_policy.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
45 #include "querycmds.h"
67 static char *months[] = {
68 "January", "February", "March", "April",
69 "May", "June", "July", "August",
70 "September", "October", "November", "December"
73 static char *weekdays[] = {
74 "Sunday", "Monday", "Tuesday", "Wednesday",
75 "Thursday", "Friday", "Saturday"
81 static struct ServerStatistics ircst;
82 struct ServerStatistics* ServerStats = &ircst;
84 char *date(time_t clock)
86 static char buf[80], plus;
94 memcpy(&gmbuf, gm, sizeof(gmbuf));
96 lt = localtime(&clock);
98 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
99 if (lt->tm_yday != gm->tm_yday)
101 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
102 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
108 plus = (minswest > 0) ? '-' : '+';
110 minswest = -minswest;
112 sprintf(buf, "%s %s %d %d -- %02d:%02d %c%02d:%02d",
113 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
114 1900 + lt->tm_year, lt->tm_hour, lt->tm_min,
115 plus, minswest / 60, minswest % 60);
123 * This is like standard ctime()-function, but it zaps away
124 * the newline from the end of that string. Also, it takes
125 * the time value as parameter, instead of pointer to it.
126 * Note that it is necessary to copy the string to alternate
127 * buffer (who knows how ctime() implements it, maybe it statically
128 * has newline there and never 'refreshes' it -- zapping that
129 * might break things in other places...)
131 char *myctime(time_t value)
136 strcpy(buf, ctime(&value));
137 if ((p = strchr(buf, '\n')) != NULL)
145 * Return the name of the client for various tracking and
146 * admin purposes. The main purpose of this function is to
147 * return the "socket host" name of the client, if that
148 * differs from the advertised name (other than case).
149 * But, this can be used to any client structure.
152 * "name[user@ip#.port]" if 'showip' is true;
153 * "name" if 'showip' is false.
156 * Watch out the allocation of "nbuf", if either sptr->name
157 * or sptr->sockhost gets changed into pointers instead of
158 * directly allocated within the structure...
161 * Function return either a pointer to the structure (sptr) or
162 * to internal buffer (nbuf). *NEVER* use the returned pointer
163 * to modify what it points!!!
165 const char* get_client_name(const struct Client* sptr, int showip)
167 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
169 if (MyConnect(sptr)) {
171 ircd_snprintf(0, nbuf, sizeof(nbuf), "%s[%s@%s]", cli_name(sptr),
172 (IsIdented(sptr)) ? cli_username(sptr) : "", cli_sock_ip(sptr));
174 return cli_name(sptr);
177 return cli_name(sptr);
180 const char *get_client_host(const struct Client *cptr)
182 return get_client_name(cptr, HIDE_IP);
186 * Form sockhost such that if the host is of form user@host, only the host
189 void get_sockhost(struct Client *cptr, char *host)
192 if ((s = strchr(host, '@')))
196 ircd_strncpy(cli_sockhost(cptr), s, HOSTLEN);
200 * Exit one client, local or remote. Assuming for local client that
201 * all dependants already have been removed, and socket is closed.
203 * Rewritten by Run - 24 sept 94
205 * bcptr : client being (s)quitted
206 * sptr : The source (prefix) of the QUIT or SQUIT
210 static void exit_one_client(struct Client* bcptr, const char* comment)
214 if (cli_serv(bcptr) && cli_serv(bcptr)->client_list) /* Was SetServerYXX called ? */
215 ClearServerYXX(bcptr); /* Removes server from server_list[] */
218 * clear out uping requests
221 uping_cancel(bcptr, 0);
223 * Stop a running /LIST clean
225 if (MyUser(bcptr) && cli_listing(bcptr)) {
226 cli_listing(bcptr)->chptr->mode.mode &= ~MODE_LISTED;
227 MyFree(cli_listing(bcptr));
228 cli_listing(bcptr) = NULL;
231 * If a person is on a channel, send a QUIT notice
232 * to every client (person) on the same channel (so
233 * that the client can show the "**signoff" message).
234 * (Note: The notice is to the local clients *only*)
236 sendcmdto_common_channels_butone(bcptr, CMD_QUIT, NULL, ":%s", comment);
238 remove_user_from_all_channels(bcptr);
240 /* Clean up invitefield */
241 while ((lp = cli_user(bcptr)->invited))
242 del_invite(bcptr, lp->value.chptr);
244 /* Clean up silencefield */
245 while ((lp = cli_user(bcptr)->silence))
246 del_silence(bcptr, lp->value.cp);
248 /* Clean up snotice lists */
250 set_snomask(bcptr, ~0, SNO_DEL);
252 if (IsInvisible(bcptr))
253 --UserStats.inv_clients;
256 if (MyConnect(bcptr))
257 Count_clientdisconnects(bcptr, UserStats);
259 Count_remoteclientquits(UserStats, bcptr);
262 else if (IsServer(bcptr))
264 /* Remove downlink list node of uplink */
265 remove_dlink(&(cli_serv(cli_serv(bcptr)->up))->down, cli_serv(bcptr)->updown);
266 cli_serv(bcptr)->updown = 0;
268 if (MyConnect(bcptr))
269 Count_serverdisconnects(UserStats);
271 Count_remoteserverquits(UserStats);
273 else if (IsMe(bcptr))
275 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
277 return; /* ...must *never* exit self! */
279 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
280 Count_unknowndisconnects(UserStats);
285 if (IsIPChecked(bcptr))
286 IPcheck_disconnect(bcptr);
289 * Remove from serv->client_list
290 * NOTE: user is *always* NULL if this is a server
292 if (cli_user(bcptr)) {
293 assert(!IsServer(bcptr));
294 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
295 RemoveYXXClient(cli_user(bcptr)->server, cli_yxx(bcptr));
298 /* Remove bcptr from the client list */
300 if (hRemClient(bcptr) != 0)
301 Debug((DEBUG_ERROR, "%p !in tab %s[%s] %p %p %p %d %d %p",
302 bcptr, cli_name(bcptr), cli_from(bcptr) ? cli_sockhost(cli_from(bcptr)) : "??host",
303 cli_from(bcptr), cli_next(bcptr), cli_prev(bcptr), cli_fd(bcptr),
304 cli_status(bcptr), cli_user(bcptr)));
308 remove_client_from_list(bcptr);
312 * exit_downlinks - added by Run 25-9-94
314 * Removes all clients and downlinks (+clients) of any server
315 * QUITs are generated and sent to local users.
317 * cptr : server that must have all dependents removed
318 * sptr : source who thought that this was a good idea
319 * comment : comment sent as sign off message to local clients
321 static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
323 struct Client *acptr;
326 struct Client **acptrp;
329 /* Run over all its downlinks */
330 for (lp = cli_serv(cptr)->down; lp; lp = next)
333 acptr = lp->value.cptr;
334 /* Remove the downlinks and client of the downlink */
335 exit_downlinks(acptr, sptr, comment);
336 /* Remove the downlink itself */
337 exit_one_client(acptr, cli_name(&me));
339 /* Remove all clients of this server */
340 acptrp = cli_serv(cptr)->client_list;
341 for (i = 0; i <= cli_serv(cptr)->nn_mask; ++acptrp, ++i) {
343 exit_one_client(*acptrp, comment);
348 * exit_client, rewritten 25-9-94 by Run
350 * This function exits a client of *any* type (user, server, etc)
351 * from this server. Also, this generates all necessary prototol
352 * messages that this exit may cause.
354 * This function implicitly exits all other clients depending on
357 * For convenience, this function returns a suitable value for
358 * m_funtion return value:
360 * CPTR_KILLED if (cptr == bcptr)
361 * 0 if (cptr != bcptr)
363 * This function can be called in two ways:
364 * 1) From before or in parse(), exitting the 'cptr', in which case it was
365 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
366 * return CPTR_KILLED.
367 * 2) Via parse from a m_function call, in which case it was invoked as
368 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
369 * that generated the message in a way that we can assume he already
370 * did remove acptr from memory himself (or in other cases we don't mind
371 * because he will be delinked.) Or invoked as:
372 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
374 * In general: No generated SQUIT or QUIT should be sent to source link
375 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
379 int exit_client(struct Client *cptr, /* Connection being handled by
380 read_message right now */
381 struct Client* victim, /* Client being killed */
382 struct Client* killer, /* The client that made the decision
383 to remove this one, never NULL */
384 const char* comment) /* Reason for the exit */
386 struct Client* acptr = 0;
390 char comment1[HOSTLEN + HOSTLEN + 2];
392 if (MyConnect(victim)) {
393 cli_flags(victim) |= FLAGS_CLOSING;
396 on_for = CurrentTime - cli_firsttime(victim);
399 log_write(LS_USER, L_TRACE, 0, "%s (%3d:%02d:%02d): %s@%s (%s)",
400 myctime(cli_firsttime(victim)), on_for / 3600,
401 (on_for % 3600) / 60, on_for % 60, cli_user(victim)->username,
402 cli_sockhost(victim), cli_name(victim));
404 if (victim != cli_from(killer) /* The source knows already */
405 && IsClient(victim)) /* Not a Ping struct or Log file */
407 if (IsServer(victim) || IsHandshake(victim))
408 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", cli_name(&me), comment);
409 else if (!IsConnecting(victim)) {
410 if (!IsDead(victim)) {
411 if (IsServer(victim))
412 sendcmdto_one(killer, CMD_ERROR, victim,
413 ":Closing Link: %s by %s (%s)", cli_name(victim),
414 cli_name(killer), comment);
416 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
417 cli_name(victim), IsServer(killer) ? cli_name(&me) :
418 cli_name(killer), comment);
421 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
422 (killer == &me || (IsServer(killer) &&
423 (strncmp(comment, "Leaf-only link", 14) ||
424 strncmp(comment, "Non-Hub link", 12)))))
427 * Note: check user == user needed to make sure we have the same
430 if (cli_serv(victim)->user && *(cli_serv(victim))->by &&
431 (acptr = findNUser(cli_serv(victim)->by))) {
432 if (cli_user(acptr) == cli_serv(victim)->user) {
433 sendcmdto_one(&me, CMD_NOTICE, acptr,
434 "%C :Link with %s cancelled: %s", acptr,
435 cli_name(victim), comment);
439 * not right client, set by to empty string
442 *(cli_serv(victim))->by = '\0';
446 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s cancelled: %s",
447 cli_name(victim), comment);
451 * Close the Client connection first.
453 close_connection(victim);
456 if (IsServer(victim))
458 #ifdef HEAD_IN_SAND_NETSPLIT
459 strcpy(comment1, "*.net *.split");
461 strcpy(comment1, cli_name(cli_serv(victim)->up));
462 strcat(comment1, " ");
463 strcat(comment1, cli_name(victim));
466 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
467 (cli_user(killer)->server == victim ||
468 cli_user(killer)->server == cli_serv(victim)->up) ?
470 get_client_name(killer, HIDE_IP),
471 cli_name(cli_user(killer)->server));
472 else if (killer != &me && cli_serv(victim)->up != killer)
473 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
474 cli_name(victim), IsServer(killer) ? cli_name(killer) :
475 get_client_name(killer, HIDE_IP));
476 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
477 cli_serv(victim)->up, victim, comment);
481 * First generate the needed protocol for the other server links
484 for (dlp = cli_serv(&me)->down; dlp; dlp = dlp->next) {
485 if (dlp->value.cptr != cli_from(killer) && dlp->value.cptr != victim) {
486 if (IsServer(victim))
487 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
488 cli_name(victim), cli_serv(victim)->timestamp, comment);
489 else if (IsUser(victim) && 0 == (cli_flags(victim) & FLAGS_KILLED))
490 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
493 /* Then remove the client structures */
494 if (IsServer(victim))
495 exit_downlinks(victim, killer, comment1);
496 exit_one_client(victim, comment);
499 * cptr can only have been killed if it was cptr itself that got killed here,
500 * because cptr can never have been a dependant of victim --Run
502 return (cptr == victim) ? CPTR_KILLED : 0;
506 * Exit client with formatted message, added 25-9-94 by Run
508 int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
509 const char *pattern, va_list vl)
512 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
513 return exit_client(cptr, bcptr, sptr, msgbuf);
516 int exit_client_msg(struct Client *cptr, struct Client *bcptr,
517 struct Client *sptr, const char *pattern, ...)
522 va_start(vl, pattern);
523 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
526 return exit_client(cptr, bcptr, sptr, msgbuf);
531 memset(&ircst, 0, sizeof(ircst));
534 void tstats(struct Client *cptr, char *name)
536 struct Client *acptr;
538 struct ServerStatistics *sp;
539 struct ServerStatistics tmp;
542 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
543 for (i = 0; i < MAXCONNECTIONS; i++)
545 if (!(acptr = LocalClientArray[i]))
549 sp->is_sbs += cli_sendB(acptr);
550 sp->is_sbr += cli_receiveB(acptr);
551 sp->is_sks += cli_sendK(acptr);
552 sp->is_skr += cli_receiveK(acptr);
553 sp->is_sti += CurrentTime - cli_firsttime(acptr);
555 if (sp->is_sbs > 1023)
557 sp->is_sks += (sp->is_sbs >> 10);
560 if (sp->is_sbr > 1023)
562 sp->is_skr += (sp->is_sbr >> 10);
566 else if (IsUser(acptr))
568 sp->is_cbs += cli_sendB(acptr);
569 sp->is_cbr += cli_receiveB(acptr);
570 sp->is_cks += cli_sendK(acptr);
571 sp->is_ckr += cli_receiveK(acptr);
572 sp->is_cti += CurrentTime - cli_firsttime(acptr);
574 if (sp->is_cbs > 1023)
576 sp->is_cks += (sp->is_cbs >> 10);
579 if (sp->is_cbr > 1023)
581 sp->is_ckr += (sp->is_cbr >> 10);
585 else if (IsUnknown(acptr))
589 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
590 sp->is_ac, sp->is_ref);
591 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
592 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
593 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
594 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
595 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
596 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
597 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
598 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
599 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
600 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
601 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
603 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
604 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
605 sp->is_cl, sp->is_sv);
606 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %u.%uK %u.%uK",
607 sp->is_cks, sp->is_cbs, sp->is_sks, sp->is_sbs);
608 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %u.%uK %u.%uK",
609 sp->is_ckr, sp->is_cbr, sp->is_skr, sp->is_sbr);
610 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Tu %Tu",
611 sp->is_cti, sp->is_sti);