]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * m_links.c: Shows what servers are currently connected. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
212380e3 AC |
23 | */ |
24 | ||
25 | #include "stdinc.h" | |
26 | #include "client.h" | |
4562c604 | 27 | #include "match.h" |
212380e3 AC |
28 | #include "ircd.h" |
29 | #include "numeric.h" | |
30 | #include "s_serv.h" | |
31 | #include "send.h" | |
32 | #include "s_conf.h" | |
33 | #include "msg.h" | |
34 | #include "parse.h" | |
35 | #include "modules.h" | |
36 | #include "hook.h" | |
994544c2 | 37 | #include "scache.h" |
77d3d2db | 38 | #include "s_assert.h" |
212380e3 | 39 | |
428ca87b AC |
40 | static int m_links(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
41 | static int mo_links(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
e3b33fe3 | 42 | static char * clean_string(char *dest, const unsigned char *src, size_t len); |
212380e3 AC |
43 | |
44 | struct Message links_msgtab = { | |
7baa37a9 | 45 | "LINKS", 0, 0, 0, 0, |
212380e3 AC |
46 | {mg_unreg, {m_links, 0}, {mo_links, 0}, mg_ignore, mg_ignore, {mo_links, 0}} |
47 | }; | |
48 | ||
49 | int doing_links_hook; | |
50 | ||
51 | mapi_clist_av1 links_clist[] = { &links_msgtab, NULL }; | |
52 | mapi_hlist_av1 links_hlist[] = { | |
53 | { "doing_links", &doing_links_hook }, | |
54 | { NULL, NULL } | |
55 | }; | |
56 | ||
f5ebe640 EM |
57 | static const char links_desc[] = |
58 | "Provides the LINKS command to view servers linked to the host server"; | |
59 | ||
60 | DECLARE_MODULE_AV2(links, NULL, NULL, links_clist, links_hlist, NULL, NULL, NULL, links_desc); | |
212380e3 | 61 | |
212380e3 AC |
62 | /* |
63 | * m_links - LINKS message handler | |
212380e3 AC |
64 | * parv[1] = servername mask |
65 | * or | |
55abcbb2 | 66 | * parv[1] = server to query |
212380e3 AC |
67 | * parv[2] = servername mask |
68 | */ | |
69 | static int | |
428ca87b | 70 | m_links(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
71 | { |
72 | if(ConfigServerHide.flatten_links && !IsExemptShide(source_p)) | |
994544c2 | 73 | scache_send_flattened_links(source_p); |
212380e3 | 74 | else |
428ca87b | 75 | mo_links(msgbuf_p, client_p, source_p, parc, parv); |
212380e3 AC |
76 | |
77 | return 0; | |
78 | } | |
79 | ||
80 | static int | |
428ca87b | 81 | mo_links(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
82 | { |
83 | const char *mask = ""; | |
84 | struct Client *target_p; | |
85 | char clean_mask[2 * HOSTLEN + 4]; | |
48a2b7c1 | 86 | hook_cdata hd; |
212380e3 | 87 | |
5b96d9a6 | 88 | rb_dlink_node *ptr; |
212380e3 AC |
89 | |
90 | if(parc > 2) | |
91 | { | |
114105b4 JT |
92 | if(strlen(parv[2]) > HOSTLEN) |
93 | return 0; | |
212380e3 AC |
94 | if(hunt_server(client_p, source_p, ":%s LINKS %s :%s", 1, parc, parv) |
95 | != HUNTED_ISME) | |
96 | return 0; | |
97 | ||
98 | mask = parv[2]; | |
99 | } | |
100 | else if(parc == 2) | |
101 | mask = parv[1]; | |
102 | ||
103 | if(*mask) /* only necessary if there is a mask */ | |
104 | mask = collapse(clean_string | |
105 | (clean_mask, (const unsigned char *) mask, 2 * HOSTLEN)); | |
106 | ||
107 | hd.client = source_p; | |
108 | hd.arg1 = mask; | |
109 | hd.arg2 = NULL; | |
110 | ||
111 | call_hook(doing_links_hook, &hd); | |
112 | ||
5b96d9a6 | 113 | RB_DLINK_FOREACH(ptr, global_serv_list.head) |
212380e3 AC |
114 | { |
115 | target_p = ptr->data; | |
116 | ||
117 | if(*mask && !match(mask, target_p->name)) | |
118 | continue; | |
119 | ||
120 | /* We just send the reply, as if theyre here theres either no SHIDE, | |
55abcbb2 | 121 | * or theyre an oper.. |
212380e3 AC |
122 | */ |
123 | sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS), | |
66c8fdd2 | 124 | target_p->name, target_p->servptr->name, |
212380e3 AC |
125 | target_p->hopcount, |
126 | target_p->info[0] ? target_p->info : "(Unknown Location)"); | |
127 | } | |
128 | ||
129 | sendto_one_numeric(source_p, RPL_ENDOFLINKS, form_str(RPL_ENDOFLINKS), | |
130 | EmptyString(mask) ? "*" : mask); | |
131 | ||
132 | return 0; | |
133 | } | |
134 | ||
e3b33fe3 VY |
135 | static char * |
136 | clean_string(char *dest, const unsigned char *src, size_t len) | |
137 | { | |
138 | char *d = dest; | |
139 | s_assert(0 != dest); | |
140 | s_assert(0 != src); | |
141 | ||
142 | if(dest == NULL || src == NULL) | |
143 | return NULL; | |
144 | ||
114105b4 | 145 | while (*src && (len > 1)) |
e3b33fe3 VY |
146 | { |
147 | if(*src & 0x80) /* if high bit is set */ | |
148 | { | |
149 | *d++ = '.'; | |
150 | --len; | |
114105b4 JT |
151 | if(len <= 1) |
152 | break; | |
e3b33fe3 VY |
153 | } |
154 | else if(!IsPrint(*src)) /* if NOT printable */ | |
155 | { | |
156 | *d++ = '^'; | |
157 | --len; | |
114105b4 JT |
158 | if(len <= 1) |
159 | break; | |
e3b33fe3 VY |
160 | *d++ = 0x40 + *src; /* turn it into a printable */ |
161 | } | |
162 | else | |
163 | *d++ = *src; | |
164 | ++src; | |
165 | --len; | |
166 | } | |
167 | *d = '\0'; | |
168 | return dest; | |
169 | } |