Mercurial > hg
annotate mcabber/src/utils.c @ 622:2b7ef605ddc6
Add /version command
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Sat, 17 Dec 2005 00:36:57 +0100 |
parents | fed6d1e4d7a9 |
children | de6837908702 |
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> |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
33 #include <glib.h> |
24 | 34 |
241 | 35 #include <config.h> |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
36 #include "logprint.h" |
241 | 37 |
76 | 38 static int DebugEnabled; |
77 | 39 static char *FName; |
24 | 40 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
241
diff
changeset
|
41 void ut_InitDebug(unsigned int level, const char *filename) |
24 | 42 { |
77 | 43 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
|
44 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
|
45 int err; |
77 | 46 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
47 if (level < 1) { |
138 | 48 DebugEnabled = 0; |
49 FName = NULL; | |
50 return; | |
51 } | |
52 | |
53 if (filename) | |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
54 FName = g_strdup(filename); |
77 | 55 else { |
138 | 56 FName = getenv("HOME"); |
57 if (!FName) | |
58 FName = "/tmp/mcabberlog"; | |
59 else { | |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
60 char *tmpname = g_new(char, strlen(FName) + 12); |
138 | 61 strcpy(tmpname, FName); |
62 strcat(tmpname, "/mcabberlog"); | |
63 FName = tmpname; | |
64 } | |
77 | 65 } |
24 | 66 |
67 DebugEnabled = level; | |
76 | 68 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
69 fp = fopen(FName, "a"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
70 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
71 fprintf(stderr, "ERROR: Cannot open tracelog file\n"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
72 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
73 } |
428
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
74 |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
75 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
|
76 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
|
77 fclose(fp); |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
78 DebugEnabled = 0; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
79 FName = NULL; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
80 if (err) { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
81 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
|
82 } else { |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
83 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
|
84 } |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
85 return; |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
86 } |
4470868f90e5
Check the tracelog file actually belongs to the user (if logging enabled)
Mikael Berthe <mikael@lilotux.net>
parents:
419
diff
changeset
|
87 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
|
88 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
89 fprintf(fp, "New trace log started.\n" |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
90 "----------------------\n"); |
24 | 91 fclose(fp); |
92 } | |
93 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
94 void ut_WriteLog(unsigned int flag, const char *data) |
24 | 95 { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
96 if (!DebugEnabled || !FName) return; |
393 | 97 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
98 if (((DebugEnabled == 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
99 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
100 FILE *fp = fopen(FName, "a+"); |
378
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
101 if (!fp) { |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
102 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file"); |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
103 return; |
2e6c7b1440d1
Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents:
374
diff
changeset
|
104 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
105 fputs(data, fp); |
24 | 106 fclose(fp); |
107 } | |
108 } | |
109 | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 // checkset_perm(name, setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 // Check the permissions of the "name" file/dir |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
112 // 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
|
113 // 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
|
114 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
|
115 { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
116 int fd; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
117 struct stat buf; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
118 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 fd = lstat(name, &buf); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 if (fd == -1) return -1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
121 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
122 if (buf.st_uid != geteuid()) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
123 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
|
124 return 1; |
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 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
127 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
|
128 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
|
129 if (setmode) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
130 mode_t newmode = 0; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
131 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
|
132 if (S_ISDIR(buf.st_mode)) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
133 newmode |= S_IXUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
134 newmode |= S_IRUSR | S_IWUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
135 if (chmod(name, newmode)) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
136 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
|
137 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
138 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
139 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
|
140 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
141 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
|
142 return 1; |
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 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
146 return 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
147 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
148 |
524
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
149 const char *ut_get_tmpdir(void) |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
150 { |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
151 static const char *tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
152 const char *tmpvars[] = { "MCABBERTMPDIR", "TMP", "TMPDIR", "TEMP" }; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
153 int i; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
154 |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
155 if (tmpdir) |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
156 return tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
157 |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
158 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
|
159 tmpdir = getenv(tmpvars[i]); |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
160 if (tmpdir && tmpdir[0] && tmpdir[0] == '/' && tmpdir[1]) { |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
161 // Looks ok. |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
162 return tmpdir; |
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 |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
166 // Default temporary directory |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
167 tmpdir = "/tmp"; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
168 return tmpdir; |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
169 } |
05c0e55c4bb1
Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents:
463
diff
changeset
|
170 |
241 | 171 // to_iso8601(dststr, timestamp) |
172 // Convert timestamp to iso8601 format, and store it in dststr. | |
173 // NOTE: dststr should be at last 19 chars long. | |
174 // Return should be 0 | |
175 int to_iso8601(char *dststr, time_t timestamp) | |
176 { | |
177 struct tm *tm_time; | |
178 int ret; | |
179 | |
180 tm_time = gmtime(×tamp); | |
181 | |
182 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", | |
183 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, | |
184 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); | |
185 | |
186 return ((ret == -1) ? -1 : 0); | |
187 } | |
188 | |
189 // from_iso8601(timestamp, utc) | |
190 // This function comes from the Gaim project, gaim_str_to_time(). | |
191 // (Actually date may not be pure iso-8601) | |
192 // Thanks, guys! | |
193 time_t from_iso8601(const char *timestamp, int utc) | |
194 { | |
195 struct tm t; | |
196 time_t retval = 0; | |
197 char buf[32]; | |
198 char *c; | |
199 int tzoff = 0; | |
200 | |
201 time(&retval); | |
202 localtime_r(&retval, &t); | |
203 | |
463
339e85418b49
from_iso8601(): set time to 00:00:00 if not specified
Mikael Berthe <mikael@lilotux.net>
parents:
456
diff
changeset
|
204 /* 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
|
205 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
|
206 |
241 | 207 snprintf(buf, sizeof(buf), "%s", timestamp); |
208 c = buf; | |
209 | |
210 /* 4 digit year */ | |
211 if (!sscanf(c, "%04d", &t.tm_year)) return 0; | |
212 c+=4; | |
213 if (*c == '-') | |
214 c++; | |
215 | |
216 t.tm_year -= 1900; | |
217 | |
218 /* 2 digit month */ | |
219 if (!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
220 c+=2; | |
221 if (*c == '-') | |
222 c++; | |
223 | |
224 t.tm_mon -= 1; | |
225 | |
226 /* 2 digit day */ | |
227 if (!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
228 c+=2; | |
229 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
230 c++; /* skip the "T" */ | |
231 | |
232 /* 2 digit hour */ | |
233 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || | |
234 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { | |
235 int tzhrs, tzmins; | |
236 c+=8; | |
237 if (*c == '.') /* dealing with precision we don't care about */ | |
238 c += 4; | |
239 | |
240 if ((*c == '+' || *c == '-') && | |
241 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
242 tzoff = tzhrs*60*60 + tzmins*60; | |
243 if (*c == '+') | |
244 tzoff *= -1; | |
245 } | |
246 | |
247 if (tzoff || utc) { | |
248 | |
249 //#ifdef HAVE_TM_GMTOFF | |
250 tzoff += t.tm_gmtoff; | |
251 //#else | |
252 //# ifdef HAVE_TIMEZONE | |
253 // tzset(); /* making sure */ | |
254 // tzoff -= timezone; | |
255 //# endif | |
256 //#endif | |
257 } | |
258 } | |
259 } | |
260 | |
261 t.tm_isdst = -1; | |
262 | |
263 retval = mktime(&t); | |
264 | |
265 retval += tzoff; | |
266 | |
267 return retval; | |
268 } | |
419 | 269 |
270 // Should only be used for delays < 1s | |
271 inline void safe_usleep(unsigned int usec) | |
272 { | |
273 struct timespec req; | |
274 req.tv_sec = 0; | |
275 req.tv_nsec = (long)usec * 1000L; | |
276 nanosleep(&req, NULL); | |
277 } | |
452 | 278 |
279 /** | |
280 * Derived from libjabber/jid.c, because the libjabber version is not | |
281 * really convenient for our usage. | |
282 * | |
283 * Check if the full JID is valid | |
284 * Return 0 if it is valid, non zero otherwise | |
285 */ | |
286 int check_jid_syntax(char *jid) | |
287 { | |
288 char *str; | |
289 char *domain, *resource; | |
290 int domlen; | |
291 | |
292 if (!jid) return 1; | |
293 | |
294 domain = strchr(jid, '@'); | |
295 | |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
296 /* the username is optional */ |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
297 if (!domain) { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
298 domain = jid; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
299 } else { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
300 /* 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
|
301 if ((domain == jid) || (domain-jid > 1023)) |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
302 return 1; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
303 domain++; |
452 | 304 |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
305 /* 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
|
306 for (str = jid; *str != '@'; str++) { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
307 if (*str <= 32 || *str == ':' || *str == '@' || |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
308 *str == '<' || *str == '>' || *str == '\'' || |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
309 *str == '"' || *str == '&') { |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
310 return 1; |
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
311 } |
452 | 312 } |
456
471c9ccde028
Make username optional in check_jid_syntax()
Mikael Berthe <mikael@lilotux.net>
parents:
452
diff
changeset
|
313 /* the username is okay as far as we can tell without LIBIDN */ |
452 | 314 } |
315 | |
316 resource = strchr(domain, '/'); | |
317 | |
318 /* the resource is optional */ | |
319 if (resource) { | |
320 domlen = resource - domain; | |
321 resource++; | |
322 /* resources may not be longer than 1023 bytes */ | |
323 if ((*resource == '\0') || strlen(resource) > 1023) | |
324 return 1; | |
325 } else { | |
326 domlen = strlen(domain); | |
327 } | |
328 | |
329 /* there must be a domain identifier */ | |
330 if (domlen == 0) return 1; | |
331 | |
332 /* and it must not be longer than 1023 bytes */ | |
333 if (domlen > 1023) return 1; | |
334 | |
335 /* make sure the hostname is valid characters */ | |
336 for (str = domain; *str != '\0' && *str != '/'; str++) { | |
337 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_')) | |
338 return 1; | |
339 } | |
340 | |
341 /* it's okay as far as we can tell without LIBIDN */ | |
342 return 0; | |
343 } | |
344 | |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
345 |
452 | 346 void mc_strtolower(char *str) |
347 { | |
348 if (!str) return; | |
349 for ( ; *str; str++) | |
350 *str = tolower(*str); | |
351 } | |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
352 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
353 // strip_arg_special_chars(string) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
354 // Remove quotes and backslashes before an escaped quote |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
355 // Only quotes need a backslash |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
356 // Ex.: ["a b"] -> [a b]; [a\"b] -> [a"b] |
554
2424bbf0a6db
Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents:
553
diff
changeset
|
357 void strip_arg_special_chars(char *s) |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
358 { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
359 int instring = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
360 int escape = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
361 char *p; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
362 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
363 for (p = s; *p; p++) { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
364 if (*p == '"') { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
365 if (!escape) { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
366 instring = !instring; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
367 strcpy(p, p+1); |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
368 p--; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
369 } else { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
370 escape = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
371 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
372 } else if (*p == '\\') { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
373 if (!escape) { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
374 if (*(p+1) == '"') { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
375 strcpy(p, p+1); |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
376 p--; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
377 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
378 escape = TRUE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
379 } else { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
380 escape = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
381 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
382 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
383 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
384 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
385 |
554
2424bbf0a6db
Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents:
553
diff
changeset
|
386 // split_arg(arg, n, preservelast) |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
387 // Split the string arg into a maximum of n pieces, taking care of |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
388 // double quotes. |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
389 // Return a null-terminated array of strings. This array should be freed |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
390 // be the caller after use, for example with free_arg_lst(). |
559
4eb579399613
Fix a small bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents:
556
diff
changeset
|
391 // If dontstriplast is true, the Nth argument isn't stripped (i.e. no |
4eb579399613
Fix a small bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents:
556
diff
changeset
|
392 // processing of quote chars) |
554
2424bbf0a6db
Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents:
553
diff
changeset
|
393 char **split_arg(const char *arg, unsigned int n, int dontstriplast) |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
394 { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
395 char **arglst; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
396 const char *p, *start, *end; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
397 int i = 0; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
398 int instring = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
399 int escape = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
400 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
401 arglst = g_new0(char*, n+1); |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
402 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
403 if (!arg || !n) return arglst; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
404 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
405 // Skip leading space |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
406 for (start = arg; *start && *start == ' '; start++) ; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
407 // End of string pointer |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
408 for (end = start; *end; end++) ; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
409 // Skip trailing space |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
410 while (end > start+1 && *(end-1) == ' ') |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
411 end--; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
412 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
413 for (p = start; p < end; p++) { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
414 if (*p == '"' && !escape) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
415 instring = !instring; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
416 if (*p == '\\' && !escape) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
417 escape = TRUE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
418 else if (escape) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
419 escape = FALSE; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
420 if (*p == ' ' && !instring && i+1 < n) { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
421 // end of parameter |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
422 *(arglst+i) = g_strndup(start, p-start); |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
423 strip_arg_special_chars(*(arglst+i)); |
556 | 424 for (start = p+1; *start && *start == ' '; start++) ; |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
425 i++; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
426 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
427 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
428 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
429 if (start < end) { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
430 *(arglst+i) = g_strndup(start, end-start); |
559
4eb579399613
Fix a small bug in split_arg()
Mikael Berthe <mikael@lilotux.net>
parents:
556
diff
changeset
|
431 if (!dontstriplast || i+1 < n) |
554
2424bbf0a6db
Some more work on do_room()
Mikael Berthe <mikael@lilotux.net>
parents:
553
diff
changeset
|
432 strip_arg_special_chars(*(arglst+i)); |
551
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
433 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
434 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
435 return arglst; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
436 } |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
437 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
438 // free_arg_lst(arglst) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
439 // Free an array allocated by split_arg() |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
440 void free_arg_lst(char **arglst) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
441 { |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
442 char **arg_elt; |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
443 |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
444 for (arg_elt = arglst; *arg_elt; arg_elt++) |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
445 g_free(*arg_elt); |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
446 g_free(arglst); |
c71699efa5cc
Add split_arg() / free_arg_lst()
Mikael Berthe <mikael@lilotux.net>
parents:
524
diff
changeset
|
447 } |
576 | 448 |
580 | 449 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |