Mercurial > hg
annotate mcabber/src/utils.c @ 547:1df26ff0ed8c
Break packethandler() out
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sun, 27 Nov 2005 00:19:07 +0100 |
parents | 05c0e55c4bb1 |
children | c71699efa5cc |
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 |
524
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
148 const char *ut_get_tmpdir(void) |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
149 { |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
150 static const char *tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
151 const char *tmpvars[] = { "MCABBERTMPDIR", "TMP", "TMPDIR", "TEMP" }; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
152 int i; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
153 |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
154 if (tmpdir) |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
155 return tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
156 |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
157 for (i = 0; i < (sizeof(tmpvars) / sizeof(const char *)); i++) { |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
158 tmpdir = getenv(tmpvars[i]); |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
159 if (tmpdir && tmpdir[0] && tmpdir[0] == '/' && tmpdir[1]) { |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
160 // Looks ok. |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
161 return tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
162 } |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
163 } |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
164 |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
165 // Default temporary directory |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
166 tmpdir = "/tmp"; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
167 return tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
168 } |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
169 |
241 | 170 // to_iso8601(dststr, timestamp) |
171 // Convert timestamp to iso8601 format, and store it in dststr. | |
172 // NOTE: dststr should be at last 19 chars long. | |
173 // Return should be 0 | |
174 int to_iso8601(char *dststr, time_t timestamp) | |
175 { | |
176 struct tm *tm_time; | |
177 int ret; | |
178 | |
179 tm_time = gmtime(×tamp); | |
180 | |
181 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", | |
182 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, | |
183 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); | |
184 | |
185 return ((ret == -1) ? -1 : 0); | |
186 } | |
187 | |
188 // from_iso8601(timestamp, utc) | |
189 // This function comes from the Gaim project, gaim_str_to_time(). | |
190 // (Actually date may not be pure iso-8601) | |
191 // Thanks, guys! | |
192 time_t from_iso8601(const char *timestamp, int utc) | |
193 { | |
194 struct tm t; | |
195 time_t retval = 0; | |
196 char buf[32]; | |
197 char *c; | |
198 int tzoff = 0; | |
199 | |
200 time(&retval); | |
201 localtime_r(&retval, &t); | |
202 | |
463
339e85418b49
from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents:
456
diff
changeset
|
203 /* 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
|
204 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
|
205 |
241 | 206 snprintf(buf, sizeof(buf), "%s", timestamp); |
207 c = buf; | |
208 | |
209 /* 4 digit year */ | |
210 if (!sscanf(c, "%04d", &t.tm_year)) return 0; | |
211 c+=4; | |
212 if (*c == '-') | |
213 c++; | |
214 | |
215 t.tm_year -= 1900; | |
216 | |
217 /* 2 digit month */ | |
218 if (!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
219 c+=2; | |
220 if (*c == '-') | |
221 c++; | |
222 | |
223 t.tm_mon -= 1; | |
224 | |
225 /* 2 digit day */ | |
226 if (!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
227 c+=2; | |
228 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
229 c++; /* skip the "T" */ | |
230 | |
231 /* 2 digit hour */ | |
232 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || | |
233 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { | |
234 int tzhrs, tzmins; | |
235 c+=8; | |
236 if (*c == '.') /* dealing with precision we don't care about */ | |
237 c += 4; | |
238 | |
239 if ((*c == '+' || *c == '-') && | |
240 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
241 tzoff = tzhrs*60*60 + tzmins*60; | |
242 if (*c == '+') | |
243 tzoff *= -1; | |
244 } | |
245 | |
246 if (tzoff || utc) { | |
247 | |
248 //#ifdef HAVE_TM_GMTOFF | |
249 tzoff += t.tm_gmtoff; | |
250 //#else | |
251 //# ifdef HAVE_TIMEZONE | |
252 // tzset(); /* making sure */ | |
253 // tzoff -= timezone; | |
254 //# endif | |
255 //#endif | |
256 } | |
257 } | |
258 } | |
259 | |
260 t.tm_isdst = -1; | |
261 | |
262 retval = mktime(&t); | |
263 | |
264 retval += tzoff; | |
265 | |
266 return retval; | |
267 } | |
419 | 268 |
269 // Should only be used for delays < 1s | |
270 inline void safe_usleep(unsigned int usec) | |
271 { | |
272 struct timespec req; | |
273 req.tv_sec = 0; | |
274 req.tv_nsec = (long)usec * 1000L; | |
275 nanosleep(&req, NULL); | |
276 } | |
452 | 277 |
278 /** | |
279 * Derived from libjabber/jid.c, because the libjabber version is not | |
280 * really convenient for our usage. | |
281 * | |
282 * Check if the full JID is valid | |
283 * Return 0 if it is valid, non zero otherwise | |
284 */ | |
285 int check_jid_syntax(char *jid) | |
286 { | |
287 char *str; | |
288 char *domain, *resource; | |
289 int domlen; | |
290 | |
291 if (!jid) return 1; | |
292 | |
293 domain = strchr(jid, '@'); | |
294 | |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
295 /* the username is optional */ |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
296 if (!domain) { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
297 domain = jid; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
298 } else { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
299 /* 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
|
300 if ((domain == jid) || (domain-jid > 1023)) |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
301 return 1; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
302 domain++; |
452 | 303 |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
304 /* 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
|
305 for (str = jid; *str != '@'; str++) { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
306 if (*str <= 32 || *str == ':' || *str == '@' || |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
307 *str == '<' || *str == '>' || *str == '\'' || |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
308 *str == '"' || *str == '&') { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
309 return 1; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
310 } |
452 | 311 } |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
312 /* the username is okay as far as we can tell without LIBIDN */ |
452 | 313 } |
314 | |
315 resource = strchr(domain, '/'); | |
316 | |
317 /* the resource is optional */ | |
318 if (resource) { | |
319 domlen = resource - domain; | |
320 resource++; | |
321 /* resources may not be longer than 1023 bytes */ | |
322 if ((*resource == '\0') || strlen(resource) > 1023) | |
323 return 1; | |
324 } else { | |
325 domlen = strlen(domain); | |
326 } | |
327 | |
328 /* there must be a domain identifier */ | |
329 if (domlen == 0) return 1; | |
330 | |
331 /* and it must not be longer than 1023 bytes */ | |
332 if (domlen > 1023) return 1; | |
333 | |
334 /* make sure the hostname is valid characters */ | |
335 for (str = domain; *str != '\0' && *str != '/'; str++) { | |
336 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_')) | |
337 return 1; | |
338 } | |
339 | |
340 /* it's okay as far as we can tell without LIBIDN */ | |
341 return 0; | |
342 } | |
343 | |
344 void mc_strtolower(char *str) | |
345 { | |
346 if (!str) return; | |
347 for ( ; *str; str++) | |
348 *str = tolower(*str); | |
349 } |