static int _undbx(char *dbx_dir, char *out_dir, char *dbx_file, dbx_options_t *options) { int deleted = 0; int saved = 0; int errors = 0; dbx_t *dbx = NULL; char *eml_dir = NULL; char *cwd = NULL; int rc = -1; cwd = sys_getcwd(); if (cwd == NULL) { dbx_progress_message(NULL, DBX_STATUS_ERROR, "can't get current working directory"); goto UNDBX_DONE; } rc = sys_chdir(dbx_dir); if (rc != 0) { dbx_progress_message(NULL, DBX_STATUS_ERROR, "can't chdir to %s", dbx_dir); goto UNDBX_DONE; } dbx = dbx_open(dbx_file, options); sys_chdir(cwd); if (dbx == NULL) { dbx_progress_message(NULL, DBX_STATUS_WARNING, "can't open DBX file %s", dbx_file); rc = -1; goto UNDBX_DONE; } if (!options->recover && dbx->type != DBX_TYPE_EMAIL) { dbx_progress_message(dbx->progress_handle, DBX_STATUS_WARNING, "DBX file %s does not contain messages", dbx_file); rc = -1; goto UNDBX_DONE; } if (!options->recover && dbx->file_size >= 0x80000000) { dbx_progress_message(dbx->progress_handle, DBX_STATUS_WARNING,"DBX file %s is corrupted (larger than 2GB)", dbx_file); } eml_dir = strdup(dbx_file); eml_dir[strlen(eml_dir) - 4] = '\0'; rc = sys_mkdir(out_dir, eml_dir); if (rc != 0) { dbx_progress_message(dbx->progress_handle, DBX_STATUS_ERROR, "can't create directory %s/%s", out_dir, eml_dir); goto UNDBX_DONE; } rc = sys_chdir(out_dir); if (rc != 0) { dbx_progress_message(dbx->progress_handle, DBX_STATUS_ERROR, "can't chdir to %s", out_dir); goto UNDBX_DONE; } if (options->recover) _recover(dbx, out_dir, eml_dir, &saved, &errors); else _extract(dbx, out_dir, eml_dir, &saved, &deleted, &errors); UNDBX_DONE: free(eml_dir); eml_dir = NULL; dbx_close(dbx); sys_chdir(cwd); free(cwd); cwd = NULL; return rc; }
int main(int argc, char **argv) { DBX *dbx, *folders; DBXFOLDER *x = NULL; DBXEMAIL *z = NULL; time_t date; int y,a, c, quiet = 0, totalEmails = 0; char *fname = NULL; FILE *outfile; char *indir = NULL, *outdir = NULL; while ((c = getopt(argc, argv, "hVi:o:q")) != EOF) { switch (c) { case 'V': //Version printf("readoe v%s - Implementing libdbx v%s\n", VERSION, LIBDBX_VERSION); exit(0); case 'h': //help usage(0); case 'i': //input directory indir = optarg; break; case 'o': //output directory outdir = optarg; break; case 'q': //quiet quiet = 1; break; default: usage(1); } } if (indir == NULL || outdir == NULL) { printf("readoe: must specify BOTH input directory and output directory\n"); usage(2); } fname = (char*) realloc(fname, strlen(indir)+strlen("Folders.dbx")+2); sprintf(fname, "%s/%s", indir, "Folders.dbx"); folders = dbx_open(fname); if (folders == NULL){ dbx_perror("Folder file open"); return 1; } if (folders->type != DBX_TYPE_FOLDER) { printf("File Folders.dbx doesn't appear to contain the correct stuff\n"); return 2; } for (y = folders->indexCount-1; y >= 0; y--) { dbx_free(folders, x); x=(DBXFOLDER*)dbx_get(folders, y, 0); if (dbx_errno != DBX_NOERROR) { dbx_perror("Folder Read"); return 2; } if (x) { if (!quiet) { printf("\nFolder Name: %s\n", x->name); } } else continue; if (x->fname != NULL) { fname = (char*) realloc (fname, strlen(indir)+strlen(x->fname)+2); sprintf(fname, "%s/%s", indir, x->fname); dbx = dbx_open(fname); if (dbx == NULL) { dbx_perror("Email Folder Open"); continue; } if (dbx->type != DBX_TYPE_EMAIL) { printf("Folder %s doesn't contain emails like I expect\n", fname); continue; } fname = (char*) realloc (fname, strlen(outdir)+strlen(x->fname)+2); sprintf(fname, "%s/%s", outdir, x->fname); if ((outfile = fopen (fname, "w")) == NULL) { printf("Cannot open output file %s\n", fname); continue; } for (a = dbx->indexCount-1; a >= 0; a--) { dbx_free(dbx, z); z = (DBXEMAIL*)dbx_get(dbx,a, DBX_FLAG_BODY); if (dbx_errno != DBX_NOERROR) { dbx_perror("Email Read"); break; } if (z && z->email != NULL) { date = FileTimeToUnixTime(&(z->date), NULL); fprintf(outfile, "From %s %s", z->sender_address, ctime(&date)); //ctime adds \n write_email(outfile, z->email); fprintf(outfile, "\n\n"); } else if (z == NULL) { printf("DBX returned a NULL email\n"); } else { if (!quiet) printf("Email has no body\n"); } } if (!quiet) printf("%d emails processed\n", dbx->indexCount+1); totalEmails += (dbx->indexCount+1); fclose(outfile); dbx_close(dbx); } } if (!quiet) printf("Total Emails processed: %d\n", totalEmails); dbx_close(folders); return 0; }