11
|
1 /*
|
|
2
|
|
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
|
|
4 the GPL version 2 or any later version at your choice available at
|
|
5 http://www.fsf.org/licenses/gpl.txt
|
|
6
|
|
7 Based on readpst.c by David Smith
|
|
8
|
|
9 */
|
|
10
|
|
11 using namespace std;
|
|
12
|
|
13 #include <stdio.h>
|
|
14 #include <stdlib.h>
|
|
15 #include <time.h>
|
|
16 #include <string.h>
|
|
17 #include <ctype.h>
|
|
18 #include <limits.h>
|
|
19 #include <errno.h>
|
|
20
|
|
21 #ifndef _WIN32
|
|
22 # include <unistd.h>
|
|
23 # include <sys/stat.h> //mkdir
|
|
24
|
|
25 // for reading of directory and clearing in function mk_separate_dir
|
|
26 # include <sys/types.h>
|
|
27 # include <dirent.h>
|
|
28 #else
|
|
29 # include <direct.h>
|
|
30 # define chdir _chdir
|
|
31 # define int32_t __int32
|
|
32 #endif
|
|
33
|
|
34 #ifndef __GNUC__
|
|
35 # include "XGetopt.h"
|
|
36 #endif
|
|
37
|
|
38 // needed for std c++ collections
|
|
39 #include <set>
|
|
40
|
|
41 extern "C" {
|
|
42 #include "libstrfunc.h" // for base64_encoding
|
|
43 #include "define.h"
|
|
44 #include "libpst.h"
|
|
45 #include "common.h"
|
|
46 #include "timeconv.h"
|
|
47 #include "lzfu.h"
|
|
48 }
|
|
49
|
|
50 #define VERSION "0.2"
|
|
51
|
|
52 int32_t usage();
|
|
53 int32_t version();
|
|
54 char *my_stristr(char *haystack, char *needle);
|
|
55 char *check_filename(char *fname);
|
|
56 char *single(char *str);
|
|
57 char *folded(char *str);
|
|
58 void multi(char *fmt, char *str);
|
|
59 char *rfc2426_escape(char *str);
|
|
60 int32_t chr_count(char *str, char x);
|
|
61
|
|
62 char *prog_name;
|
|
63 pst_file pstfile;
|
|
64 char *ldap_base = NULL; // 'o=some.domain.tld, c=US'
|
|
65 char *ldap_class = NULL; // 'newPerson'
|
|
66 char *ldap_org = NULL; // 'o=some.domain.tld', computed from ldap_base
|
|
67
|
|
68
|
|
69 ////////////////////////////////////////////////
|
|
70 // define our ordering
|
|
71 struct ltstr {
|
|
72 bool operator()(char* s1, char* s2) const {
|
|
73 return strcasecmp(s1, s2) < 0;
|
|
74 }
|
|
75 };
|
|
76 // define our set
|
|
77 typedef set<char *, ltstr> string_set;
|
|
78 // make a static set to hold the cn values
|
|
79 static string_set all_strings;
|
|
80
|
|
81
|
|
82 ////////////////////////////////////////////////
|
|
83 // helper to register a string in a string set
|
|
84 //
|
|
85 static char* register_string(string_set &s, char *name);
|
|
86 static char* register_string(string_set &s, char *name) {
|
|
87 string_set::iterator i = s.find(name);
|
|
88 if (i != s.end()) return *i;
|
|
89 char *x = strdup(name);
|
|
90 s.insert(x);
|
|
91 return x;
|
|
92 }
|
|
93
|
|
94 ////////////////////////////////////////////////
|
|
95 // register a global string
|
|
96 //
|
|
97 static char* register_string(char *name);
|
|
98 static char* register_string(char *name) {
|
|
99 return register_string(all_strings, name);
|
|
100 }
|
|
101
|
|
102
|
|
103 ////////////////////////////////////////////////
|
|
104 // make a unique string
|
|
105 //
|
|
106 static char* unique_string(char *name);
|
|
107 static char* unique_string(char *name) {
|
|
108 int unique = 2;
|
|
109 string_set::iterator i = all_strings.find(name);
|
|
110 if (i == all_strings.end()) return register_string(name);
|
|
111 while (true) {
|
|
112 char n[strlen(name)+10];
|
|
113 snprintf(n, sizeof(n), "%s %d", name, unique++);
|
|
114 string_set::iterator i = all_strings.find(n);
|
|
115 if (i == all_strings.end()) return register_string(n);
|
|
116 }
|
|
117 }
|
|
118
|
|
119
|
|
120 ////////////////////////////////////////////////
|
|
121 // remove leading and trailing blanks
|
|
122 //
|
|
123 static char *trim(char *name);
|
|
124 static char *trim(char *name) {
|
|
125 char *p;
|
|
126 while (*name == ' ') name++;
|
|
127 p = name + strlen(name) - 1;
|
|
128 while ((p >= name) && (*p == ' ')) *p-- = '\0';
|
|
129 return name;
|
|
130 }
|
|
131
|
|
132
|
|
133 static void process(pst_desc_ll *d_ptr);
|
|
134 static void process(pst_desc_ll *d_ptr) {
|
|
135 pst_item *item = NULL;
|
|
136 while (d_ptr) {
|
|
137 if (d_ptr->desc) {
|
|
138 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr);
|
|
139 if (item) {
|
|
140 if (item->message_store) {
|
|
141 // there should only be one message_store, and we have already done it
|
|
142 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
|
|
143 }
|
|
144
|
|
145 if (item->folder && d_ptr->child && strcasecmp(item->file_as, "Deleted Items")) {
|
|
146 //if this is a non-empty folder other than deleted items, we want to recurse into it
|
|
147 fprintf(stderr, "entering folder %s\n", item->file_as);
|
|
148 process(d_ptr->child);
|
|
149 } else if (item->contact) {
|
|
150 // deal with a contact
|
|
151 if (item->type != PST_TYPE_CONTACT) {
|
|
152 DIE(("type should be contact\n"));
|
|
153 }
|
|
154 else if (item->contact == NULL) { // this is an incorrect situation. Inform user
|
|
155 DIE(("null item contact\n"));
|
|
156 } else {
|
|
157 char cn[1000];
|
|
158 snprintf(cn, sizeof(cn), "%s %s %s %s",
|
|
159 single(item->contact->display_name_prefix),
|
|
160 single(item->contact->first_name),
|
|
161 single(item->contact->surname),
|
|
162 single(item->contact->suffix));
|
|
163 if (strcmp(cn, " ")) {
|
|
164 // fprintf(stderr, "\n\n\n");
|
|
165 // fprintf(stderr, "access_method %s\n", item->contact->access_method);
|
|
166 // fprintf(stderr, "account_name %s\n", item->contact->account_name);
|
|
167 // fprintf(stderr, "address1 %s\n", item->contact->address1);
|
|
168 // fprintf(stderr, "address1_desc %s\n", item->contact->address1_desc);
|
|
169 // fprintf(stderr, "address1_transport %s\n", item->contact->address1_transport);
|
|
170 // fprintf(stderr, "address2 %s\n", item->contact->address2);
|
|
171 // fprintf(stderr, "address2_desc %s\n", item->contact->address2_desc);
|
|
172 // fprintf(stderr, "address2_transport %s\n", item->contact->address2_transport);
|
|
173 // fprintf(stderr, "address3 %s\n", item->contact->address3);
|
|
174 // fprintf(stderr, "address3_desc %s\n", item->contact->address3_desc);
|
|
175 // fprintf(stderr, "address3_transport %s\n", item->contact->address3_transport);
|
|
176 // fprintf(stderr, "assistant_name %s\n", item->contact->assistant_name);
|
|
177 // fprintf(stderr, "assistant_phone %s\n", item->contact->assistant_phone);
|
|
178 // fprintf(stderr, "billing_information %s\n", item->contact->billing_information);
|
|
179 // fprintf(stderr, "business_address %s\n", item->contact->business_address);
|
|
180 // fprintf(stderr, "business_city %s\n", item->contact->business_city);
|
|
181 // fprintf(stderr, "business_country %s\n", item->contact->business_country);
|
|
182 // fprintf(stderr, "business_fax %s\n", item->contact->business_fax);
|
|
183 // fprintf(stderr, "business_homepage %s\n", item->contact->business_homepage);
|
|
184 // fprintf(stderr, "business_phone %s\n", item->contact->business_phone);
|
|
185 // fprintf(stderr, "business_phone2 %s\n", item->contact->business_phone2);
|
|
186 // fprintf(stderr, "business_po_box %s\n", item->contact->business_po_box);
|
|
187 // fprintf(stderr, "business_postal_code %s\n", item->contact->business_postal_code);
|
|
188 // fprintf(stderr, "business_state %s\n", item->contact->business_state);
|
|
189 // fprintf(stderr, "business_street %s\n", item->contact->business_street);
|
|
190 // fprintf(stderr, "callback_phone %s\n", item->contact->callback_phone);
|
|
191 // fprintf(stderr, "car_phone %s\n", item->contact->car_phone);
|
|
192 // fprintf(stderr, "company_main_phone %s\n", item->contact->company_main_phone);
|
|
193 // fprintf(stderr, "company_name %s\n", item->contact->company_name);
|
|
194 // fprintf(stderr, "computer_name %s\n", item->contact->computer_name);
|
|
195 // fprintf(stderr, "customer_id %s\n", item->contact->customer_id);
|
|
196 // fprintf(stderr, "def_postal_address %s\n", item->contact->def_postal_address);
|
|
197 // fprintf(stderr, "department %s\n", item->contact->department);
|
|
198 // fprintf(stderr, "display_name_prefix %s\n", item->contact->display_name_prefix);
|
|
199 // fprintf(stderr, "first_name %s\n", item->contact->first_name);
|
|
200 // fprintf(stderr, "followup %s\n", item->contact->followup);
|
|
201 // fprintf(stderr, "free_busy_address %s\n", item->contact->free_busy_address);
|
|
202 // fprintf(stderr, "ftp_site %s\n", item->contact->ftp_site);
|
|
203 // fprintf(stderr, "fullname %s\n", item->contact->fullname);
|
|
204 // fprintf(stderr, "gov_id %s\n", item->contact->gov_id);
|
|
205 // fprintf(stderr, "hobbies %s\n", item->contact->hobbies);
|
|
206 // fprintf(stderr, "home_address %s\n", item->contact->home_address);
|
|
207 // fprintf(stderr, "home_city %s\n", item->contact->home_city);
|
|
208 // fprintf(stderr, "home_country %s\n", item->contact->home_country);
|
|
209 // fprintf(stderr, "home_fax %s\n", item->contact->home_fax);
|
|
210 // fprintf(stderr, "home_phone %s\n", item->contact->home_phone);
|
|
211 // fprintf(stderr, "home_phone2 %s\n", item->contact->home_phone2);
|
|
212 // fprintf(stderr, "home_po_box %s\n", item->contact->home_po_box);
|
|
213 // fprintf(stderr, "home_postal_code %s\n", item->contact->home_postal_code);
|
|
214 // fprintf(stderr, "home_state %s\n", item->contact->home_state);
|
|
215 // fprintf(stderr, "home_street %s\n", item->contact->home_street);
|
|
216 // fprintf(stderr, "initials %s\n", item->contact->initials);
|
|
217 // fprintf(stderr, "isdn_phone %s\n", item->contact->isdn_phone);
|
|
218 // fprintf(stderr, "job_title %s\n", item->contact->job_title);
|
|
219 // fprintf(stderr, "keyword %s\n", item->contact->keyword);
|
|
220 // fprintf(stderr, "language %s\n", item->contact->language);
|
|
221 // fprintf(stderr, "location %s\n", item->contact->location);
|
|
222 // fprintf(stderr, "manager_name %s\n", item->contact->manager_name);
|
|
223 // fprintf(stderr, "middle_name %s\n", item->contact->middle_name);
|
|
224 // fprintf(stderr, "mileage %s\n", item->contact->mileage);
|
|
225 // fprintf(stderr, "mobile_phone %s\n", item->contact->mobile_phone);
|
|
226 // fprintf(stderr, "nickname %s\n", item->contact->nickname);
|
|
227 // fprintf(stderr, "office_loc %s\n", item->contact->office_loc);
|
|
228 // fprintf(stderr, "org_id %s\n", item->contact->org_id);
|
|
229 // fprintf(stderr, "other_address %s\n", item->contact->other_address);
|
|
230 // fprintf(stderr, "other_city %s\n", item->contact->other_city);
|
|
231 // fprintf(stderr, "other_country %s\n", item->contact->other_country);
|
|
232 // fprintf(stderr, "other_phone %s\n", item->contact->other_phone);
|
|
233 // fprintf(stderr, "other_po_box %s\n", item->contact->other_po_box);
|
|
234 // fprintf(stderr, "other_postal_code %s\n", item->contact->other_postal_code);
|
|
235 // fprintf(stderr, "other_state %s\n", item->contact->other_state);
|
|
236 // fprintf(stderr, "other_street %s\n", item->contact->other_street);
|
|
237 // fprintf(stderr, "pager_phone %s\n", item->contact->pager_phone);
|
|
238 // fprintf(stderr, "personal_homepage %s\n", item->contact->personal_homepage);
|
|
239 // fprintf(stderr, "pref_name %s\n", item->contact->pref_name);
|
|
240 // fprintf(stderr, "primary_fax %s\n", item->contact->primary_fax);
|
|
241 // fprintf(stderr, "primary_phone %s\n", item->contact->primary_phone);
|
|
242 // fprintf(stderr, "profession %s\n", item->contact->profession);
|
|
243 // fprintf(stderr, "radio_phone %s\n", item->contact->radio_phone);
|
|
244 // fprintf(stderr, "spouse_name %s\n", item->contact->spouse_name);
|
|
245 // fprintf(stderr, "suffix %s\n", item->contact->suffix);
|
|
246 // fprintf(stderr, "surname %s\n", item->contact->surname);
|
|
247 // fprintf(stderr, "telex %s\n", item->contact->telex);
|
|
248 // fprintf(stderr, "transmittable_display_name %s\n", item->contact->transmittable_display_name);
|
|
249 // fprintf(stderr, "ttytdd_phone %s\n", item->contact->ttytdd_phone);
|
|
250 // have a valid cn
|
|
251 char *ucn = unique_string(folded(trim(cn)));
|
|
252 printf("dn: cn=%s, %s\n", ucn, ldap_base);
|
|
253 printf("cn: %s\n", ucn);
|
|
254 if (item->contact->first_name) {
|
|
255 snprintf(cn, sizeof(cn), "%s %s",
|
|
256 single(item->contact->display_name_prefix),
|
|
257 single(item->contact->first_name));
|
|
258 printf("givenName: %s\n", trim(cn));
|
|
259 }
|
|
260 if (item->contact->surname) {
|
|
261 snprintf(cn, sizeof(cn), "%s %s",
|
|
262 single(item->contact->surname),
|
|
263 single(item->contact->suffix));
|
|
264 printf("sn: %s\n", trim(cn));
|
|
265 }
|
|
266 else if (item->contact->company_name) {
|
|
267 printf("sn: %s\n", single(item->contact->company_name));
|
|
268 }
|
|
269 else
|
|
270 printf("sn: %s\n", ucn); // use cn as sn if we cannot find something better
|
|
271
|
|
272 if (item->contact->job_title)
|
|
273 printf("personalTitle: %s\n", single(item->contact->job_title));
|
|
274 if (item->contact->company_name)
|
|
275 printf("company: %s\n", single(item->contact->company_name));
|
|
276 if (item->contact->address1 && *item->contact->address1)
|
|
277 printf("mail: %s\n", single(item->contact->address1));
|
|
278 if (item->contact->address2 && *item->contact->address2)
|
|
279 printf("mail: %s\n", single(item->contact->address2));
|
|
280 if (item->contact->address3 && *item->contact->address3)
|
|
281 printf("mail: %s\n", single(item->contact->address3));
|
|
282 if (item->contact->address1a && *item->contact->address1a)
|
|
283 printf("mail: %s\n", single(item->contact->address1a));
|
|
284 if (item->contact->address2a && *item->contact->address2a)
|
|
285 printf("mail: %s\n", single(item->contact->address2a));
|
|
286 if (item->contact->address3a && *item->contact->address3a)
|
|
287 printf("mail: %s\n", single(item->contact->address3a));
|
|
288 if (item->contact->business_address) {
|
|
289 if (item->contact->business_po_box)
|
|
290 printf("postalAddress: %s\n", single(item->contact->business_po_box));
|
|
291 if (item->contact->business_street)
|
|
292 multi("postalAddress: %s\n", item->contact->business_street);
|
|
293 if (item->contact->business_city)
|
|
294 printf("l: %s\n", single(item->contact->business_city));
|
|
295 if (item->contact->business_state)
|
|
296 printf("st: %s\n", single(item->contact->business_state));
|
|
297 if (item->contact->business_postal_code)
|
|
298 printf("postalCode: %s\n", single(item->contact->business_postal_code));
|
|
299 }
|
|
300 else if (item->contact->home_address) {
|
|
301 if (item->contact->home_po_box)
|
|
302 printf("postalAddress: %s\n", single(item->contact->home_po_box));
|
|
303 if (item->contact->home_street)
|
|
304 multi("postalAddress: %s\n", item->contact->home_street);
|
|
305 if (item->contact->home_city)
|
|
306 printf("l: %s\n", single(item->contact->home_city));
|
|
307 if (item->contact->home_state)
|
|
308 printf("st: %s\n", single(item->contact->home_state));
|
|
309 if (item->contact->home_postal_code)
|
|
310 printf("postalCode: %s\n", single(item->contact->home_postal_code));
|
|
311 }
|
|
312 else if (item->contact->other_address) {
|
|
313 if (item->contact->other_po_box)
|
|
314 printf("postalAddress: %s\n", single(item->contact->other_po_box));
|
|
315 if (item->contact->other_street)
|
|
316 multi("postalAddress: %s\n", item->contact->other_street);
|
|
317 if (item->contact->other_city)
|
|
318 printf("l: %s\n", single(item->contact->other_city));
|
|
319 if (item->contact->other_state)
|
|
320 printf("st: %s\n", single(item->contact->other_state));
|
|
321 if (item->contact->other_postal_code)
|
|
322 printf("postalCode: %s\n", single(item->contact->other_postal_code));
|
|
323 }
|
|
324 if (item->contact->business_fax)
|
|
325 printf("facsimileTelephoneNumber: %s\n", single(item->contact->business_fax));
|
|
326 else if (item->contact->home_fax)
|
|
327 printf("facsimileTelephoneNumber: %s\n", single(item->contact->home_fax));
|
|
328
|
|
329 if (item->contact->business_phone)
|
|
330 printf("telephoneNumber: %s\n", single(item->contact->business_phone));
|
|
331 if (item->contact->home_phone)
|
|
332 printf("homePhone: %s\n", single(item->contact->home_phone));
|
|
333
|
|
334 if (item->contact->car_phone)
|
|
335 printf("mobile: %s\n", single(item->contact->car_phone));
|
|
336 else if (item->contact->mobile_phone)
|
|
337 printf("mobile: %s\n", single(item->contact->mobile_phone));
|
|
338 else if (item->contact->other_phone)
|
|
339 printf("mobile: %s\n", single(item->contact->other_phone));
|
|
340
|
|
341
|
|
342 if (item->comment)
|
|
343 printf("description: %s\n", single(item->comment));
|
|
344
|
|
345 printf("objectClass: %s\n\n", ldap_class);
|
|
346 }
|
|
347 }
|
|
348 }
|
|
349 }
|
|
350 _pst_freeItem(item);
|
|
351 }
|
|
352 d_ptr = d_ptr->next;
|
|
353 }
|
|
354 }
|
|
355
|
|
356
|
|
357 int main(int argc, char** argv) {
|
|
358 pst_desc_ll *d_ptr;
|
|
359 char *fname = NULL;
|
|
360 char *temp = NULL; //temporary char pointer
|
|
361 char c;
|
|
362 prog_name = argv[0];
|
|
363 pst_item *item = NULL;
|
|
364
|
|
365 while ((c = getopt(argc, argv, "b:c:Vh"))!= -1) {
|
|
366 switch (c) {
|
|
367 case 'b':
|
|
368 ldap_base = optarg;
|
|
369 temp = strchr(ldap_base, ',');
|
|
370 if (temp) {
|
|
371 *temp = '\0';
|
|
372 ldap_org = strdup(ldap_base+2); // assume first 2 chars are o=
|
|
373 *temp = ',';
|
|
374 }
|
|
375 break;
|
|
376 case 'c':
|
|
377 ldap_class = optarg;
|
|
378 break;
|
|
379 case 'h':
|
|
380 usage();
|
|
381 exit(0);
|
|
382 break;
|
|
383 case 'V':
|
|
384 version();
|
|
385 exit(0);
|
|
386 break;
|
|
387 default:
|
|
388 usage();
|
|
389 exit(1);
|
|
390 break;
|
|
391 }
|
|
392 }
|
|
393
|
|
394 if ((argc > optind) && (ldap_base) && (ldap_class) && (ldap_org)) {
|
|
395 fname = argv[optind];
|
|
396 } else {
|
|
397 usage();
|
|
398 exit(2);
|
|
399 }
|
|
400
|
|
401 DEBUG_INIT("pst2ldif.log");
|
|
402 DEBUG_REGISTER_CLOSE();
|
|
403 DEBUG_ENT("main");
|
|
404 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
|
|
405 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
|
|
406
|
|
407 pst_load_extended_attributes(&pstfile);
|
|
408
|
|
409 d_ptr = pstfile.d_head; // first record is main record
|
|
410 item = (pst_item*)_pst_parse_item(&pstfile, d_ptr);
|
|
411 if (!item || !item->message_store) {
|
|
412 DIE(("main: Could not get root record\n"));
|
|
413 }
|
|
414
|
|
415 d_ptr = pst_getTopOfFolders(&pstfile, item);
|
|
416 if (!d_ptr) {
|
|
417 DIE(("Top of folders record not found. Cannot continue\n"));
|
|
418 }
|
|
419
|
|
420 _pst_freeItem(item);
|
|
421
|
|
422 // write the ldap header
|
|
423 printf("dn: %s\n", ldap_base);
|
|
424 printf("o: %s\n", ldap_org);
|
|
425 printf("objectClass: organization\n\n");
|
|
426 printf("dn: cn=root, %s\n", ldap_base);
|
|
427 printf("cn: root\n");
|
|
428 printf("objectClass: %s\n\n", ldap_class);
|
|
429
|
|
430 process(d_ptr->child); // do the children of TOPF
|
|
431 pst_close(&pstfile);
|
|
432 return 0;
|
|
433 }
|
|
434
|
|
435
|
|
436 int usage() {
|
|
437 version();
|
|
438 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
|
|
439 printf("OPTIONS:\n");
|
|
440 printf("\t-h\t- Help. This screen\n");
|
|
441 printf("\t-V\t- Version. Display program version\n");
|
|
442 printf("\t-b ldapbase\t- set the ldap base value\n");
|
|
443 printf("\t-c class \t- set the class of the ldap objects\n");
|
|
444 return 0;
|
|
445 }
|
|
446
|
|
447
|
|
448 int version() {
|
|
449 printf("pst2ldif v%s using LibPST v%s\n", VERSION, PST_VERSION);
|
|
450 #if BYTE_ORDER == BIG_ENDIAN
|
|
451 printf("Big Endian implementation being used.\n");
|
|
452 #elif BYTE_ORDER == LITTLE_ENDIAN
|
|
453 printf("Little Endian implementation being used.\n");
|
|
454 #else
|
|
455 # error "Byte order not supported by this library"
|
|
456 #endif
|
|
457 #ifdef __GNUC__
|
|
458 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
|
|
459 #endif
|
|
460 return 0;
|
|
461 }
|
|
462
|
|
463
|
|
464 // my_stristr varies from strstr in that its searches are case-insensitive
|
|
465 char * my_stristr(char *haystack, char *needle) {
|
|
466 char *x=haystack, *y=needle, *z = NULL;
|
|
467 if (haystack == NULL || needle == NULL)
|
|
468 return NULL;
|
|
469 while (*y != '\0' && *x != '\0') {
|
|
470 if (tolower(*y) == tolower(*x)) {
|
|
471 // move y on one
|
|
472 y++;
|
|
473 if (z == NULL) {
|
|
474 z = x; // store first position in haystack where a match is made
|
|
475 }
|
|
476 } else {
|
|
477 y = needle; // reset y to the beginning of the needle
|
|
478 z = NULL; // reset the haystack storage point
|
|
479 }
|
|
480 x++; // advance the search in the haystack
|
|
481 }
|
|
482 return z;
|
|
483 }
|
|
484
|
|
485
|
|
486 char *check_filename(char *fname) {
|
|
487 char *t = fname;
|
|
488 if (t == NULL) {
|
|
489 return fname;
|
|
490 }
|
|
491 while ((t = strpbrk(t, "/\\:"))) {
|
|
492 // while there are characters in the second string that we don't want
|
|
493 *t = '_'; //replace them with an underscore
|
|
494 }
|
|
495 return fname;
|
|
496 }
|
|
497
|
|
498
|
|
499 char *single(char *str) {
|
|
500 if (!str) return "";
|
|
501 char *ret = rfc2426_escape(str);
|
|
502 char *n = strchr(ret, '\n');
|
|
503 if (n) *n = '\0';
|
|
504 return ret;
|
|
505 }
|
|
506
|
|
507
|
|
508 char *folded(char *str) {
|
|
509 if (!str) return "";
|
|
510 char *ret = rfc2426_escape(str);
|
|
511 char *n = ret;
|
|
512 while (n = strchr(n, '\n')) {
|
|
513 *n = ' ';
|
|
514 }
|
|
515 n = ret;
|
|
516 while (n = strchr(n, ',')) {
|
|
517 *n = ' ';
|
|
518 }
|
|
519 return ret;
|
|
520 }
|
|
521
|
|
522
|
|
523 void multi(char *fmt, char *str) {
|
|
524 if (!str) return;
|
|
525 char *ret = rfc2426_escape(str);
|
|
526 char *n = ret;
|
|
527 while (n = strchr(ret, '\n')) {
|
|
528 *n = '\0';
|
|
529 printf(fmt, ret);
|
|
530 ret = n+1;
|
|
531 }
|
|
532 if (*ret) printf(fmt, ret);
|
|
533 }
|
|
534
|
|
535
|
|
536 char *rfc2426_escape(char *str) {
|
|
537 static char* buf = NULL;
|
|
538 char *ret, *a, *b;
|
|
539 int x = 0, y, z;
|
|
540 if (str == NULL)
|
|
541 ret = str;
|
|
542 else {
|
|
543
|
|
544 // calculate space required to escape all the following characters
|
|
545 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2));
|
|
546 z = chr_count(str, '\r');
|
|
547 if (y == 0 && z == 0)
|
|
548 // there isn't any extra space required
|
|
549 ret = str;
|
|
550 else {
|
|
551 buf = (char*) realloc(buf, x+1);
|
|
552 a = str;
|
|
553 b = buf;
|
|
554 while (*a != '\0') {
|
|
555 switch(*a) {
|
|
556 // case ',' :
|
|
557 case '\\':
|
|
558 case ';' :
|
|
559 // case '\n':
|
|
560 *(b++)='\\';
|
|
561 *b=*a;
|
|
562 break;
|
|
563 case '\r':
|
|
564 break;
|
|
565 default:
|
|
566 *b=*a;
|
|
567 }
|
|
568 b++;
|
|
569 a++;
|
|
570 }
|
|
571 *b = '\0';
|
|
572 ret = buf;
|
|
573 }
|
|
574 }
|
|
575 return ret;
|
|
576 }
|
|
577
|
|
578
|
|
579 int chr_count(char *str, char x) {
|
|
580 int r = 0;
|
|
581 while (*str != '\0') {
|
|
582 if (*str == x)
|
|
583 r++;
|
|
584 str++;
|
|
585 }
|
|
586 return r;
|
|
587 }
|
|
588
|