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 "ircd_alloc.h"
37 #include "hash.h"
38 #include "list.h"
39 #include "match.h"
40 #include "msg.h"
41 #include "numeric.h"
42 #include "s_user.h"
43 #include "s_serv.h"
44 #include "send.h"
45 #include "querycmds.h"
46 #include "map.h"
47
48 #include <assert.h>
49 #include <stdio.h>
50 #include <string.h>
51
52 #ifdef HEAD_IN_SAND_MAP
53
54 static struct Map *MapList = 0;
55
56 /* Add a server to the map list. */
57 static void map_add(struct Client *server)
58 {
59   struct Map *map = (struct Map *)MyMalloc(sizeof(struct Map));
60
61   assert(IsServer(server));
62   assert(!IsHub(server));
63   assert(!IsService(server));
64
65   map->lasttime = TStime();
66   strcpy(map->name, cli_name(server));
67   map->maxclients = cli_serv(server)->clients;
68
69   map->prev = 0;
70   map->next = MapList;
71
72   if(MapList)
73     MapList->prev = map;
74   
75   MapList = map;
76 }
77
78 /* Remove a server from the map list */
79 static void map_remove(struct Map *cptr)
80 {
81   assert(cptr != 0);
82   
83   if(cptr->next)
84     cptr->next->prev = cptr->prev;
85   
86   if(cptr->prev)
87     cptr->prev->next = cptr->next;
88   
89   if(MapList == cptr)
90     MapList = cptr->next;
91
92   MyFree(cptr);
93
94 }
95
96 /* Update a server in the list. Called when a server connects
97  * splits, or we haven't checked in more than a week. */
98 void map_update(struct Client *cptr)
99 {
100   struct Map *map = 0;
101
102   assert(IsServer(cptr));
103
104   /* Find the server in the list and update it */ 
105   for(map = MapList; map; map = map->next)
106   {
107     /* Show max clients not current, otherwise a split can be detected. */
108     if(!ircd_strcmp(cli_name(cptr), map->name)) 
109     { 
110       map->lasttime = TStime();
111       if(map->maxclients < cli_serv(cptr)->clients) 
112         map->maxclients = cli_serv(cptr)->clients;  
113       break;
114     }
115   }
116
117   /* We do this check after finding a matching map because
118    * a client server can become a hub or service (such as 
119    * santaclara)
120    */
121   if(IsHub(cptr) || IsService(cptr))
122   {
123     if(map)
124       map_remove(map);
125     return;
126   }
127
128   /* If we haven't seen it before, add it to the list. */
129   if(!map)
130     map_add(cptr);
131 }
132
133 void map_dump_head_in_sand(struct Client *cptr)
134 {
135   struct Map *map = 0;
136   struct Map *smap = 0;
137   struct Client *acptr = 0;
138
139   /* Send me first */
140   send_reply(cptr, RPL_MAP, "", "", cli_name(&me), "", UserStats.local_clients);
141
142   for(map = MapList; map; map = smap)
143   {
144     smap = map->next;
145
146     /* Don't show servers we haven't seen in more than a week */
147     if(map->lasttime < TStime() - 604800)
148     {
149       acptr = FindServer(map->name);
150       if(!acptr)
151       {
152         map_remove(map);
153         continue;
154       }
155       else
156         map_update(acptr);
157     }
158     send_reply(cptr, RPL_MAP, smap ? "|" : "`", "-", map->name, "", map->maxclients);
159   }
160 }
161
162 #endif /* HEAD_IN_SAND_MAP */ 
163   
164 void map_dump(struct Client *cptr, struct Client *server, char *mask, int prompt_length)
165 {
166   static char prompt[64];
167   struct DLink *lp;
168   char *p = &prompt[prompt_length];
169   int cnt = 0;
170
171   *p = '\0';
172   if (prompt_length > 60)
173     send_reply(cptr, RPL_MAPMORE, prompt, cli_name(server));
174   else {
175     char lag[512];
176     if (cli_serv(server)->lag>10000)
177         lag[0]=0;
178     else if (cli_serv(server)->lag<0)
179         strcpy(lag,"(0s)");
180     else
181         sprintf(lag,"(%is)",cli_serv(server)->lag);
182     send_reply(cptr, RPL_MAP, prompt, (
183                 (IsBurst(server)) ? "*" : (IsBurstAck(server) ? "!" : "")),
184                cli_name(server), lag, (server == &me) ? UserStats.local_clients :
185                cli_serv(server)->clients);
186   }
187   if (prompt_length > 0)
188   {
189     p[-1] = ' ';
190     if (p[-2] == '`')
191       p[-2] = ' ';
192   }
193   if (prompt_length > 60)
194     return;
195   strcpy(p, "|-");
196   for (lp = cli_serv(server)->down; lp; lp = lp->next)
197     if (match(mask, cli_name(lp->value.cptr)))
198       cli_flags(lp->value.cptr) &= ~FLAGS_MAP;
199     else
200     {
201       cli_flags(lp->value.cptr) |= FLAGS_MAP;
202       cnt++;
203     }
204   for (lp = cli_serv(server)->down; lp; lp = lp->next)
205   {
206     if ((cli_flags(lp->value.cptr) & FLAGS_MAP) == 0)
207       continue;
208     if (--cnt == 0)
209       *p = '`';
210     map_dump(cptr, lp->value.cptr, mask, prompt_length + 2);
211   }
212   if (prompt_length > 0)
213     p[-1] = '-';
214 }
215
216