implement IAUTH client code to query a separate server before allowing
[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 "map.h"
30 #include "client.h"
31 #include "ircd.h"
32 #include "ircd_defs.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
47 #include <assert.h>
48 #include <stdio.h>
49 #include <string.h>
50
51 static struct Map *MapList = 0;
52
53 /* Add a server to the map list. */
54 static void map_add(struct Client *server)
55 {
56   struct Map *map = (struct Map *)MyMalloc(sizeof(struct Map));
57
58   assert(IsServer(server));
59   assert(!IsHub(server));
60   assert(!IsService(server));
61
62   map->lasttime = TStime();
63   strcpy(map->name, cli_name(server));
64   strcpy(map->info, cli_info(server));
65   map->prot = Protocol(server);
66   map->maxclients = cli_serv(server)->clients;
67
68   map->prev = 0;
69   map->next = MapList;
70
71   if(MapList)
72     MapList->prev = map;
73   
74   MapList = map;
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   struct Map *map = 0;
100
101   assert(IsServer(cptr));
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       map->prot = Protocol(cptr);
111       strcpy(map->info, cli_info(cptr));
112       if(map->maxclients < cli_serv(cptr)->clients) 
113         map->maxclients = cli_serv(cptr)->clients;  
114       break;
115     }
116   }
117
118   /* We do this check after finding a matching map because
119    * a client server can become a hub or service (such as 
120    * santaclara)
121    */
122   if(IsHub(cptr) || IsService(cptr))
123   {
124     if(map)
125       map_remove(map);
126     return;
127   }
128
129   /* If we haven't seen it before, add it to the list. */
130   if(!map)
131     map_add(cptr);
132 }
133
134 #ifdef HEAD_IN_SAND_MAP
135
136 void map_dump_head_in_sand(struct Client *cptr)
137 {
138   struct Map *map = 0;
139   struct Map *smap = 0;
140   struct Client *acptr = 0;
141
142   /* Send me first */
143   send_reply(cptr, RPL_MAP, "", "", cli_name(&me), "", UserStats.local_clients);
144
145   for(map = MapList; map; map = smap)
146   {
147     smap = map->next;
148
149     /* Don't show servers we haven't seen in more than a week */
150     if(map->lasttime < TStime() - MAP_CACHE_TIME)
151     {
152       acptr = FindServer(map->name);
153       if(!acptr)
154       {
155         map_remove(map);
156         continue;
157       }
158       else
159         map_update(acptr);
160     }
161     send_reply(cptr, RPL_MAP, smap ? "|" : "`", "-", map->name, "", map->maxclients);
162   }
163 }
164
165 #endif /* HEAD_IN_SAND_MAP */
166
167 #ifdef HEAD_IN_SAND_LINKS
168 void map_dump_links_head_in_sand(struct Client *sptr, char *mask)
169 {
170   struct Map *link = 0;
171   struct Map *slink = 0;
172   struct Client *acptr = 0;
173
174   collapse(mask);
175   
176   for(link = MapList; link; link = slink)
177   {
178     slink = link->next;
179
180     if(link->lasttime < TStime() - MAP_CACHE_TIME)
181     {
182       acptr = FindServer(link->name);
183       if(!acptr)
184       {
185         map_remove(link);
186         continue;
187       }
188       else
189         map_update(acptr);
190     }
191     if (!BadPtr(mask) && match(mask, link->name))
192       continue;
193     send_reply(sptr, RPL_LINKS, link->name, cli_name(&me), 1, link->prot, 
194                link->info);
195   }
196   /* don't forget to send me */
197   send_reply(sptr, RPL_LINKS, cli_name(&me), cli_name(&me), 0, Protocol(&me),
198              cli_info(&me));
199 }
200 #endif /* HEAD_IN_SAND_LINKS */
201   
202 void map_dump(struct Client *cptr, struct Client *server, char *mask, int prompt_length)
203 {
204   static char prompt[64];
205   struct DLink *lp;
206   char *p = &prompt[prompt_length];
207   int cnt = 0;
208
209   *p = '\0';
210   if (prompt_length > 60)
211     send_reply(cptr, RPL_MAPMORE, prompt, cli_name(server));
212   else {
213     char lag[512];
214     if (cli_serv(server)->lag>10000)
215         lag[0]=0;
216     else if (cli_serv(server)->lag<0)
217         strcpy(lag,"(0s)");
218     else
219         sprintf(lag,"(%is)",cli_serv(server)->lag);
220     send_reply(cptr, RPL_MAP, prompt, (
221                 (IsBurst(server)) ? "*" : (IsBurstAck(server) ? "!" : "")),
222                cli_name(server), lag, (server == &me) ? UserStats.local_clients :
223                cli_serv(server)->clients);
224   }
225   if (prompt_length > 0)
226   {
227     p[-1] = ' ';
228     if (p[-2] == '`')
229       p[-2] = ' ';
230   }
231   if (prompt_length > 60)
232     return;
233   strcpy(p, "|-");
234   for (lp = cli_serv(server)->down; lp; lp = lp->next)
235     if (match(mask, cli_name(lp->value.cptr)))
236       ClrFlag(lp->value.cptr, FLAG_MAP);
237     else
238     {
239       SetFlag(lp->value.cptr, FLAG_MAP);
240       cnt++;
241     }
242   for (lp = cli_serv(server)->down; lp; lp = lp->next)
243   {
244     if (!HasFlag(lp->value.cptr, FLAG_MAP))
245       continue;
246     if (--cnt == 0)
247       *p = '`';
248     map_dump(cptr, lp->value.cptr, mask, prompt_length + 2);
249   }
250   if (prompt_length > 0)
251     p[-1] = '-';
252 }
253
254