1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(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 |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
#include "stdinc.h" |
26 |
#include "client.h" |
27 |
#include "modules.h" |
28 |
#include "handlers.h" |
29 |
#include "numeric.h" |
30 |
#include "send.h" |
31 |
#include "s_conf.h" |
32 |
#include "ircd.h" |
33 |
#include "irc_string.h" |
34 |
#include "sprintf_irc.h" |
35 |
|
36 |
|
37 |
static char buf[IRCD_BUFSIZE]; |
38 |
static void m_map(struct Client *, struct Client *, int, char *[]); |
39 |
static void mo_map(struct Client *, struct Client *, int, char *[]); |
40 |
static void dump_map(struct Client *, const struct Client *, int, char *); |
41 |
|
42 |
struct Message map_msgtab = { |
43 |
"MAP", 0, 0, 0, 0, MFLG_SLOW, 0, |
44 |
{ m_unregistered, m_map, m_ignore, m_ignore, mo_map, m_ignore } |
45 |
}; |
46 |
|
47 |
#ifndef STATIC_MODULES |
48 |
void |
49 |
_modinit(void) |
50 |
{ |
51 |
mod_add_cmd(&map_msgtab); |
52 |
} |
53 |
|
54 |
void |
55 |
_moddeinit(void) |
56 |
{ |
57 |
mod_del_cmd(&map_msgtab); |
58 |
} |
59 |
|
60 |
const char *_version = "$Revision$"; |
61 |
#endif |
62 |
|
63 |
|
64 |
/* m_map() |
65 |
* parv[0] = sender prefix |
66 |
*/ |
67 |
static void |
68 |
m_map(struct Client *client_p, struct Client *source_p, |
69 |
int parc, char *parv[]) |
70 |
{ |
71 |
static time_t last_used = 0; |
72 |
|
73 |
if (ConfigServerHide.flatten_links) |
74 |
{ |
75 |
m_not_oper(client_p, source_p, parc, parv); |
76 |
return; |
77 |
} |
78 |
|
79 |
if ((last_used + ConfigFileEntry.pace_wait) > CurrentTime) |
80 |
{ |
81 |
/* safe enough to give this on a local connect only */ |
82 |
sendto_one(source_p, form_str(RPL_LOAD2HI), |
83 |
me.name, source_p->name); |
84 |
return; |
85 |
} |
86 |
|
87 |
last_used = CurrentTime; |
88 |
|
89 |
dump_map(source_p, &me, 0, buf); |
90 |
sendto_one(source_p, form_str(RPL_MAPEND), me.name, source_p->name); |
91 |
} |
92 |
|
93 |
/* mo_map() |
94 |
* parv[0] = sender prefix |
95 |
*/ |
96 |
static void |
97 |
mo_map(struct Client *client_p, struct Client *source_p, |
98 |
int parc, char *parv[]) |
99 |
{ |
100 |
dump_map(source_p, &me, 0, buf); |
101 |
sendto_one(source_p, form_str(RPL_MAPEND), me.name, source_p->name); |
102 |
} |
103 |
|
104 |
/* dump_map() |
105 |
* dumps server map, called recursively. |
106 |
*/ |
107 |
static void |
108 |
dump_map(struct Client *client_p, const struct Client *root_p, |
109 |
int start_len, char *pbuf) |
110 |
{ |
111 |
int cnt = 0, i = 0, l = 0, len = start_len; |
112 |
int users, dashes; |
113 |
const dlink_node *ptr = NULL; |
114 |
char *pb; |
115 |
|
116 |
*pbuf= '\0'; |
117 |
pb = pbuf; |
118 |
|
119 |
l = ircsprintf(pb, "%s", root_p->name); |
120 |
pb += l; |
121 |
len += l; |
122 |
|
123 |
if (root_p->id[0] != '\0') |
124 |
{ |
125 |
l = ircsprintf(pb, "[%s]", root_p->id); |
126 |
pb += l; |
127 |
len += l; |
128 |
} |
129 |
|
130 |
*pb++ = ' '; |
131 |
len++; |
132 |
dashes = 50 - len; |
133 |
|
134 |
for (i = 0; i < dashes; i++) |
135 |
*pb++ = '-'; |
136 |
|
137 |
*pb++ = ' '; |
138 |
*pb++ = '|'; |
139 |
|
140 |
users = dlink_list_length(&root_p->serv->client_list); |
141 |
|
142 |
sprintf(pb, " Users: %5d (%1.1f%%)", users, |
143 |
100 * (float)users / (float)Count.total); |
144 |
|
145 |
sendto_one(client_p, form_str(RPL_MAP), me.name, client_p->name, buf); |
146 |
|
147 |
if (root_p->serv->server_list.head) |
148 |
{ |
149 |
cnt += dlink_list_length(&root_p->serv->server_list); |
150 |
|
151 |
if (cnt) |
152 |
{ |
153 |
if (pbuf > buf + 3) |
154 |
{ |
155 |
pbuf[-2] = ' '; |
156 |
|
157 |
if (pbuf[-3] == '`') |
158 |
pbuf[-3] = ' '; |
159 |
} |
160 |
} |
161 |
} |
162 |
|
163 |
i = 1; |
164 |
|
165 |
DLINK_FOREACH(ptr, root_p->serv->server_list.head) |
166 |
{ |
167 |
const struct Client *server_p = ptr->data; |
168 |
|
169 |
*pbuf = ' '; |
170 |
|
171 |
if (i < cnt) |
172 |
*(pbuf + 1) = '|'; |
173 |
else |
174 |
*(pbuf + 1) = '`'; |
175 |
|
176 |
*(pbuf + 2) = '-'; |
177 |
*(pbuf + 3) = ' '; |
178 |
dump_map(client_p, server_p, start_len + 4, pbuf + 4); |
179 |
|
180 |
++i; |
181 |
} |
182 |
} |