annotate src/readpst.c @ 31:b88ceb81dba2

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