]> jfr.im git - solanum.git/blob - modules/m_links.c
Replace RPL_WHOISTEXT(337) with RPL_WHOISSPECIAL(320) (#419)
[solanum.git] / modules / m_links.c
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
23 */
24
25 #include "stdinc.h"
26 #include "client.h"
27 #include "match.h"
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"
37 #include "scache.h"
38 #include "s_assert.h"
39
40 static const char links_desc[] =
41 "Provides the LINKS command to view servers linked to the host server";
42
43 static void m_links(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
44 static void mo_links(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
45 static char * clean_string(char *dest, const unsigned char *src, size_t len);
46
47 struct Message links_msgtab = {
48 "LINKS", 0, 0, 0, 0,
49 {mg_unreg, {m_links, 0}, {mo_links, 0}, mg_ignore, mg_ignore, {mo_links, 0}}
50 };
51
52 int doing_links_hook;
53
54 mapi_clist_av1 links_clist[] = { &links_msgtab, NULL };
55 mapi_hlist_av1 links_hlist[] = {
56 { "doing_links", &doing_links_hook },
57 { NULL, NULL }
58 };
59
60 DECLARE_MODULE_AV2(links, NULL, NULL, links_clist, links_hlist, NULL, NULL, NULL, links_desc);
61
62 /*
63 * m_links - LINKS message handler
64 * parv[1] = servername mask
65 * or
66 * parv[1] = server to query
67 * parv[2] = servername mask
68 */
69 static void
70 m_links(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
71 {
72 if(ConfigServerHide.flatten_links && !IsExemptShide(source_p))
73 scache_send_flattened_links(source_p);
74 else
75 mo_links(msgbuf_p, client_p, source_p, parc, parv);
76 }
77
78 static void
79 mo_links(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
80 {
81 const char *mask = "";
82 struct Client *target_p;
83 char clean_mask[2 * HOSTLEN + 4];
84 hook_cdata hd;
85
86 rb_dlink_node *ptr;
87
88 if(parc > 2)
89 {
90 if(strlen(parv[2]) > HOSTLEN)
91 return;
92 if(hunt_server(client_p, source_p, ":%s LINKS %s :%s", 1, parc, parv)
93 != HUNTED_ISME)
94 return;
95
96 mask = parv[2];
97 }
98 else if(parc == 2)
99 mask = parv[1];
100
101 if(*mask) /* only necessary if there is a mask */
102 mask = collapse(clean_string
103 (clean_mask, (const unsigned char *) mask, 2 * HOSTLEN));
104
105 hd.client = source_p;
106 hd.arg1 = mask;
107 hd.arg2 = NULL;
108
109 call_hook(doing_links_hook, &hd);
110
111 RB_DLINK_FOREACH(ptr, global_serv_list.head)
112 {
113 target_p = ptr->data;
114
115 if(*mask && !match(mask, target_p->name))
116 continue;
117
118 /* We just send the reply, as if theyre here theres either no SHIDE,
119 * or theyre an oper..
120 */
121 sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS),
122 target_p->name, target_p->servptr->name,
123 target_p->hopcount,
124 target_p->info[0] ? target_p->info : "(Unknown Location)");
125 }
126
127 sendto_one_numeric(source_p, RPL_ENDOFLINKS, form_str(RPL_ENDOFLINKS),
128 EmptyString(mask) ? "*" : mask);
129 }
130
131 static char *
132 clean_string(char *dest, const unsigned char *src, size_t len)
133 {
134 char *d = dest;
135 s_assert(0 != dest);
136 s_assert(0 != src);
137
138 if(dest == NULL || src == NULL)
139 return NULL;
140
141 while (*src && (len > 1))
142 {
143 if(*src & 0x80) /* if high bit is set */
144 {
145 *d++ = '.';
146 --len;
147 if(len <= 1)
148 break;
149 }
150 else if(!IsPrint(*src)) /* if NOT printable */
151 {
152 *d++ = '^';
153 --len;
154 if(len <= 1)
155 break;
156 *d++ = 0x40 + *src; /* turn it into a printable */
157 }
158 else
159 *d++ = *src;
160 ++src;
161 --len;
162 }
163 *d = '\0';
164 return dest;
165 }