Mercurial > hg
annotate mcabber/src/utils.c @ 495:26f3c7695fe1 0.7.0
Ready for 0.7.0 release
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 09 Oct 2005 22:05:11 +0200 |
parents | 339e85418b49 |
children | 05c0e55c4bb1 |
rev | line source |
---|---|
241 | 1 /* |
2 * utils.c -- Various utility functions | |
393 | 3 * |
241 | 4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> |
5 * ut_* functions are derived from Cabber debug/log code. | |
6 * from_iso8601() comes from the Gaim project. | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or (at | |
11 * your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
21 * USA | |
22 */ | |
23 | |
24 | 24 #include <stdio.h> |
25 #include <stdlib.h> | |
77 | 26 #include <string.h> |
24 | 27 #include <stdarg.h> |
28 #include <time.h> | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
29 #include <unistd.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
30 #include <sys/types.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
31 #include <sys/stat.h> |
452 | 32 #include <ctype.h> |
24 | 33 |
241 | 34 #include <config.h> |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
35 #include "logprint.h" |
241 | 36 |
76 | 37 static int DebugEnabled; |
77 | 38 static char *FName; |
24 | 39 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
241
diff
changeset
|
40 void ut_InitDebug(unsigned int level, const char *filename) |
24 | 41 { |
77 | 42 FILE *fp; |
428
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
43 struct stat buf; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
44 int err; |
77 | 45 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
46 if (level < 1) { |
138 | 47 DebugEnabled = 0; |
48 FName = NULL; | |
49 return; | |
50 } | |
51 | |
52 if (filename) | |
53 FName = strdup(filename); | |
77 | 54 else { |
138 | 55 FName = getenv("HOME"); |
56 if (!FName) | |
57 FName = "/tmp/mcabberlog"; | |
58 else { | |
59 char *tmpname = malloc(strlen(FName) + 12); | |
60 strcpy(tmpname, FName); | |
61 strcat(tmpname, "/mcabberlog"); | |
62 FName = tmpname; | |
63 } | |
77 | 64 } |
24 | 65 |
66 DebugEnabled = level; | |
76 | 67 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
68 fp = fopen(FName, "a"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
69 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
70 fprintf(stderr, "ERROR: Cannot open tracelog file\n"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
71 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
72 } |
428
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
73 |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
74 err = fstat(fileno(fp), &buf); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
75 if (err || buf.st_uid != geteuid()) { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
76 fclose(fp); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
77 DebugEnabled = 0; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
78 FName = NULL; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
79 if (err) { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
80 fprintf(stderr, "ERROR: cannot stat the tracelog file!\n"); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
81 } else { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
82 fprintf(stderr, "ERROR: tracelog file does not belong to you!\n"); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
83 } |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
84 return; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
85 } |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
86 fchmod(fileno(fp), S_IRUSR|S_IWUSR); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
87 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
88 fprintf(fp, "New trace log started.\n" |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
89 "----------------------\n"); |
24 | 90 fclose(fp); |
91 } | |
92 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
93 void ut_WriteLog(unsigned int flag, const char *data) |
24 | 94 { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
95 if (!DebugEnabled || !FName) return; |
393 | 96 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
97 if (((DebugEnabled == 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
98 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
99 FILE *fp = fopen(FName, "a+"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
100 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
101 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
102 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
103 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
104 fputs(data, fp); |
24 | 105 fclose(fp); |
106 } | |
107 } | |
108 | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
109 // checkset_perm(name, setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 // Check the permissions of the "name" file/dir |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 // If setmode is true, correct the permissions if they are wrong |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
112 // Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
113 int checkset_perm(const char *name, unsigned int setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
114 { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
115 int fd; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
116 struct stat buf; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
117 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
118 fd = lstat(name, &buf); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 if (fd == -1) return -1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
121 if (buf.st_uid != geteuid()) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
122 scr_LogPrint(LPRINT_LOGNORM, "Wrong file owner [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
123 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
124 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
125 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
126 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) || |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
127 buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
128 if (setmode) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
129 mode_t newmode = 0; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
130 scr_LogPrint(LPRINT_LOGNORM, "Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
131 if (S_ISDIR(buf.st_mode)) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
132 newmode |= S_IXUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
133 newmode |= S_IRUSR | S_IWUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
134 if (chmod(name, newmode)) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
135 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Failed to correct permissions!"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
136 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
137 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
138 scr_LogPrint(LPRINT_LOGNORM, "Permissions have been corrected"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
139 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
140 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
141 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
142 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
143 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
144 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
145 return 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
146 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
147 |
241 | 148 // to_iso8601(dststr, timestamp) |
149 // Convert timestamp to iso8601 format, and store it in dststr. | |
150 // NOTE: dststr should be at last 19 chars long. | |
151 // Return should be 0 | |
152 int to_iso8601(char *dststr, time_t timestamp) | |
153 { | |
154 struct tm *tm_time; | |
155 int ret; | |
156 | |
157 tm_time = gmtime(×tamp); | |
158 | |
159 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", | |
160 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, | |
161 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); | |
162 | |
163 return ((ret == -1) ? -1 : 0); | |
164 } | |
165 | |
166 // from_iso8601(timestamp, utc) | |
167 // This function comes from the Gaim project, gaim_str_to_time(). | |
168 // (Actually date may not be pure iso-8601) | |
169 // Thanks, guys! | |
170 time_t from_iso8601(const char *timestamp, int utc) | |
171 { | |
172 struct tm t; | |
173 time_t retval = 0; | |
174 char buf[32]; | |
175 char *c; | |
176 int tzoff = 0; | |
177 | |
178 time(&retval); | |
179 localtime_r(&retval, &t); | |
180 | |
463
339e85418b49
from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents:
456
diff
changeset
|
181 /* Reset time to midnight (00:00:00) */ |
339e85418b49
from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents:
456
diff
changeset
|
182 t.tm_hour = t.tm_min = t.tm_sec = 0; |
339e85418b49
from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents:
456
diff
changeset
|
183 |
241 | 184 snprintf(buf, sizeof(buf), "%s", timestamp); |
185 c = buf; | |
186 | |
187 /* 4 digit year */ | |
188 if (!sscanf(c, "%04d", &t.tm_year)) return 0; | |
189 c+=4; | |
190 if (*c == '-') | |
191 c++; | |
192 | |
193 t.tm_year -= 1900; | |
194 | |
195 /* 2 digit month */ | |
196 if (!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
197 c+=2; | |
198 if (*c == '-') | |
199 c++; | |
200 | |
201 t.tm_mon -= 1; | |
202 | |
203 /* 2 digit day */ | |
204 if (!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
205 c+=2; | |
206 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
207 c++; /* skip the "T" */ | |
208 | |
209 /* 2 digit hour */ | |
210 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || | |
211 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { | |
212 int tzhrs, tzmins; | |
213 c+=8; | |
214 if (*c == '.') /* dealing with precision we don't care about */ | |
215 c += 4; | |
216 | |
217 if ((*c == '+' || *c == '-') && | |
218 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
219 tzoff = tzhrs*60*60 + tzmins*60; | |
220 if (*c == '+') | |
221 tzoff *= -1; | |
222 } | |
223 | |
224 if (tzoff || utc) { | |
225 | |
226 //#ifdef HAVE_TM_GMTOFF | |
227 tzoff += t.tm_gmtoff; | |
228 //#else | |
229 //# ifdef HAVE_TIMEZONE | |
230 // tzset(); /* making sure */ | |
231 // tzoff -= timezone; | |
232 //# endif | |
233 //#endif | |
234 } | |
235 } | |
236 } | |
237 | |
238 t.tm_isdst = -1; | |
239 | |
240 retval = mktime(&t); | |
241 | |
242 retval += tzoff; | |
243 | |
244 return retval; | |
245 } | |
419 | 246 |
247 // Should only be used for delays < 1s | |
248 inline void safe_usleep(unsigned int usec) | |
249 { | |
250 struct timespec req; | |
251 req.tv_sec = 0; | |
252 req.tv_nsec = (long)usec * 1000L; | |
253 nanosleep(&req, NULL); | |
254 } | |
452 | 255 |
256 /** | |
257 * Derived from libjabber/jid.c, because the libjabber version is not | |
258 * really convenient for our usage. | |
259 * | |
260 * Check if the full JID is valid | |
261 * Return 0 if it is valid, non zero otherwise | |
262 */ | |
263 int check_jid_syntax(char *jid) | |
264 { | |
265 char *str; | |
266 char *domain, *resource; | |
267 int domlen; | |
268 | |
269 if (!jid) return 1; | |
270 | |
271 domain = strchr(jid, '@'); | |
272 | |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
273 /* the username is optional */ |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
274 if (!domain) { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
275 domain = jid; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
276 } else { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
277 /* node identifiers may not be longer than 1023 bytes */ |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
278 if ((domain == jid) || (domain-jid > 1023)) |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
279 return 1; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
280 domain++; |
452 | 281 |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
282 /* check for low and invalid ascii characters in the username */ |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
283 for (str = jid; *str != '@'; str++) { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
284 if (*str <= 32 || *str == ':' || *str == '@' || |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
285 *str == '<' || *str == '>' || *str == '\'' || |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
286 *str == '"' || *str == '&') { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
287 return 1; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
288 } |
452 | 289 } |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
290 /* the username is okay as far as we can tell without LIBIDN */ |
452 | 291 } |
292 | |
293 resource = strchr(domain, '/'); | |
294 | |
295 /* the resource is optional */ | |
296 if (resource) { | |
297 domlen = resource - domain; | |
298 resource++; | |
299 /* resources may not be longer than 1023 bytes */ | |
300 if ((*resource == '\0') || strlen(resource) > 1023) | |
301 return 1; | |
302 } else { | |
303 domlen = strlen(domain); | |
304 } | |
305 | |
306 /* there must be a domain identifier */ | |
307 if (domlen == 0) return 1; | |
308 | |
309 /* and it must not be longer than 1023 bytes */ | |
310 if (domlen > 1023) return 1; | |
311 | |
312 /* make sure the hostname is valid characters */ | |
313 for (str = domain; *str != '\0' && *str != '/'; str++) { | |
314 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_')) | |
315 return 1; | |
316 } | |
317 | |
318 /* it's okay as far as we can tell without LIBIDN */ | |
319 return 0; | |
320 } | |
321 | |
322 void mc_strtolower(char *str) | |
323 { | |
324 if (!str) return; | |
325 for ( ; *str; str++) | |
326 *str = tolower(*str); | |
327 } |