]>
Commit | Line | Data |
---|---|---|
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 | ||
428ca87b AC |
33 | static int m_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]); |
34 | static int mo_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]); | |
212380e3 AC |
35 | |
36 | struct Message map_msgtab = { | |
7baa37a9 | 37 | "MAP", 0, 0, 0, 0, |
212380e3 AC |
38 | {mg_unreg, {m_map, 0}, mg_ignore, mg_ignore, mg_ignore, {mo_map, 0}} |
39 | }; | |
40 | ||
41 | mapi_clist_av1 map_clist[] = { &map_msgtab, NULL }; | |
f5ebe640 EM |
42 | |
43 | static const char map_desc[] = "Provides the MAP command to view network topology information"; | |
44 | ||
bc89faeb | 45 | DECLARE_MODULE_AV2(map, NULL, NULL, map_clist, NULL, NULL, NULL, NULL, map_desc); |
212380e3 AC |
46 | |
47 | static void dump_map(struct Client *client_p, struct Client *root, char *pbuf); | |
e1fda0d8 | 48 | static void flattened_map(struct Client *client_p); |
212380e3 AC |
49 | |
50 | static char buf[BUFSIZE]; | |
51 | ||
52 | /* m_map | |
212380e3 AC |
53 | */ |
54 | static int | |
428ca87b | 55 | m_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)); | |
212380e3 AC |
62 | return 0; |
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 | return 0; |
68 | } | |
69 | ||
70 | /* | |
71 | ** mo_map | |
212380e3 AC |
72 | */ |
73 | static int | |
428ca87b | 74 | mo_map(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
75 | { |
76 | dump_map(client_p, &me, buf); | |
c0bc9fe3 | 77 | scache_send_missing(client_p); |
88520303 | 78 | sendto_one_numeric(client_p, RPL_MAPEND, form_str(RPL_MAPEND)); |
212380e3 AC |
79 | |
80 | return 0; | |
81 | } | |
82 | ||
83 | /* | |
84 | ** dump_map | |
85 | ** dumps server map, called recursively. | |
86 | */ | |
87 | static void | |
88 | dump_map(struct Client *client_p, struct Client *root_p, char *pbuf) | |
89 | { | |
28d337da | 90 | int cnt = 0, i = 0, len, frac; |
212380e3 | 91 | struct Client *server_p; |
5b96d9a6 | 92 | rb_dlink_node *ptr; |
212380e3 AC |
93 | *pbuf = '\0'; |
94 | ||
1f9de103 | 95 | rb_strlcat(pbuf, root_p->name, BUFSIZE); |
212380e3 AC |
96 | if (has_id(root_p)) |
97 | { | |
1f9de103 VY |
98 | rb_strlcat(pbuf, "[", BUFSIZE); |
99 | rb_strlcat(pbuf, root_p->id, BUFSIZE); | |
100 | rb_strlcat(pbuf, "]", BUFSIZE); | |
212380e3 AC |
101 | } |
102 | len = strlen(buf); | |
103 | buf[len] = ' '; | |
104 | ||
105 | if(len < USER_COL) | |
106 | { | |
107 | for (i = len + 1; i < USER_COL; i++) | |
108 | { | |
109 | buf[i] = '-'; | |
110 | } | |
111 | } | |
112 | ||
28d337da | 113 | frac = (1000 * rb_dlink_list_length(&root_p->serv->users) + Count.total / 2) / Count.total; |
5203cba5 | 114 | snprintf(buf + USER_COL, BUFSIZE - USER_COL, |
28d337da JT |
115 | " | Users: %5lu (%2d.%1d%%)", rb_dlink_list_length(&root_p->serv->users), |
116 | frac / 10, frac % 10); | |
212380e3 | 117 | |
88520303 | 118 | sendto_one_numeric(client_p, RPL_MAP, form_str(RPL_MAP), buf); |
212380e3 AC |
119 | |
120 | if(root_p->serv->servers.head != NULL) | |
121 | { | |
5b96d9a6 | 122 | cnt += rb_dlink_list_length(&root_p->serv->servers); |
212380e3 AC |
123 | |
124 | if(cnt) | |
125 | { | |
126 | if(pbuf > buf + 3) | |
127 | { | |
128 | pbuf[-2] = ' '; | |
129 | if(pbuf[-3] == '`') | |
130 | pbuf[-3] = ' '; | |
131 | } | |
132 | } | |
133 | } | |
134 | i = 1; | |
5b96d9a6 | 135 | RB_DLINK_FOREACH(ptr, root_p->serv->servers.head) |
212380e3 AC |
136 | { |
137 | server_p = ptr->data; | |
138 | *pbuf = ' '; | |
139 | if(i < cnt) | |
140 | *(pbuf + 1) = '|'; | |
141 | else | |
142 | *(pbuf + 1) = '`'; | |
143 | ||
144 | *(pbuf + 2) = '-'; | |
145 | *(pbuf + 3) = ' '; | |
146 | dump_map(client_p, server_p, pbuf + 4); | |
147 | ||
148 | i++; | |
149 | } | |
150 | } | |
e1fda0d8 AC |
151 | |
152 | /* | |
153 | * flattened_map - display a version of map that doesn't give away routing | |
154 | * information to users when flattened links is enabled. | |
155 | */ | |
156 | static void | |
157 | flattened_map(struct Client *client_p) | |
158 | { | |
159 | char buf[BUFSIZE]; | |
160 | rb_dlink_node *ptr; | |
161 | struct Client *target_p; | |
162 | int i, len; | |
163 | int cnt = 0; | |
164 | ||
165 | /* First display me as the root */ | |
166 | rb_strlcpy(buf, me.name, BUFSIZE); | |
167 | len = strlen(buf); | |
168 | buf[len] = ' '; | |
169 | ||
170 | if(len < USER_COL) | |
171 | { | |
172 | for (i = len + 1; i < USER_COL; i++) | |
173 | { | |
174 | buf[i] = '-'; | |
175 | } | |
176 | } | |
177 | ||
178 | snprintf(buf + USER_COL, BUFSIZE - USER_COL, | |
179 | " | Users: %5lu (%4.1f%%)", rb_dlink_list_length(&me.serv->users), | |
180 | 100 * (float) rb_dlink_list_length(&me.serv->users) / (float) Count.total); | |
181 | ||
182 | sendto_one_numeric(client_p, RPL_MAP, form_str(RPL_MAP), buf); | |
183 | ||
184 | /* Next, we run through every other server and list them */ | |
185 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
186 | { | |
187 | target_p = ptr->data; | |
188 | ||
189 | cnt++; | |
190 | ||
191 | /* Skip ourselves, it's already counted */ | |
192 | if(IsMe(target_p)) | |
193 | continue; | |
194 | ||
195 | /* if we're hidden, go on to the next leaf */ | |
196 | if(!ConfigServerHide.disable_hidden && IsHidden(target_p)) | |
197 | continue; | |
198 | ||
199 | if (cnt == rb_dlink_list_length(&global_serv_list)) | |
200 | rb_strlcpy(buf, " `- ", BUFSIZE); | |
201 | else | |
202 | rb_strlcpy(buf, " |- ", BUFSIZE); | |
203 | ||
0e06053c | 204 | rb_strlcat(buf, target_p->name, BUFSIZE); |
e1fda0d8 AC |
205 | len = strlen(buf); |
206 | buf[len] = ' '; | |
207 | ||
208 | if(len < USER_COL) | |
209 | { | |
210 | for (i = len + 1; i < USER_COL; i++) | |
211 | { | |
212 | buf[i] = '-'; | |
213 | } | |
214 | } | |
215 | ||
216 | snprintf(buf + USER_COL, BUFSIZE - USER_COL, | |
217 | " | Users: %5lu (%4.1f%%)", rb_dlink_list_length(&target_p->serv->users), | |
218 | 100 * (float) rb_dlink_list_length(&target_p->serv->users) / (float) Count.total); | |
219 | ||
220 | sendto_one_numeric(client_p, RPL_MAP, form_str(RPL_MAP), buf); | |
221 | } | |
222 | } |