]> jfr.im git - solanum.git/blame - modules/m_map.c
Replace RPL_WHOISTEXT(337) with RPL_WHOISSPECIAL(320) (#419)
[solanum.git] / modules / m_map.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: A slightly useful ircd.
3 * m_map.c: Sends an Undernet compatible map to a user.
4 *
5 * Copyright (C) 2002 by the past and present ircd coders, and others.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
212380e3
AC
21 */
22
23#include "stdinc.h"
24#include "client.h"
25#include "modules.h"
26#include "numeric.h"
27#include "send.h"
28#include "s_conf.h"
c0bc9fe3 29#include "scache.h"
212380e3
AC
30
31#define USER_COL 50 /* display | Users: %d at col 50 */
32
eeabf33a
EM
33static const char map_desc[] = "Provides the MAP command to view network topology information";
34
3c7d6fcc
EM
35static void m_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
36static void mo_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
212380e3
AC
37
38struct Message map_msgtab = {
7baa37a9 39 "MAP", 0, 0, 0, 0,
212380e3
AC
40 {mg_unreg, {m_map, 0}, mg_ignore, mg_ignore, mg_ignore, {mo_map, 0}}
41};
42
43mapi_clist_av1 map_clist[] = { &map_msgtab, NULL };
f5ebe640 44
bc89faeb 45DECLARE_MODULE_AV2(map, NULL, NULL, map_clist, NULL, NULL, NULL, NULL, map_desc);
212380e3
AC
46
47static void dump_map(struct Client *client_p, struct Client *root, char *pbuf);
e1fda0d8 48static void flattened_map(struct Client *client_p);
212380e3
AC
49
50static char buf[BUFSIZE];
51
52/* m_map
212380e3 53*/
3c7d6fcc 54static void
428ca87b 55m_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
56{
57 if((!IsExemptShide(source_p) && ConfigServerHide.flatten_links) ||
58 ConfigFileEntry.map_oper_only)
59 {
e1fda0d8
AC
60 flattened_map(client_p);
61 sendto_one_numeric(client_p, RPL_MAPEND, form_str(RPL_MAPEND));
3c7d6fcc 62 return;
212380e3
AC
63 }
64
65 dump_map(client_p, &me, buf);
88520303 66 sendto_one_numeric(client_p, RPL_MAPEND, form_str(RPL_MAPEND));
212380e3
AC
67}
68
69/*
70** mo_map
212380e3 71*/
3c7d6fcc 72static void
428ca87b 73mo_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
74{
75 dump_map(client_p, &me, buf);
c0bc9fe3 76 scache_send_missing(client_p);
88520303 77 sendto_one_numeric(client_p, RPL_MAPEND, form_str(RPL_MAPEND));
212380e3
AC
78}
79
80/*
81** dump_map
82** dumps server map, called recursively.
83*/
84static void
85dump_map(struct Client *client_p, struct Client *root_p, char *pbuf)
86{
28d337da 87 int cnt = 0, i = 0, len, frac;
212380e3 88 struct Client *server_p;
5b96d9a6 89 rb_dlink_node *ptr;
212380e3
AC
90 *pbuf = '\0';
91
1f9de103 92 rb_strlcat(pbuf, root_p->name, BUFSIZE);
212380e3
AC
93 if (has_id(root_p))
94 {
1f9de103
VY
95 rb_strlcat(pbuf, "[", BUFSIZE);
96 rb_strlcat(pbuf, root_p->id, BUFSIZE);
97 rb_strlcat(pbuf, "]", BUFSIZE);
212380e3
AC
98 }
99 len = strlen(buf);
100 buf[len] = ' ';
101
102 if(len < USER_COL)
103 {
104 for (i = len + 1; i < USER_COL; i++)
105 {
106 buf[i] = '-';
107 }
108 }
109
28d337da 110 frac = (1000 * rb_dlink_list_length(&root_p->serv->users) + Count.total / 2) / Count.total;
57aa79ac 111 snprintf(buf + USER_COL, sizeof buf - USER_COL,
28d337da
JT
112 " | Users: %5lu (%2d.%1d%%)", rb_dlink_list_length(&root_p->serv->users),
113 frac / 10, frac % 10);
212380e3 114
88520303 115 sendto_one_numeric(client_p, RPL_MAP, form_str(RPL_MAP), buf);
212380e3
AC
116
117 if(root_p->serv->servers.head != NULL)
118 {
5b96d9a6 119 cnt += rb_dlink_list_length(&root_p->serv->servers);
212380e3
AC
120
121 if(cnt)
122 {
123 if(pbuf > buf + 3)
124 {
125 pbuf[-2] = ' ';
126 if(pbuf[-3] == '`')
127 pbuf[-3] = ' ';
128 }
129 }
130 }
131 i = 1;
5b96d9a6 132 RB_DLINK_FOREACH(ptr, root_p->serv->servers.head)
212380e3
AC
133 {
134 server_p = ptr->data;
135 *pbuf = ' ';
136 if(i < cnt)
137 *(pbuf + 1) = '|';
138 else
139 *(pbuf + 1) = '`';
140
141 *(pbuf + 2) = '-';
142 *(pbuf + 3) = ' ';
143 dump_map(client_p, server_p, pbuf + 4);
144
145 i++;
146 }
147}
e1fda0d8
AC
148
149/*
150 * flattened_map - display a version of map that doesn't give away routing
151 * information to users when flattened links is enabled.
152 */
153static void
154flattened_map(struct Client *client_p)
155{
156 char buf[BUFSIZE];
157 rb_dlink_node *ptr;
158 struct Client *target_p;
159 int i, len;
66769bc1 160 unsigned long cnt = 0;
e1fda0d8
AC
161
162 /* First display me as the root */
163 rb_strlcpy(buf, me.name, BUFSIZE);
164 len = strlen(buf);
165 buf[len] = ' ';
166
167 if(len < USER_COL)
168 {
169 for (i = len + 1; i < USER_COL; i++)
170 {
171 buf[i] = '-';
172 }
173 }
174
57aa79ac 175 snprintf(buf + USER_COL, sizeof buf - USER_COL,
e1fda0d8
AC
176 " | Users: %5lu (%4.1f%%)", rb_dlink_list_length(&me.serv->users),
177 100 * (float) rb_dlink_list_length(&me.serv->users) / (float) Count.total);
178
179 sendto_one_numeric(client_p, RPL_MAP, form_str(RPL_MAP), buf);
180
181 /* Next, we run through every other server and list them */
182 RB_DLINK_FOREACH(ptr, global_serv_list.head)
183 {
184 target_p = ptr->data;
185
186 cnt++;
187
188 /* Skip ourselves, it's already counted */
189 if(IsMe(target_p))
190 continue;
191
192 /* if we're hidden, go on to the next leaf */
193 if(!ConfigServerHide.disable_hidden && IsHidden(target_p))
194 continue;
195
196 if (cnt == rb_dlink_list_length(&global_serv_list))
197 rb_strlcpy(buf, " `- ", BUFSIZE);
198 else
199 rb_strlcpy(buf, " |- ", BUFSIZE);
200
0e06053c 201 rb_strlcat(buf, target_p->name, BUFSIZE);
e1fda0d8
AC
202 len = strlen(buf);
203 buf[len] = ' ';
204
205 if(len < USER_COL)
206 {
207 for (i = len + 1; i < USER_COL; i++)
208 {
209 buf[i] = '-';
210 }
211 }
212
57aa79ac 213 snprintf(buf + USER_COL, sizeof buf - USER_COL,
e1fda0d8
AC
214 " | Users: %5lu (%4.1f%%)", rb_dlink_list_length(&target_p->serv->users),
215 100 * (float) rb_dlink_list_length(&target_p->serv->users) / (float) Count.total);
216
217 sendto_one_numeric(client_p, RPL_MAP, form_str(RPL_MAP), buf);
218 }
219}