]> jfr.im git - solanum.git/blame - modules/m_links.c
Merge pull request #149 from anarcat/reproducible
[solanum.git] / modules / m_links.c
CommitLineData
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
23 *
24 * $Id: m_links.c 254 2005-09-21 23:35:12Z nenolod $
25 */
26
27#include "stdinc.h"
28#include "client.h"
4562c604 29#include "match.h"
212380e3
AC
30#include "ircd.h"
31#include "numeric.h"
32#include "s_serv.h"
33#include "send.h"
34#include "s_conf.h"
35#include "msg.h"
36#include "parse.h"
37#include "modules.h"
38#include "hook.h"
994544c2 39#include "scache.h"
77d3d2db 40#include "s_assert.h"
212380e3 41
428ca87b
AC
42static int m_links(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
43static int mo_links(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
e3b33fe3 44static char * clean_string(char *dest, const unsigned char *src, size_t len);
212380e3
AC
45
46struct Message links_msgtab = {
47 "LINKS", 0, 0, 0, MFLG_SLOW,
48 {mg_unreg, {m_links, 0}, {mo_links, 0}, mg_ignore, mg_ignore, {mo_links, 0}}
49};
50
51int doing_links_hook;
52
53mapi_clist_av1 links_clist[] = { &links_msgtab, NULL };
54mapi_hlist_av1 links_hlist[] = {
55 { "doing_links", &doing_links_hook },
56 { NULL, NULL }
57};
58
59DECLARE_MODULE_AV1(links, NULL, NULL, links_clist, links_hlist, NULL, "$Revision: 254 $");
60
212380e3
AC
61/*
62 * m_links - LINKS message handler
212380e3
AC
63 * parv[1] = servername mask
64 * or
55abcbb2 65 * parv[1] = server to query
212380e3
AC
66 * parv[2] = servername mask
67 */
68static int
428ca87b 69m_links(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
70{
71 if(ConfigServerHide.flatten_links && !IsExemptShide(source_p))
994544c2 72 scache_send_flattened_links(source_p);
212380e3 73 else
428ca87b 74 mo_links(msgbuf_p, client_p, source_p, parc, parv);
212380e3
AC
75
76 return 0;
77}
78
79static int
428ca87b 80mo_links(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
212380e3
AC
81{
82 const char *mask = "";
83 struct Client *target_p;
84 char clean_mask[2 * HOSTLEN + 4];
85 hook_data hd;
86
5b96d9a6 87 rb_dlink_node *ptr;
212380e3
AC
88
89 if(parc > 2)
90 {
114105b4
JT
91 if(strlen(parv[2]) > HOSTLEN)
92 return 0;
212380e3
AC
93 if(hunt_server(client_p, source_p, ":%s LINKS %s :%s", 1, parc, parv)
94 != HUNTED_ISME)
95 return 0;
96
97 mask = parv[2];
98 }
99 else if(parc == 2)
100 mask = parv[1];
101
102 if(*mask) /* only necessary if there is a mask */
103 mask = collapse(clean_string
104 (clean_mask, (const unsigned char *) mask, 2 * HOSTLEN));
105
106 hd.client = source_p;
107 hd.arg1 = mask;
108 hd.arg2 = NULL;
109
110 call_hook(doing_links_hook, &hd);
111
5b96d9a6 112 RB_DLINK_FOREACH(ptr, global_serv_list.head)
212380e3
AC
113 {
114 target_p = ptr->data;
115
116 if(*mask && !match(mask, target_p->name))
117 continue;
118
119 /* We just send the reply, as if theyre here theres either no SHIDE,
55abcbb2 120 * or theyre an oper..
212380e3
AC
121 */
122 sendto_one_numeric(source_p, RPL_LINKS, form_str(RPL_LINKS),
66c8fdd2 123 target_p->name, target_p->servptr->name,
212380e3
AC
124 target_p->hopcount,
125 target_p->info[0] ? target_p->info : "(Unknown Location)");
126 }
127
128 sendto_one_numeric(source_p, RPL_ENDOFLINKS, form_str(RPL_ENDOFLINKS),
129 EmptyString(mask) ? "*" : mask);
130
131 return 0;
132}
133
e3b33fe3
VY
134static char *
135clean_string(char *dest, const unsigned char *src, size_t len)
136{
137 char *d = dest;
138 s_assert(0 != dest);
139 s_assert(0 != src);
140
141 if(dest == NULL || src == NULL)
142 return NULL;
143
114105b4 144 while (*src && (len > 1))
e3b33fe3
VY
145 {
146 if(*src & 0x80) /* if high bit is set */
147 {
148 *d++ = '.';
149 --len;
114105b4
JT
150 if(len <= 1)
151 break;
e3b33fe3
VY
152 }
153 else if(!IsPrint(*src)) /* if NOT printable */
154 {
155 *d++ = '^';
156 --len;
114105b4
JT
157 if(len <= 1)
158 break;
e3b33fe3
VY
159 *d++ = 0x40 + *src; /* turn it into a printable */
160 }
161 else
162 *d++ = *src;
163 ++src;
164 --len;
165 }
166 *d = '\0';
167 return dest;
168}