annotate src/readpst.c @ 26:9eeba3f4ca4b

more cleanup from Arne
author carl
date Sat, 25 Feb 2006 12:45:57 -0800
parents 73e8959cd86b
children 51d826f31329
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
1 /***
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
2 * readpst.c
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
3 * Part of the LibPST project
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
4 * Written by David Smith
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
5 * dave.s@earthcorp.com
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
6 */
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
7 #include <stdio.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
8 #include <stdlib.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
9 #include <time.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 #include <string.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11 #include <ctype.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
12 #include <limits.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
13 #include <errno.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
14
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
15 #ifndef _WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
16 # include <unistd.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
17 # include <sys/stat.h> //mkdir
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
19 // for reading of directory and clearing in function mk_seperate_dir
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
20 # include <sys/types.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
21 # include <dirent.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
22 #else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
23 # include <direct.h>
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
24 # define chdir _chdir
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
25 # define int32_t __int32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
26 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
27
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
28 #ifndef __GNUC__
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
29 # include "XGetopt.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
30 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
31
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
32 #include "libstrfunc.h" // for base64_encoding
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
33
21
e5418051878c switch to automake/autoconf
carl
parents: 16
diff changeset
34 #include "version.h"
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
35 #include "define.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
36 #include "libpst.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
37 #include "common.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
38 #include "timeconv.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
39 #include "lzfu.h"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
40 #define OUTPUT_TEMPLATE "%s"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
41 #define OUTPUT_KMAIL_DIR_TEMPLATE ".%s.directory"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
42 #define KMAIL_INDEX ".%s.index"
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
43 #define SEP_MAIL_FILE_TEMPLATE "%i" /* "%09i" */
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
44
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
45 // max size of the c_time char*. It will store the date of the email
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
46 #define C_TIME_SIZE 500
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
47 #define PERM_DIRS 0777
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
48
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
49 // macro used for creating directories
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
50 #ifndef WIN32
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
51 #define D_MKDIR(x) mkdir(x, PERM_DIRS)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 #else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53 #define D_MKDIR(x) mkdir(x)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
54 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
55 struct file_ll {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
56 char *name;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
57 char *dname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
58 FILE * output;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
59 int32_t stored_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
60 int32_t email_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
61 int32_t skip_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
62 int32_t type;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
63 struct file_ll *next;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
64 };
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
65 void write_email_body(FILE *f, char *body);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
66 char *removeCR (char *c);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
67 int32_t usage();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
68 int32_t version();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
69 char *mk_kmail_dir(char*);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
70 int32_t close_kmail_dir();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
71 char *mk_recurse_dir(char*);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
72 int32_t close_recurse_dir();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
73 char *mk_seperate_dir(char *dir, int overwrite);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
74 int32_t close_seperate_dir();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
75 int32_t mk_seperate_file(struct file_ll *f);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
76 char *my_stristr(char *haystack, char *needle);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
77 char *check_filename(char *fname);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
78 char *rfc2426_escape(char *str);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
79 int32_t chr_count(char *str, char x);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
80 char *rfc2425_datetime_format(FILETIME *ft);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
81 char *rfc2445_datetime_format(FILETIME *ft);
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
82 char *skip_header_prologue(char *headers);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
83 void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
84 void write_inline_attachment(FILE* f_output, pst_item_attach* current_attach, char boundary[], pst_file* pst);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
85 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
86 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[]);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
87 void write_appointment(FILE* f_output, pst_item_appointment* appointment,
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
88 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
89 void create_enter_dir(struct file_ll* f, char file_as[], int mode, int overwrite);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
90 char *prog_name;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
91 char *output_dir = ".";
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
92 char *kmail_chdir = NULL;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
93 // Normal mode just creates mbox format files in the current directory. Each file is named
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
94 // the same as the folder's name that it represents
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
95 #define MODE_NORMAL 0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
96 // KMail mode creates a directory structure suitable for being used directly
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
97 // by the KMail application
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
98 #define MODE_KMAIL 1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
99 // recurse mode creates a directory structure like the PST file. Each directory
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
100 // contains only one file which stores the emails in mbox format.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
101 #define MODE_RECURSE 2
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
102 // seperate mode is similar directory structure to RECURSE. The emails are stored in
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
103 // seperate files, numbering from 1 upward. Attachments belonging to the emails are
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
104 // saved as email_no-filename (e.g. 1-samplefile.doc or 000001-Attachment2.zip)
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
105 #define MODE_SEPERATE 3
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
106
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
107
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
108 // Output Normal just prints the standard information about what is going on
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
109 #define OUTPUT_NORMAL 0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
110 // Output Quiet is provided so that only errors are printed
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
111 #define OUTPUT_QUIET 1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
112
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
113 // default mime-type for attachments that have a null mime-type
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
114 #define MIME_TYPE_DEFAULT "application/octet-stream"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
115
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
116
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
117 // output mode for contacts
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
118 #define CMODE_VCARD 0
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
119 #define CMODE_LIST 1
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
120
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
121 // output settings for RTF bodies
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
122 // filename for the attachment
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
123 #define RTF_ATTACH_NAME "rtf-body.rtf"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
124 // mime type for the attachment
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
125 #define RTF_ATTACH_TYPE "application/rtf"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
126 int main(int argc, char** argv) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
127 pst_item *item = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
128 pst_file pstfile;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
129 pst_desc_ll *d_ptr;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
130 char * fname = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
131 char *d_log=NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
132 int c,x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
133 int mode = MODE_NORMAL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
134 int mode_MH = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
135 int output_mode = OUTPUT_NORMAL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
136 int contact_mode = CMODE_VCARD;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
137 int overwrite = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
138 // int encrypt = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
139 char *temp = NULL; //temporary char pointer
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
140 int skip_child = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
141 struct file_ll *f, *head;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
142 prog_name = argv[0];
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
143
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
144 while ((c = getopt(argc, argv, "d:hko:qrMSVwc:"))!= -1) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
145 switch (c) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
146 case 'c':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
147 if (optarg!=NULL && optarg[0]=='v')
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
148 contact_mode=CMODE_VCARD;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
149 else if (optarg!=NULL && optarg[0]=='l')
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
150 contact_mode=CMODE_LIST;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
151 else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
152 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
153 exit(0);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
154 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
155 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
156 case 'd':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
157 d_log = optarg;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
158 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
159 case 'h':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
160 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
161 exit(0);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
162 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
163 case 'V':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
164 version();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
165 exit(0);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
166 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
167 case 'k':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
168 mode = MODE_KMAIL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
169 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
170 case 'M':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
171 mode = MODE_SEPERATE;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
172 mode_MH = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
173 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
174 case 'o':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
175 output_dir = optarg;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
176 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
177 case 'q':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
178 output_mode = OUTPUT_QUIET;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
179 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
180 case 'r':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
181 mode = MODE_RECURSE;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
182 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
183 case 'S':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
184 mode = MODE_SEPERATE;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
185 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
186 case 'w':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
187 overwrite = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
188 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
189 default:
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
190 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
191 exit(1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
192 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
193 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
194 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
195
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
196 #ifdef DEBUG_ALL
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
197 // initialize log file
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
198 if (d_log == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
199 d_log = "readpst.log";
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
200 DEBUG_INIT(d_log);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
201 DEBUG_REGISTER_CLOSE();
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
202 #endif // defined DEBUG_ALL
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
203
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
204 DEBUG_ENT("main");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
205
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
206 if (argc > optind) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
207 fname = argv[optind];
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
208 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
209 usage();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
210 exit(2);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
211 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
212
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
213 if (output_mode != OUTPUT_QUIET) printf("Opening PST file and indexes...\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
214
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
215 DEBUG_MAIN(("main: Opening PST file '%s'\n", fname));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
216 RET_DERROR(pst_open(&pstfile, fname, "r"), 1, ("Error opening File\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
217 DEBUG_MAIN(("main: Loading Indexes\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
218 RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
219 DEBUG_MAIN(("processing file items\n"));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
220
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
221 pst_load_extended_attributes(&pstfile);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
222
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
223 if (chdir(output_dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
224 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
225 pst_close(&pstfile);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
226 DIE(("main: Cannot change to output dir %s: %s\n", output_dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
227 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
228
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
229 if (output_mode != OUTPUT_QUIET) printf("About to start processing first record...\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
230
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
231 d_ptr = pstfile.d_head; // first record is main record
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
232 if ((item = _pst_parse_item(&pstfile, d_ptr)) == NULL || item->message_store == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
233 DIE(("main: Could not get root record\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
234 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
235
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
236 // default the file_as to the same as the main filename if it doesn't exist
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
237 if (item->file_as == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
238 if ((temp = strrchr(fname, '/')) == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
239 if ((temp = strrchr(fname, '\\')) == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
240 temp = fname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
241 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
242 temp++; // get past the "\\"
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
243 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
244 temp++; // get past the "/"
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
245 item->file_as = (char*)xmalloc(strlen(temp)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
246 strcpy(item->file_as, temp);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
247 DEBUG_MAIN(("file_as was blank, so am using %s\n", item->file_as));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
248 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
249 DEBUG_MAIN(("main: Root Folder Name: %s\n", item->file_as));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
250
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
251
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
252 f = (struct file_ll*) malloc(sizeof(struct file_ll));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
253 memset(f, 0, sizeof(struct file_ll));
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
254 f->email_count = 0;
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
255 f->skip_count = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
256 f->next = NULL;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
257 head = f;
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
258 create_enter_dir(f, item->file_as, mode, overwrite);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
259 f->type = item->type;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
260
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
261 if ((d_ptr = pst_getTopOfFolders(&pstfile, item)) == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
262 DIE(("Top of folders record not found. Cannot continue\n"));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
263 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
264
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
265 if (item){
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
266 _pst_freeItem(item);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
267 item = NULL;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
268 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
269
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
270 /* if ((item = _pst_parse_item(&pstfile, d_ptr)) == NULL || item->folder == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
271 DEBUG_MAIN(("main: Could not get \"Top Of Personal Folder\" record\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
272 return -2;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
273 }*/
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
274 d_ptr = d_ptr->child; // do the children of TOPF
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
275
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
276 if (output_mode != OUTPUT_QUIET) printf("Processing items...\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
277
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
278 DEBUG_MAIN(("main: About to do email stuff\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
279 while (d_ptr != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
280 DEBUG_MAIN(("main: New item record\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
281 if (d_ptr->desc == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
282 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
283 f->skip_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
284 goto check_parent;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
285 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
286 DEBUG_MAIN(("main: Desc Email ID %#x [d_ptr->id = %#x]\n", d_ptr->desc->id, d_ptr->id));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
287
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
288 item = _pst_parse_item(&pstfile, d_ptr);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
289 DEBUG_MAIN(("main: About to process item\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
290 if (item != NULL && item->email != NULL && item->email->subject != NULL &&
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
291 item->email->subject->subj != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
292 // DEBUG_EMAIL(("item->email->subject = %p\n", item->email->subject));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
293 // DEBUG_EMAIL(("item->email->subject->subj = %p\n", item->email->subject->subj));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
294 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
295 if (item != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
296 if (item->message_store != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
297 // there should only be one message_store, and we have already done it
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
298 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
299 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
300
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
301
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
302 if (item->folder != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
303 // if this is a folder, we want to recurse into it
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
304 if (output_mode != OUTPUT_QUIET) printf("Processing Folder \"%s\"\n", item->file_as);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
305 // f->email_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
306 DEBUG_MAIN(("main: I think I may try to go into folder \"%s\"\n", item->file_as));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
307 f = (struct file_ll*) malloc(sizeof(struct file_ll));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
308 memset(f, 0, sizeof(struct file_ll));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
309
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
310 f->next = head;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
311 f->email_count = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
312 f->type = item->type;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
313 f->stored_count = item->folder->email_count;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
314 head = f;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
315
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
316 temp = item->file_as;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
317 temp = check_filename(temp);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
318 create_enter_dir(f, item->file_as, mode, overwrite);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
319 if (d_ptr->child != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
320 d_ptr = d_ptr->child;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
321 skip_child = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
322 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
323 DEBUG_MAIN(("main: Folder has NO children. Creating directory, and closing again\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
324 if (output_mode != OUTPUT_QUIET)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
325 printf("\tNo items to process in folder \"%s\", should have been %i\n", f->dname, f->stored_count);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
326 head = f->next;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
327 if (f->output != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
328 fclose(f->output);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
329 if (mode == MODE_KMAIL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
330 close_kmail_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
331 else if (mode == MODE_RECURSE)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
332 close_recurse_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
333 else if (mode == MODE_SEPERATE)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
334 close_seperate_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
335 free(f->dname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
336 free(f->name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
337 free(f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
338
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
339 f = head;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
340 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
341 _pst_freeItem(item);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
342 item = NULL; // just for the odd situations!
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
343 goto check_parent;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
344 } else if (item->contact != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
345 // deal with a contact
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
346 // write them to the file, one per line in this format
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
347 // Desc Name <email@address>\n
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
348 if (mode == MODE_SEPERATE) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
349 mk_seperate_file(f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
350 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
351 f->email_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
352
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
353 DEBUG_MAIN(("main: Processing Contact\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
354 if (f->type != PST_TYPE_CONTACT) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
355 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. "
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
356 "Will process anyway\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
357 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
358 if (item->type != PST_TYPE_CONTACT) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
359 DEBUG_MAIN(("main: I have an item that has contact info, but doesn't say that"
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
360 " it is a contact. Type is \"%s\"\n", item->ascii_type));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
361 DEBUG_MAIN(("main: Processing anyway\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
362 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
363 if (item->contact == NULL) { // this is an incorrect situation. Inform user
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
364 DEBUG_MAIN(("main: ERROR. This contact has not been fully parsed. one of the pre-requisties is NULL\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
365 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
366 if (contact_mode == CMODE_VCARD)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
367 write_vcard(f->output, item->contact, item->comment);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
368 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
369 fprintf(f->output, "%s <%s>\n", item->contact->fullname, item->contact->address1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
370 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
371 } else if (item->email != NULL &&
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
372 (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
373 if (mode == MODE_SEPERATE) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
374 mk_seperate_file(f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
375 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
376
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
377 f->email_count++;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
378
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
379 DEBUG_MAIN(("main: seen an email\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
380 write_normal_email(f->output, f->name, item, mode, mode_MH, &pstfile);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
381 } else if (item->type == PST_TYPE_JOURNAL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
382 // deal with journal items
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
383 if (mode == MODE_SEPERATE) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
384 mk_seperate_file(f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
385 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
386 f->email_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
387
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
388 DEBUG_MAIN(("main: Processing Journal Entry\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
389 if (f->type != PST_TYPE_JOURNAL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
390 DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
391 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
392
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
393 /* if (item->type != PST_TYPE_JOURNAL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
394 DEBUG_MAIN(("main: I have an item with journal info, but it's type is \"%s\" \n. Processing...\n",
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
395 item->ascii_type));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
396 }*/
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
397 fprintf(f->output, "BEGIN:VJOURNAL\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
398 if (item->email->subject != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
399 fprintf(f->output, "SUMMARY:%s\n", rfc2426_escape(item->email->subject->subj));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
400 if (item->email->body != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
401 fprintf(f->output, "DESCRIPTION:%s\n", rfc2426_escape(item->email->body));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
402 if (item->journal->start != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
403 fprintf(f->output, "DTSTART;VALUE=DATE-TIME:%s\n", rfc2445_datetime_format(item->journal->start));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
404 fprintf(f->output, "END:VJOURNAL\n\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
405 } else if (item->type == PST_TYPE_APPOINTMENT) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
406 // deal with Calendar appointments
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
407 if (mode == MODE_SEPERATE) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
408 mk_seperate_file(f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
409 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
410 f->email_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
411
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
412 DEBUG_MAIN(("main: Processing Appointment Entry\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
413 if (f->type != PST_TYPE_APPOINTMENT) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
414 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
415 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
416 write_appointment(f->output, item->appointment, item->email, item->create_date, item->modify_date);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
417 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
418 f->skip_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
419 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n",
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
420 item->type, item->ascii_type));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
421 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
422 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
423 f->skip_count++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
424 DEBUG_MAIN(("main: A NULL item was seen\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
425 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
426
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
427 DEBUG_MAIN(("main: Going to next d_ptr\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
428
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
429 check_parent:
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
430 // _pst_freeItem(item);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
431 while (!skip_child && d_ptr->next == NULL && d_ptr->parent != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
432 DEBUG_MAIN(("main: Going to Parent\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
433 head = f->next;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
434 if (f->output != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
435 fclose(f->output);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
436 DEBUG_MAIN(("main: Email Count for folder %s is %i\n", f->dname, f->email_count));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
437 if (output_mode != OUTPUT_QUIET)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
438 printf("\t\"%s\" - %i items done, skipped %i, should have been %i\n",
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
439 f->dname, f->email_count, f->skip_count, f->stored_count);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
440 if (mode == MODE_KMAIL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
441 close_kmail_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
442 else if (mode == MODE_RECURSE)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
443 close_recurse_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
444 else if (mode == MODE_SEPERATE)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
445 close_seperate_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
446 free(f->name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
447 free(f->dname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
448 free(f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
449 f = head;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
450 if (head == NULL) { //we can't go higher. Must be at start?
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
451 DEBUG_MAIN(("main: We are now trying to go above the highest level. We must be finished\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
452 break; //from main while loop
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
453 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
454 d_ptr = d_ptr->parent;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
455 skip_child = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
456 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
457
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
458 if (item != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
459 DEBUG_MAIN(("main: Freeing memory used by item\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
460 _pst_freeItem(item);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
461 item = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
462 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
463
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
464 if (!skip_child)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
465 d_ptr = d_ptr->next;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
466 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
467 skip_child = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
468
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
469 if (d_ptr == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
470 DEBUG_MAIN(("main: d_ptr is now NULL\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
471 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
472 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
473 if (output_mode != OUTPUT_QUIET) printf("Finished.\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
474 DEBUG_MAIN(("main: Finished.\n"));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
475
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
476 pst_close(&pstfile);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
477 // fclose(pstfile.fp);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
478 while (f != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
479 if (f->output != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
480 fclose(f->output);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
481 free(f->name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
482 free(f->dname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
483
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
484 if (mode == MODE_KMAIL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
485 close_kmail_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
486 else if (mode == MODE_RECURSE)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
487 close_recurse_dir();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
488 else if (mode == MODE_SEPERATE)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
489 // DO SOMETHING HERE
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
490 ;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
491 head = f->next;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
492 free (f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
493 f = head;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
494 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
495
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
496 DEBUG_RET();
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
497
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
498 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
499 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
500 void write_email_body(FILE *f, char *body) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
501 char *n = body;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
502 // DEBUG_MAIN(("write_email_body(): \"%s\"\n", body));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
503 DEBUG_ENT("write_email_body");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
504 while (n != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
505 if (strncmp(body, "From ", 5) == 0)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
506 fprintf(f, ">");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
507 if ((n = strchr(body, '\n'))) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
508 n++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
509 fwrite(body, n-body, 1, f); //write just a line
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
510
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
511 body = n;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
512 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
513 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
514 fwrite(body, strlen(body), 1, f);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
515 DEBUG_RET();
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
516 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
517 char *removeCR (char *c) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
518 // converts /r/n to /n
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
519 char *a, *b;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
520 DEBUG_ENT("removeCR");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
521 a = b = c;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
522 while (*a != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
523 *b = *a;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
524 if (*a != '\r')
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
525 b++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
526 a++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
527 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
528 *b = '\0';
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
529 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
530 return c;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
531 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
532 int usage() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
533 DEBUG_ENT("usage");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
534 version();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
535 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
536 printf("OPTIONS:\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
537 printf("\t-c[v|l]\t- Set the Contact output mode. -cv = VCard, -cl = EMail list\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
538 printf("\t-d\t- Debug to file. This is a binary log. Use readlog to print it\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
539 printf("\t-h\t- Help. This screen\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
540 printf("\t-k\t- KMail. Output in kmail format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
541 printf("\t-M\t- MH. Write emails in the MH format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
542 printf("\t-o\t- Output Dir. Directory to write files to. CWD is changed *after* opening pst file\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
543 printf("\t-q\t- Quiet. Only print error messages\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
544 printf("\t-r\t- Recursive. Output in a recursive format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
545 printf("\t-S\t- Seperate. Write emails in the seperate format\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
546 printf("\t-V\t- Version. Display program version\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
547 printf("\t-w\t- Overwrite any output mbox files\n");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
548 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
549 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
550 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
551 int version() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
552 DEBUG_ENT("version");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
553 printf("ReadPST v%s\n", VERSION);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
554 #if BYTE_ORDER == BIG_ENDIAN
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
555 printf("Big Endian implementation being used.\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
556 #elif BYTE_ORDER == LITTLE_ENDIAN
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
557 printf("Little Endian implementation being used.\n");
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
558 #else
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
559 # error "Byte order not supported by this library"
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
560 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
561 #ifdef __GNUC__
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
562 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
563 #endif
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
564 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
565 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
566 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
567 char *mk_kmail_dir(char *fname) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
568 //change to that directory
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
569 //make a directory based on OUTPUT_KMAIL_DIR_TEMPLATE
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
570 //allocate space for OUTPUT_TEMPLATE and form a char* with fname
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
571 //return that value
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
572 char *dir, *out_name, *index;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
573 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
574 DEBUG_ENT("mk_kmail_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
575 if (kmail_chdir != NULL && chdir(kmail_chdir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
576 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
577 DIE(("mk_kmail_dir: Cannot change to directory %s: %s\n", kmail_chdir, strerror(x)));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
578 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
579 dir = malloc(strlen(fname)+strlen(OUTPUT_KMAIL_DIR_TEMPLATE)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
580 sprintf(dir, OUTPUT_KMAIL_DIR_TEMPLATE, fname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
581 dir = check_filename(dir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
582 if (D_MKDIR(dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
583 //error occured
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
584 if (errno != EEXIST) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
585 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
586 DIE(("mk_kmail_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
587 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
588 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
589 kmail_chdir = realloc(kmail_chdir, strlen(dir)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
590 strcpy(kmail_chdir, dir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
591 free (dir);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
592
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
593 //we should remove any existing indexes created by KMail, cause they might be different now
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
594 index = malloc(strlen(fname)+strlen(KMAIL_INDEX)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
595 sprintf(index, KMAIL_INDEX, fname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
596 unlink(index);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
597 free(index);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
598
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
599 out_name = malloc(strlen(fname)+strlen(OUTPUT_TEMPLATE)+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
600 sprintf(out_name, OUTPUT_TEMPLATE, fname);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
601 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
602 return out_name;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
603 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
604 int close_kmail_dir() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
605 // change ..
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
606 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
607 DEBUG_ENT("close_kmail_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
608 if (kmail_chdir != NULL) { //only free kmail_chdir if not NULL. do not change directory
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
609 free(kmail_chdir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
610 kmail_chdir = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
611 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
612 if (chdir("..")) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
613 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
614 DIE(("close_kmail_dir: Cannot move up dir (..): %s\n", strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
615 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
616 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
617 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
618 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
619 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
620 // this will create a directory by that name, then make an mbox file inside
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
621 // that dir. any subsequent dirs will be created by name, and they will
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
622 // contain mbox files
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
623 char *mk_recurse_dir(char *dir) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
624 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
625 char *out_name;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
626 DEBUG_ENT("mk_recurse_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
627 dir = check_filename(dir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
628 if (D_MKDIR (dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
629 if (errno != EEXIST) { // not an error because it exists
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
630 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
631 DIE(("mk_recurse_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
632 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
633 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
634 if (chdir (dir)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
635 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
636 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
637 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
638 out_name = malloc(strlen("mbox")+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
639 strcpy(out_name, "mbox");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
640 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
641 return out_name;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
642 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
643 int close_recurse_dir() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
644 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
645 DEBUG_ENT("close_recurse_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
646 if (chdir("..")) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
647 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
648 DIE(("close_recurse_dir: Cannot go up dir (..): %s\n", strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
649 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
650 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
651 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
652 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
653 char *mk_seperate_dir(char *dir, int overwrite) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
654 #if !defined(WIN32) && !defined(__CYGWIN__)
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
655 DIR * sdir = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
656 struct dirent *dirent = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
657 struct stat *filestat = xmalloc(sizeof(struct stat));
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
658 #endif
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
659
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
660 char *dir_name = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
661 int x = 0, y = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
662 DEBUG_ENT("mk_seperate_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
663 /*#if defined(WIN32) || defined(__CYGWIN__)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
664 DIE(("mk_seperate_dir: Win32 applications cannot use this function yet.\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
665 #endif*/
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
666
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
667 dir_name = xmalloc(strlen(dir)+10);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
668
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
669 do {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
670 if (y == 0)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
671 sprintf(dir_name, "%s", dir);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
672 else
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
673 sprintf(dir_name, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y); // enough for 9 digits allocated above
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
674
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
675 dir_name = check_filename(dir_name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
676 DEBUG_MAIN(("mk_seperate_dir: about to try creating %s\n", dir_name));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
677 if (D_MKDIR(dir_name)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
678 if (errno != EEXIST) { // if there is an error, and it doesn't already exist
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
679 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
680 DIE(("mk_seperate_dir: Cannot create directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
681 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
682 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
683 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
684 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
685 y++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
686 } while (overwrite == 0);
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
687
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
688 if (chdir (dir_name)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
689 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
690 DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
691 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
692
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
693 if (overwrite) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
694 // we should probably delete all files from this directory
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
695 #if !defined(WIN32) && !defined(__CYGWIN__)
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
696 if ((sdir = opendir("./")) == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
697 WARN(("mk_seperate_dir: Cannot open dir \"%s\" for deletion of old contents\n", "./"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
698 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
699 while ((dirent = readdir(sdir)) != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
700 if (lstat(dirent->d_name, filestat) != -1)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
701 if (S_ISREG(filestat->st_mode)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
702 if (unlink(dirent->d_name)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
703 y = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
704 DIE(("mk_seperate_dir: unlink returned error on file %s: %s\n", dirent->d_name, strerror(y)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
705 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
706 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
707 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
708 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
709 #endif
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
710 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
711
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
712 // overwrite will never change during this function, it is just there so that
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
713 // if overwrite is set, we only go through this loop once.
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
714
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
715 // we don't return a filename here cause it isn't necessary.
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
716 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
717 return NULL;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
718 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
719 int close_seperate_dir() {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
720 int x;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
721 DEBUG_ENT("close_seperate_dir");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
722 if (chdir("..")) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
723 x = errno;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
724 DIE(("close_seperate_dir: Cannot go up dir (..): %s\n", strerror(x)));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
725 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
726 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
727 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
728 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
729 int mk_seperate_file(struct file_ll *f) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
730 const int name_offset = 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
731 DEBUG_ENT("mk_seperate_file");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
732 DEBUG_MAIN(("mk_seperate_file: opening next file to save email\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
733 if (f->email_count > 999999999) { // bigger than nine 9's
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
734 DIE(("mk_seperate_file: The number of emails in this folder has become too high to handle"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
735 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
736 sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count + name_offset);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
737 if (f->output != NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
738 fclose(f->output);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
739 f->output = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
740 f->name = check_filename(f->name);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
741 if ((f->output = fopen(f->name, "w")) == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
742 DIE(("mk_seperate_file: Cannot open file to save email \"%s\"\n", f->name));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
743 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
744 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
745 return 0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
746 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
747 char *my_stristr(char *haystack, char *needle) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
748 // my_stristr varies from strstr in that its searches are case-insensitive
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
749 char *x=haystack, *y=needle, *z = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
750 DEBUG_ENT("my_stristr");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
751 if (haystack == NULL || needle == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
752 return NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
753 while (*y != '\0' && *x != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
754 if (tolower(*y) == tolower(*x)) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
755 // move y on one
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
756 y++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
757 if (z == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
758 z = x; // store first position in haystack where a match is made
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
759 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
760 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
761 y = needle; // reset y to the beginning of the needle
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
762 z = NULL; // reset the haystack storage point
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
763 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
764 x++; // advance the search in the haystack
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
765 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
766 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
767 return z;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
768 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
769 char *check_filename(char *fname) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
770 char *t = fname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
771 DEBUG_ENT("check_filename");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
772 if (t == NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
773 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
774 return fname;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
775 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
776 while ((t = strpbrk(t, "/\\:")) != NULL) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
777 // while there are characters in the second string that we don't want
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
778 *t = '_'; //replace them with an underscore
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
779 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
780 DEBUG_RET();
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
781 return fname;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
782 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
783 char *rfc2426_escape(char *str) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
784 static char* buf = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
785 char *ret, *a, *b;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
786 int x = 0, y, z;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
787 DEBUG_ENT("rfc2426_escape");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
788 if (str == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
789 ret = str;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
790 else {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
791
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
792 // calculate space required to escape all the following characters
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
793 x = strlen(str) +(y=(chr_count(str, ',')*2) + (chr_count(str, '\\')*2) + (chr_count(str, ';')*2) + (chr_count(str, '\n')*2));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
794 z = chr_count(str, '\r');
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
795 if (y == 0 && z == 0)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
796 // there isn't any extra space required
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
797 ret = str;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
798 else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
799 buf = (char*) realloc(buf, x+1);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
800 a = str;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
801 b = buf;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
802 while (*a != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
803 switch(*a) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
804 case ',' :
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
805 case '\\':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
806 case ';' :
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
807 case '\n':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
808 *(b++)='\\';
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
809 *b=*a;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
810 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
811 case '\r':
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
812 break;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
813 default:
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
814 *b=*a;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
815 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
816 b++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
817 a++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
818 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
819 *b = '\0';
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
820 ret = buf;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
821 }
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
822 }
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
823 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
824 return ret;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
825 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
826 int chr_count(char *str, char x) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
827 int r = 0;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
828 while (*str != '\0') {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
829 if (*str == x)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
830 r++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
831 str++;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
832 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
833 return r;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
834 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
835 char *rfc2425_datetime_format(FILETIME *ft) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
836 static char * buffer = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
837 struct tm *stm = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
838 DEBUG_ENT("rfc2425_datetime_format");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
839 if (buffer == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
840 buffer = malloc(30); // should be enough for the date as defined below
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
841
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
842 stm = fileTimeToStructTM(ft);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
843 //Year[4]-Month[2]-Day[2] Hour[2]:Min[2]:Sec[2]
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
844 if (strftime(buffer, 30, "%Y-%m-%dT%H:%M:%SZ", stm)==0) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
845 DEBUG_INFO(("Problem occured formatting date\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
846 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
847 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
848 return buffer;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
849 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
850 char *rfc2445_datetime_format(FILETIME *ft) {
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
851 static char* buffer = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
852 struct tm *stm = NULL;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
853 DEBUG_ENT("rfc2445_datetime_format");
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
854 if (buffer == NULL)
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
855 buffer = malloc(30); // should be enough
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
856 stm = fileTimeToStructTM(ft);
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
857 if (strftime(buffer, 30, "%Y%m%dT%H%M%SZ", stm)==0) {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
858 DEBUG_INFO(("Problem occured formatting date\n"));
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
859 }
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
860 DEBUG_RET();
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
861 return buffer;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
862 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
863 // The sole purpose of this function is to bypass the pseudo-header prologue
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
864 // that Microsoft Outlook inserts at the beginning of the internet email
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
865 // headers for emails stored in their "Personal Folders" files.
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
866 char *skip_header_prologue(char *headers) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
867 const char *bad = "Microsoft Mail Internet Headers";
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
868
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
869 if ( strncmp(headers, bad, strlen(bad)) == 0 ) {
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
870 // Found the offensive header prologue
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
871 char *pc;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
872
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
873 pc = strchr(headers, '\n');
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
874 return pc + 1;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
875 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
876
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
877 return headers;
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
878 }
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
879
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
880 // vim:sw=4 ts=4:
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
881 // vim600: set foldlevel=0 foldmethod=marker:
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
882 void write_separate_attachment(char f_name[], pst_item_attach* current_attach, int attach_num, pst_file* pst)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
883 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
884 FILE *fp = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
885 int x = 0;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
886 char *temp;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
887
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
888 check_filename(f_name);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
889 if (current_attach->filename2 == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
890 temp = xmalloc(strlen(f_name)+15);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
891 sprintf(temp, "%s-attach%i", f_name, attach_num);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
892 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
893 temp = xmalloc(strlen(f_name)+strlen(current_attach->filename2)+15);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
894 do {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
895 if (fp != NULL) fclose(fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
896 if (x == 0)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
897 sprintf(temp, "%s-%s", f_name, current_attach->filename2);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
898 else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
899 sprintf(temp, "%s-%s-%i", f_name, current_attach->filename2, x);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
900 } while ((fp = fopen(temp, "r"))!=NULL && ++x < 99999999);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
901 if (x > 99999999) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
902 DIE(("error finding attachment name. exhausted possibilities to %s\n", temp));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
903 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
904 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
905 DEBUG_MAIN(("write_separate_attachment: Saving attachment to %s\n", temp));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
906 if ((fp = fopen(temp, "w")) == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
907 WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
908 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
909 if (current_attach->data != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
910 fwrite(current_attach->data, 1, current_attach->size, fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
911 else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
912 pst_attach_to_file(pst, current_attach, fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
913 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
914 fclose(fp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
915 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
916 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
917
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
918 void write_inline_attachment(FILE* f_output, pst_item_attach* current_attach, char boundary[], pst_file* pst)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
919 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
920 char *enc; // base64 encoded attachment
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
921 DEBUG_MAIN(("write_inline_attachment: Attachment Size is %i\n", item->current_attach->size));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
922 DEBUG_MAIN(("write_inline_attachment: Attachment Pointer is %p\n", item->current_attach->data));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
923 if (current_attach->data != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
924 if ((enc = base64_encode (current_attach->data, current_attach->size)) == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
925 DEBUG_MAIN(("write_inline_attachment: ERROR base64_encode returned NULL. Must have failed\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
926 return;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
927 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
928 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
929 if (boundary) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
930 fprintf(f_output, "\n--%s\n", boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
931 if (current_attach->mimetype == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
932 fprintf(f_output, "Content-type: %s\n", MIME_TYPE_DEFAULT);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
933 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
934 fprintf(f_output, "Content-type: %s\n", current_attach->mimetype);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
935 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
936 fprintf(f_output, "Content-transfer-encoding: base64\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
937 if (current_attach->filename2 == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
938 fprintf(f_output, "Content-Disposition: inline\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
939 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
940 fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
941 current_attach->filename2);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
942 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
943 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
944 if (current_attach->data != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
945 fwrite(enc, 1, strlen(enc), f_output);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
946 DEBUG_MAIN(("Attachment Size after encoding is %i\n", strlen(enc)));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
947 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
948 pst_attach_to_file_base64(pst, current_attach, f_output);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
949 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
950 fprintf(f_output, "\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
951 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
952
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
953 void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
954 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
955 char *boundary = NULL; // the boundary marker between multipart sections
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
956 char *temp = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
957 int attach_num, base64_body = 0;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
958 time_t em_time;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
959 char *c_time;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
960
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
961 // convert the sent date if it exists, or set it to a fixed date
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
962 if (item->email->sent_date != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
963 em_time = fileTimeToUnixTime(item->email->sent_date, 0);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
964 c_time = ctime(&em_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
965 if (c_time != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
966 c_time[strlen(c_time)-1] = '\0'; //remove end \n
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
967 else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
968 c_time = "Fri Dec 28 12:06:21 2001";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
969 } else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
970 c_time= "Fri Dec 28 12:06:21 2001";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
971
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
972 // we will always look at the header to discover some stuff
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
973 if (item->email->header != NULL ) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
974 char *b1, *b2;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
975 // see if there is a boundary variable there
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
976 // this search MUST be made case insensitive (DONE).
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
977 // Also, some check to find out if we
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
978 // are looking at the boundary associated with content-type, and that the content
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
979 // type really is "multipart"
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
980
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
981 removeCR(item->email->header);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
982
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
983 if ((b2 = my_stristr(item->email->header, "boundary=")) != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
984 b2 += strlen("boundary="); // move boundary to first char of marker
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
985
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
986 if (*b2 == '"') {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
987 b2++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
988 b1 = strchr(b2, '"'); // find terminating quote
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
989 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
990 b1 = b2;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
991 while (isgraph(*b1)) // find first char that isn't part of boundary
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
992 b1++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
993 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
994
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
995 boundary = malloc ((b1-b2)+1); //malloc that length
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
996 memset (boundary, 0, (b1-b2)+1); // blank it
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
997 strncpy(boundary, b2, b1-b2); // copy boundary to another variable
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
998 b1 = b2 = boundary;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
999 while (*b2 != '\0') { // remove any CRs and Tabs
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1000 if (*b2 != '\n' && *b2 != '\r' && *b2 != '\t') {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1001 *b1 = *b2;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1002 b1++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1003 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1004 b2++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1005 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1006 *b1 = '\0';
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1007
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1008 DEBUG_MAIN(("write_normal_email: Found boundary of - %s\n", boundary));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1009 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1010 DEBUG_MAIN(("write_normal_email: boundary not found in header\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1011 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1012
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1013 // also possible to set 7bit encoding detection here.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1014 if ((b2 = my_stristr(item->email->header, "Content-Transfer-Encoding:")) != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1015 if ((b2 = strchr(b2, ':')) != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1016 b2++; // skip to the : at the end of the string
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1017
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1018 while (*b2 == ' ' || *b2 == '\t')
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1019 b2++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1020 if (pst_strincmp(b2, "base64", 6)==0) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1021 DEBUG_MAIN(("body is base64 encoded\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1022 base64_body = 1;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1023 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1024 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1025 DEBUG_WARN(("found a ':' during the my_stristr, but not after that..\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1026 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1027 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1028
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1029 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1030
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1031 DEBUG_MAIN(("write_normal_email: About to print Header\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1032
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1033 if (item != NULL && item->email != NULL && item->email->subject != NULL &&
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1034 item->email->subject->subj != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1035 DEBUG_EMAIL(("item->email->subject->subj = %p\n", item->email->subject->subj));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1036 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1037
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1038 if (item->email->header != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1039 // some of the headers we get from the file are not properly defined.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1040 // they can contain some email stuff too. We will cut off the header
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1041 // when we see a \n\n or \r\n\r\n
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1042 temp = strstr(item->email->header, "\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1043
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1044 if (temp != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1045 DEBUG_MAIN(("write_normal_email: Found body text in header\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1046 *temp = '\0';
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
1047 } else {
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
1048 temp = item->email->header + strlen(item->email->header) - 1;
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
1049 if(*temp == '\n')
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
1050 *temp = '\0';
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1051 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1052
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1053 if (mode != MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1054 char *soh = NULL; // real start of headers.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1055 // don't put rubbish in if we are doing seperate
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1056 fprintf(f_output, "From \"%s\" %s\n", item->email->outlook_sender_name, c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1057 soh = skip_header_prologue(item->email->header);
26
9eeba3f4ca4b more cleanup from Arne
carl
parents: 25
diff changeset
1058 fprintf(f_output, "%s\n", soh);
25
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1059 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1060 fprintf(f_output, "%s\n", item->email->header);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1061 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1062 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1063 //make up our own header!
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1064 if (mode != MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1065 // don't want this first line for this mode
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1066 if (item->email->outlook_sender_name != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1067 temp = item->email->outlook_sender_name;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1068 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1069 temp = "(readpst_null)";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1070 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1071 fprintf(f_output, "From \"%s\" %s\n", temp, c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1072 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1073 if ((temp = item->email->outlook_sender) == NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1074 temp = "";
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1075 fprintf(f_output, "From: \"%s\" <%s>\n", item->email->outlook_sender_name, temp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1076 if (item->email->subject != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1077 fprintf(f_output, "Subject: %s\n", item->email->subject->subj);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1078 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1079 fprintf(f_output, "Subject: \n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1080 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1081 fprintf(f_output, "To: %s\n", item->email->sentto_address);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1082 if (item->email->cc_address != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1083 fprintf(f_output, "Cc: %s\n", item->email->cc_address);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1084 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1085 if (item->email->sent_date != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1086 c_time = (char*) xmalloc(C_TIME_SIZE);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1087 strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", gmtime(&em_time));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1088 fprintf(f_output, "Date: %s\n", c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1089 free(c_time);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1090 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1091
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1092 fprintf(f_output, "MIME-Version: 1.0\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1093 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1094
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1095 if (boundary == NULL && (item->attach ||(item->email->body && item->email->htmlbody)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1096 || item->email->rtf_compressed || item->email->encrypted_body
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1097 || item->email->encrypted_htmlbody)) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1098 // we need to create a boundary here.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1099 DEBUG_EMAIL(("write_normal_email: must create own boundary. oh dear.\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1100 boundary = malloc(50 * sizeof(char)); // allow 50 chars for boundary
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1101 boundary[0] = '\0';
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1102 sprintf(boundary, "--boundary-LibPST-iamunique-%i_-_-", rand());
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1103 DEBUG_EMAIL(("write_normal_email: created boundary is %s\n", boundary));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1104
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1105 /* If boundary != NULL, then it has already been printed with existing
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1106 * headers. Otherwise we generate it here and print it.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1107 */
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1108 if (item->attach != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1109 // write the boundary stuff if we have attachments
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1110 fprintf(f_output, "Content-type: multipart/mixed;\n\tboundary=\"%s\"\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1111 boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1112 } else if (boundary != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1113 // else if we have multipart/alternative then tell it so
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1114 fprintf(f_output, "Content-type: multipart/alternative;\n\tboundary=\"%s\"\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1115 boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1116 } else if (item->email->htmlbody) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1117 fprintf(f_output, "Content-type: text/html\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1118 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1119 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1120
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1121 fprintf(f_output, "\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1122
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1123 DEBUG_MAIN(("write_normal_email: About to print Body\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1124
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1125 if (item->email->body != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1126 if (boundary) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1127 fprintf(f_output, "\n--%s\n", boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1128 fprintf(f_output, "Content-type: text/plain\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1129 if (base64_body)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1130 fprintf(f_output, "Content-Transfer-Encoding: base64\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1131 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1132 removeCR(item->email->body);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1133 if (base64_body)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1134 write_email_body(f_output, base64_encode(item->email->body,
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1135 strlen(item->email->body)));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1136 else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1137 write_email_body(f_output, item->email->body);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1138 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1139
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1140 if (item->email->htmlbody != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1141 if (boundary) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1142 fprintf(f_output, "\n--%s\n", boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1143 fprintf(f_output, "Content-type: text/html\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1144 if (base64_body)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1145 fprintf(f_output, "Content-Transfer-Encoding: base64\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1146 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1147 removeCR(item->email->htmlbody);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1148 if (base64_body)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1149 write_email_body(f_output, base64_encode(item->email->htmlbody,
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1150 strlen(item->email->htmlbody)));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1151 else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1152 write_email_body(f_output, item->email->htmlbody);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1153 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1154
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1155 if (item->email->rtf_compressed != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1156 DEBUG_MAIN(("Adding RTF body as attachment\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1157 item->current_attach = (pst_item_attach*)xmalloc(sizeof(pst_item_attach));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1158 memset(item->current_attach, 0, sizeof(pst_item_attach));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1159 item->current_attach->next = item->attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1160 item->attach = item->current_attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1161 item->current_attach->data = lzfu_decompress(item->email->rtf_compressed);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1162 item->current_attach->filename2 = xmalloc(strlen(RTF_ATTACH_NAME)+2);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1163 strcpy(item->current_attach->filename2, RTF_ATTACH_NAME);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1164 item->current_attach->mimetype = xmalloc(strlen(RTF_ATTACH_TYPE)+2);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1165 strcpy(item->current_attach->mimetype, RTF_ATTACH_TYPE);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1166 memcpy(&(item->current_attach->size), item->email->rtf_compressed+sizeof(int32_t), sizeof(int32_t));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1167 LE32_CPU(item->current_attach->size);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1168 // item->email->rtf_compressed = ;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1169 // attach_num++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1170 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1171 if (item->email->encrypted_body || item->email->encrypted_htmlbody) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1172 // if either the body or htmlbody is encrypted, add them as attachments
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1173 if (item->email->encrypted_body) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1174 DEBUG_MAIN(("Adding Encrypted Body as attachment\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1175 item->current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1176 memset(item->current_attach, 0, sizeof(pst_item_attach));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1177 item->current_attach->next = item->attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1178 item->attach = item->current_attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1179
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1180 item->current_attach->data = item->email->encrypted_body;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1181 item->current_attach->size = item->email->encrypted_body_size;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1182 item->email->encrypted_body = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1183 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1184 if (item->email->encrypted_htmlbody) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1185 DEBUG_MAIN(("Adding encrypted HTML body as attachment\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1186 item->current_attach = (pst_item_attach*) xmalloc(sizeof(pst_item_attach));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1187 memset(item->current_attach, 0, sizeof(pst_item_attach));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1188 item->current_attach->next = item->attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1189 item->attach = item->current_attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1190
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1191 item->current_attach->data = item->email->encrypted_htmlbody;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1192 item->current_attach->size = item->email->encrypted_htmlbody_size;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1193 item->email->encrypted_htmlbody = NULL;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1194 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1195 write_email_body(f_output, "The body of this email is encrypted. This isn't supported yet, but the body is now an attachment\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1196 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1197 // attachments
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1198 attach_num = 0;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1199 for(item->current_attach = item->attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1200 item->current_attach;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1201 item->current_attach = item->current_attach->next) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1202 DEBUG_MAIN(("write_normal_email: Attempting Attachment encoding\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1203 if (item->current_attach->data == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1204 DEBUG_MAIN(("write_normal_email: Data of attachment is NULL!. Size is supposed to be %i\n", item->current_attach->size));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1205 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1206 attach_num++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1207 if (mode == MODE_SEPERATE && !mode_MH)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1208 write_separate_attachment(f_name, item->current_attach, attach_num, pst);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1209 else
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1210 write_inline_attachment(f_output, item->current_attach, boundary, pst);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1211 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1212 if (mode != MODE_SEPERATE) { /* do not add a boundary after the last attachment for mode_MH */
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1213 DEBUG_MAIN(("write_normal_email: Writing buffer between emails\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1214 if (boundary)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1215 fprintf(f_output, "\n--%s--\n", boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1216 fprintf(f_output, "\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1217 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1218 if (boundary)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1219 free (boundary);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1220 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1221
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1222 void write_vcard(FILE* f_output, pst_item_contact* contact, char comment[])
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1223 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1224 // the specification I am following is (hopefully) RFC2426 vCard Mime Directory Profile
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1225 fprintf(f_output, "BEGIN:VCARD\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1226 fprintf(f_output, "FN:%s\n", rfc2426_escape(contact->fullname));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1227 fprintf(f_output, "N:%s;%s;%s;%s;%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1228 rfc2426_escape((contact->surname==NULL?"":contact->surname)),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1229 rfc2426_escape((contact->first_name==NULL?"":contact->first_name)),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1230 rfc2426_escape((contact->middle_name==NULL?"":contact->middle_name)),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1231 rfc2426_escape((contact->display_name_prefix==NULL?"":contact->display_name_prefix)),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1232 rfc2426_escape((contact->suffix==NULL?"":contact->suffix)));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1233 if (contact->nickname != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1234 fprintf(f_output, "NICKNAME:%s\n", rfc2426_escape(contact->nickname));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1235 if (contact->address1 != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1236 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address1));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1237 if (contact->address2 != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1238 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address2));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1239 if (contact->address3 != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1240 fprintf(f_output, "EMAIL:%s\n", rfc2426_escape(contact->address3));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1241 if (contact->birthday != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1242 fprintf(f_output, "BDAY:%s\n", rfc2425_datetime_format(contact->birthday));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1243 if (contact->home_address != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1244 fprintf(f_output, "ADR;TYPE=home:%s;%s;%s;%s;%s;%s;%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1245 rfc2426_escape((contact->home_po_box!=NULL?contact->home_po_box:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1246 "", // extended Address
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1247 rfc2426_escape((contact->home_street!=NULL?contact->home_street:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1248 rfc2426_escape((contact->home_city!=NULL?contact->home_city:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1249 rfc2426_escape((contact->home_state!=NULL?contact->home_state:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1250 rfc2426_escape((contact->home_postal_code!=NULL?contact->home_postal_code:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1251 rfc2426_escape((contact->home_country!=NULL?contact->home_country:"")));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1252 fprintf(f_output, "LABEL;TYPE=home:%s\n", rfc2426_escape(contact->home_address));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1253 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1254 if (contact->business_address != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1255 fprintf(f_output, "ADR;TYPE=work:%s;%s;%s;%s;%s;%s;%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1256 rfc2426_escape((contact->business_po_box!=NULL?contact->business_po_box:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1257 "", // extended Address
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1258 rfc2426_escape((contact->business_street!=NULL?contact->business_street:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1259 rfc2426_escape((contact->business_city!=NULL?contact->business_city:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1260 rfc2426_escape((contact->business_state!=NULL?contact->business_state:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1261 rfc2426_escape((contact->business_postal_code!=NULL?contact->business_postal_code:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1262 rfc2426_escape((contact->business_country!=NULL?contact->business_country:"")));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1263 fprintf(f_output, "LABEL;TYPE=work:%s\n", rfc2426_escape(contact->business_address));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1264 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1265 if (contact->other_address != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1266 fprintf(f_output, "ADR;TYPE=postal:%s;%s;%s;%s;%s;%s;%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1267 rfc2426_escape((contact->other_po_box != NULL ?
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1268 contact->business_po_box:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1269 "", // extended Address
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1270 rfc2426_escape((contact->other_street != NULL ?
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1271 contact->other_street:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1272 rfc2426_escape((contact->other_city != NULL ?
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1273 contact->other_city:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1274 rfc2426_escape((contact->other_state != NULL ?
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1275 contact->other_state:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1276 rfc2426_escape((contact->other_postal_code != NULL ?
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1277 contact->other_postal_code:"")),
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1278 rfc2426_escape((contact->other_country != NULL ?
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1279 contact->other_country:"")));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1280 fprintf(f_output, "ADR;TYPE=postal:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1281 rfc2426_escape(contact->other_address));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1282 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1283 if (contact->business_fax != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1284 fprintf(f_output, "TEL;TYPE=work,fax:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1285 rfc2426_escape(contact->business_fax));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1286 if (contact->business_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1287 fprintf(f_output, "TEL;TYPE=work,voice:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1288 rfc2426_escape(contact->business_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1289 if (contact->business_phone2 != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1290 fprintf(f_output, "TEL;TYPE=work,voice:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1291 rfc2426_escape(contact->business_phone2));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1292 if (contact->car_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1293 fprintf(f_output, "TEL;TYPE=car,voice:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1294 rfc2426_escape(contact->car_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1295 if (contact->home_fax != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1296 fprintf(f_output, "TEL;TYPE=home,fax:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1297 rfc2426_escape(contact->home_fax));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1298 if (contact->home_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1299 fprintf(f_output, "TEL;TYPE=home,voice:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1300 rfc2426_escape(contact->home_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1301 if (contact->home_phone2 != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1302 fprintf(f_output, "TEL;TYPE=home,voice:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1303 rfc2426_escape(contact->home_phone2));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1304 if (contact->isdn_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1305 fprintf(f_output, "TEL;TYPE=isdn:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1306 rfc2426_escape(contact->isdn_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1307 if (contact->mobile_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1308 fprintf(f_output, "TEL;TYPE=cell,voice:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1309 rfc2426_escape(contact->mobile_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1310 if (contact->other_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1311 fprintf(f_output, "TEL;TYPE=msg:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1312 rfc2426_escape(contact->other_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1313 if (contact->pager_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1314 fprintf(f_output, "TEL;TYPE=pager:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1315 rfc2426_escape(contact->pager_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1316 if (contact->primary_fax != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1317 fprintf(f_output, "TEL;TYPE=fax,pref:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1318 rfc2426_escape(contact->primary_fax));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1319 if (contact->primary_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1320 fprintf(f_output, "TEL;TYPE=phone,pref:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1321 rfc2426_escape(contact->primary_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1322 if (contact->radio_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1323 fprintf(f_output, "TEL;TYPE=pcs:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1324 rfc2426_escape(contact->radio_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1325 if (contact->telex != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1326 fprintf(f_output, "TEL;TYPE=bbs:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1327 rfc2426_escape(contact->telex));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1328 if (contact->job_title != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1329 fprintf(f_output, "TITLE:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1330 rfc2426_escape(contact->job_title));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1331 if (contact->profession != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1332 fprintf(f_output, "ROLE:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1333 rfc2426_escape(contact->profession));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1334 if (contact->assistant_name != NULL
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1335 || contact->assistant_phone != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1336 fprintf(f_output, "AGENT:BEGIN:VCARD\\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1337 if (contact->assistant_name != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1338 fprintf(f_output, "FN:%s\\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1339 rfc2426_escape(contact->assistant_name));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1340 if (contact->assistant_phone != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1341 fprintf(f_output, "TEL:%s\\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1342 rfc2426_escape(contact->assistant_phone));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1343 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1344 if (contact->company_name != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1345 fprintf(f_output, "ORG:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1346 rfc2426_escape(contact->company_name));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1347 if (comment != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1348 fprintf(f_output, "NOTE:%s\n", rfc2426_escape(comment));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1349
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1350 fprintf(f_output, "VERSION: 3.0\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1351 fprintf(f_output, "END:VCARD\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1352 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1353
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1354 void write_appointment(FILE* f_output, pst_item_appointment* appointment,
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1355 pst_item_email* email, FILETIME* create_date, FILETIME* modify_date)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1356 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1357 fprintf(f_output, "BEGIN:VEVENT\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1358 if (create_date != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1359 fprintf(f_output, "CREATED:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1360 rfc2445_datetime_format(create_date));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1361 if (modify_date != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1362 fprintf(f_output, "LAST-MOD:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1363 rfc2445_datetime_format(modify_date));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1364 if (email != NULL && email->subject != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1365 fprintf(f_output, "SUMMARY:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1366 rfc2426_escape(email->subject->subj));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1367 if (email != NULL && email->body != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1368 fprintf(f_output, "DESCRIPTION:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1369 rfc2426_escape(email->body));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1370 if (appointment != NULL && appointment->start != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1371 fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1372 rfc2445_datetime_format(appointment->start));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1373 if (appointment != NULL && appointment->end != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1374 fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1375 rfc2445_datetime_format(appointment->end));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1376 if (appointment != NULL && appointment->location != NULL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1377 fprintf(f_output, "LOCATION:%s\n",
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1378 rfc2426_escape(appointment->location));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1379 if (appointment != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1380 switch (appointment->showas) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1381 case PST_FREEBUSY_TENTATIVE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1382 fprintf(f_output, "STATUS:TENTATIVE\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1383 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1384 case PST_FREEBUSY_FREE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1385 // mark as transparent and as confirmed
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1386 fprintf(f_output, "TRANSP:TRANSPARENT\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1387 case PST_FREEBUSY_BUSY:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1388 case PST_FREEBUSY_OUT_OF_OFFICE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1389 fprintf(f_output, "STATUS:CONFIRMED\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1390 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1391 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1392 switch (appointment->label) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1393 case PST_APP_LABEL_NONE:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1394 fprintf(f_output, "CATEGORIES:NONE\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1395 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1396 case PST_APP_LABEL_IMPORTANT:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1397 fprintf(f_output, "CATEGORIES:IMPORTANT\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1398 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1399 case PST_APP_LABEL_BUSINESS:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1400 fprintf(f_output, "CATEGORIES:BUSINESS\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1401 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1402 case PST_APP_LABEL_PERSONAL:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1403 fprintf(f_output, "CATEGORIES:PERSONAL\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1404 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1405 case PST_APP_LABEL_VACATION:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1406 fprintf(f_output, "CATEGORIES:VACATION\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1407 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1408 case PST_APP_LABEL_MUST_ATTEND:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1409 fprintf(f_output, "CATEGORIES:MUST-ATTEND\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1410 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1411 case PST_APP_LABEL_TRAVEL_REQ:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1412 fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1413 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1414 case PST_APP_LABEL_NEEDS_PREP:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1415 fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1416 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1417 case PST_APP_LABEL_BIRTHDAY:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1418 fprintf(f_output, "CATEGORIES:BIRTHDAY\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1419 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1420 case PST_APP_LABEL_ANNIVERSARY:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1421 fprintf(f_output, "CATEGORIES:ANNIVERSARY\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1422 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1423 case PST_APP_LABEL_PHONE_CALL:
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1424 fprintf(f_output, "CATEGORIES:PHONE-CALL\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1425 break;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1426 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1427 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1428 fprintf(f_output, "END:VEVENT\n\n");
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1429 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1430
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1431 void create_enter_dir(struct file_ll* f, char file_as[], int mode, int overwrite)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1432 {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1433 if (mode == MODE_KMAIL)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1434 f->name = mk_kmail_dir(file_as); //create directory and form filename
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1435 else if (mode == MODE_RECURSE)
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1436 f->name = mk_recurse_dir(file_as);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1437 else if (mode == MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1438 // do similar stuff to recurse here.
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1439 mk_seperate_dir(file_as, overwrite);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1440 f->name = (char*) xmalloc(10);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1441 memset(f->name, 0, 10);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1442 // sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->email_count);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1443 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1444 f->name = (char*) xmalloc(strlen(file_as)+strlen(OUTPUT_TEMPLATE+1));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1445 sprintf(f->name, OUTPUT_TEMPLATE, file_as);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1446 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1447
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1448 f->dname = (char*) xmalloc(strlen(file_as)+1);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1449 strcpy(f->dname, file_as);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1450
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1451 if (overwrite != 1) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1452 int x = 0;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1453 char *temp = (char*) xmalloc (strlen(f->name)+10); //enough room for 10 digits
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1454
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1455 sprintf(temp, "%s", f->name);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1456 temp = check_filename(temp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1457 while ((f->output = fopen(temp, "r")) != NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1458 DEBUG_MAIN(("create_enter_dir: need to increase filename cause one already exists with that name\n"));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1459 DEBUG_MAIN(("create_enter_dir: - increasing it to %s%d\n", f->name, x));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1460 x++;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1461 sprintf(temp, "%s%08d", f->name, x);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1462 DEBUG_MAIN(("create_enter_dir: - trying \"%s\"\n", f->name));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1463 if (x == 99999999) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1464 DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1465 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1466 fclose(f->output);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1467 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1468 if (x > 0) { //then the f->name should change
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1469 free (f->name);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1470 f->name = temp;
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1471 } else {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1472 free(temp);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1473 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1474 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1475
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1476 DEBUG_MAIN(("create_enter_dir: f->name = %s\nitem->folder_name = %s\n", f->name, file_as));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1477 if (mode != MODE_SEPERATE) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1478 f->name = check_filename(f->name);
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1479 if ((f->output = fopen(f->name, "w")) == NULL) {
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1480 DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name));
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1481 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1482 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1483 }
73e8959cd86b patches from Arne
carl
parents: 21
diff changeset
1484