]> jfr.im git - solanum.git/blob - src/irc_string.c
no need for inetntop* now - removed
[solanum.git] / src / irc_string.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * irc_string.c: IRC string functions.
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: irc_string.c 678 2006-02-03 20:25:01Z jilles $
25 */
26
27 #include "stdinc.h"
28 #include "sprintf_irc.h"
29 #include "irc_string.h"
30 #include "client.h"
31 #include "setup.h"
32
33 #ifndef INADDRSZ
34 #define INADDRSZ 4
35 #endif
36
37 #ifdef RB_IPV6
38 #ifndef IN6ADDRSZ
39 #define IN6ADDRSZ 16
40 #endif
41 #endif
42
43 #ifndef INT16SZ
44 #define INT16SZ 2
45 #endif
46 /*
47 * myctime - This is like standard ctime()-function, but it zaps away
48 * the newline from the end of that string. Also, it takes
49 * the time value as parameter, instead of pointer to it.
50 * Note that it is necessary to copy the string to alternate
51 * buffer (who knows how ctime() implements it, maybe it statically
52 * has newline there and never 'refreshes' it -- zapping that
53 * might break things in other places...)
54 *
55 *
56 * Thu Nov 24 18:22:48 1986
57 */
58 const char *
59 myctime(time_t value)
60 {
61 static char buf[32];
62 char *p;
63
64 strcpy(buf, ctime(&value));
65 if((p = strchr(buf, '\n')) != NULL)
66 *p = '\0';
67 return buf;
68 }
69
70
71 /*
72 * clean_string - clean up a string possibly containing garbage
73 *
74 * *sigh* Before the kiddies find this new and exciting way of
75 * annoying opers, lets clean up what is sent to local opers
76 * -Dianora
77 */
78 char *
79 clean_string(char *dest, const unsigned char *src, size_t len)
80 {
81 char *d = dest;
82 s_assert(0 != dest);
83 s_assert(0 != src);
84
85 if(dest == NULL || src == NULL)
86 return NULL;
87
88 len -= 3; /* allow for worst case, '^A\0' */
89
90 while(*src && (len > 0))
91 {
92 if(*src & 0x80) /* if high bit is set */
93 {
94 *d++ = '.';
95 --len;
96 }
97 else if(!IsPrint(*src)) /* if NOT printable */
98 {
99 *d++ = '^';
100 --len;
101 *d++ = 0x40 + *src; /* turn it into a printable */
102 }
103 else
104 *d++ = *src;
105 ++src;
106 --len;
107 }
108 *d = '\0';
109 return dest;
110 }
111
112 /*
113 * strip_tabs(dst, src, length)
114 *
115 * Copies src to dst, while converting all \t (tabs) into spaces.
116 *
117 * NOTE: jdc: I have a gut feeling there's a faster way to do this.
118 */
119 char *
120 strip_tabs(char *dest, const unsigned char *src, size_t len)
121 {
122 char *d = dest;
123 /* Sanity check; we don't want anything nasty... */
124 s_assert(0 != dest);
125 s_assert(0 != src);
126
127 if(dest == NULL || src == NULL)
128 return NULL;
129
130 while(*src && (len > 0))
131 {
132 if(*src == '\t')
133 {
134 *d++ = ' '; /* Translate the tab into a space */
135 }
136 else
137 {
138 *d++ = *src; /* Copy src to dst */
139 }
140 ++src;
141 --len;
142 }
143 *d = '\0'; /* Null terminate, thanks and goodbye */
144 return dest;
145 }
146
147 /*
148 * strtoken - walk through a string of tokens, using a set of separators
149 * argv 9/90
150 *
151 */
152 char *
153 strtoken(char **save, char *str, const char *fs)
154 {
155 char *pos = *save; /* keep last position across calls */
156 char *tmp;
157
158 if(str)
159 pos = str; /* new string scan */
160
161 while(pos && *pos && strchr(fs, *pos) != NULL)
162 ++pos; /* skip leading separators */
163
164 if(!pos || !*pos)
165 return (pos = *save = NULL); /* string contains only sep's */
166
167 tmp = pos; /* now, keep position of the token */
168
169 while(*pos && strchr(fs, *pos) == NULL)
170 ++pos; /* skip content of the token */
171
172 if(*pos)
173 *pos++ = '\0'; /* remove first sep after the token */
174 else
175 pos = NULL; /* end of string */
176
177 *save = pos;
178 return tmp;
179 }
180
181 /*
182 * Copyright (c) 1996-1999 by Internet Software Consortium.
183 *
184 * Permission to use, copy, modify, and distribute this software for any
185 * purpose with or without fee is hereby granted, provided that the above
186 * copyright notice and this permission notice appear in all copies.
187 *
188 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
189 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
190 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
191 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
192 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
193 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
194 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
195 * SOFTWARE.
196 */
197
198 #define SPRINTF(x) ((size_t)rb_sprintf x)
199
200 char *
201 strip_colour(char *string)
202 {
203 char *c = string;
204 char *c2 = string;
205 char *last_non_space = NULL;
206 /* c is source, c2 is target */
207 for(; c && *c; c++)
208 switch (*c)
209 {
210 case 3:
211 if(isdigit(c[1]))
212 {
213 c++;
214 if(isdigit(c[1]))
215 c++;
216 if(c[1] == ',' && isdigit(c[2]))
217 {
218 c += 2;
219 if(isdigit(c[1]))
220 c++;
221 }
222 }
223 break;
224 case 2:
225 case 6:
226 case 7:
227 case 22:
228 case 23:
229 case 27:
230 case 31:
231 break;
232 case 32:
233 *c2++ = *c;
234 break;
235 default:
236 *c2++ = *c;
237 last_non_space = c2;
238 break;
239 }
240 *c2 = '\0';
241 if(last_non_space)
242 *last_non_space = '\0';
243 return string;
244 }