annotate src/readpst.c @ 35:b2f247463b83 stable-0-5-6

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