home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / other / irc / ircd / whowas.c < prev   
Encoding:
C/C++ Source or Header  |  1996-08-14  |  4.6 KB  |  215 lines

  1. /************************************************************************
  2.  *   IRC - Internet Relay Chat, ircd/whowas.c
  3.  *   Copyright (C) 1990 Markku Savela
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 1, or (at your option)
  8.  *   any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /*
  21.  * --- avalon --- 6th April 1992
  22.  * rewritten to scrap linked lists and use a table of structures which
  23.  * is referenced like a circular loop. Should be faster and more efficient.
  24.  */
  25.  
  26. #ifndef lint
  27. static  char sccsid[] = "@(#)whowas.c    2.16 08 Nov 1993 (C) 1988 Markku Savela";
  28. #endif
  29.  
  30. #include "struct.h"
  31. #include "common.h"
  32. #include "sys.h"
  33. #include "numeric.h"
  34. #include "whowas.h"
  35. #include "h.h"
  36.  
  37. static    aName    was[NICKNAMEHISTORYLENGTH];
  38. static    int    ww_index = 0;
  39.  
  40. void    add_history(cptr)
  41. Reg1    aClient    *cptr;
  42. {
  43.     aName    ntmp;
  44.     Reg2    aName    *np = &ntmp, *np2;
  45.  
  46.     strncpyzt(np->ww_nick, cptr->name, NICKLEN+1);
  47.     strncpyzt(np->ww_info, cptr->info, REALLEN+1);
  48.     np->ww_user = cptr->user;
  49.     np->ww_logout = time(NULL);
  50.     np->ww_online = (cptr->from != NULL) ? cptr : NULL;
  51.     np->ww_user->refcnt++;
  52.  
  53.     np2 = &was[ww_index];
  54.     if (np2->ww_user)
  55.         free_user(np2->ww_user, np2->ww_online);
  56.  
  57.     bcopy((char *)&ntmp, (char *)np2, sizeof(aName));
  58.  
  59.     ww_index++;
  60.     if (ww_index >= NICKNAMEHISTORYLENGTH)
  61.         ww_index = 0;
  62.     return;
  63. }
  64.  
  65. /*
  66. ** get_history
  67. **      Return the current client that was using the given
  68. **      nickname within the timelimit. Returns NULL, if no
  69. **      one found...
  70. */
  71. aClient    *get_history(nick, timelimit)
  72. char    *nick;
  73. time_t    timelimit;
  74. {
  75.     Reg1    aName    *wp, *wp2;
  76.     Reg2    int    i = 0;
  77.  
  78.     wp = wp2 = &was[ww_index];
  79.     timelimit = time(NULL)-timelimit;
  80.  
  81.     do {
  82.         if (!mycmp(nick, wp->ww_nick) && wp->ww_logout >= timelimit)
  83.             break;
  84.         wp++;
  85.         if (wp == &was[NICKNAMEHISTORYLENGTH])
  86.             i = 1, wp = was;
  87.     } while (wp != wp2);
  88.  
  89.     if (wp != wp2 || !i)
  90.         return (wp->ww_online);
  91.     return (NULL);
  92. }
  93.  
  94. void    off_history(cptr)
  95. Reg3    aClient    *cptr;
  96. {
  97.     Reg1    aName    *wp;
  98.     Reg2    int    i;
  99.  
  100.     for (i = NICKNAMEHISTORYLENGTH, wp = was; i; wp++, i--)
  101.         if (wp->ww_online == cptr)
  102.             wp->ww_online = NULL;
  103.     return;
  104. }
  105.  
  106. void    initwhowas()
  107. {
  108.     Reg1    int    i;
  109.  
  110.     for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
  111.         bzero((char *)&was[i], sizeof(aName));
  112.     return;
  113. }
  114.  
  115.  
  116. /*
  117. ** m_whowas
  118. **    parv[0] = sender prefix
  119. **    parv[1] = nickname queried
  120. */
  121. int    m_whowas(cptr, sptr, parc, parv)
  122. aClient    *cptr, *sptr;
  123. int    parc;
  124. char    *parv[];
  125. {
  126.     Reg1    aName    *wp, *wp2 = NULL;
  127.     Reg2    int    j = 0;
  128.     Reg3    anUser    *up = NULL;
  129.     int    max = -1;
  130.     char    *p, *nick, *s;
  131.  
  132.      if (parc < 2)
  133.         {
  134.         sendto_one(sptr, err_str(ERR_NONICKNAMEGIVEN),
  135.                me.name, parv[0]);
  136.         return 0;
  137.         }
  138.     if (parc > 2)
  139.         max = atoi(parv[2]);
  140.     if (parc > 3)
  141.         if (hunt_server(cptr,sptr,":%s WHOWAS %s %s :%s", 3,parc,parv))
  142.             return 0;
  143.  
  144.     for (s = parv[1]; (nick = strtoken(&p, s, ",")); s = NULL)
  145.         {
  146.         wp = wp2 = &was[ww_index - 1];
  147.  
  148.         do {
  149.             if (wp < was)
  150.                 wp = &was[NICKNAMEHISTORYLENGTH - 1];
  151.             if (mycmp(nick, wp->ww_nick) == 0)
  152.                 {
  153.                 up = wp->ww_user;
  154.                 sendto_one(sptr, rpl_str(RPL_WHOWASUSER),
  155.                        me.name, parv[0], wp->ww_nick,
  156.                        up->username,
  157.                        up->host, wp->ww_info);
  158.                 sendto_one(sptr, rpl_str(RPL_WHOISSERVER),
  159.                        me.name, parv[0], wp->ww_nick,
  160.                        up->server, myctime(wp->ww_logout));
  161.                 if (up->away)
  162.                     sendto_one(sptr, rpl_str(RPL_AWAY),
  163.                            me.name, parv[0],
  164.                            wp->ww_nick, up->away);
  165.                 j++;
  166.                 }
  167.             if (max > 0 && j >= max)
  168.                 break;
  169.             wp--;
  170.         } while (wp != wp2);
  171.  
  172.         if (up == NULL)
  173.             sendto_one(sptr, err_str(ERR_WASNOSUCHNICK),
  174.                    me.name, parv[0], parv[1]);
  175.  
  176.         if (p)
  177.             p[-1] = ',';
  178.         }
  179.     sendto_one(sptr, rpl_str(RPL_ENDOFWHOWAS), me.name, parv[0], parv[1]);
  180.     return 0;
  181.     }
  182.  
  183.  
  184. void    count_whowas_memory(wwu, wwa, wwam)
  185. int    *wwu, *wwa;
  186. u_long    *wwam;
  187. {
  188.     Reg1    anUser    *tmp;
  189.     Reg2    int    i, j;
  190.     int    u = 0, a = 0;
  191.     u_long    am = 0;
  192.  
  193.     for (i = 0; i < NICKNAMEHISTORYLENGTH; i++)
  194.         if ((tmp = was[i].ww_user))
  195.             if (!was[i].ww_online)
  196.                 {
  197.                 for (j = 0; j < i; j++)
  198.                     if (was[j].ww_user == tmp)
  199.                         break;
  200.                 if (j < i)
  201.                     continue;
  202.                 u++;
  203.                 if (tmp->away)
  204.                     {
  205.                     a++;
  206.                     am += (strlen(tmp->away)+1);
  207.                     }
  208.                 }
  209.     *wwu = u;
  210.     *wwa = a;
  211.     *wwam = am;
  212.  
  213.     return;
  214. }
  215.