diff --git a/AUTHORS b/AUTHORS index fbf48fb..3a5cb40 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,41 +1,41 @@ Original version by: Dave Smith Dave Smith Current maintainer: Carl Byington With contributions by: Joseph Nahmias -- bounces Joseph Nahmias Arne Ahrend Nigel Horne Chris Halls Stevens Miller Brad Hards Alexander Grau Antonio Palama Sean Loaring James Woodcock Joachim Metz Robert Simpson Justin Greer Bharath Acharya Robert Harris David Cuadrado Chris Eagle Fridrich Strba Emmanuel Andry hggdh bharder Chris White Roberto Polli Lee Ayres Hugo DesRosiers Kenneth Berland - + Leo 'costela' Antunes Testing team: Mac OSX - Michael Watson Cygwin/Mingw - Fridrich Strba Cygwin - Chris Eagle diff --git a/src/libpst.h b/src/libpst.h index afe8a1c..a6751f9 100644 --- a/src/libpst.h +++ b/src/libpst.h @@ -1,1126 +1,1138 @@ /*** * libpst.h * Part of LibPST project * Written by David Smith * dave.s@earthcorp.com */ // LibPST - Library for Accessing Outlook .pst files // Dave Smith - davesmith@users.sourceforge.net #ifndef __PST_LIBPST_H #define __PST_LIBPST_H #include "common.h" // switch to maximal packing for all structures in the libpst interface // this is reverted at the end of this file #ifdef _MSC_VER #pragma pack(push, 1) #endif #if defined(__GNUC__) || defined (__SUNPRO_C) || defined(__SUNPRO_CC) #pragma pack(1) #endif #define PST_TYPE_NOTE 1 #define PST_TYPE_SCHEDULE 2 #define PST_TYPE_APPOINTMENT 8 #define PST_TYPE_CONTACT 9 #define PST_TYPE_JOURNAL 10 #define PST_TYPE_STICKYNOTE 11 #define PST_TYPE_TASK 12 #define PST_TYPE_OTHER 13 #define PST_TYPE_REPORT 14 // defines types of possible encryption #define PST_NO_ENCRYPT 0 #define PST_COMP_ENCRYPT 1 #define PST_ENCRYPT 2 // defines different types of mappings #define PST_MAP_ATTRIB (uint32_t)1 #define PST_MAP_HEADER (uint32_t)2 // define my custom email attributes. #define PST_ATTRIB_HEADER -1 // defines types of free/busy values for appointment->showas #define PST_FREEBUSY_FREE 0 #define PST_FREEBUSY_TENTATIVE 1 #define PST_FREEBUSY_BUSY 2 #define PST_FREEBUSY_OUT_OF_OFFICE 3 // defines labels for appointment->label #define PST_APP_LABEL_NONE 0 #define PST_APP_LABEL_IMPORTANT 1 #define PST_APP_LABEL_BUSINESS 2 #define PST_APP_LABEL_PERSONAL 3 #define PST_APP_LABEL_VACATION 4 #define PST_APP_LABEL_MUST_ATTEND 5 #define PST_APP_LABEL_TRAVEL_REQ 6 #define PST_APP_LABEL_NEEDS_PREP 7 #define PST_APP_LABEL_BIRTHDAY 8 #define PST_APP_LABEL_ANNIVERSARY 9 #define PST_APP_LABEL_PHONE_CALL 10 // define type of recuring event #define PST_APP_RECUR_NONE 0 #define PST_APP_RECUR_DAILY 1 #define PST_APP_RECUR_WEEKLY 2 #define PST_APP_RECUR_MONTHLY 3 #define PST_APP_RECUR_YEARLY 4 // define attachment types #define PST_ATTACH_NONE 0 #define PST_ATTACH_BY_VALUE 1 #define PST_ATTACH_BY_REF 2 #define PST_ATTACH_BY_REF_RESOLV 3 #define PST_ATTACH_BY_REF_ONLY 4 #define PST_ATTACH_EMBEDDED 5 #define PST_ATTACH_OLE 6 +// define flags +#define PST_FLAG_READ 0x01 +#define PST_FLAG_UNMODIFIED 0x02 +#define PST_FLAG_SUBMIT 0x04 +#define PST_FLAG_UNSENT 0x08 +#define PST_FLAG_HAS_ATTACHMENT 0x10 +#define PST_FLAG_FROM_ME 0x20 +#define PST_FLAG_ASSOCIATED 0x40 +#define PST_FLAG_RESEND 0x80 +#define PST_FLAG_RN_PENDING 0x100 +#define PST_FLAG_NRN_PENDING 0x200 + typedef struct pst_entryid { int32_t u1; char entryid[16]; uint32_t id; } pst_entryid; typedef struct pst_index_ll { uint64_t i_id; uint64_t offset; uint64_t size; int64_t u1; struct pst_index_ll *next; } pst_index_ll; typedef struct pst_id2_tree { uint64_t id2; pst_index_ll *id; struct pst_id2_tree *child; struct pst_id2_tree *next; } pst_id2_tree; typedef struct pst_desc_tree { uint64_t d_id; uint64_t parent_d_id; pst_index_ll *desc; pst_index_ll *assoc_tree; int32_t no_child; struct pst_desc_tree *prev; struct pst_desc_tree *next; struct pst_desc_tree *parent; struct pst_desc_tree *child; struct pst_desc_tree *child_tail; } pst_desc_tree; /** The string is either utf8 encoded, or it is in the code page * specified by the containing mapi object. It can be forced into * utf8 by calling pst_convert_utf8() or pst_convert_utf8_null(). */ typedef struct pst_string { /** @li 1 true * @li 0 false */ int is_utf8; char *str; } pst_string; /** a simple wrapper for binary blobs */ typedef struct pst_binary { size_t size; char *data; } pst_binary; /** This contains the email related mapi elements */ typedef struct pst_item_email { /** mapi element 0x0e06 PR_MESSAGE_DELIVERY_TIME */ FILETIME *arrival_date; /** mapi element 0x0002 PR_ALTERNATE_RECIPIENT_ALLOWED * @li 1 true * @li 0 not set * @li -1 false */ int autoforward; /** mapi element 0x0e03 PR_DISPLAY_CC */ pst_string cc_address; /** mapi element 0x0e02 PR_DISPLAY_BCC */ pst_string bcc_address; /** mapi element 0x0071 PR_CONVERSATION_INDEX */ pst_binary conversation_index; /** mapi element 0x3a03 PR_CONVERSION_PROHIBITED * @li 1 true * @li 0 false */ int conversion_prohibited; /** mapi element 0x0e01 PR_DELETE_AFTER_SUBMIT * @li 1 true * @li 0 false */ int delete_after_submit; /** mapi element 0x0023 PR_ORIGINATOR_DELIVERY_REPORT_REQUESTED * @li 1 true * @li 0 false */ int delivery_report; /** mapi element 0x6f04 */ pst_binary encrypted_body; /** mapi element 0x6f02 */ pst_binary encrypted_htmlbody; /** mapi element 0x007d PR_TRANSPORT_MESSAGE_HEADERS */ pst_string header; /** mapi element 0x1013 */ pst_string htmlbody; /** mapi element 0x0017 PR_IMPORTANCE * @li 0 low * @li 1 normal * @li 2 high */ int32_t importance; /** mapi element 0x1042 */ pst_string in_reply_to; /** mapi element 0x0058 PR_MESSAGE_CC_ME, this user is listed explicitly in the CC address * @li 1 true * @li 0 false */ int message_cc_me; /** mapi element 0x0059 PR_MESSAGE_RECIP_ME, this user appears in TO, CC or BCC address list * @li 1 true * @li 0 false */ int message_recip_me; /** mapi element 0x0057 PR_MESSAGE_TO_ME, this user is listed explicitly in the TO address * @li 1 true * @li 0 false */ int message_to_me; /** mapi element 0x1035 */ pst_string messageid; /** mapi element 0x002e PR_ORIGINAL_SENSITIVITY * @li 0=none * @li 1=personal * @li 2=private * @li 3=company confidential */ int32_t original_sensitivity; /** mapi element 0x0072 PR_ORIGINAL_DISPLAY_BCC */ pst_string original_bcc; /** mapi element 0x0073 PR_ORIGINAL_DISPLAY_CC */ pst_string original_cc; /** mapi element 0x0074 PR_ORIGINAL_DISPLAY_TO */ pst_string original_to; /** mapi element 0x0051 PR_RECEIVED_BY_SEARCH_KEY */ pst_string outlook_recipient; /** mapi element 0x0044 PR_RCVD_REPRESENTING_NAME */ pst_string outlook_recipient_name; /** mapi element 0x0052 PR_RCVD_REPRESENTING_SEARCH_KEY */ pst_string outlook_recipient2; /** mapi element 0x003b PR_SENT_REPRESENTING_SEARCH_KEY */ pst_string outlook_sender; /** mapi element 0x0042 PR_SENT_REPRESENTING_NAME */ pst_string outlook_sender_name; /** mapi element 0x0c1d PR_SENDER_SEARCH_KEY */ pst_string outlook_sender2; /** mapi element 0x0026 PR_PRIORITY * @li 0 nonurgent * @li 1 normal * @li 2 urgent */ /** mapi element */ int32_t priority; /** mapi element 0x0070 PR_CONVERSATION_TOPIC */ pst_string processed_subject; /** mapi element 0x0029 PR_READ_RECEIPT_REQUESTED * @li 1 true * @li 0 false */ int read_receipt; /** mapi element 0x0075 PR_RECEIVED_BY_ADDRTYPE */ pst_string recip_access; /** mapi element 0x0076 PR_RECEIVED_BY_EMAIL_ADDRESS */ pst_string recip_address; /** mapi element 0x0077 PR_RCVD_REPRESENTING_ADDRTYPE */ pst_string recip2_access; /** mapi element 0x0078 PR_RCVD_REPRESENTING_EMAIL_ADDRESS */ pst_string recip2_address; /** mapi element 0x0c17 PR_REPLY_REQUESTED * @li 1 true * @li 0 false */ int reply_requested; /** mapi element 0x0050 PR_REPLY_RECIPIENT_NAMES */ pst_string reply_to; /** mapi element 0x1046, this seems to be the message-id of the rfc822 mail that is being returned */ pst_string return_path_address; /** mapi element 0x1007 PR_RTF_SYNC_BODY_COUNT, * a count of the *significant* charcters in the rtf body. Doesn't count * whitespace and other ignorable characters. */ int32_t rtf_body_char_count; /** mapi element 0x1006 PR_RTF_SYNC_BODY_CRC */ int32_t rtf_body_crc; /** mapi element 0x1008 PR_RTF_SYNC_BODY_TAG, * the first couple of lines of RTF body so that after modification, then beginning can * once again be found. */ pst_string rtf_body_tag; /** mapi element 0x1009 PR_RTF_COMPRESSED, * the compressed rtf body data. * Use pst_lzfu_decompress() to retrieve the actual rtf body data. */ pst_binary rtf_compressed; /** mapi element 0x0e1f PR_RTF_IN_SYNC, * True means that the rtf version is same as text body. * False means rtf version is more up-to-date than text body. * If this value doesn't exist, text body is more up-to-date than rtf and * cannot update to the rtf. * @li 1 true * @li 0 false */ int rtf_in_sync; /** mapi element 0x1010 PR_RTF_SYNC_PREFIX_COUNT, * a count of the ignored characters before the first significant character */ int32_t rtf_ws_prefix_count; /** mapi element 0x1011 PR_RTF_SYNC_TRAILING_COUNT, * a count of the ignored characters after the last significant character */ int32_t rtf_ws_trailing_count; /** mapi element 0x0064 PR_SENT_REPRESENTING_ADDRTYPE */ pst_string sender_access; /** mapi element 0x0065 PR_SENT_REPRESENTING_EMAIL_ADDRESS */ pst_string sender_address; /** mapi element 0x0c1e PR_SENDER_ADDRTYPE */ pst_string sender2_access; /** mapi element 0x0c1f PR_SENDER_EMAIL_ADDRESS */ pst_string sender2_address; /** mapi element 0x0036 PR_SENSITIVITY * @li 0=none * @li 1=personal * @li 2=private * @li 3=company confidential */ int32_t sensitivity; /** mapi element 0x0039 PR_CLIENT_SUBMIT_TIME */ FILETIME *sent_date; /** mapi element 0x0e0a PR_SENTMAIL_ENTRYID */ pst_entryid *sentmail_folder; /** mapi element 0x0e04 PR_DISPLAY_TO */ pst_string sentto_address; /** mapi element 0x1001 PR_REPORT_TEXT, delivery report dsn body */ pst_string report_text; /** mapi element 0x0032 PR_REPORT_TIME, delivery report time */ FILETIME *report_time; /** mapi element 0x0c04 PR_NDR_REASON_CODE */ int32_t ndr_reason_code; /** mapi element 0x0c05 PR_NDR_DIAG_CODE */ int32_t ndr_diag_code; /** mapi element 0x0c1b PR_SUPPLEMENTARY_INFO */ pst_string supplementary_info; /** mapi element 0x0c20 PR_NDR_STATUS_CODE */ int32_t ndr_status_code; } pst_item_email; /** This contains the folder related mapi elements */ typedef struct pst_item_folder { /** mapi element 0x3602 PR_CONTENT_COUNT */ int32_t item_count; /** mapi element 0x3603 PR_CONTENT_UNREAD */ int32_t unseen_item_count; /** mapi element 0x3617 PR_ASSOC_CONTENT_COUNT Associated content are items that are attached to this folder, but are hidden from users. */ int32_t assoc_count; /** mapi element 0x360a PR_SUBFOLDERS * @li 1 true * @li 0 false */ /** mapi element */ int subfolder; } pst_item_folder; /** This contains the message store related mapi elements */ typedef struct pst_item_message_store { /** mapi element 0x35e0 */ pst_entryid *top_of_personal_folder; /** mapi element 0x35e2 */ pst_entryid *default_outbox_folder; /** mapi element 0x35e3 */ pst_entryid *deleted_items_folder; /** mapi element 0x35e4 */ pst_entryid *sent_items_folder; /** mapi element 0x35e5 */ pst_entryid *user_views_folder; /** mapi element 0x35e6 */ pst_entryid *common_view_folder; /** mapi element 0x35e7 */ pst_entryid *search_root_folder; /** mapi element 0x7c07 */ pst_entryid *top_of_folder; /** mapi element 0x35df, * bit mask of folders in this message store * @li 0x1 FOLDER_IPM_SUBTREE_VALID * @li 0x2 FOLDER_IPM_INBOX_VALID * @li 0x4 FOLDER_IPM_OUTBOX_VALID * @li 0x8 FOLDER_IPM_WASTEBOX_VALID * @li 0x10 FOLDER_IPM_SENTMAIL_VALID * @li 0x20 FOLDER_VIEWS_VALID * @li 0x40 FOLDER_COMMON_VIEWS_VALID * @li 0x80 FOLDER_FINDER_VALID */ int32_t valid_mask; /** mapi element 0x76ff */ int32_t pwd_chksum; } pst_item_message_store; /** This contains the contact related mapi elements */ typedef struct pst_item_contact { /** mapi element 0x3a00 PR_ACCOUNT */ pst_string account_name; /** mapi element 0x3003 PR_EMAIL_ADDRESS, or 0x8083 */ pst_string address1; /** mapi element 0x8085 */ pst_string address1a; /** mapi element 0x8084 */ pst_string address1_desc; /** mapi element 0x3002 PR_ADDRTYPE, or 0x8082 */ pst_string address1_transport; /** mapi element 0x8093 */ pst_string address2; /** mapi element 0x8095 */ pst_string address2a; /** mapi element 0x8094 */ pst_string address2_desc; /** mapi element 0x8092 */ pst_string address2_transport; /** mapi element 0x80a3 */ pst_string address3; /** mapi element 0x80a5 */ pst_string address3a; /** mapi element 0x80a4 */ pst_string address3_desc; /** mapi element 0x80a2 */ pst_string address3_transport; /** mapi element 0x3a30 PR_ASSISTANT */ pst_string assistant_name; /** mapi element 0x3a2e PR_ASSISTANT_TELEPHONE_NUMBER */ pst_string assistant_phone; /** mapi element 0x8535 */ pst_string billing_information; /** mapi element 0x3a42 PR_BIRTHDAY */ FILETIME *birthday; /** mapi element 0x801b */ pst_string business_address; /** mapi element 0x3a27 PR_BUSINESS_ADDRESS_CITY */ pst_string business_city; /** mapi element 0x3a26 PR_BUSINESS_ADDRESS_COUNTRY */ pst_string business_country; /** mapi element 0x3a24 PR_BUSINESS_FAX_NUMBER */ pst_string business_fax; /** mapi element 0x3a51 PR_BUSINESS_HOME_PAGE */ pst_string business_homepage; /** mapi element 0x3a08 PR_BUSINESS_TELEPHONE_NUMBER */ pst_string business_phone; /** mapi element 0x3a1b PR_BUSINESS2_TELEPHONE_NUMBER */ pst_string business_phone2; /** mapi element 0x3a2b PR_BUSINESS_PO_BOX */ pst_string business_po_box; /** mapi element 0x3a2a PR_BUSINESS_POSTAL_CODE */ pst_string business_postal_code; /** mapi element 0x3a28 PR_BUSINESS_ADDRESS_STATE_OR_PROVINCE */ pst_string business_state; /** mapi element 0x3a29 PR_BUSINESS_ADDRESS_STREET */ pst_string business_street; /** mapi element 0x3a02 PR_CALLBACK_TELEPHONE_NUMBER */ pst_string callback_phone; /** mapi element 0x3a1e PR_CAR_TELEPHONE_NUMBER */ pst_string car_phone; /** mapi element 0x3a57 PR_COMPANY_MAIN_PHONE_NUMBER */ pst_string company_main_phone; /** mapi element 0x3a16 PR_COMPANY_NAME */ pst_string company_name; /** mapi element 0x3a49 PR_COMPUTER_NETWORK_NAME */ pst_string computer_name; /** mapi element 0x3a4a PR_CUSTOMER_ID */ pst_string customer_id; /** mapi element 0x3a15 PR_POSTAL_ADDRESS */ pst_string def_postal_address; /** mapi element 0x3a18 PR_DEPARTMENT_NAME */ pst_string department; /** mapi element 0x3a45 PR_DISPLAY_NAME_PREFIX */ pst_string display_name_prefix; /** mapi element 0x3a06 PR_GIVEN_NAME */ pst_string first_name; /** mapi element 0x8530 */ pst_string followup; /** mapi element 0x80d8 */ pst_string free_busy_address; /** mapi element 0x3a4c PR_FTP_SITE */ pst_string ftp_site; /** mapi element 0x8005 */ pst_string fullname; /** mapi element 0x3a4d PR_GENDER * @li 0 unspecified * @li 1 female * @li 2 male */ int16_t gender; /** mapi element 0x3a07 PR_GOVERNMENT_ID_NUMBER */ pst_string gov_id; /** mapi element 0x3a43 PR_HOBBIES */ pst_string hobbies; /** mapi element 0x801a */ pst_string home_address; /** mapi element 0x3a59 PR_HOME_ADDRESS_CITY */ pst_string home_city; /** mapi element 0x3a5a PR_HOME_ADDRESS_COUNTRY */ pst_string home_country; /** mapi element 0x3a25 PR_HOME_FAX_NUMBER */ pst_string home_fax; /** mapi element 0x3a09 PR_HOME_TELEPHONE_NUMBER */ pst_string home_phone; /** mapi element 0x3a2f PR_HOME2_TELEPHONE_NUMBER */ pst_string home_phone2; /** mapi element 0x3a5e PR_HOME_ADDRESS_POST_OFFICE_BOX */ pst_string home_po_box; /** mapi element 0x3a5b PR_HOME_ADDRESS_POSTAL_CODE */ pst_string home_postal_code; /** mapi element 0x3a5c PR_HOME_ADDRESS_STATE_OR_PROVINCE */ pst_string home_state; /** mapi element 0x3a5d PR_HOME_ADDRESS_STREET */ pst_string home_street; /** mapi element 0x3a0a PR_INITIALS */ pst_string initials; /** mapi element 0x3a2d PR_ISDN_NUMBER */ pst_string isdn_phone; /** mapi element 0x3a17 PR_TITLE */ pst_string job_title; /** mapi element 0x3a0b PR_KEYWORD */ pst_string keyword; /** mapi element 0x3a0c PR_LANGUAGE */ pst_string language; /** mapi element 0x3a0d PR_LOCATION */ pst_string location; /** mapi element 0x3a0e PR_MAIL_PERMISSION * @li 1 true * @li 0 false */ int mail_permission; /** mapi element 0x3a4e PR_MANAGER_NAME */ pst_string manager_name; /** mapi element 0x3a44 PR_MIDDLE_NAME */ pst_string middle_name; /** mapi element 0x8534 */ pst_string mileage; /** mapi element 0x3a1c PR_MOBILE_TELEPHONE_NUMBER */ pst_string mobile_phone; /** mapi element 0x3a4f PR_NICKNAME */ pst_string nickname; /** mapi element 0x3a19 PR_OFFICE_LOCATION */ pst_string office_loc; /** mapi element 0x3a0f PR_MHS_COMMON_NAME */ pst_string common_name; /** mapi element 0x3a10 PR_ORGANIZATIONAL_ID_NUMBER */ pst_string org_id; /** mapi element 0x801c */ pst_string other_address; /** mapi element 0x3a5f PR_OTHER_ADDRESS_CITY */ pst_string other_city; /** mapi element 0x3a60 PR_OTHER_ADDRESS_COUNTRY */ pst_string other_country; /** mapi element 0x3a1f PR_OTHER_TELEPHONE_NUMBER */ pst_string other_phone; /** mapi element 0x3a64 PR_OTHER_ADDRESS_POST_OFFICE_BOX */ pst_string other_po_box; /** mapi element 0x3a61 PR_OTHER_ADDRESS_POSTAL_CODE */ pst_string other_postal_code; /** mapi element 0x3a62 PR_OTHER_ADDRESS_STATE_OR_PROVINCE */ pst_string other_state; /** mapi element 0x3a63 PR_OTHER_ADDRESS_STREET */ pst_string other_street; /** mapi element 0x3a21 PR_PAGER_TELEPHONE_NUMBER */ pst_string pager_phone; /** mapi element 0x3a50 PR_PERSONAL_HOME_PAGE */ pst_string personal_homepage; /** mapi element 0x3a47 PR_PREFERRED_BY_NAME */ pst_string pref_name; /** mapi element 0x3a23 PR_PRIMARY_FAX_NUMBER */ pst_string primary_fax; /** mapi element 0x3a1a PR_PRIMARY_TELEPHONE_NUMBER */ pst_string primary_phone; /** mapi element 0x3a46 PR_PROFESSION */ pst_string profession; /** mapi element 0x3a1d PR_RADIO_TELEPHONE_NUMBER */ pst_string radio_phone; /** mapi element 0x3a40 PR_SEND_RICH_INFO * @li 1 true * @li 0 false */ int rich_text; /** mapi element 0x3a48 PR_SPOUSE_NAME */ pst_string spouse_name; /** mapi element 0x3a05 PR_GENERATION (Jr., Sr., III, etc) */ pst_string suffix; /** mapi element 0x3a11 PR_SURNAME */ pst_string surname; /** mapi element 0x3a2c PR_TELEX_NUMBER */ pst_string telex; /** mapi element 0x3a20 PR_TRANSMITTABLE_DISPLAY_NAME */ pst_string transmittable_display_name; /** mapi element 0x3a4b PR_TTYTDD_PHONE_NUMBER */ pst_string ttytdd_phone; /** mapi element 0x3a41 PR_WEDDING_ANNIVERSARY */ FILETIME *wedding_anniversary; /** mapi element 0x8045 */ pst_string work_address_street; /** mapi element 0x8046 */ pst_string work_address_city; /** mapi element 0x8047 */ pst_string work_address_state; /** mapi element 0x8048 */ pst_string work_address_postalcode; /** mapi element 0x8049 */ pst_string work_address_country; /** mapi element 0x804a */ pst_string work_address_postofficebox; } pst_item_contact; /** This contains the attachment related mapi elements */ typedef struct pst_item_attach { /** mapi element 0x3704 PR_ATTACH_FILENAME */ pst_string filename1; /** mapi element 0x3707 PR_ATTACH_LONG_FILENAME */ pst_string filename2; /** mapi element 0x370e PR_ATTACH_MIME_TAG */ pst_string mimetype; /** mapi element 0x3701 PR_ATTACH_DATA_OBJ */ pst_binary data; /** only used if the attachment is by reference, in which case this is the id2 reference */ uint64_t id2_val; /** calculated from id2_val during creation of record */ uint64_t i_id; /** id2 tree needed to resolve attachments by reference */ pst_id2_tree *id2_head; /** mapi element 0x3705 PR_ATTACH_METHOD * @li 0 no attachment * @li 1 attach by value * @li 2 attach by reference * @li 3 attach by reference resolve * @li 4 attach by reference only * @li 5 embedded message * @li 6 OLE */ int32_t method; /** mapi element 0x370b PR_RENDERING_POSITION */ int32_t position; /** mapi element 0x3710 PR_ATTACH_MIME_SEQUENCE */ int32_t sequence; struct pst_item_attach *next; } pst_item_attach; /** linked list of extra header fields */ typedef struct pst_item_extra_field { char *field_name; char *value; struct pst_item_extra_field *next; } pst_item_extra_field; /** This contains the journal related mapi elements */ typedef struct pst_item_journal { /** mapi element 0x8706 */ FILETIME *start; /** mapi element 0x8708 */ FILETIME *end; /** mapi element 0x8700 */ pst_string type; /** mapi element 0x8712 */ pst_string description; } pst_item_journal; /** This contains the recurrence data separated into fields. http://www.geocities.com/cainrandom/dev/MAPIRecurrence.html */ typedef struct pst_recurrence { /** 0x30043004 */ uint32_t signature; /** @li 0 daily * @li 1 weekly * @li 2 monthly * @li 3 yearly */ uint32_t type; /** implies number of recurrence parameters * @li 0 has 3 parameters * @li 1 has 4 parameters * @li 2 has 4 parameters * @li 3 has 5 parameters */ uint32_t sub_type; /** must be contiguous, not an array to make python interface easier */ uint32_t parm1; uint32_t parm2; uint32_t parm3; uint32_t parm4; uint32_t parm5; /** type of termination of the recurrence @li 0 terminates on a date @li 1 terminates based on integer number of occurrences @li 2 never terminates */ uint32_t termination; /** recurrence interval in terms of the recurrence type */ uint32_t interval; /** bit mask of days of the week */ uint32_t bydaymask; /** day of month for monthly and yearly recurrences */ uint32_t dayofmonth; /** month of year for yearly recurrences */ uint32_t monthofyear; /** occurence of day for 2nd Tuesday of month, in which case position is 2 */ uint32_t position; /** number of occurrences, even if recurrence terminates based on date */ uint32_t count; // there is more data, including the termination date, // but we can get that from other mapi elements. } pst_recurrence; /** This contains the appointment related mapi elements */ typedef struct pst_item_appointment { /** mapi element 0x820d PR_OUTLOOK_EVENT_START_DATE */ FILETIME *start; /** mapi element 0x820e PR_OUTLOOK_EVENT_START_END */ FILETIME *end; /** mapi element 0x8208 PR_OUTLOOK_EVENT_LOCATION */ pst_string location; /** mapi element 0x8503 PR_OUTLOOK_COMMON_REMINDER_SET * @li 1 true * @li 0 false */ int alarm; /** mapi element 0x8560 */ FILETIME *reminder; /** mapi element 0x8501 PR_OUTLOOK_COMMON_REMINDER_MINUTES_BEFORE */ int32_t alarm_minutes; /** mapi element 0x851f */ pst_string alarm_filename; /** mapi element 0x8234 */ pst_string timezonestring; /** mapi element 0x8205 PR_OUTLOOK_EVENT_SHOW_TIME_AS * @li 0 free * @li 1 tentative * @li 2 busy * @li 3 out of office*/ int32_t showas; /** mapi element 0x8214 * @li 0 None * @li 1 Important * @li 2 Business * @li 3 Personal * @li 4 Vacation * @li 5 Must Attend * @li 6 Travel Required * @li 7 Needs Preparation * @li 8 Birthday * @li 9 Anniversary * @li 10 Phone Call */ int32_t label; /** mapi element 0x8215 PR_OUTLOOK_EVENT_ALL_DAY * @li 1 true * @li 0 false */ int all_day; /** mapi element 0x8223 PR_OUTLOOK_EVENT_IS_RECURRING * @li 1 true * @li 0 false */ int is_recurring; /** mapi element 0x8231 * @li 0 none * @li 1 daily * @li 2 weekly * @li 3 monthly * @li 4 yearly */ int32_t recurrence_type; /** mapi element 0x8232 recurrence description */ pst_string recurrence_description; /** mapi element 0x8216 recurrence data */ pst_binary recurrence_data; /** mapi element 0x8235 PR_OUTLOOK_EVENT_RECURRENCE_START */ FILETIME *recurrence_start; /** mapi element 0x8236 PR_OUTLOOK_EVENT_RECURRENCE_END */ FILETIME *recurrence_end; } pst_item_appointment; /** This contains the common mapi elements, and pointers to structures for * each major mapi item type. It represents a complete mapi object. */ typedef struct pst_item { /** email mapi elements */ pst_item_email *email; /** folder mapi elements */ pst_item_folder *folder; /** contact mapi elements */ pst_item_contact *contact; /** linked list of attachments */ pst_item_attach *attach; /** message store mapi elements */ pst_item_message_store *message_store; /** linked list of extra headers and such */ pst_item_extra_field *extra_fields; /** journal mapi elements */ pst_item_journal *journal; /** calendar mapi elements */ pst_item_appointment *appointment; /** derived from mapi elements 0x001a PR_MESSAGE_CLASS or 0x3613 PR_CONTAINER_CLASS * @li 1 PST_TYPE_NOTE * @li 2 PST_TYPE_SCHEDULE * @li 8 PST_TYPE_APPOINTMENT * @li 9 PST_TYPE_CONTACT * @li 10 PST_TYPE_JOURNAL * @li 11 PST_TYPE_STICKYNOTE * @li 12 PST_TYPE_TASK * @li 13 PST_TYPE_OTHER * @li 14 PST_TYPE_REPORT */ int type; /** mapi element 0x001a PR_MESSAGE_CLASS or 0x3613 PR_CONTAINER_CLASS */ char *ascii_type; /** mapi element 0x0e07 PR_MESSAGE_FLAGS * @li 0x01 Read * @li 0x02 Unmodified * @li 0x04 Submit * @li 0x08 Unsent * @li 0x10 Has Attachments * @li 0x20 From Me * @li 0x40 Associated * @li 0x80 Resend * @li 0x100 RN Pending * @li 0x200 NRN Pending */ int32_t flags; /** mapi element 0x3001 PR_DISPLAY_NAME */ pst_string file_as; /** mapi element 0x3004 PR_COMMENT */ pst_string comment; /** derived from extra_fields["content-type"] if it contains a charset= subfield */ pst_string body_charset; /** mapi element 0x1000 PR_BODY */ pst_string body; /** mapi element 0x0037 PR_SUBJECT */ pst_string subject; /** mapi element 0x3fde PR_INTERNET_CPID */ int32_t internet_cpid; /** mapi element 0x3ffd PR_MESSAGE_CODEPAGE */ int32_t message_codepage; /** mapi element 0x0e08 PR_MESSAGE_SIZE */ int32_t message_size; /** mapi element 0x8554 PR_OUTLOOK_VERSION */ pst_string outlook_version; /** mapi element 0x0ff9 PR_RECORD_KEY */ pst_binary record_key; /** mapi element 0x65e3 PR_PREDECESSOR_CHANGE_LIST */ pst_binary predecessor_change; /** mapi element 0x0063 PR_RESPONSE_REQUESTED * @li 1 true * @li 0 false */ int response_requested; /** mapi element 0x3007 PR_CREATION_TIME */ FILETIME *create_date; /** mapi element 0x3008 PR_LAST_MODIFICATION_TIME */ FILETIME *modify_date; /** mapi element 0x002b PR_RECIPIENT_REASSIGNMENT_PROHIBITED * @li 1 true * @li 0 false */ int private_member; } pst_item; /** Linked list of extended attributes. * This is used to convert mapi_id values in the pst file into * cannonical mapi_id values to be used in this code. This list * is kept in sorted order, where the key is the 'map' field. * Some mapi_id values are converted to cannonical mapi_id values * (PST_MAP_ATTRIB), and others are converted to a string * (PST_ATTRIB_HEADER). */ typedef struct pst_x_attrib_ll { /** @li 1 PST_MAP_ATTRIB map->int attribute @li 2 PST_MAP_HEADER map->string header */ uint32_t mytype; /** key for the mapping */ uint32_t map; /** data target of the mapping, either uint32_t or string */ void *data; /** link to next item in the list */ struct pst_x_attrib_ll *next; } pst_x_attrib_ll; /** this is only used for internal debugging */ typedef struct pst_block_recorder { struct pst_block_recorder *next; int64_t offset; size_t size; int readcount; } pst_block_recorder; typedef struct pst_file { /** file pointer to opened PST file */ FILE* fp; /** original cwd when the file was opened */ char* cwd; /** original file name when the file was opened */ char* fname; /** the head and tail of the linked list of index structures */ pst_index_ll *i_head, *i_tail; /** the head and tail of the top level of the descriptor tree */ pst_desc_tree *d_head, *d_tail; /** the head of the extended attributes linked list */ pst_x_attrib_ll *x_head; /** the head of the block recorder, a debug artifact * used to detect cases where we might read the same * block multiple times while processing a pst file. */ pst_block_recorder *block_head; /** @li 0 is 32-bit pst file, pre Outlook 2003; * @li 1 is 64-bit pst file, Outlook 2003 or later */ int do_read64; /** file offset of the first b-tree node in the index tree */ uint64_t index1; /** back pointer value in the first b-tree node in the index tree */ uint64_t index1_back; /** file offset of the first b-tree node in the descriptor tree*/ uint64_t index2; /** back pointer value in the first b-tree node in the descriptor tree */ uint64_t index2_back; /** size of the pst file */ uint64_t size; /** @li 0 PST_NO_ENCRYPT, none * @li 1 PST_COMP_ENCRYPT, simple byte substitution cipher with fixed key * @li 2 PST_ENCRYPT, german enigma 3 rotor cipher with fixed key */ unsigned char encryption; /** index type or file type * @li 0x0e 32 bit pre Outlook 2003 * @li 0x0f 32 bit pre Outlook 2003 * @li 0x15 64 bit Outlook 2003 or later * @li 0x17 64 bit Outlook 2003 or later */ unsigned char ind_type; } pst_file; /** Open a pst file. * @param pf pointer to uninitialized pst_file structure. This structure * will be filled in by this function. * @param name name of the file, suitable for fopen(). * @return 0 if ok, -1 if error */ int pst_open(pst_file *pf, const char *name); /** Reopen the pst file after a fork * @param pf pointer to the pst_file structure setup by pst_open(). * @return 0 if ok, -1 if error */ int pst_reopen(pst_file *pf); /** Load the index entries from the pst file. This loads both the * i_id linked list, and the d_id tree, and should normally be the * first call after pst_open(). * @param pf pointer to the pst_file structure setup by pst_open(). */ int pst_load_index (pst_file *pf); /** Load the extended attribute mapping table from the pst file. This * should normally be the second call after pst_open(). * @param pf pointer to the pst_file structure setup by pst_open(). */ int pst_load_extended_attributes(pst_file *pf); /** Close a pst file. * @param pf pointer to the pst_file structure setup by pst_open(). */ int pst_close(pst_file *pf); /** Get the top of folders descriptor tree. This is the main descriptor tree * that needs to be walked to look at every item in the pst file. * @param pf pointer to the pst_file structure setup by pst_open(). * @param root root item, which can be obtained by pst_parse_item(pf, pf->d_head, NULL). */ pst_desc_tree* pst_getTopOfFolders(pst_file *pf, const pst_item *root); /** Assemble the binary attachment into a single buffer. * @param pf pointer to the pst_file structure setup by pst_open(). * @param attach pointer to the attachment record * @return structure containing size of and pointer to the buffer. * the caller must free this buffer. */ pst_binary pst_attach_to_mem(pst_file *pf, pst_item_attach *attach); /** Write a binary attachment to a file. * @param pf pointer to the pst_file structure setup by pst_open(). * @param attach pointer to the attachment record * @param fp pointer to an open FILE. */ size_t pst_attach_to_file(pst_file *pf, pst_item_attach *attach, FILE* fp); /** Write a binary attachment base64 encoded to a file. * @param pf pointer to the pst_file structure setup by pst_open(). * @param attach pointer to the attachment record * @param fp pointer to an open FILE. */ size_t pst_attach_to_file_base64(pst_file *pf, pst_item_attach *attach, FILE* fp); /** Walk the descriptor tree. * @param d pointer to the current item in the descriptor tree. * @return pointer to the next item in the descriptor tree. */ pst_desc_tree* pst_getNextDptr(pst_desc_tree* d); /** Assemble a mapi object from a descriptor pointer. * @param pf pointer to the pst_file structure setup by pst_open(). * @param d_ptr pointer to an item in the descriptor tree. * @param m_head normally NULL. This is only used when processing embedded * attached rfc822 messages, in which case it is attach->id2_head. * @return pointer to the mapi object. Must be free'd by pst_freeItem(). */ pst_item* pst_parse_item (pst_file *pf, pst_desc_tree *d_ptr, pst_id2_tree *m_head); /** Free the item returned by pst_parse_item(). * @param item pointer to item returned from pst_parse_item(). */ void pst_freeItem(pst_item *item); /** Lookup the i_id in the index linked list, and return a pointer to the element. * @param pf pointer to the pst_file structure setup by pst_open(). * @param i_id key for the index linked list * @return pointer to the element, or NULL if not found. */ pst_index_ll* pst_getID(pst_file* pf, uint64_t i_id); /** Get an ID block from the file using pst_ff_getIDblock() and decrypt if necessary. * @param pf pointer to the pst_file structure setup by pst_open(). * @param i_id ID of block to retrieve * @param buf reference to pointer to buffer that will contain the data block. * If this pointer is non-NULL, it will first be free()d. * @return Size of block read into memory */ size_t pst_ff_getIDblock_dec(pst_file *pf, uint64_t i_id, char **buf); /** fwrite with checking for null pointer. * @param ptr pointer to the buffer * @param size size of each item * @param nmemb number of items * @param stream output file * @return number of bytes written, zero if ptr==NULL */ size_t pst_fwrite(const void* ptr, size_t size, size_t nmemb, FILE* stream); /** Add any necessary escape characters for rfc2426 vcard format * @param[in] str pointer to input string * @param[in,out] result pointer to a char* pointer that may be realloc'ed if needed * @param[in,out] resultlen size of the result buffer * @return pointer to output string, either the input pointer if * there are no characters that need escapes, or a pointer * to a possibly realloc'ed result buffer. */ char* pst_rfc2426_escape(char* str, char** result, size_t* resultlen); /** Convert a FILETIME into rfc2425 date/time format 1953-10-15T23:10:00Z * which is the same as one of the forms in the ISO3601 standard * @param[in] ft time to be converted * @param[in] buflen length of the output buffer * @param[out] result pointer to output buffer, must be at least 30 bytes * @return time in rfc2425 format */ char* pst_rfc2425_datetime_format(const FILETIME* ft, int buflen, char* result); /** Convert a FILETIME into rfc2445 date/time format 19531015T231000Z * @param[in] ft time to be converted * @param[in] buflen length of the output buffer * @param[out] result pointer to output buffer, must be at least 30 bytes * @return time in rfc2445 format */ char* pst_rfc2445_datetime_format(const FILETIME* ft, int buflen, char* result); /** Convert the current time rfc2445 date/time format 19531015T231000Z * @param[in] buflen length of the output buffer * @param[out] result pointer to output buffer, must be at least 30 bytes * @return time in rfc2445 format */ char* pst_rfc2445_datetime_format_now(int buflen, char* result); /** Get the default character set for this item. This is used to find * the charset for pst_string elements that are not already in utf8 encoding. * @param item pointer to the mapi item of interest * @param[in] buflen length of the output buffer * @param[out] result pointer to output buffer, must be at least 30 bytes * @return default character set as a string useable by iconv() */ const char* pst_default_charset(pst_item *item, int buflen, char* result); /** Convert str to rfc2231 encoding of str * @param str pointer to the mapi string of interest */ void pst_rfc2231(pst_string *str); /** Convert str to rfc2047 encoding of str, possibly enclosed in quotes if it contains spaces * @param item pointer to the containing mapi item * @param str pointer to the mapi string of interest * @param needs_quote true if strings containing spaces should be wrapped in quotes */ void pst_rfc2047(pst_item *item, pst_string *str, int needs_quote); /** Convert str to utf8 if possible; null strings are preserved. * @param item pointer to the containing mapi item * @param str pointer to the mapi string of interest */ void pst_convert_utf8_null(pst_item *item, pst_string *str); /** Convert str to utf8 if possible; null strings are converted into empty strings. * @param item pointer to the containing mapi item * @param str pointer to the mapi string of interest */ void pst_convert_utf8(pst_item *item, pst_string *str); /** Decode raw recurrence data into a better structure. * @param appt pointer to appointment structure * @return pointer to decoded recurrence structure that must be free'd by the caller. */ pst_recurrence* pst_convert_recurrence(pst_item_appointment* appt); /** Free a recurrence structure. * @param r input pointer to be freed */ void pst_free_recurrence(pst_recurrence* r); // switch from maximal packing back to default packing // undo the packing from the beginning of this file #ifdef _MSC_VER #pragma pack(pop) #endif #if defined(__GNUC__) || defined (__SUNPRO_C) || defined(__SUNPRO_CC) #pragma pack() #endif #endif diff --git a/src/readpst.c b/src/readpst.c index 1f579bf..083f98b 100644 --- a/src/readpst.c +++ b/src/readpst.c @@ -1,2163 +1,2168 @@ /*** * readpst.c * Part of the LibPST project * Written by David Smith * dave.s@earthcorp.com */ #include "define.h" #include "lzfu.h" #define OUTPUT_TEMPLATE "%s" #define OUTPUT_KMAIL_DIR_TEMPLATE ".%s.directory" #define KMAIL_INDEX ".%s.index" #define SEP_MAIL_FILE_TEMPLATE "%i%s" // max size of the c_time char*. It will store the date of the email #define C_TIME_SIZE 500 struct file_ll { char *name; char *dname; FILE * output; int32_t stored_count; int32_t item_count; int32_t skip_count; int32_t type; }; int grim_reaper(); pid_t try_fork(char* folder); void process(pst_item *outeritem, pst_desc_tree *d_ptr); void write_email_body(FILE *f, char *body); void removeCR(char *c); void usage(); void version(); char* mk_kmail_dir(char* fname); int close_kmail_dir(); char* mk_recurse_dir(char* dir, int32_t folder_type); int close_recurse_dir(); char* mk_separate_dir(char *dir); int close_separate_dir(); void mk_separate_file(struct file_ll *f, char *extension); void close_separate_file(struct file_ll *f); char* my_stristr(char *haystack, char *needle); void check_filename(char *fname); void write_separate_attachment(char f_name[], pst_item_attach* attach, int attach_num, pst_file* pst); void write_embedded_message(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pf, char** extra_mime_headers); void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst); int valid_headers(char *header); void header_has_field(char *header, char *field, int *flag); void header_get_subfield(char *field, const char *subfield, char *body_subfield, size_t size_subfield); char* header_get_field(char *header, char *field); char* header_end_field(char *field); void header_strip_field(char *header, char *field); int test_base64(char *body); void find_html_charset(char *html, char *charset, size_t charsetlen); void find_rfc822_headers(char** extra_mime_headers); void write_body_part(FILE* f_output, pst_string *body, char *mime, char *charset, char *boundary, pst_file* pst); void write_schedule_part_data(FILE* f_output, pst_item* item, const char* sender, const char* method); void write_schedule_part(FILE* f_output, pst_item* item, const char* sender, const char* boundary); void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf, char** extra_mime_headers); void write_vcard(FILE* f_output, pst_item *item, pst_item_contact* contact, char comment[]); int write_extra_categories(FILE* f_output, pst_item* item); void write_journal(FILE* f_output, pst_item* item); void write_appointment(FILE* f_output, pst_item *item); void create_enter_dir(struct file_ll* f, pst_item *item); void close_enter_dir(struct file_ll *f); const char* prog_name; char* output_dir = "."; char* kmail_chdir = NULL; // Normal mode just creates mbox format files in the current directory. Each file is named // the same as the folder's name that it represents #define MODE_NORMAL 0 // KMail mode creates a directory structure suitable for being used directly // by the KMail application #define MODE_KMAIL 1 // recurse mode creates a directory structure like the PST file. Each directory // contains only one file which stores the emails in mboxrd format. #define MODE_RECURSE 2 // separate mode creates the same directory structure as recurse. The emails are stored in // separate files, numbering from 1 upward. Attachments belonging to the emails are // saved as email_no-filename (e.g. 1-samplefile.doc or 1-Attachment2.zip) #define MODE_SEPARATE 3 // Output Normal just prints the standard information about what is going on #define OUTPUT_NORMAL 0 // Output Quiet is provided so that only errors are printed #define OUTPUT_QUIET 1 // default mime-type for attachments that have a null mime-type #define MIME_TYPE_DEFAULT "application/octet-stream" #define RFC822 "message/rfc822" // output mode for contacts #define CMODE_VCARD 0 #define CMODE_LIST 1 // output mode for deleted items #define DMODE_EXCLUDE 0 #define DMODE_INCLUDE 1 // Output type mode flags #define OTMODE_EMAIL 1 #define OTMODE_APPOINTMENT 2 #define OTMODE_JOURNAL 4 #define OTMODE_CONTACT 8 // output settings for RTF bodies // filename for the attachment #define RTF_ATTACH_NAME "rtf-body.rtf" // mime type for the attachment #define RTF_ATTACH_TYPE "application/rtf" // global settings int mode = MODE_NORMAL; int mode_MH = 0; // a submode of MODE_SEPARATE int mode_EX = 0; // a submode of MODE_SEPARATE int mode_thunder = 0; // a submode of MODE_RECURSE int output_mode = OUTPUT_NORMAL; int contact_mode = CMODE_VCARD; int deleted_mode = DMODE_EXCLUDE; int output_type_mode = 0xff; // Default to all. int contact_mode_specified = 0; int overwrite = 0; int save_rtf_body = 1; int file_name_len = 10; // enough room for MODE_SPEARATE file name pst_file pstfile; regex_t meta_charset_pattern; int number_processors = 1; // number of cpus we have int max_children = 0; // based on number of cpus and command line args int max_child_specified = 0;// have command line arg -j int active_children; // number of children of this process, cannot be larger than max_children pid_t* child_processes; // setup by main(), and at the start of new child process #ifdef HAVE_SEMAPHORE_H int shared_memory_id; sem_t* global_children = NULL; sem_t* output_mutex = NULL; #endif int grim_reaper(int waitall) { int available = 0; #ifdef HAVE_FORK #ifdef HAVE_SEMAPHORE_H if (global_children) { //sem_getvalue(global_children, &available); //printf("grim reaper %s for pid %d (parent %d) with %d children, %d available\n", (waitall) ? "all" : "", getpid(), getppid(), active_children, available); //fflush(stdout); int i,j; for (i=0; inext) { DEBUG_INFO(("New item record\n")); if (!d_ptr->desc) { ff.skip_count++; DEBUG_WARN(("ERROR item's desc record is NULL\n")); continue; } DEBUG_INFO(("Desc Email ID %#"PRIx64" [d_ptr->d_id = %#"PRIx64"]\n", d_ptr->desc->i_id, d_ptr->d_id)); item = pst_parse_item(&pstfile, d_ptr, NULL); DEBUG_INFO(("About to process item\n")); if (!item) { ff.skip_count++; DEBUG_INFO(("A NULL item was seen\n")); continue; } if (item->subject.str) { DEBUG_INFO(("item->subject = %s\n", item->subject.str)); } if (item->folder && item->file_as.str) { DEBUG_INFO(("Processing Folder \"%s\"\n", item->file_as.str)); if (output_mode != OUTPUT_QUIET) { pst_debug_lock(); printf("Processing Folder \"%s\"\n", item->file_as.str); fflush(stdout); pst_debug_unlock(); } ff.item_count++; if (d_ptr->child && (deleted_mode == DMODE_INCLUDE || strcasecmp(item->file_as.str, "Deleted Items"))) { //if this is a non-empty folder other than deleted items, we want to recurse into it pid_t parent = getpid(); pid_t child = try_fork(item->file_as.str); if (child == 0) { // we are the child process, or the original parent if no children were available pid_t me = getpid(); process(item, d_ptr->child); #ifdef HAVE_FORK #ifdef HAVE_SEMAPHORE_H if (me != parent) { // we really were a child, forked for the sole purpose of processing this folder // free my child count slot before really exiting, since // all I am doing here is waiting for my children to exit sem_post(global_children); grim_reaper(1); // wait for all my child processes to exit exit(0); // really exit } #endif #endif } } } else if (item->contact && (item->type == PST_TYPE_CONTACT)) { DEBUG_INFO(("Processing Contact\n")); if (!(output_type_mode & OTMODE_CONTACT)) { ff.skip_count++; DEBUG_INFO(("skipping contact: not in output type list\n")); } else { if (!ff.type) ff.type = item->type; if ((ff.type != PST_TYPE_CONTACT) && (mode != MODE_SEPARATE)) { ff.skip_count++; DEBUG_INFO(("I have a contact, but the folder type %"PRIi32" isn't a contacts folder. Skipping it\n", ff.type)); } else { ff.item_count++; if (mode == MODE_SEPARATE) mk_separate_file(&ff, (mode_EX) ? ".vcf" : ""); if (contact_mode == CMODE_VCARD) { pst_convert_utf8_null(item, &item->comment); write_vcard(ff.output, item, item->contact, item->comment.str); } else { pst_convert_utf8(item, &item->contact->fullname); pst_convert_utf8(item, &item->contact->address1); fprintf(ff.output, "%s <%s>\n", item->contact->fullname.str, item->contact->address1.str); } if (mode == MODE_SEPARATE) close_separate_file(&ff); } } } else if (item->email && ((item->type == PST_TYPE_NOTE) || (item->type == PST_TYPE_SCHEDULE) || (item->type == PST_TYPE_REPORT))) { DEBUG_INFO(("Processing Email\n")); if (!(output_type_mode & OTMODE_EMAIL)) { ff.skip_count++; DEBUG_INFO(("skipping email: not in output type list\n")); } else { if (!ff.type) ff.type = item->type; if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_SCHEDULE) && (ff.type != PST_TYPE_REPORT) && (mode != MODE_SEPARATE)) { ff.skip_count++; DEBUG_INFO(("I have an email type %"PRIi32", but the folder type %"PRIi32" isn't an email folder. Skipping it\n", item->type, ff.type)); } else { char *extra_mime_headers = NULL; ff.item_count++; if (mode == MODE_SEPARATE) { // process this single email message, possibly forking pid_t parent = getpid(); pid_t child = try_fork(item->file_as.str); if (child == 0) { // we are the child process, or the original parent if no children were available pid_t me = getpid(); mk_separate_file(&ff, (mode_EX) ? ".eml" : ""); write_normal_email(ff.output, ff.name, item, mode, mode_MH, &pstfile, save_rtf_body, &extra_mime_headers); close_separate_file(&ff); #ifdef HAVE_FORK #ifdef HAVE_SEMAPHORE_H if (me != parent) { // we really were a child, forked for the sole purpose of processing this message // free my child count slot before really exiting, since // all I am doing here is waiting for my children to exit sem_post(global_children); grim_reaper(1); // wait for all my child processes to exit - there should not be any exit(0); // really exit } #endif #endif } } else { // process this single email message, cannot fork since not separate mode write_normal_email(ff.output, ff.name, item, mode, mode_MH, &pstfile, save_rtf_body, &extra_mime_headers); } } } } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) { DEBUG_INFO(("Processing Journal Entry\n")); if (!(output_type_mode & OTMODE_JOURNAL)) { ff.skip_count++; DEBUG_INFO(("skipping journal entry: not in output type list\n")); } else { if (!ff.type) ff.type = item->type; if ((ff.type != PST_TYPE_JOURNAL) && (mode != MODE_SEPARATE)) { ff.skip_count++; DEBUG_INFO(("I have a journal entry, but the folder type %"PRIi32" isn't a journal folder. Skipping it\n", ff.type)); } else { ff.item_count++; if (mode == MODE_SEPARATE) mk_separate_file(&ff, (mode_EX) ? ".ics" : ""); write_journal(ff.output, item); fprintf(ff.output, "\n"); if (mode == MODE_SEPARATE) close_separate_file(&ff); } } } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) { DEBUG_INFO(("Processing Appointment Entry\n")); if (!(output_type_mode & OTMODE_APPOINTMENT)) { ff.skip_count++; DEBUG_INFO(("skipping appointment: not in output type list\n")); } else { if (!ff.type) ff.type = item->type; if ((ff.type != PST_TYPE_APPOINTMENT) && (mode != MODE_SEPARATE)) { ff.skip_count++; DEBUG_INFO(("I have an appointment, but the folder type %"PRIi32" isn't an appointment folder. Skipping it\n", ff.type)); } else { ff.item_count++; if (mode == MODE_SEPARATE) mk_separate_file(&ff, (mode_EX) ? ".ics" : ""); write_schedule_part_data(ff.output, item, NULL, NULL); fprintf(ff.output, "\n"); if (mode == MODE_SEPARATE) close_separate_file(&ff); } } } else if (item->message_store) { // there should only be one message_store, and we have already done it ff.skip_count++; DEBUG_INFO(("item with message store content, type %i %s folder type %i, skipping it\n", item->type, item->ascii_type, ff.type)); } else { ff.skip_count++; DEBUG_INFO(("Unknown item type %i (%s) name (%s)\n", item->type, item->ascii_type, item->file_as.str)); } pst_freeItem(item); } close_enter_dir(&ff); DEBUG_RET(); } int main(int argc, char* const* argv) { pst_item *item = NULL; pst_desc_tree *d_ptr; char * fname = NULL; char *d_log = NULL; int c,x; char *temp = NULL; //temporary char pointer prog_name = argv[0]; time_t now = time(NULL); srand((unsigned)now); if (regcomp(&meta_charset_pattern, "]*content=\"[^>]*charset=([^>\";]*)[\";]", REG_ICASE | REG_EXTENDED)) { printf("cannot compile regex pattern to find content charset in html bodies\n"); exit(3); } // command-line option handling while ((c = getopt(argc, argv, "bc:Dd:ehj:kMo:qrSt:uVw"))!= -1) { switch (c) { case 'b': save_rtf_body = 0; break; case 'c': if (optarg && optarg[0]=='v') { contact_mode=CMODE_VCARD; contact_mode_specified = 1; } else if (optarg && optarg[0]=='l') { contact_mode=CMODE_LIST; contact_mode_specified = 1; } else { usage(); exit(0); } break; case 'D': deleted_mode = DMODE_INCLUDE; break; case 'd': d_log = optarg; break; case 'h': usage(); exit(0); break; case 'j': max_children = atoi(optarg); max_child_specified = 1; break; case 'k': mode = MODE_KMAIL; break; case 'M': mode = MODE_SEPARATE; mode_MH = 1; mode_EX = 0; break; case 'e': mode = MODE_SEPARATE; mode_MH = 1; mode_EX = 1; file_name_len = 14; break; case 'o': output_dir = optarg; break; case 'q': output_mode = OUTPUT_QUIET; break; case 'r': mode = MODE_RECURSE; mode_thunder = 0; break; case 'S': mode = MODE_SEPARATE; mode_MH = 0; mode_EX = 0; break; case 't': // email, appointment, contact, other if (!optarg) { usage(); exit(0); } temp = optarg; output_type_mode = 0; while (*temp > 0) { switch (temp[0]) { case 'e': output_type_mode |= OTMODE_EMAIL; break; case 'a': output_type_mode |= OTMODE_APPOINTMENT; break; case 'j': output_type_mode |= OTMODE_JOURNAL; break; case 'c': output_type_mode |= OTMODE_CONTACT; break; default: usage(); exit(0); break; } temp++; } break; case 'u': mode = MODE_RECURSE; mode_thunder = 1; break; case 'V': version(); exit(0); break; case 'w': overwrite = 1; break; default: usage(); exit(1); break; } } if (argc > optind) { fname = argv[optind]; } else { usage(); exit(2); } #ifdef _SC_NPROCESSORS_ONLN number_processors = sysconf(_SC_NPROCESSORS_ONLN); #endif max_children = (max_child_specified) ? max_children : number_processors * 4; active_children = 0; child_processes = (pid_t *)pst_malloc(sizeof(pid_t) * max_children); memset(child_processes, 0, sizeof(pid_t) * max_children); #ifdef HAVE_SEMAPHORE_H if (max_children) { shared_memory_id = shmget(IPC_PRIVATE, sizeof(sem_t)*2, 0777); if (shared_memory_id >= 0) { global_children = (sem_t *)shmat(shared_memory_id, NULL, 0); if (global_children == (sem_t *)-1) global_children = NULL; if (global_children) { output_mutex = &(global_children[1]); sem_init(global_children, 1, max_children); sem_init(output_mutex, 1, 1); } shmctl(shared_memory_id, IPC_RMID, NULL); } } #endif #ifdef DEBUG_ALL // force a log file if (!d_log) d_log = "readpst.log"; #endif // defined DEBUG_ALL #ifdef HAVE_SEMAPHORE_H DEBUG_INIT(d_log, output_mutex); #else DEBUG_INIT(d_log, NULL); #endif DEBUG_ENT("main"); if (output_mode != OUTPUT_QUIET) printf("Opening PST file and indexes...\n"); RET_DERROR(pst_open(&pstfile, fname), 1, ("Error opening File\n")); RET_DERROR(pst_load_index(&pstfile), 2, ("Index Error\n")); pst_load_extended_attributes(&pstfile); if (chdir(output_dir)) { x = errno; pst_close(&pstfile); DEBUG_RET(); DIE(("Cannot change to output dir %s: %s\n", output_dir, strerror(x))); } d_ptr = pstfile.d_head; // first record is main record item = pst_parse_item(&pstfile, d_ptr, NULL); if (!item || !item->message_store) { DEBUG_RET(); DIE(("Could not get root record\n")); } // default the file_as to the same as the main filename if it doesn't exist if (!item->file_as.str) { if (!(temp = strrchr(fname, '/'))) if (!(temp = strrchr(fname, '\\'))) temp = fname; else temp++; // get past the "\\" else temp++; // get past the "/" item->file_as.str = (char*)pst_malloc(strlen(temp)+1); strcpy(item->file_as.str, temp); item->file_as.is_utf8 = 1; DEBUG_INFO(("file_as was blank, so am using %s\n", item->file_as.str)); } DEBUG_INFO(("Root Folder Name: %s\n", item->file_as.str)); d_ptr = pst_getTopOfFolders(&pstfile, item); if (!d_ptr) { DEBUG_RET(); DIE(("Top of folders record not found. Cannot continue\n")); } process(item, d_ptr->child); // do the children of TOPF grim_reaper(1); // wait for all child processes pst_freeItem(item); pst_close(&pstfile); DEBUG_RET(); #ifdef HAVE_SEMAPHORE_H if (global_children) { sem_destroy(global_children); sem_destroy(output_mutex); shmdt(global_children); } #endif regfree(&meta_charset_pattern); return 0; } void write_email_body(FILE *f, char *body) { char *n = body; DEBUG_ENT("write_email_body"); if (mode != MODE_SEPARATE) { while (n) { char *p = body; while (*p == '>') p++; if (strncmp(p, "From ", 5) == 0) fprintf(f, ">"); if ((n = strchr(body, '\n'))) { n++; pst_fwrite(body, n-body, 1, f); //write just a line body = n; } } } pst_fwrite(body, strlen(body), 1, f); DEBUG_RET(); } void removeCR (char *c) { // converts \r\n to \n char *a, *b; DEBUG_ENT("removeCR"); a = b = c; while (*a != '\0') { *b = *a; if (*a != '\r') b++; a++; } *b = '\0'; DEBUG_RET(); } void usage() { DEBUG_ENT("usage"); version(); printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name); printf("OPTIONS:\n"); printf("\t-V\t- Version. Display program version\n"); printf("\t-D\t- Include deleted items in output\n"); printf("\t-M\t- Write emails in the MH (rfc822) format\n"); printf("\t-S\t- Separate. Write emails in the separate format\n"); printf("\t-b\t- Don't save RTF-Body attachments\n"); printf("\t-c[v|l]\t- Set the Contact output mode. -cv = VCard, -cl = EMail list\n"); printf("\t-d \t- Debug to file.\n"); printf("\t-e\t- As with -M, but include extensions on output files\n"); printf("\t-h\t- Help. This screen\n"); printf("\t-j \t- Number of parallel jobs to run\n"); printf("\t-k\t- KMail. Output in kmail format\n"); printf("\t-o \t- Output directory to write files to. CWD is changed *after* opening pst file\n"); printf("\t-q\t- Quiet. Only print error messages\n"); printf("\t-r\t- Recursive. Output in a recursive format\n"); printf("\t-t[eajc]\t- Set the output type list. e = email, a = attachment, j = journal, c = contact\n"); printf("\t-u\t- Thunderbird mode. Write two extra .size and .type files\n"); printf("\t-w\t- Overwrite any output mbox files\n"); printf("\n"); printf("Only one of -k -M -r -S should be specified\n"); DEBUG_RET(); } void version() { DEBUG_ENT("version"); printf("ReadPST / LibPST v%s\n", VERSION); #if BYTE_ORDER == BIG_ENDIAN printf("Big Endian implementation being used.\n"); #elif BYTE_ORDER == LITTLE_ENDIAN printf("Little Endian implementation being used.\n"); #else # error "Byte order not supported by this library" #endif DEBUG_RET(); } char *mk_kmail_dir(char *fname) { //change to that directory //make a directory based on OUTPUT_KMAIL_DIR_TEMPLATE //allocate space for OUTPUT_TEMPLATE and form a char* with fname //return that value char *dir, *out_name, *index; int x; DEBUG_ENT("mk_kmail_dir"); if (kmail_chdir && chdir(kmail_chdir)) { x = errno; DIE(("mk_kmail_dir: Cannot change to directory %s: %s\n", kmail_chdir, strerror(x))); } dir = pst_malloc(strlen(fname)+strlen(OUTPUT_KMAIL_DIR_TEMPLATE)+1); sprintf(dir, OUTPUT_KMAIL_DIR_TEMPLATE, fname); check_filename(dir); if (D_MKDIR(dir)) { if (errno != EEXIST) { // not an error because it exists x = errno; DIE(("mk_kmail_dir: Cannot create directory %s: %s\n", dir, strerror(x))); } } kmail_chdir = pst_realloc(kmail_chdir, strlen(dir)+1); strcpy(kmail_chdir, dir); free (dir); //we should remove any existing indexes created by KMail, cause they might be different now index = pst_malloc(strlen(fname)+strlen(KMAIL_INDEX)+1); sprintf(index, KMAIL_INDEX, fname); unlink(index); free(index); out_name = pst_malloc(strlen(fname)+strlen(OUTPUT_TEMPLATE)+1); sprintf(out_name, OUTPUT_TEMPLATE, fname); DEBUG_RET(); return out_name; } int close_kmail_dir() { // change .. int x; DEBUG_ENT("close_kmail_dir"); if (kmail_chdir) { //only free kmail_chdir if not NULL. do not change directory free(kmail_chdir); kmail_chdir = NULL; } else { if (chdir("..")) { x = errno; DIE(("close_kmail_dir: Cannot move up dir (..): %s\n", strerror(x))); } } DEBUG_RET(); return 0; } // this will create a directory by that name, // then make an mbox file inside that directory. char *mk_recurse_dir(char *dir, int32_t folder_type) { int x; char *out_name; DEBUG_ENT("mk_recurse_dir"); check_filename(dir); if (D_MKDIR (dir)) { if (errno != EEXIST) { // not an error because it exists x = errno; DIE(("mk_recurse_dir: Cannot create directory %s: %s\n", dir, strerror(x))); } } if (chdir (dir)) { x = errno; DIE(("mk_recurse_dir: Cannot change to directory %s: %s\n", dir, strerror(x))); } switch (folder_type) { case PST_TYPE_APPOINTMENT: out_name = strdup("calendar"); break; case PST_TYPE_CONTACT: out_name = strdup("contacts"); break; case PST_TYPE_JOURNAL: out_name = strdup("journal"); break; case PST_TYPE_STICKYNOTE: case PST_TYPE_TASK: case PST_TYPE_NOTE: case PST_TYPE_OTHER: case PST_TYPE_REPORT: default: out_name = strdup("mbox"); break; } DEBUG_RET(); return out_name; } int close_recurse_dir() { int x; DEBUG_ENT("close_recurse_dir"); if (chdir("..")) { x = errno; DIE(("close_recurse_dir: Cannot go up dir (..): %s\n", strerror(x))); } DEBUG_RET(); return 0; } char *mk_separate_dir(char *dir) { size_t dirsize = strlen(dir) + 10; char dir_name[dirsize]; int x = 0, y = 0; DEBUG_ENT("mk_separate_dir"); do { if (y == 0) snprintf(dir_name, dirsize, "%s", dir); else snprintf(dir_name, dirsize, "%s" SEP_MAIL_FILE_TEMPLATE, dir, y, ""); // enough for 9 digits allocated above check_filename(dir_name); DEBUG_INFO(("about to try creating %s\n", dir_name)); if (D_MKDIR(dir_name)) { if (errno != EEXIST) { // if there is an error, and it doesn't already exist x = errno; DIE(("mk_separate_dir: Cannot create directory %s: %s\n", dir, strerror(x))); } } else { break; } y++; } while (overwrite == 0); if (chdir(dir_name)) { x = errno; DIE(("mk_separate_dir: Cannot change to directory %s: %s\n", dir, strerror(x))); } if (overwrite) { // we should probably delete all files from this directory #if !defined(WIN32) && !defined(__CYGWIN__) DIR * sdir = NULL; struct dirent *dirent = NULL; struct stat filestat; if (!(sdir = opendir("./"))) { DEBUG_WARN(("mk_separate_dir: Cannot open dir \"%s\" for deletion of old contents\n", "./")); } else { while ((dirent = readdir(sdir))) { if (lstat(dirent->d_name, &filestat) != -1) if (S_ISREG(filestat.st_mode)) { if (unlink(dirent->d_name)) { y = errno; DIE(("mk_separate_dir: unlink returned error on file %s: %s\n", dirent->d_name, strerror(y))); } } } } #endif } // we don't return a filename here cause it isn't necessary. DEBUG_RET(); return NULL; } int close_separate_dir() { int x; DEBUG_ENT("close_separate_dir"); if (chdir("..")) { x = errno; DIE(("close_separate_dir: Cannot go up dir (..): %s\n", strerror(x))); } DEBUG_RET(); return 0; } void mk_separate_file(struct file_ll *f, char *extension) { DEBUG_ENT("mk_separate_file"); DEBUG_INFO(("opening next file to save email\n")); if (f->item_count > 999999999) { // bigger than nine 9's DIE(("mk_separate_file: The number of emails in this folder has become too high to handle\n")); } sprintf(f->name, SEP_MAIL_FILE_TEMPLATE, f->item_count, extension); check_filename(f->name); if (!(f->output = fopen(f->name, "w"))) { DIE(("mk_separate_file: Cannot open file to save email \"%s\"\n", f->name)); } DEBUG_RET(); } void close_separate_file(struct file_ll *f) { DEBUG_ENT("close_separate_file"); if (f->output) { struct stat st; fclose(f->output); stat(f->name, &st); if (!st.st_size) { DEBUG_WARN(("removing empty output file %s\n", f->name)); remove(f->name); } f->output = NULL; } DEBUG_RET(); } char *my_stristr(char *haystack, char *needle) { // my_stristr varies from strstr in that its searches are case-insensitive char *x=haystack, *y=needle, *z = NULL; if (!haystack || !needle) { return NULL; } while (*y != '\0' && *x != '\0') { if (tolower(*y) == tolower(*x)) { // move y on one y++; if (!z) { z = x; // store first position in haystack where a match is made } } else { y = needle; // reset y to the beginning of the needle z = NULL; // reset the haystack storage point } x++; // advance the search in the haystack } // If the haystack ended before our search finished, it's not a match. if (*y != '\0') return NULL; return z; } void check_filename(char *fname) { char *t = fname; DEBUG_ENT("check_filename"); if (!t) { DEBUG_RET(); return; } while ((t = strpbrk(t, "/\\:"))) { // while there are characters in the second string that we don't want *t = '_'; //replace them with an underscore } DEBUG_RET(); } void write_separate_attachment(char f_name[], pst_item_attach* attach, int attach_num, pst_file* pst) { FILE *fp = NULL; int x = 0; char *temp = NULL; // If there is a long filename (filename2) use that, otherwise // use the 8.3 filename (filename1) char *attach_filename = (attach->filename2.str) ? attach->filename2.str : attach->filename1.str; DEBUG_ENT("write_separate_attachment"); DEBUG_INFO(("Attachment %s Size is %#"PRIx64", data = %#"PRIxPTR", id %#"PRIx64"\n", attach_filename, (uint64_t)attach->data.size, attach->data.data, attach->i_id)); if (!attach->data.data) { // make sure we can fetch data from the id pst_index_ll *ptr = pst_getID(pst, attach->i_id); if (!ptr) { DEBUG_WARN(("Couldn't find i_id %#"PRIx64". Cannot save attachment to file\n", attach->i_id)); DEBUG_RET(); return; } } check_filename(f_name); if (!attach_filename) { // generate our own (dummy) filename for the attachement temp = pst_malloc(strlen(f_name)+15); sprintf(temp, "%s-attach%i", f_name, attach_num); } else { // have an attachment name, make sure it's unique temp = pst_malloc(strlen(f_name)+strlen(attach_filename)+15); do { if (fp) fclose(fp); if (x == 0) sprintf(temp, "%s-%s", f_name, attach_filename); else sprintf(temp, "%s-%s-%i", f_name, attach_filename, x); } while ((fp = fopen(temp, "r")) && ++x < 99999999); if (x > 99999999) { DIE(("error finding attachment name. exhausted possibilities to %s\n", temp)); } } DEBUG_INFO(("Saving attachment to %s\n", temp)); if (!(fp = fopen(temp, "w"))) { DEBUG_WARN(("write_separate_attachment: Cannot open attachment save file \"%s\"\n", temp)); } else { (void)pst_attach_to_file(pst, attach, fp); fclose(fp); } if (temp) free(temp); DEBUG_RET(); } void write_embedded_message(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pf, char** extra_mime_headers) { pst_index_ll *ptr; DEBUG_ENT("write_embedded_message"); ptr = pst_getID(pf, attach->i_id); pst_desc_tree d_ptr; d_ptr.d_id = 0; d_ptr.parent_d_id = 0; d_ptr.assoc_tree = NULL; d_ptr.desc = ptr; d_ptr.no_child = 0; d_ptr.prev = NULL; d_ptr.next = NULL; d_ptr.parent = NULL; d_ptr.child = NULL; d_ptr.child_tail = NULL; pst_item *item = pst_parse_item(pf, &d_ptr, attach->id2_head); // It appears that if the embedded message contains an appointment/ // calendar item, pst_parse_item returns NULL due to the presence of // an unexpected reference type of 0x1048, which seems to represent // an array of GUIDs representing a CLSID. It's likely that this is // a reference to an internal Outlook COM class. // Log the skipped item and continue on. if (!item) { DEBUG_WARN(("write_embedded_message: pst_parse_item was unable to parse the embedded message in attachment ID %llu", attach->i_id)); } else { if (!item->email) { DEBUG_WARN(("write_embedded_message: pst_parse_item returned type %d, not an email message", item->type)); } else { fprintf(f_output, "\n--%s\n", boundary); fprintf(f_output, "Content-Type: %s\n\n", attach->mimetype.str); write_normal_email(f_output, "", item, MODE_NORMAL, 0, pf, 0, extra_mime_headers); } pst_freeItem(item); } DEBUG_RET(); } void write_inline_attachment(FILE* f_output, pst_item_attach* attach, char *boundary, pst_file* pst) { DEBUG_ENT("write_inline_attachment"); DEBUG_INFO(("Attachment Size is %#"PRIx64", data = %#"PRIxPTR", id %#"PRIx64"\n", (uint64_t)attach->data.size, attach->data.data, attach->i_id)); if (!attach->data.data) { // make sure we can fetch data from the id pst_index_ll *ptr = pst_getID(pst, attach->i_id); if (!ptr) { DEBUG_WARN(("Couldn't find ID pointer. Cannot save attachment to file\n")); DEBUG_RET(); return; } } fprintf(f_output, "\n--%s\n", boundary); if (!attach->mimetype.str) { fprintf(f_output, "Content-Type: %s\n", MIME_TYPE_DEFAULT); } else { fprintf(f_output, "Content-Type: %s\n", attach->mimetype.str); } fprintf(f_output, "Content-Transfer-Encoding: base64\n"); if (attach->filename2.str) { // use the long filename, converted to proper encoding if needed. // it is already utf8 pst_rfc2231(&attach->filename2); fprintf(f_output, "Content-Disposition: attachment; \n filename*=%s\n\n", attach->filename2.str); } else if (attach->filename1.str) { // short filename never needs encoding fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", attach->filename1.str); } else { // no filename is inline fprintf(f_output, "Content-Disposition: inline\n\n"); } (void)pst_attach_to_file_base64(pst, attach, f_output); fprintf(f_output, "\n\n"); DEBUG_RET(); } int valid_headers(char *header) { // headers are sometimes really bogus - they seem to be fragments of the // message body, so we only use them if they seem to be real rfc822 headers. // this list is composed of ones that we have seen in real pst files. // there are surely others. the problem is - given an arbitrary character // string, is it a valid (or even reasonable) set of rfc822 headers? if (header) { if ((strncasecmp(header, "X-Barracuda-URL: ", 17) == 0) || (strncasecmp(header, "X-ASG-Debug-ID: ", 16) == 0) || (strncasecmp(header, "Return-Path: ", 13) == 0) || (strncasecmp(header, "Received: ", 10) == 0) || (strncasecmp(header, "Subject: ", 9) == 0) || (strncasecmp(header, "Date: ", 6) == 0) || (strncasecmp(header, "From: ", 6) == 0) || (strncasecmp(header, "X-x: ", 5) == 0) || (strncasecmp(header, "Microsoft Mail Internet Headers", 31) == 0)) { return 1; } else { if (strlen(header) > 2) { DEBUG_INFO(("Ignore bogus headers = %s\n", header)); } return 0; } } else return 0; } void header_has_field(char *header, char *field, int *flag) { DEBUG_ENT("header_has_field"); if (my_stristr(header, field) || (strncasecmp(header, field+1, strlen(field)-1) == 0)) { DEBUG_INFO(("header block has %s header\n", field+1)); *flag = 1; } DEBUG_RET(); } void header_get_subfield(char *field, const char *subfield, char *body_subfield, size_t size_subfield) { if (!field) return; DEBUG_ENT("header_get_subfield"); char search[60]; snprintf(search, sizeof(search), " %s=", subfield); field++; char *n = header_end_field(field); char *s = my_stristr(field, search); if (n && s && (s < n)) { char *e, *f, save; s += strlen(search); // skip over subfield= if (*s == '"') { s++; e = strchr(s, '"'); } else { e = strchr(s, ';'); f = strchr(s, '\n'); if (e && f && (f < e)) e = f; } if (!e || (e > n)) e = n; // use the trailing lf as terminator if nothing better save = *e; *e = '\0'; snprintf(body_subfield, size_subfield, "%s", s); // copy the subfield to our buffer *e = save; DEBUG_INFO(("body %s %s from headers\n", subfield, body_subfield)); } DEBUG_RET(); } char* header_get_field(char *header, char *field) { char *t = my_stristr(header, field); if (!t && (strncasecmp(header, field+1, strlen(field)-1) == 0)) t = header; return t; } // return pointer to \n at the end of this header field, // or NULL if this field goes to the end of the string. char *header_end_field(char *field) { char *e = strchr(field+1, '\n'); while (e && ((e[1] == ' ') || (e[1] == '\t'))) { e = strchr(e+1, '\n'); } return e; } void header_strip_field(char *header, char *field) { char *t = header_get_field(header, field); if (t) { char *e = header_end_field(t); if (e) { if (t == header) e++; // if *t is not \n, we don't want to keep the \n at *e either. while (*e != '\0') { *t = *e; t++; e++; } *t = '\0'; } else { // this was the last header field, truncate the headers *t = '\0'; } } } int test_base64(char *body) { int b64 = 0; uint8_t *b = (uint8_t *)body; DEBUG_ENT("test_base64"); while (*b) { if ((*b < 32) && (*b != 9) && (*b != 10)) { DEBUG_INFO(("found base64 byte %d\n", (int)*b)); DEBUG_HEXDUMPC(body, strlen(body), 0x10); b64 = 1; break; } b++; } DEBUG_RET(); return b64; } void find_html_charset(char *html, char *charset, size_t charsetlen) { const int index = 1; const int nmatch = index+1; regmatch_t match[nmatch]; DEBUG_ENT("find_html_charset"); int rc = regexec(&meta_charset_pattern, html, nmatch, match, 0); if (rc == 0) { int s = match[index].rm_so; int e = match[index].rm_eo; if (s != -1) { char save = html[e]; html[e] = '\0'; snprintf(charset, charsetlen, "%s", html+s); // copy the html charset html[e] = save; DEBUG_INFO(("charset %s from html text\n", charset)); } else { DEBUG_INFO(("matching %d %d %d %d\n", match[0].rm_so, match[0].rm_eo, match[1].rm_so, match[1].rm_eo)); DEBUG_HEXDUMPC(html, strlen(html), 0x10); } } else { DEBUG_INFO(("regexec returns %d\n", rc)); } DEBUG_RET(); } void find_rfc822_headers(char** extra_mime_headers) { DEBUG_ENT("find_rfc822_headers"); char *headers = *extra_mime_headers; if (headers) { char *temp, *t; while ((temp = strstr(headers, "\n\n"))) { temp[1] = '\0'; t = header_get_field(headers, "\nContent-Type: "); if (t) { t++; DEBUG_INFO(("found content type header\n")); char *n = strchr(t, '\n'); char *s = strstr(t, ": "); char *e = strchr(t, ';'); if (!e || (e > n)) e = n; if (s && (s < e)) { s += 2; if (!strncasecmp(s, RFC822, e-s)) { headers = temp+2; // found rfc822 header DEBUG_INFO(("found 822 headers\n%s\n", headers)); break; } } } //DEBUG_INFO(("skipping to next block after\n%s\n", headers)); headers = temp+2; // skip to next chunk of headers } *extra_mime_headers = headers; } DEBUG_RET(); } void write_body_part(FILE* f_output, pst_string *body, char *mime, char *charset, char *boundary, pst_file* pst) { DEBUG_ENT("write_body_part"); if (body->is_utf8 && (strcasecmp("utf-8", charset))) { // try to convert to the specified charset since the target // is not utf-8, and the data came from a unicode (utf16) field // and is now in utf-8. size_t rc; DEBUG_INFO(("Convert %s utf-8 to %s\n", mime, charset)); pst_vbuf *newer = pst_vballoc(2); rc = pst_vb_utf8to8bit(newer, body->str, strlen(body->str), charset); if (rc == (size_t)-1) { // unable to convert, change the charset to utf8 free(newer->b); DEBUG_INFO(("Failed to convert %s utf-8 to %s\n", mime, charset)); charset = "utf-8"; } else { // null terminate the output string pst_vbgrow(newer, 1); newer->b[newer->dlen] = '\0'; free(body->str); body->str = newer->b; } free(newer); } removeCR(body->str); int base64 = test_base64(body->str); fprintf(f_output, "\n--%s\n", boundary); fprintf(f_output, "Content-Type: %s; charset=\"%s\"\n", mime, charset); if (base64) fprintf(f_output, "Content-Transfer-Encoding: base64\n"); fprintf(f_output, "\n"); if (base64) { char *enc = pst_base64_encode(body->str, strlen(body->str)); if (enc) { write_email_body(f_output, enc); fprintf(f_output, "\n"); free(enc); } } else { write_email_body(f_output, body->str); } DEBUG_RET(); } void write_schedule_part_data(FILE* f_output, pst_item* item, const char* sender, const char* method) { fprintf(f_output, "BEGIN:VCALENDAR\n"); fprintf(f_output, "VERSION:2.0\n"); fprintf(f_output, "PRODID:LibPST v%s\n", VERSION); if (method) fprintf(f_output, "METHOD:%s\n", method); fprintf(f_output, "BEGIN:VEVENT\n"); if (sender) { if (item->email->outlook_sender_name.str) { fprintf(f_output, "ORGANIZER;CN=\"%s\":MAILTO:%s\n", item->email->outlook_sender_name.str, sender); } else { fprintf(f_output, "ORGANIZER;CN=\"\":MAILTO:%s\n", sender); } } write_appointment(f_output, item); fprintf(f_output, "END:VCALENDAR\n"); } void write_schedule_part(FILE* f_output, pst_item* item, const char* sender, const char* boundary) { const char* method = "REQUEST"; const char* charset = "utf-8"; char fname[30]; if (!item->appointment) return; // inline appointment request fprintf(f_output, "\n--%s\n", boundary); fprintf(f_output, "Content-Type: %s; method=\"%s\"; charset=\"%s\"\n\n", "text/calendar", method, charset); write_schedule_part_data(f_output, item, sender, method); fprintf(f_output, "\n"); // attachment appointment request snprintf(fname, sizeof(fname), "i%i.ics", rand()); fprintf(f_output, "\n--%s\n", boundary); fprintf(f_output, "Content-Type: %s; charset=\"%s\"; name=\"%s\"\n", "text/calendar", "utf-8", fname); fprintf(f_output, "Content-Disposition: attachment; filename=\"%s\"\n\n", fname); write_schedule_part_data(f_output, item, sender, method); fprintf(f_output, "\n"); } void write_normal_email(FILE* f_output, char f_name[], pst_item* item, int mode, int mode_MH, pst_file* pst, int save_rtf, char** extra_mime_headers) { char boundary[60]; char altboundary[66]; char *altboundaryp = NULL; char body_charset[30]; char buffer_charset[30]; char body_report[60]; char sender[60]; int sender_known = 0; char *temp = NULL; time_t em_time; char *c_time; char *headers = NULL; int has_from, has_subject, has_to, has_cc, has_date, has_msgid; has_from = has_subject = has_to = has_cc = has_date = has_msgid = 0; DEBUG_ENT("write_normal_email"); pst_convert_utf8_null(item, &item->email->header); headers = valid_headers(item->email->header.str) ? item->email->header.str : valid_headers(*extra_mime_headers) ? *extra_mime_headers : NULL; // setup default body character set and report type strncpy(body_charset, pst_default_charset(item, sizeof(buffer_charset), buffer_charset), sizeof(body_charset)); body_charset[sizeof(body_charset)-1] = '\0'; strncpy(body_report, "delivery-status", sizeof(body_report)); body_report[sizeof(body_report)-1] = '\0'; // setup default sender pst_convert_utf8(item, &item->email->sender_address); if (item->email->sender_address.str && strchr(item->email->sender_address.str, '@')) { temp = item->email->sender_address.str; sender_known = 1; } else { temp = "MAILER-DAEMON"; } strncpy(sender, temp, sizeof(sender)); sender[sizeof(sender)-1] = '\0'; // convert the sent date if it exists, or set it to a fixed date if (item->email->sent_date) { em_time = pst_fileTimeToUnixTime(item->email->sent_date); c_time = ctime(&em_time); if (c_time) c_time[strlen(c_time)-1] = '\0'; //remove end \n else c_time = "Fri Dec 28 12:06:21 2001"; } else c_time = "Fri Dec 28 12:06:21 2001"; // create our MIME boundaries here. snprintf(boundary, sizeof(boundary), "--boundary-LibPST-iamunique-%i_-_-", rand()); snprintf(altboundary, sizeof(altboundary), "alt-%s", boundary); // we will always look at the headers to discover some stuff if (headers ) { char *t; removeCR(headers); temp = strstr(headers, "\n\n"); if (temp) { // cut off our real rfc822 headers here temp[1] = '\0'; // pointer to all the embedded MIME headers. // we use these to find the actual rfc822 headers for embedded message/rfc822 mime parts // but only for the outermost message if (!*extra_mime_headers) *extra_mime_headers = temp+2; DEBUG_INFO(("Found extra mime headers\n%s\n", temp+2)); } // Check if the headers have all the necessary fields header_has_field(headers, "\nFrom: ", &has_from); header_has_field(headers, "\nTo: ", &has_to); header_has_field(headers, "\nSubject: ", &has_subject); header_has_field(headers, "\nDate: ", &has_date); header_has_field(headers, "\nCC: ", &has_cc); header_has_field(headers, "\nMessage-Id: ", &has_msgid); // look for charset and report-type in Content-Type header t = header_get_field(headers, "\nContent-Type: "); header_get_subfield(t, "charset", body_charset, sizeof(body_charset)); header_get_subfield(t, "report-type", body_report, sizeof(body_report)); // derive a proper sender email address if (!sender_known) { t = header_get_field(headers, "\nFrom: "); if (t) { // assume address is on the first line, rather than on a continuation line t++; char *n = strchr(t, '\n'); char *s = strchr(t, '<'); char *e = strchr(t, '>'); if (s && e && n && (s < e) && (e < n)) { char save = *e; *e = '\0'; snprintf(sender, sizeof(sender), "%s", s+1); *e = save; } } } // Strip out the mime headers and some others that we don't want to emit header_strip_field(headers, "\nMicrosoft Mail Internet Headers"); header_strip_field(headers, "\nMIME-Version: "); header_strip_field(headers, "\nContent-Type: "); header_strip_field(headers, "\nContent-Transfer-Encoding: "); header_strip_field(headers, "\nContent-class: "); header_strip_field(headers, "\nX-MimeOLE: "); header_strip_field(headers, "\nBcc:"); header_strip_field(headers, "\nX-From_: "); } DEBUG_INFO(("About to print Header\n")); if (item && item->subject.str) { pst_convert_utf8(item, &item->subject); DEBUG_INFO(("item->subject = %s\n", item->subject.str)); } if (mode != MODE_SEPARATE) { // most modes need this separator line. // procmail produces this separator without the quotes around the // sender email address, but apparently some Mac email client needs // those quotes, and they don't seem to cause problems for anyone else. fprintf(f_output, "From \"%s\" %s\n", sender, c_time); } // print the supplied email headers if (headers) { int len = strlen(headers); if (len > 0) { fprintf(f_output, "%s", headers); // make sure the headers end with a \n if (headers[len-1] != '\n') fprintf(f_output, "\n"); //char *h = headers; //while (*h) { // char *e = strchr(h, '\n'); // int d = 1; // normally e points to trailing \n // if (!e) { // e = h + strlen(h); // e points to trailing null // d = 0; // } // // we could do rfc2047 encoding here if needed // fprintf(f_output, "%.*s\n", (int)(e-h), h); // h = e + d; //} } } + // record read status + if ((item->flags & PST_FLAG_READ) == PST_FLAG_READ) { + fprintf(f_output, "Status: RO\n"); + } + // create required header fields that are not already written if (!has_from) { if (item->email->outlook_sender_name.str){ pst_rfc2047(item, &item->email->outlook_sender_name, 1); fprintf(f_output, "From: %s <%s>\n", item->email->outlook_sender_name.str, sender); } else { fprintf(f_output, "From: <%s>\n", sender); } } if (!has_subject) { if (item->subject.str) { pst_rfc2047(item, &item->subject, 0); fprintf(f_output, "Subject: %s\n", item->subject.str); } else { fprintf(f_output, "Subject: \n"); } } if (!has_to && item->email->sentto_address.str) { pst_rfc2047(item, &item->email->sentto_address, 0); fprintf(f_output, "To: %s\n", item->email->sentto_address.str); } if (!has_cc && item->email->cc_address.str) { pst_rfc2047(item, &item->email->cc_address, 0); fprintf(f_output, "Cc: %s\n", item->email->cc_address.str); } if (!has_date && item->email->sent_date) { char c_time[C_TIME_SIZE]; struct tm stm; gmtime_r(&em_time, &stm); strftime(c_time, C_TIME_SIZE, "%a, %d %b %Y %H:%M:%S %z", &stm); fprintf(f_output, "Date: %s\n", c_time); } if (!has_msgid && item->email->messageid.str) { pst_convert_utf8(item, &item->email->messageid); fprintf(f_output, "Message-Id: %s\n", item->email->messageid.str); } // add forensic headers to capture some .pst stuff that is not really // needed or used by mail clients pst_convert_utf8_null(item, &item->email->sender_address); if (item->email->sender_address.str && !strchr(item->email->sender_address.str, '@') && strcmp(item->email->sender_address.str, ".") && (strlen(item->email->sender_address.str) > 0)) { fprintf(f_output, "X-libpst-forensic-sender: %s\n", item->email->sender_address.str); } if (item->email->bcc_address.str) { pst_convert_utf8(item, &item->email->bcc_address); fprintf(f_output, "X-libpst-forensic-bcc: %s\n", item->email->bcc_address.str); } // add our own mime headers fprintf(f_output, "MIME-Version: 1.0\n"); if (item->type == PST_TYPE_REPORT) { // multipart/report for DSN/MDN reports fprintf(f_output, "Content-Type: multipart/report; report-type=%s;\n\tboundary=\"%s\"\n", body_report, boundary); } else { fprintf(f_output, "Content-Type: multipart/mixed;\n\tboundary=\"%s\"\n", boundary); } fprintf(f_output, "\n"); // end of headers, start of body // now dump the body parts if ((item->type == PST_TYPE_REPORT) && (item->email->report_text.str)) { write_body_part(f_output, &item->email->report_text, "text/plain", body_charset, boundary, pst); fprintf(f_output, "\n"); } if (item->body.str && item->email->htmlbody.str) { // start the nested alternative part fprintf(f_output, "\n--%s\n", boundary); fprintf(f_output, "Content-Type: multipart/alternative;\n\tboundary=\"%s\"\n", altboundary); altboundaryp = altboundary; } else { altboundaryp = boundary; } if (item->body.str) { write_body_part(f_output, &item->body, "text/plain", body_charset, altboundaryp, pst); } if (item->email->htmlbody.str) { find_html_charset(item->email->htmlbody.str, body_charset, sizeof(body_charset)); write_body_part(f_output, &item->email->htmlbody, "text/html", body_charset, altboundaryp, pst); } if (item->body.str && item->email->htmlbody.str) { // end the nested alternative part fprintf(f_output, "\n--%s--\n", altboundary); } if (item->email->rtf_compressed.data && save_rtf) { pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach)); DEBUG_INFO(("Adding RTF body as attachment\n")); memset(attach, 0, sizeof(pst_item_attach)); attach->next = item->attach; item->attach = attach; attach->data.data = pst_lzfu_decompress(item->email->rtf_compressed.data, item->email->rtf_compressed.size, &attach->data.size); attach->filename2.str = strdup(RTF_ATTACH_NAME); attach->filename2.is_utf8 = 1; attach->mimetype.str = strdup(RTF_ATTACH_TYPE); attach->mimetype.is_utf8 = 1; } if (item->email->encrypted_body.data) { pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach)); DEBUG_INFO(("Adding encrypted text body as attachment\n")); attach = (pst_item_attach*) pst_malloc(sizeof(pst_item_attach)); memset(attach, 0, sizeof(pst_item_attach)); attach->next = item->attach; item->attach = attach; attach->data.data = item->email->encrypted_body.data; attach->data.size = item->email->encrypted_body.size; item->email->encrypted_body.data = NULL; } if (item->email->encrypted_htmlbody.data) { pst_item_attach* attach = (pst_item_attach*)pst_malloc(sizeof(pst_item_attach)); DEBUG_INFO(("Adding encrypted HTML body as attachment\n")); attach = (pst_item_attach*) pst_malloc(sizeof(pst_item_attach)); memset(attach, 0, sizeof(pst_item_attach)); attach->next = item->attach; item->attach = attach; attach->data.data = item->email->encrypted_htmlbody.data; attach->data.size = item->email->encrypted_htmlbody.size; item->email->encrypted_htmlbody.data = NULL; } if (item->type == PST_TYPE_SCHEDULE) { write_schedule_part(f_output, item, sender, boundary); } // other attachments { pst_item_attach* attach; int attach_num = 0; for (attach = item->attach; attach; attach = attach->next) { pst_convert_utf8_null(item, &attach->filename1); pst_convert_utf8_null(item, &attach->filename2); pst_convert_utf8_null(item, &attach->mimetype); DEBUG_INFO(("Attempting Attachment encoding\n")); if (attach->method == PST_ATTACH_EMBEDDED) { DEBUG_INFO(("have an embedded rfc822 message attachment\n")); if (attach->mimetype.str) { DEBUG_INFO(("which already has a mime-type of %s\n", attach->mimetype.str)); free(attach->mimetype.str); } attach->mimetype.str = strdup(RFC822); attach->mimetype.is_utf8 = 1; find_rfc822_headers(extra_mime_headers); write_embedded_message(f_output, attach, boundary, pst, extra_mime_headers); } else if (attach->data.data || attach->i_id) { if (mode == MODE_SEPARATE && !mode_MH) write_separate_attachment(f_name, attach, ++attach_num, pst); else write_inline_attachment(f_output, attach, boundary, pst); } } } fprintf(f_output, "\n--%s--\n\n", boundary); DEBUG_RET(); } void write_vcard(FILE* f_output, pst_item* item, pst_item_contact* contact, char comment[]) { char* result = NULL; size_t resultlen = 0; char time_buffer[30]; // We can only call rfc escape once per printf, since the second call // may free the buffer returned by the first call. // I had tried to place those into a single printf - Carl. DEBUG_ENT("write_vcard"); // make everything utf8 pst_convert_utf8_null(item, &contact->fullname); pst_convert_utf8_null(item, &contact->surname); pst_convert_utf8_null(item, &contact->first_name); pst_convert_utf8_null(item, &contact->middle_name); pst_convert_utf8_null(item, &contact->display_name_prefix); pst_convert_utf8_null(item, &contact->suffix); pst_convert_utf8_null(item, &contact->nickname); pst_convert_utf8_null(item, &contact->address1); pst_convert_utf8_null(item, &contact->address2); pst_convert_utf8_null(item, &contact->address3); pst_convert_utf8_null(item, &contact->home_po_box); pst_convert_utf8_null(item, &contact->home_street); pst_convert_utf8_null(item, &contact->home_city); pst_convert_utf8_null(item, &contact->home_state); pst_convert_utf8_null(item, &contact->home_postal_code); pst_convert_utf8_null(item, &contact->home_country); pst_convert_utf8_null(item, &contact->home_address); pst_convert_utf8_null(item, &contact->business_po_box); pst_convert_utf8_null(item, &contact->business_street); pst_convert_utf8_null(item, &contact->business_city); pst_convert_utf8_null(item, &contact->business_state); pst_convert_utf8_null(item, &contact->business_postal_code); pst_convert_utf8_null(item, &contact->business_country); pst_convert_utf8_null(item, &contact->business_address); pst_convert_utf8_null(item, &contact->other_po_box); pst_convert_utf8_null(item, &contact->other_street); pst_convert_utf8_null(item, &contact->other_city); pst_convert_utf8_null(item, &contact->other_state); pst_convert_utf8_null(item, &contact->other_postal_code); pst_convert_utf8_null(item, &contact->other_country); pst_convert_utf8_null(item, &contact->other_address); pst_convert_utf8_null(item, &contact->business_fax); pst_convert_utf8_null(item, &contact->business_phone); pst_convert_utf8_null(item, &contact->business_phone2); pst_convert_utf8_null(item, &contact->car_phone); pst_convert_utf8_null(item, &contact->home_fax); pst_convert_utf8_null(item, &contact->home_phone); pst_convert_utf8_null(item, &contact->home_phone2); pst_convert_utf8_null(item, &contact->isdn_phone); pst_convert_utf8_null(item, &contact->mobile_phone); pst_convert_utf8_null(item, &contact->other_phone); pst_convert_utf8_null(item, &contact->pager_phone); pst_convert_utf8_null(item, &contact->primary_fax); pst_convert_utf8_null(item, &contact->primary_phone); pst_convert_utf8_null(item, &contact->radio_phone); pst_convert_utf8_null(item, &contact->telex); pst_convert_utf8_null(item, &contact->job_title); pst_convert_utf8_null(item, &contact->profession); pst_convert_utf8_null(item, &contact->assistant_name); pst_convert_utf8_null(item, &contact->assistant_phone); pst_convert_utf8_null(item, &contact->company_name); pst_convert_utf8_null(item, &item->body); // the specification I am following is (hopefully) RFC2426 vCard Mime Directory Profile fprintf(f_output, "BEGIN:VCARD\n"); fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->fullname.str, &result, &resultlen)); //fprintf(f_output, "N:%s;%s;%s;%s;%s\n", fprintf(f_output, "N:%s;", (!contact->surname.str) ? "" : pst_rfc2426_escape(contact->surname.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->first_name.str) ? "" : pst_rfc2426_escape(contact->first_name.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->middle_name.str) ? "" : pst_rfc2426_escape(contact->middle_name.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->display_name_prefix.str) ? "" : pst_rfc2426_escape(contact->display_name_prefix.str, &result, &resultlen)); fprintf(f_output, "%s\n", (!contact->suffix.str) ? "" : pst_rfc2426_escape(contact->suffix.str, &result, &resultlen)); if (contact->nickname.str) fprintf(f_output, "NICKNAME:%s\n", pst_rfc2426_escape(contact->nickname.str, &result, &resultlen)); if (contact->address1.str) fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address1.str, &result, &resultlen)); if (contact->address2.str) fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address2.str, &result, &resultlen)); if (contact->address3.str) fprintf(f_output, "EMAIL:%s\n", pst_rfc2426_escape(contact->address3.str, &result, &resultlen)); if (contact->birthday) fprintf(f_output, "BDAY:%s\n", pst_rfc2425_datetime_format(contact->birthday, sizeof(time_buffer), time_buffer)); if (contact->home_address.str) { //fprintf(f_output, "ADR;TYPE=home:%s;%s;%s;%s;%s;%s;%s\n", fprintf(f_output, "ADR;TYPE=home:%s;", (!contact->home_po_box.str) ? "" : pst_rfc2426_escape(contact->home_po_box.str, &result, &resultlen)); fprintf(f_output, "%s;", ""); // extended Address fprintf(f_output, "%s;", (!contact->home_street.str) ? "" : pst_rfc2426_escape(contact->home_street.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->home_city.str) ? "" : pst_rfc2426_escape(contact->home_city.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->home_state.str) ? "" : pst_rfc2426_escape(contact->home_state.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->home_postal_code.str) ? "" : pst_rfc2426_escape(contact->home_postal_code.str, &result, &resultlen)); fprintf(f_output, "%s\n", (!contact->home_country.str) ? "" : pst_rfc2426_escape(contact->home_country.str, &result, &resultlen)); fprintf(f_output, "LABEL;TYPE=home:%s\n", pst_rfc2426_escape(contact->home_address.str, &result, &resultlen)); } if (contact->business_address.str) { //fprintf(f_output, "ADR;TYPE=work:%s;%s;%s;%s;%s;%s;%s\n", fprintf(f_output, "ADR;TYPE=work:%s;", (!contact->business_po_box.str) ? "" : pst_rfc2426_escape(contact->business_po_box.str, &result, &resultlen)); fprintf(f_output, "%s;", ""); // extended Address fprintf(f_output, "%s;", (!contact->business_street.str) ? "" : pst_rfc2426_escape(contact->business_street.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->business_city.str) ? "" : pst_rfc2426_escape(contact->business_city.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->business_state.str) ? "" : pst_rfc2426_escape(contact->business_state.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->business_postal_code.str) ? "" : pst_rfc2426_escape(contact->business_postal_code.str, &result, &resultlen)); fprintf(f_output, "%s\n", (!contact->business_country.str) ? "" : pst_rfc2426_escape(contact->business_country.str, &result, &resultlen)); fprintf(f_output, "LABEL;TYPE=work:%s\n", pst_rfc2426_escape(contact->business_address.str, &result, &resultlen)); } if (contact->other_address.str) { //fprintf(f_output, "ADR;TYPE=postal:%s;%s;%s;%s;%s;%s;%s\n", fprintf(f_output, "ADR;TYPE=postal:%s;",(!contact->other_po_box.str) ? "" : pst_rfc2426_escape(contact->other_po_box.str, &result, &resultlen)); fprintf(f_output, "%s;", ""); // extended Address fprintf(f_output, "%s;", (!contact->other_street.str) ? "" : pst_rfc2426_escape(contact->other_street.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->other_city.str) ? "" : pst_rfc2426_escape(contact->other_city.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->other_state.str) ? "" : pst_rfc2426_escape(contact->other_state.str, &result, &resultlen)); fprintf(f_output, "%s;", (!contact->other_postal_code.str) ? "" : pst_rfc2426_escape(contact->other_postal_code.str, &result, &resultlen)); fprintf(f_output, "%s\n", (!contact->other_country.str) ? "" : pst_rfc2426_escape(contact->other_country.str, &result, &resultlen)); fprintf(f_output, "LABEL;TYPE=postal:%s\n", pst_rfc2426_escape(contact->other_address.str, &result, &resultlen)); } if (contact->business_fax.str) fprintf(f_output, "TEL;TYPE=work,fax:%s\n", pst_rfc2426_escape(contact->business_fax.str, &result, &resultlen)); if (contact->business_phone.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone.str, &result, &resultlen)); if (contact->business_phone2.str) fprintf(f_output, "TEL;TYPE=work,voice:%s\n", pst_rfc2426_escape(contact->business_phone2.str, &result, &resultlen)); if (contact->car_phone.str) fprintf(f_output, "TEL;TYPE=car,voice:%s\n", pst_rfc2426_escape(contact->car_phone.str, &result, &resultlen)); if (contact->home_fax.str) fprintf(f_output, "TEL;TYPE=home,fax:%s\n", pst_rfc2426_escape(contact->home_fax.str, &result, &resultlen)); if (contact->home_phone.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone.str, &result, &resultlen)); if (contact->home_phone2.str) fprintf(f_output, "TEL;TYPE=home,voice:%s\n", pst_rfc2426_escape(contact->home_phone2.str, &result, &resultlen)); if (contact->isdn_phone.str) fprintf(f_output, "TEL;TYPE=isdn:%s\n", pst_rfc2426_escape(contact->isdn_phone.str, &result, &resultlen)); if (contact->mobile_phone.str) fprintf(f_output, "TEL;TYPE=cell,voice:%s\n", pst_rfc2426_escape(contact->mobile_phone.str, &result, &resultlen)); if (contact->other_phone.str) fprintf(f_output, "TEL;TYPE=msg:%s\n", pst_rfc2426_escape(contact->other_phone.str, &result, &resultlen)); if (contact->pager_phone.str) fprintf(f_output, "TEL;TYPE=pager:%s\n", pst_rfc2426_escape(contact->pager_phone.str, &result, &resultlen)); if (contact->primary_fax.str) fprintf(f_output, "TEL;TYPE=fax,pref:%s\n", pst_rfc2426_escape(contact->primary_fax.str, &result, &resultlen)); if (contact->primary_phone.str) fprintf(f_output, "TEL;TYPE=phone,pref:%s\n", pst_rfc2426_escape(contact->primary_phone.str, &result, &resultlen)); if (contact->radio_phone.str) fprintf(f_output, "TEL;TYPE=pcs:%s\n", pst_rfc2426_escape(contact->radio_phone.str, &result, &resultlen)); if (contact->telex.str) fprintf(f_output, "TEL;TYPE=bbs:%s\n", pst_rfc2426_escape(contact->telex.str, &result, &resultlen)); if (contact->job_title.str) fprintf(f_output, "TITLE:%s\n", pst_rfc2426_escape(contact->job_title.str, &result, &resultlen)); if (contact->profession.str) fprintf(f_output, "ROLE:%s\n", pst_rfc2426_escape(contact->profession.str, &result, &resultlen)); if (contact->assistant_name.str || contact->assistant_phone.str) { fprintf(f_output, "AGENT:BEGIN:VCARD\n"); if (contact->assistant_name.str) fprintf(f_output, "FN:%s\n", pst_rfc2426_escape(contact->assistant_name.str, &result, &resultlen)); if (contact->assistant_phone.str) fprintf(f_output, "TEL:%s\n", pst_rfc2426_escape(contact->assistant_phone.str, &result, &resultlen)); } if (contact->company_name.str) fprintf(f_output, "ORG:%s\n", pst_rfc2426_escape(contact->company_name.str, &result, &resultlen)); if (comment) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(comment, &result, &resultlen)); if (item->body.str) fprintf(f_output, "NOTE:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen)); write_extra_categories(f_output, item); fprintf(f_output, "VERSION: 3.0\n"); fprintf(f_output, "END:VCARD\n\n"); if (result) free(result); DEBUG_RET(); } /** * write extra vcard or vcalendar categories from the extra keywords fields * * @param f_output open file pointer * @param item pst item containing the keywords * @return true if we write a categories line */ int write_extra_categories(FILE* f_output, pst_item* item) { char* result = NULL; size_t resultlen = 0; pst_item_extra_field *ef = item->extra_fields; const char *fmt = "CATEGORIES:%s"; int category_started = 0; while (ef) { if (strcmp(ef->field_name, "Keywords") == 0) { fprintf(f_output, fmt, pst_rfc2426_escape(ef->value, &result, &resultlen)); fmt = ", %s"; category_started = 1; } ef = ef->next; } if (category_started) fprintf(f_output, "\n"); if (result) free(result); return category_started; } void write_journal(FILE* f_output, pst_item* item) { char* result = NULL; size_t resultlen = 0; char time_buffer[30]; pst_item_journal* journal = item->journal; // make everything utf8 pst_convert_utf8_null(item, &item->subject); pst_convert_utf8_null(item, &item->body); fprintf(f_output, "BEGIN:VJOURNAL\n"); fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer)); if (item->create_date) fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer)); if (item->modify_date) fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer)); if (item->subject.str) fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen)); if (item->body.str) fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen)); if (journal && journal->start) fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(journal->start, sizeof(time_buffer), time_buffer)); fprintf(f_output, "END:VJOURNAL\n"); if (result) free(result); } void write_appointment(FILE* f_output, pst_item* item) { char* result = NULL; size_t resultlen = 0; char time_buffer[30]; pst_item_appointment* appointment = item->appointment; // make everything utf8 pst_convert_utf8_null(item, &item->subject); pst_convert_utf8_null(item, &item->body); pst_convert_utf8_null(item, &appointment->location); fprintf(f_output, "DTSTAMP:%s\n", pst_rfc2445_datetime_format_now(sizeof(time_buffer), time_buffer)); if (item->create_date) fprintf(f_output, "CREATED:%s\n", pst_rfc2445_datetime_format(item->create_date, sizeof(time_buffer), time_buffer)); if (item->modify_date) fprintf(f_output, "LAST-MOD:%s\n", pst_rfc2445_datetime_format(item->modify_date, sizeof(time_buffer), time_buffer)); if (item->subject.str) fprintf(f_output, "SUMMARY:%s\n", pst_rfc2426_escape(item->subject.str, &result, &resultlen)); if (item->body.str) fprintf(f_output, "DESCRIPTION:%s\n", pst_rfc2426_escape(item->body.str, &result, &resultlen)); if (appointment && appointment->start) fprintf(f_output, "DTSTART;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->start, sizeof(time_buffer), time_buffer)); if (appointment && appointment->end) fprintf(f_output, "DTEND;VALUE=DATE-TIME:%s\n", pst_rfc2445_datetime_format(appointment->end, sizeof(time_buffer), time_buffer)); if (appointment && appointment->location.str) fprintf(f_output, "LOCATION:%s\n", pst_rfc2426_escape(appointment->location.str, &result, &resultlen)); if (appointment) { switch (appointment->showas) { case PST_FREEBUSY_TENTATIVE: fprintf(f_output, "STATUS:TENTATIVE\n"); break; case PST_FREEBUSY_FREE: // mark as transparent and as confirmed fprintf(f_output, "TRANSP:TRANSPARENT\n"); case PST_FREEBUSY_BUSY: case PST_FREEBUSY_OUT_OF_OFFICE: fprintf(f_output, "STATUS:CONFIRMED\n"); break; } if (appointment->is_recurring) { const char* rules[] = {"DAILY", "WEEKLY", "MONTHLY", "YEARLY"}; const char* days[] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"}; pst_recurrence *rdata = pst_convert_recurrence(appointment); fprintf(f_output, "RRULE:FREQ=%s", rules[rdata->type]); if (rdata->count) fprintf(f_output, ";COUNT=%u", rdata->count); if ((rdata->interval != 1) && (rdata->interval)) fprintf(f_output, ";INTERVAL=%u", rdata->interval); if (rdata->dayofmonth) fprintf(f_output, ";BYMONTHDAY=%d", rdata->dayofmonth); if (rdata->monthofyear) fprintf(f_output, ";BYMONTH=%d", rdata->monthofyear); if (rdata->position) fprintf(f_output, ";BYSETPOS=%d", rdata->position); if (rdata->bydaymask) { char byday[40]; int empty = 1; int i=0; memset(byday, 0, sizeof(byday)); for (i=0; i<6; i++) { int bit = 1 << i; if (bit & rdata->bydaymask) { char temp[40]; snprintf(temp, sizeof(temp), "%s%s%s", byday, (empty) ? ";BYDAY=" : ";", days[i]); strcpy(byday, temp); empty = 0; } } fprintf(f_output, "%s", byday); } fprintf(f_output, "\n"); pst_free_recurrence(rdata); } switch (appointment->label) { case PST_APP_LABEL_NONE: if (!write_extra_categories(f_output, item)) fprintf(f_output, "CATEGORIES:NONE\n"); break; case PST_APP_LABEL_IMPORTANT: fprintf(f_output, "CATEGORIES:IMPORTANT\n"); break; case PST_APP_LABEL_BUSINESS: fprintf(f_output, "CATEGORIES:BUSINESS\n"); break; case PST_APP_LABEL_PERSONAL: fprintf(f_output, "CATEGORIES:PERSONAL\n"); break; case PST_APP_LABEL_VACATION: fprintf(f_output, "CATEGORIES:VACATION\n"); break; case PST_APP_LABEL_MUST_ATTEND: fprintf(f_output, "CATEGORIES:MUST-ATTEND\n"); break; case PST_APP_LABEL_TRAVEL_REQ: fprintf(f_output, "CATEGORIES:TRAVEL-REQUIRED\n"); break; case PST_APP_LABEL_NEEDS_PREP: fprintf(f_output, "CATEGORIES:NEEDS-PREPARATION\n"); break; case PST_APP_LABEL_BIRTHDAY: fprintf(f_output, "CATEGORIES:BIRTHDAY\n"); break; case PST_APP_LABEL_ANNIVERSARY: fprintf(f_output, "CATEGORIES:ANNIVERSARY\n"); break; case PST_APP_LABEL_PHONE_CALL: fprintf(f_output, "CATEGORIES:PHONE-CALL\n"); break; } } fprintf(f_output, "END:VEVENT\n"); if (result) free(result); } void create_enter_dir(struct file_ll* f, pst_item *item) { pst_convert_utf8(item, &item->file_as); f->type = item->type; f->stored_count = (item->folder) ? item->folder->item_count : 0; DEBUG_ENT("create_enter_dir"); if (mode == MODE_KMAIL) f->name = mk_kmail_dir(item->file_as.str); else if (mode == MODE_RECURSE) { f->name = mk_recurse_dir(item->file_as.str, f->type); if (mode_thunder) { FILE *type_file = fopen(".type", "w"); fprintf(type_file, "%d\n", item->type); fclose(type_file); } } else if (mode == MODE_SEPARATE) { // do similar stuff to recurse here. mk_separate_dir(item->file_as.str); f->name = (char*) pst_malloc(file_name_len); memset(f->name, 0, file_name_len); } else { f->name = (char*) pst_malloc(strlen(item->file_as.str)+strlen(OUTPUT_TEMPLATE)+1); sprintf(f->name, OUTPUT_TEMPLATE, item->file_as.str); } f->dname = (char*) pst_malloc(strlen(item->file_as.str)+1); strcpy(f->dname, item->file_as.str); if (overwrite != 1) { int x = 0; char *temp = (char*) pst_malloc (strlen(f->name)+10); //enough room for 10 digits sprintf(temp, "%s", f->name); check_filename(temp); while ((f->output = fopen(temp, "r"))) { DEBUG_INFO(("need to increase filename because one already exists with that name\n")); DEBUG_INFO(("- increasing it to %s%d\n", f->name, x)); x++; sprintf(temp, "%s%08d", f->name, x); DEBUG_INFO(("- trying \"%s\"\n", f->name)); if (x == 99999999) { DIE(("create_enter_dir: Why can I not create a folder %s? I have tried %i extensions...\n", f->name, x)); } fclose(f->output); } if (x > 0) { //then the f->name should change free (f->name); f->name = temp; } else { free(temp); } } DEBUG_INFO(("f->name = %s\nitem->folder_name = %s\n", f->name, item->file_as.str)); if (mode != MODE_SEPARATE) { check_filename(f->name); if (!(f->output = fopen(f->name, "w"))) { DIE(("create_enter_dir: Could not open file \"%s\" for write\n", f->name)); } } DEBUG_RET(); } void close_enter_dir(struct file_ll *f) { DEBUG_INFO(("processed item count for folder %s is %i, skipped %i, total %i \n", f->dname, f->item_count, f->skip_count, f->stored_count)); if (output_mode != OUTPUT_QUIET) { pst_debug_lock(); printf("\t\"%s\" - %i items done, %i items skipped.\n", f->dname, f->item_count, f->skip_count); fflush(stdout); pst_debug_unlock(); } if (f->output) { if (mode == MODE_SEPARATE) DEBUG_WARN(("close_enter_dir finds open separate file\n")); struct stat st; fclose(f->output); stat(f->name, &st); if (!st.st_size) { DEBUG_WARN(("removing empty output file %s\n", f->name)); remove(f->name); } f->output = NULL; } free(f->name); free(f->dname); if (mode == MODE_KMAIL) close_kmail_dir(); else if (mode == MODE_RECURSE) { if (mode_thunder) { FILE *type_file = fopen(".size", "w"); fprintf(type_file, "%i %i\n", f->item_count, f->stored_count); fclose(type_file); } close_recurse_dir(); } else if (mode == MODE_SEPARATE) close_separate_dir(); }