Author: Ghostwolf <foxxe@wtfs.net>
[ircu2.10.12-pk.git] / ircd / map.c
1 /*
2  * IRC - Internet Relay Chat, ircd/map.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  * Copyright (C) 2002 Joseph Bongaarts <foxxe@wtfs.net>
6  *
7  * See file AUTHORS in IRC package for additional names of
8  * the programmers.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 1, or (at your option)
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  * $Id$
25  */
26
27 #include "config.h"
28
29 #include "client.h"
30 #include "ircd.h"
31 #include "ircd_defs.h"
32 #include "ircd_policy.h"
33 #include "ircd_reply.h"
34 #include "ircd_snprintf.h"
35 #include "ircd_string.h"
36 #include "hash.h"
37 #include "list.h"
38 #include "match.h"
39 #include "msg.h"
40 #include "numeric.h"
41 #include "s_user.h"
42 #include "s_serv.h"
43 #include "send.h"
44 #include "querycmds.h"
45 #include "map.h"
46
47 #include <assert.h>
48 #include <stdio.h>
49 #include <string.h>
50
51 #ifdef HEAD_IN_SAND_MAP
52
53 static struct Map *MapList = 0;
54
55 /* Add a server to the map list. */
56 static void map_add(struct Client *server)
57 {
58 /*  assert(server != 0);
59   assert(!IsHub(server));
60   assert(!IsService(server));
61 */
62   struct Map *map = (struct Map *)MyAlloc(sizeof(struct Map));
63
64   map->lasttime = TStime();
65   strcpy(map->name, cli_name(server));
66   map->maxclients = cli_serv(server)->clients;
67
68   map->prev = 0;
69   map->next = MapList;
70
71   if(MapList)
72     MapList->prev = new;
73   
74   MapList = new;
75 }
76
77 /* Remove a server from the map list */
78 static void map_remove(struct Map *cptr)
79 {
80   /*  assert(cptr != 0);*/
81   
82   if(cptr->next)
83     cptr->next->prev = cptr->prev;
84   
85   if(cptr->prev)
86     cptr->prev->next = cptr->next;
87   
88   if(MapList == cptr)
89     MapList = cptr->next;
90
91   MyFree(cptr);
92
93 }
94
95 /* Update a server in the list. Called when a server connects
96  * splits, or we haven't checked in more than a week. */
97 void map_update(struct Client *cptr)
98 {
99   /*  assert(IsServer(cptr)); */
100
101   struct Map *map = 0;
102   
103   /* Find the server in the list and update it */ 
104   for(map = MapList; map; map = map->next)
105   {
106     /* Show max clients not current, otherwise a split can be detected. */
107     if(!ircd_strcmp(cli_name(cptr), map->name)) 
108     { 
109       map->lasttime = TStime();
110       if(map->maxclients < cli_serv(cptr)->clients) 
111         map->maxclients = cli_serv(cptr)->clients;  
112       break;
113     }
114   }
115
116   /* We do this check after finding a matching map because
117    * a client server can become a hub or service (such as 
118    * santaclara)
119    */
120   if(IsHub(cptr) || IsService(cptr))
121   {
122     if(map)
123       map_remove(map);
124     return;
125   }
126
127   /* If we haven't seen it before, add it to the list. */
128   if(!map)
129     map_add(cli_name(cptr));
130 }
131
132 void map_dump_head_in_sand(struct Client *cptr)
133 {
134   struct Map *map = 0;
135   struct Map *smap = 0;
136   struct Client *acptr = 0;
137
138   /* Send me first */
139   send_reply(cptr, RPL_MAP, "", "", cli_name(&me), "", UserStats.local_clients);
140
141   for(map = MapList; map; map = smap)
142   {
143     smap = map->next;
144
145     /* Don't show servers we haven't seen in more than a week */
146     if(map->lasttime < TStime() - 604800)
147     {
148       acptr = FindServer(map->name);
149       if(!acptr)
150       {
151         map_remove(map);
152         continue;
153       }
154       else
155         map_update(acptr);
156     }
157     send_reply(cptr, RPL_MAP, "", "|-", map->name, "", map->maxclients);
158   }
159 }
160
161 #endif /* HEAD_IN_SAND_MAP */ 
162   
163 void map_dump(struct Client *cptr, struct Client *server, char *mask, int prompt_length)
164 {
165   static char prompt[64];
166   struct DLink *lp;
167   char *p = &prompt[prompt_length];
168   int cnt = 0;
169
170   *p = '\0';
171   if (prompt_length > 60)
172     send_reply(cptr, RPL_MAPMORE, prompt, cli_name(server));
173   else {
174     char lag[512];
175     if (cli_serv(server)->lag>10000)
176         lag[0]=0;
177     else if (cli_serv(server)->lag<0)
178         strcpy(lag,"(0s)");
179     else
180         sprintf(lag,"(%is)",cli_serv(server)->lag);
181     send_reply(cptr, RPL_MAP, prompt, (
182                 (IsBurst(server)) ? "*" : (IsBurstAck(server) ? "!" : "")),
183                cli_name(server), lag, (server == &me) ? UserStats.local_clients :
184                cli_serv(server)->clients);
185   }
186   if (prompt_length > 0)
187   {
188     p[-1] = ' ';
189     if (p[-2] == '`')
190       p[-2] = ' ';
191   }
192   if (prompt_length > 60)
193     return;
194   strcpy(p, "|-");
195   for (lp = cli_serv(server)->down; lp; lp = lp->next)
196     if (match(mask, cli_name(lp->value.cptr)))
197       cli_flags(lp->value.cptr) &= ~FLAGS_MAP;
198     else
199     {
200       cli_flags(lp->value.cptr) |= FLAGS_MAP;
201       cnt++;
202     }
203   for (lp = cli_serv(server)->down; lp; lp = lp->next)
204   {
205     if ((cli_flags(lp->value.cptr) & FLAGS_MAP) == 0)
206       continue;
207     if (--cnt == 0)
208       *p = '`';
209     map_dump(cptr, lp->value.cptr, mask, prompt_length + 2);
210   }
211   if (prompt_length > 0)
212     p[-1] = '-';
213 }
214
215