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.
31 #include "ircd_alloc.h"
33 #include "ircd_reply.h"
34 #include "ircd_string.h"
41 #include "querycmds.h"
48 #include "sprintf_irc.h"
64 static char *months[] = {
65 "January", "February", "March", "April",
66 "May", "June", "July", "August",
67 "September", "October", "November", "December"
70 static char *weekdays[] = {
71 "Sunday", "Monday", "Tuesday", "Wednesday",
72 "Thursday", "Friday", "Saturday"
78 static struct ServerStatistics ircst;
79 struct ServerStatistics* ServerStats = &ircst;
81 char *date(time_t clock)
83 static char buf[80], plus;
91 memcpy(&gmbuf, gm, sizeof(gmbuf));
93 lt = localtime(&clock);
95 minswest = (gm->tm_hour - lt->tm_hour) * 60 + (gm->tm_min - lt->tm_min);
96 if (lt->tm_yday != gm->tm_yday)
98 if ((lt->tm_yday > gm->tm_yday && lt->tm_year == gm->tm_year) ||
99 (lt->tm_yday < gm->tm_yday && lt->tm_year != gm->tm_year))
105 plus = (minswest > 0) ? '-' : '+';
107 minswest = -minswest;
109 sprintf(buf, "%s %s %d %d -- %02d:%02d %c%02d:%02d",
110 weekdays[lt->tm_wday], months[lt->tm_mon], lt->tm_mday,
111 1900 + lt->tm_year, lt->tm_hour, lt->tm_min,
112 plus, minswest / 60, minswest % 60);
120 * This is like standard ctime()-function, but it zaps away
121 * the newline from the end of that string. Also, it takes
122 * the time value as parameter, instead of pointer to it.
123 * Note that it is necessary to copy the string to alternate
124 * buffer (who knows how ctime() implements it, maybe it statically
125 * has newline there and never 'refreshes' it -- zapping that
126 * might break things in other places...)
128 char *myctime(time_t value)
133 strcpy(buf, ctime(&value));
134 if ((p = strchr(buf, '\n')) != NULL)
142 * Return the name of the client for various tracking and
143 * admin purposes. The main purpose of this function is to
144 * return the "socket host" name of the client, if that
145 * differs from the advertised name (other than case).
146 * But, this can be used to any client structure.
149 * "name[user@ip#.port]" if 'showip' is true;
150 * "name[sockethost]", if name and sockhost are different and
151 * showip is false; else
155 * Watch out the allocation of "nbuf", if either sptr->name
156 * or sptr->sockhost gets changed into pointers instead of
157 * directly allocated within the structure...
160 * Function return either a pointer to the structure (sptr) or
161 * to internal buffer (nbuf). *NEVER* use the returned pointer
162 * to modify what it points!!!
164 const char* get_client_name(const struct Client* sptr, int showip)
166 static char nbuf[HOSTLEN * 2 + USERLEN + 5];
168 if (MyConnect(sptr)) {
170 sprintf_irc(nbuf, "%s[%s@%s]", sptr->name,
171 (IsIdented(sptr)) ? sptr->username : "", sptr->sock_ip);
173 if (0 != ircd_strcmp(sptr->name, sptr->sockhost))
174 sprintf_irc(nbuf, "%s[%s]", sptr->name, sptr->sockhost);
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(cptr->sockhost, 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 (bcptr->serv && bcptr->serv->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) && bcptr->listing) {
229 bcptr->listing->chptr->mode.mode &= ~MODE_LISTED;
230 MyFree(bcptr->listing);
231 bcptr->listing = 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(bcptr, CMD_QUIT, ":%s", comment);
241 remove_user_from_all_channels(bcptr);
243 /* Clean up invitefield */
244 while ((lp = bcptr->user->invited))
245 del_invite(bcptr, lp->value.chptr);
247 /* Clean up silencefield */
248 while ((lp = bcptr->user->silence))
249 del_silence(bcptr, lp->value.cp);
251 if (IsInvisible(bcptr))
252 --UserStats.inv_clients;
255 if (MyConnect(bcptr))
256 Count_clientdisconnects(bcptr, UserStats);
258 Count_remoteclientquits(UserStats, bcptr);
260 else if (IsServer(bcptr))
262 /* Remove downlink list node of uplink */
263 remove_dlink(&bcptr->serv->up->serv->down, bcptr->serv->updown);
264 bcptr->serv->updown = 0;
266 if (MyConnect(bcptr))
267 Count_serverdisconnects(UserStats);
269 Count_remoteserverquits(UserStats);
271 else if (IsMe(bcptr))
273 sendto_opmask_butone(0, SNO_OLDSNO, "ERROR: tried to exit me! : %s",
275 return; /* ...must *never* exit self! */
277 else if (IsUnknown(bcptr) || IsConnecting(bcptr) || IsHandshake(bcptr))
278 Count_unknowndisconnects(UserStats);
280 /* Update IPregistry */
281 ip_registry_disconnect(bcptr);
285 * Remove from serv->client_list
286 * NOTE: user is *always* NULL if this is a server
289 assert(!IsServer(bcptr));
290 /* bcptr->user->server->serv->client_list[IndexYXX(bcptr)] = NULL; */
291 RemoveYXXClient(bcptr->user->server, bcptr->yxx);
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, bcptr->name, bcptr->from ? bcptr->from->sockhost : "??host",
299 bcptr->from, bcptr->next, bcptr->prev, bcptr->fd,
300 bcptr->status, bcptr->user));
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.
312 * cptr : server that must have all dependents removed
313 * sptr : source who thought that this was a good idea
314 * comment : comment sent as sign off message to local clients
316 static void exit_downlinks(struct Client *cptr, struct Client *sptr, char *comment)
318 struct Client *acptr;
321 struct Client **acptrp;
324 /* Run over all its downlinks */
325 for (lp = cptr->serv->down; lp; lp = next)
328 acptr = lp->value.cptr;
329 /* Remove the downlinks and client of the downlink */
330 exit_downlinks(acptr, sptr, comment);
331 /* Remove the downlink itself */
332 exit_one_client(acptr, me.name);
334 /* Remove all clients of this server */
335 acptrp = cptr->serv->client_list;
336 for (i = 0; i <= cptr->serv->nn_mask; ++acptrp, ++i) {
338 exit_one_client(*acptrp, comment);
343 * exit_client, rewritten 25-9-94 by Run
345 * This function exits a client of *any* type (user, server, etc)
346 * from this server. Also, this generates all necessary prototol
347 * messages that this exit may cause.
349 * This function implicitly exits all other clients depending on
352 * For convenience, this function returns a suitable value for
353 * m_funtion return value:
355 * CPTR_KILLED if (cptr == bcptr)
356 * 0 if (cptr != bcptr)
358 * This function can be called in two ways:
359 * 1) From before or in parse(), exitting the 'cptr', in which case it was
360 * invoked as exit_client(cptr, cptr, &me,...), causing it to always
361 * return CPTR_KILLED.
362 * 2) Via parse from a m_function call, in which case it was invoked as
363 * exit_client(cptr, acptr, sptr, ...). Here 'sptr' is known; the client
364 * that generated the message in a way that we can assume he already
365 * did remove acptr from memory himself (or in other cases we don't mind
366 * because he will be delinked.) Or invoked as:
367 * exit_client(cptr, acptr/sptr, &me, ...) when WE decide this one should
369 * In general: No generated SQUIT or QUIT should be sent to source link
370 * sptr->from. And CPTR_KILLED should be returned if cptr got removed (too).
374 int exit_client(struct Client *cptr, /* Connection being handled by
375 read_message right now */
376 struct Client* victim, /* Client being killed */
377 struct Client* killer, /* The client that made the decision
378 to remove this one, never NULL */
379 const char* comment) /* Reason for the exit */
381 struct Client* acptr = 0;
386 char comment1[HOSTLEN + HOSTLEN + 2];
388 if (MyConnect(victim)) {
389 victim->flags |= FLAGS_CLOSING;
392 on_for = CurrentTime - victim->firsttime;
393 #if defined(USE_SYSLOG) && defined(SYSLOG_USERS)
395 ircd_log(L_TRACE, "%s (%3d:%02d:%02d): %s@%s (%s)\n",
396 myctime(victim->firsttime), on_for / 3600, (on_for % 3600) / 60,
397 on_for % 60, victim->user->username, victim->sockhost, victim->name);
400 write_log(FNAME_USERLOG,
401 "%s (%3d:%02d:%02d): %s@%s [%s]\n",
402 myctime(victim->firsttime),
403 on_for / 3600, (on_for % 3600) / 60,
405 victim->user->username, victim->user->host, victim->username);
408 if (victim != killer->from /* The source knows already */
409 && IsClient(victim)) /* Not a Ping struct or Log file */
411 if (IsServer(victim) || IsHandshake(victim))
412 sendcmdto_one(killer, CMD_SQUIT, victim, "%s 0 :%s", me.name, comment);
413 else if (!IsConnecting(victim)) {
415 sendrawto_one(victim, MSG_ERROR " :Closing Link: %s by %s (%s)",
416 victim->name, killer->name, comment);
418 if ((IsServer(victim) || IsHandshake(victim) || IsConnecting(victim)) &&
419 (killer == &me || (IsServer(killer) &&
420 (strncmp(comment, "Leaf-only link", 14) ||
421 strncmp(comment, "Non-Hub link", 12)))))
424 * Note: check user == user needed to make sure we have the same
427 if (victim->serv->user && *victim->serv->by &&
428 (acptr = findNUser(victim->serv->by))) {
429 if (acptr->user == victim->serv->user) {
430 sendcmdto_one(&me, CMD_NOTICE, acptr,
431 "%C :Link with %s cancelled: %s", acptr,
432 victim->name, comment);
436 * not right client, set by to empty string
439 *victim->serv->by = '\0';
443 sendto_opmask_butone(acptr, SNO_OLDSNO, "Link with %s cancelled: %s",
444 victim->name, comment);
448 * Close the Client connection first.
450 close_connection(victim);
453 if (IsServer(victim))
455 strcpy(comment1, victim->serv->up->name);
456 strcat(comment1, " ");
457 strcat(comment1, victim->name);
459 sendto_opmask_butone(killer, SNO_OLDSNO, "%s SQUIT by %s [%s]:",
460 (killer->user->server == victim ||
461 killer->user->server == victim->serv->up) ?
463 get_client_name(killer, HIDE_IP),
464 killer->user->server->name);
465 else if (killer != &me && victim->serv->up != killer)
466 sendto_opmask_butone(0, SNO_OLDSNO, "Received SQUIT %s from %s :",
467 victim->name, IsServer(killer) ? killer->name :
468 get_client_name(killer, HIDE_IP));
469 sendto_opmask_butone(0, SNO_NETWORK, "Net break: %s (%s)", comment1,
474 * First generate the needed protocol for the other server links
477 for (dlp = me.serv->down; dlp; dlp = dlp->next) {
478 if (dlp->value.cptr != killer->from && dlp->value.cptr != victim) {
479 if (IsServer(victim))
480 sendcmdto_one(killer, CMD_SQUIT, dlp->value.cptr, "%s %Tu :%s",
481 victim->name, victim->serv->timestamp, comment);
482 else if (IsUser(victim) && 0 == (victim->flags & FLAGS_KILLED))
483 sendcmdto_one(victim, CMD_QUIT, dlp->value.cptr, ":%s", comment);
486 /* Then remove the client structures */
487 if (IsServer(victim))
488 exit_downlinks(victim, killer, comment1);
489 exit_one_client(victim, comment);
492 * cptr can only have been killed if it was cptr itself that got killed here,
493 * because cptr can never have been a dependant of victim --Run
495 return (cptr == victim) ? CPTR_KILLED : 0;
499 * Exit client with formatted message, added 25-9-94 by Run
501 int vexit_client_msg(struct Client *cptr, struct Client *bcptr, struct Client *sptr,
502 const char *pattern, va_list vl)
505 vsprintf_irc(msgbuf, pattern, vl);
506 return exit_client(cptr, bcptr, sptr, msgbuf);
509 int exit_client_msg(struct Client *cptr, struct Client *bcptr,
510 struct Client *sptr, const char *pattern, ...)
515 va_start(vl, pattern);
516 vsprintf_irc(msgbuf, pattern, vl);
519 return exit_client(cptr, bcptr, sptr, msgbuf);
524 memset(&ircst, 0, sizeof(ircst));
527 void tstats(struct Client *cptr, char *name)
529 struct Client *acptr;
531 struct ServerStatistics *sp;
532 struct ServerStatistics tmp;
535 memcpy(sp, ServerStats, sizeof(struct ServerStatistics));
536 for (i = 0; i < MAXCONNECTIONS; i++)
538 if (!(acptr = LocalClientArray[i]))
542 sp->is_sbs += acptr->sendB;
543 sp->is_sbr += acptr->receiveB;
544 sp->is_sks += acptr->sendK;
545 sp->is_skr += acptr->receiveK;
546 sp->is_sti += CurrentTime - acptr->firsttime;
548 if (sp->is_sbs > 1023)
550 sp->is_sks += (sp->is_sbs >> 10);
553 if (sp->is_sbr > 1023)
555 sp->is_skr += (sp->is_sbr >> 10);
559 else if (IsUser(acptr))
561 sp->is_cbs += acptr->sendB;
562 sp->is_cbr += acptr->receiveB;
563 sp->is_cks += acptr->sendK;
564 sp->is_ckr += acptr->receiveK;
565 sp->is_cti += CurrentTime - acptr->firsttime;
567 if (sp->is_cbs > 1023)
569 sp->is_cks += (sp->is_cbs >> 10);
572 if (sp->is_cbr > 1023)
574 sp->is_ckr += (sp->is_cbr >> 10);
578 else if (IsUnknown(acptr))
582 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":accepts %u refused %u",
583 sp->is_ac, sp->is_ref);
584 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
585 ":unknown commands %u prefixes %u", sp->is_unco, sp->is_unpf);
586 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
587 ":nick collisions %u unknown closes %u", sp->is_kill, sp->is_ni);
588 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
589 ":wrong direction %u empty %u", sp->is_wrdi, sp->is_empt);
590 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
591 ":numerics seen %u mode fakes %u", sp->is_num, sp->is_fake);
592 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG,
593 ":auth successes %u fails %u", sp->is_asuc, sp->is_abad);
594 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":local connections %u",
596 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":Client server");
597 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":connected %u %u",
598 sp->is_cl, sp->is_sv);
599 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes sent %u.%uK %u.%uK",
600 sp->is_cks, sp->is_cbs, sp->is_sks, sp->is_sbs);
601 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":bytes recv %u.%uK %u.%uK",
602 sp->is_ckr, sp->is_cbr, sp->is_skr, sp->is_sbr);
603 send_reply(cptr, SND_EXPLICIT | RPL_STATSDEBUG, ":time connected %Tu %Tu",
604 sp->is_cti, sp->is_sti);