]>
Commit | Line | Data |
---|---|---|
212380e3 | 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" | |
212380e3 | 29 | #include "irc_string.h" |
30 | #include "client.h" | |
212380e3 | 31 | #include "setup.h" |
32 | ||
212380e3 | 33 | #ifndef INT16SZ |
34 | #define INT16SZ 2 | |
35 | #endif | |
212380e3 | 36 | |
37 | /* | |
38 | * clean_string - clean up a string possibly containing garbage | |
39 | * | |
40 | * *sigh* Before the kiddies find this new and exciting way of | |
41 | * annoying opers, lets clean up what is sent to local opers | |
42 | * -Dianora | |
43 | */ | |
44 | char * | |
45 | clean_string(char *dest, const unsigned char *src, size_t len) | |
46 | { | |
47 | char *d = dest; | |
48 | s_assert(0 != dest); | |
49 | s_assert(0 != src); | |
50 | ||
51 | if(dest == NULL || src == NULL) | |
52 | return NULL; | |
53 | ||
54 | len -= 3; /* allow for worst case, '^A\0' */ | |
55 | ||
56 | while(*src && (len > 0)) | |
57 | { | |
58 | if(*src & 0x80) /* if high bit is set */ | |
59 | { | |
60 | *d++ = '.'; | |
61 | --len; | |
62 | } | |
63 | else if(!IsPrint(*src)) /* if NOT printable */ | |
64 | { | |
65 | *d++ = '^'; | |
66 | --len; | |
67 | *d++ = 0x40 + *src; /* turn it into a printable */ | |
68 | } | |
69 | else | |
70 | *d++ = *src; | |
71 | ++src; | |
72 | --len; | |
73 | } | |
74 | *d = '\0'; | |
75 | return dest; | |
76 | } | |
77 | ||
78 | /* | |
79 | * strip_tabs(dst, src, length) | |
80 | * | |
81 | * Copies src to dst, while converting all \t (tabs) into spaces. | |
82 | * | |
83 | * NOTE: jdc: I have a gut feeling there's a faster way to do this. | |
84 | */ | |
85 | char * | |
86 | strip_tabs(char *dest, const unsigned char *src, size_t len) | |
87 | { | |
88 | char *d = dest; | |
89 | /* Sanity check; we don't want anything nasty... */ | |
90 | s_assert(0 != dest); | |
91 | s_assert(0 != src); | |
92 | ||
93 | if(dest == NULL || src == NULL) | |
94 | return NULL; | |
95 | ||
96 | while(*src && (len > 0)) | |
97 | { | |
98 | if(*src == '\t') | |
99 | { | |
100 | *d++ = ' '; /* Translate the tab into a space */ | |
101 | } | |
102 | else | |
103 | { | |
104 | *d++ = *src; /* Copy src to dst */ | |
105 | } | |
106 | ++src; | |
107 | --len; | |
108 | } | |
109 | *d = '\0'; /* Null terminate, thanks and goodbye */ | |
110 | return dest; | |
111 | } | |
112 | ||
113 | /* | |
114 | * strtoken - walk through a string of tokens, using a set of separators | |
115 | * argv 9/90 | |
116 | * | |
117 | */ | |
118 | char * | |
119 | strtoken(char **save, char *str, const char *fs) | |
120 | { | |
121 | char *pos = *save; /* keep last position across calls */ | |
122 | char *tmp; | |
123 | ||
124 | if(str) | |
125 | pos = str; /* new string scan */ | |
126 | ||
127 | while(pos && *pos && strchr(fs, *pos) != NULL) | |
128 | ++pos; /* skip leading separators */ | |
129 | ||
130 | if(!pos || !*pos) | |
131 | return (pos = *save = NULL); /* string contains only sep's */ | |
132 | ||
133 | tmp = pos; /* now, keep position of the token */ | |
134 | ||
135 | while(*pos && strchr(fs, *pos) == NULL) | |
136 | ++pos; /* skip content of the token */ | |
137 | ||
138 | if(*pos) | |
139 | *pos++ = '\0'; /* remove first sep after the token */ | |
140 | else | |
141 | pos = NULL; /* end of string */ | |
142 | ||
143 | *save = pos; | |
144 | return tmp; | |
145 | } | |
146 | ||
212380e3 | 147 | /* |
148 | * Copyright (c) 1996-1999 by Internet Software Consortium. | |
149 | * | |
150 | * Permission to use, copy, modify, and distribute this software for any | |
151 | * purpose with or without fee is hereby granted, provided that the above | |
152 | * copyright notice and this permission notice appear in all copies. | |
153 | * | |
154 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
155 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
156 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
157 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
158 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
159 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
160 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
161 | * SOFTWARE. | |
162 | */ | |
163 | ||
38e6acdd | 164 | #define SPRINTF(x) ((size_t)rb_sprintf x) |
212380e3 | 165 | |
212380e3 | 166 | char * |
167 | strip_colour(char *string) | |
168 | { | |
169 | char *c = string; | |
170 | char *c2 = string; | |
171 | char *last_non_space = NULL; | |
172 | /* c is source, c2 is target */ | |
173 | for(; c && *c; c++) | |
174 | switch (*c) | |
175 | { | |
176 | case 3: | |
177 | if(isdigit(c[1])) | |
178 | { | |
179 | c++; | |
180 | if(isdigit(c[1])) | |
181 | c++; | |
182 | if(c[1] == ',' && isdigit(c[2])) | |
183 | { | |
184 | c += 2; | |
185 | if(isdigit(c[1])) | |
186 | c++; | |
187 | } | |
188 | } | |
189 | break; | |
190 | case 2: | |
191 | case 6: | |
192 | case 7: | |
193 | case 22: | |
194 | case 23: | |
195 | case 27: | |
196 | case 31: | |
197 | break; | |
198 | case 32: | |
199 | *c2++ = *c; | |
200 | break; | |
201 | default: | |
202 | *c2++ = *c; | |
203 | last_non_space = c2; | |
204 | break; | |
205 | } | |
206 | *c2 = '\0'; | |
207 | if(last_non_space) | |
208 | *last_non_space = '\0'; | |
209 | return string; | |
210 | } |