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_features.h"
36 #include "ircd_reply.h"
37 #include "ircd_snprintf.h"
38 #include "ircd_string.h"
46 #include "querycmds.h"
69 static char *months[] = {
70 "January", "February", "March", "April",
71 "May", "June", "July", "August",
72 "September", "October", "November", "December"
75 static char *weekdays[] = {
76 "Sunday", "Monday", "Tuesday", "Wednesday",
77 "Thursday", "Friday", "Saturday"
83 static struct ServerStatistics ircst;
84 struct ServerStatistics* ServerStats = &ircst;
86 char *date(time_t clock)
88 static char buf[80], plus;
96 memcpy(&gmbuf, gm, sizeof(gmbuf));
98 lt = localtime(&clock);
100 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
101 if (lt->tm_yday != gm->tm_yday)
103 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
104 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
110 plus = (minswest > 0) ? '-' : '+';
112 minswest = -minswest;
114 sprintf(buf, "%s %s %d %d -- %02d:%02d %c%02d:%02d",
115 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
116 1900 + lt->tm_year, lt->tm_hour, lt->tm_min,
117 plus, minswest / 60, minswest % 60);
125 * This is like standard ctime()-function, but it zaps away
126 * the newline from the end of that string. Also, it takes
127 * the time value as parameter, instead of pointer to it.
128 * Note that it is necessary to copy the string to alternate
129 * buffer (who knows how ctime() implements it, maybe it statically
130 * has newline there and never 'refreshes' it -- zapping that
131 * might break things in other places...)
133 char *myctime(time_t value)
138 strcpy(buf, ctime(&value));
139 if ((p = strchr(buf, '\n')) != NULL)
147 * Return the name of the client for various tracking and
148 * admin purposes. The main purpose of this function is to
149 * return the "socket host" name of the client, if that
150 * differs from the advertised name (other than case).
151 * But, this can be used to any client structure.
154 * "name[user@ip#.port]" if 'showip' is true;
155 * "name" if 'showip' is false.
158 * Watch out the allocation of "nbuf", if either sptr->name
159 * or sptr->sockhost gets changed into pointers instead of
160 * directly allocated within the structure...
163 * Function return either a pointer to the structure (sptr) or
164 * to internal buffer (nbuf). *NEVER* use the returned pointer
165 * to modify what it points!!!
167 const char* get_client_name(const struct Client* sptr, int showip)
169 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
171 if (MyConnect(sptr)) {
173 ircd_snprintf(0, nbuf, sizeof(nbuf), "%s[%s@%s]", cli_name(sptr),
174 IsIdented(sptr) ? cli_username(sptr) : "unknown",
177 return cli_name(sptr);
180 return cli_name(sptr);
183 const char *get_client_host(const struct Client *cptr)
185 return get_client_name(cptr, HIDE_IP);
189 * Form sockhost such that if the host is of form user@host, only the host
192 void get_sockhost(struct Client *cptr, char *host)
195 if ((s = strchr(host, '@')))
199 ircd_strncpy(cli_sockhost(cptr), s, HOSTLEN);
203 * Exit one client, local or remote. Assuming for local client that
204 * all dependants already have been removed, and socket is closed.
206 * Rewritten by Run - 24 sept 94
208 * bcptr : client being (s)quitted
209 * sptr : The source (prefix) of the QUIT or SQUIT
213 static void exit_one_client(struct Client* bcptr, const char* comment)
217 if (cli_serv(bcptr) && cli_serv(bcptr)->client_list) /* Was SetServerYXX called ? */
218 ClearServerYXX(bcptr); /* Removes server from server_list[] */
221 * clear out uping requests
224 uping_cancel(bcptr, 0);
226 * Stop a running /LIST clean
228 if (MyUser(bcptr) && cli_listing(bcptr)) {
229 cli_listing(bcptr)->chptr->mode.mode &= ~MODE_LISTED;
230 MyFree(cli_listing(bcptr));
231 cli_listing(bcptr) = NULL;
234 * If a person is on a channel, send a QUIT notice
235 * to every client (person) on the same channel (so
236 * that the client can show the "**signoff" message).
237 * (Note: The notice is to the local clients *only*)
239 sendcmdto_common_channels_butone(bcptr, CMD_QUIT, NULL, ":%s", comment);
241 remove_user_from_all_channels(bcptr);
243 /* Clean up invitefield */
244 while ((lp = cli_user(bcptr)->invited))
245 del_invite(bcptr, lp->value.chptr);
247 /* Clean up silencefield */
248 while ((lp = cli_user(bcptr)->silence))
249 del_silence(bcptr, lp->value.cp);
251 /* Clean up snotice lists */
253 set_snomask(bcptr, ~0, SNO_DEL);
255 if (IsInvisible(bcptr))
256 --UserStats.inv_clients;
259 if (MyConnect(bcptr))
260 Count_clientdisconnects(bcptr, UserStats);
262 Count_remoteclientquits(UserStats, bcptr);
265 else if (IsServer(bcptr))
267 /* Remove downlink list node of uplink */
268 remove_dlink(&(cli_serv(cli_serv(bcptr)->up))->down, cli_serv(bcptr)->updown);
269 cli_serv(bcptr)->updown = 0;
271 if (MyConnect(bcptr))
272 Count_serverdisconnects(UserStats);
274 Count_remoteserverquits(UserStats);
276 else if (IsMe(bcptr))
278 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
280 return; /* ...must *never* exit self! */
282 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
283 Count_unknowndisconnects(UserStats);
288 if (IsIPChecked(bcptr))
289 IPcheck_disconnect(bcptr);
292 * Remove from serv->client_list
293 * NOTE: user is *always* NULL if this is a server
295 if (cli_user(bcptr)) {
296 assert(!IsServer(bcptr));
297 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
298 RemoveYXXClient(cli_user(bcptr)->server, cli_yxx(bcptr));
301 /* Remove bcptr from the client list */
303 if (hRemClient(bcptr) != 0)
304 Debug((DEBUG_ERROR, "%p !in tab %s[%s] %p %p %p %d %d %p",
305 bcptr, cli_name(bcptr), cli_from(bcptr) ? cli_sockhost(cli_from(bcptr)) : "??host",
306 cli_from(bcptr), cli_next(bcptr), cli_prev(bcptr), cli_fd(bcptr),
307 cli_status(bcptr), cli_user(bcptr)));
311 remove_client_from_list(bcptr);
315 * exit_downlinks - added by Run 25-9-94
317 * Removes all clients and downlinks (+clients) of any server
318 * QUITs are generated and sent to local users.
320 * cptr : server that must have all dependents removed
321 * sptr : source who thought that this was a good idea
322 * comment : comment sent as sign off message to local clients
324 static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
326 struct Client *acptr;
329 struct Client **acptrp;
332 /* Run over all its downlinks */
333 for (lp = cli_serv(cptr)->down; lp; lp = next)
336 acptr = lp->value.cptr;
337 /* Remove the downlinks and client of the downlink */
338 exit_downlinks(acptr, sptr, comment);
339 /* Remove the downlink itself */
340 exit_one_client(acptr, cli_name(&me));
342 /* Remove all clients of this server */
343 acptrp = cli_serv(cptr)->client_list;
344 for (i = 0; i <= cli_serv(cptr)->nn_mask; ++acptrp, ++i) {
346 exit_one_client(*acptrp, comment);
351 * exit_client, rewritten 25-9-94 by Run
353 * This function exits a client of *any* type (user, server, etc)
354 * from this server. Also, this generates all necessary prototol
355 * messages that this exit may cause.
357 * This function implicitly exits all other clients depending on
360 * For convenience, this function returns a suitable value for
361 * m_funtion return value:
363 * CPTR_KILLED if (cptr == bcptr)
364 * 0 if (cptr != bcptr)
366 * This function can be called in two ways:
367 * 1) From before or in parse(), exitting the 'cptr', in which case it was
368 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
369 * return CPTR_KILLED.
370 * 2) Via parse from a m_function call, in which case it was invoked as
371 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
372 * that generated the message in a way that we can assume he already
373 * did remove acptr from memory himself (or in other cases we don't mind
374 * because he will be delinked.) Or invoked as:
375 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
377 * In general: No generated SQUIT or QUIT should be sent to source link
378 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
382 int exit_client(struct Client *cptr, /* Connection being handled by
383 read_message right now */
384 struct Client* victim, /* Client being killed */
385 struct Client* killer, /* The client that made the decision
386 to remove this one, never NULL */
387 const char* comment) /* Reason for the exit */
389 struct Client* acptr = 0;
393 char comment1[HOSTLEN + HOSTLEN + 2];
395 if (MyConnect(victim))
397 SetFlag(victim, FLAG_CLOSING);
399 if (feature_bool(FEAT_CONNEXIT_NOTICES) && IsUser(victim))
400 sendto_opmask_butone(0, SNO_CONNEXIT,
401 "Client exiting: %s (%s@%s) [%s] [%s] <%s%s>",
402 cli_name(victim), cli_user(victim)->username,
403 cli_user(victim)->host, comment,
404 ircd_ntoa((const char*) &(cli_ip(victim))),
405 NumNick(victim) /* two %s's */);
408 on_for = CurrentTime - cli_firsttime(victim);
411 log_write(LS_USER, L_TRACE, 0, "%Tu %i %s@%s %s %s %s%s %s :%s",
412 cli_firsttime(victim), on_for,
413 cli_user(victim)->username, cli_sockhost(victim),
414 ircd_ntoa((const char*) &(cli_ip(victim))),
415 IsAccount(victim) ? cli_username(victim) : "0",
416 NumNick(victim), /* two %s's */
417 cli_name(victim), cli_info(victim));
419 if (victim != cli_from(killer) /* The source knows already */
420 && IsClient(victim)) /* Not a Ping struct or Log file */
422 if (IsServer(victim) || IsHandshake(victim))
423 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", cli_name(&me), comment);
424 else if (!IsConnecting(victim)) {
425 if (!IsDead(victim)) {
426 if (IsServer(victim))
427 sendcmdto_one(killer, CMD_ERROR, victim,
428 ":Closing Link: %s by %s (%s)", cli_name(victim),
429 cli_name(killer), comment);
431 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
432 cli_name(victim), IsServer(killer) ? cli_name(&me) :
433 cli_name(killer), comment);
436 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
437 (killer == &me || (IsServer(killer) &&
438 (strncmp(comment, "Leaf-only link", 14) ||
439 strncmp(comment, "Non-Hub link", 12)))))
442 * Note: check user == user needed to make sure we have the same
445 if (cli_serv(victim)->user && *(cli_serv(victim))->by &&
446 (acptr = findNUser(cli_serv(victim)->by))) {
447 if (cli_user(acptr) == cli_serv(victim)->user) {
448 sendcmdto_one(&me, CMD_NOTICE, acptr,
449 "%C :Link with %s cancelled: %s", acptr,
450 cli_name(victim), comment);
454 * not right client, set by to empty string
457 *(cli_serv(victim))->by = '\0';
461 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s cancelled: %s",
462 cli_name(victim), comment);
466 * Close the Client connection first.
468 close_connection(victim);
471 if (IsServer(victim))
473 if (feature_bool(FEAT_HIS_NETSPLIT))
474 strcpy(comment1, "*.net *.split");
477 strcpy(comment1, cli_name(cli_serv(victim)->up));
478 strcat(comment1, " ");
479 strcat(comment1, cli_name(victim));
483 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
484 (cli_user(killer)->server == victim ||
485 cli_user(killer)->server == cli_serv(victim)->up) ?
487 get_client_name(killer, HIDE_IP),
488 cli_name(cli_user(killer)->server));
489 else if (killer != &me && cli_serv(victim)->up != killer)
490 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
491 cli_name(victim), IsServer(killer) ? cli_name(killer) :
492 get_client_name(killer, HIDE_IP));
493 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %C %C (%s)",
494 cli_serv(victim)->up, victim, comment);
496 if (feature_bool(FEAT_HIS_MAP) || feature_bool(FEAT_HIS_LINKS))
501 * First generate the needed protocol for the other server links
504 for (dlp = cli_serv(&me)->down; dlp; dlp = dlp->next) {
505 if (dlp->value.cptr != cli_from(killer) && dlp->value.cptr != victim)
507 if (IsServer(victim))
508 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
509 cli_name(victim), cli_serv(victim)->timestamp, comment);
510 else if (IsUser(victim) && !HasFlag(victim, FLAG_KILLED))
511 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
514 /* Then remove the client structures */
515 if (IsServer(victim))
516 exit_downlinks(victim, killer, comment1);
517 exit_one_client(victim, comment);
520 * cptr can only have been killed if it was cptr itself that got killed here,
521 * because cptr can never have been a dependant of victim --Run
523 return (cptr == victim) ? CPTR_KILLED : 0;
527 * Exit client with formatted message, added 25-9-94 by Run
529 int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
530 const char *pattern, va_list vl)
533 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
534 return exit_client(cptr, bcptr, sptr, msgbuf);
537 int exit_client_msg(struct Client *cptr, struct Client *bcptr,
538 struct Client *sptr, const char *pattern, ...)
543 va_start(vl, pattern);
544 ircd_vsnprintf(0, msgbuf, sizeof(msgbuf), pattern, vl);
547 return exit_client(cptr, bcptr, sptr, msgbuf);
552 memset(&ircst, 0, sizeof(ircst));
555 void tstats(struct Client *cptr, struct StatDesc *sd, int stat, 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_sks += cli_sendK(acptr);
573 sp->is_skr += cli_receiveK(acptr);
574 sp->is_sti += CurrentTime - cli_firsttime(acptr);
576 if (sp->is_sbs > 1023)
578 sp->is_sks += (sp->is_sbs >> 10);
581 if (sp->is_sbr > 1023)
583 sp->is_skr += (sp->is_sbr >> 10);
587 else if (IsUser(acptr))
589 sp->is_cbs += cli_sendB(acptr);
590 sp->is_cbr += cli_receiveB(acptr);
591 sp->is_cks += cli_sendK(acptr);
592 sp->is_ckr += cli_receiveK(acptr);
593 sp->is_cti += CurrentTime - cli_firsttime(acptr);
595 if (sp->is_cbs > 1023)
597 sp->is_cks += (sp->is_cbs >> 10);
600 if (sp->is_cbr > 1023)
602 sp->is_ckr += (sp->is_cbr >> 10);
606 else if (IsUnknown(acptr))
610 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
611 sp->is_ac, sp->is_ref);
612 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
613 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
614 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
615 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
616 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
617 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
618 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
619 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
620 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
621 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
622 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
624 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
625 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
626 sp->is_cl, sp->is_sv);
627 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %u.%uK %u.%uK",
628 sp->is_cks, sp->is_cbs, sp->is_sks, sp->is_sbs);
629 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %u.%uK %u.%uK",
630 sp->is_ckr, sp->is_cbr, sp->is_skr, sp->is_sbr);
631 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Tu %Tu",
632 sp->is_cti, sp->is_sti);