📄 mail-loadsave.c
字号:
while (i<len) { if ((message[i+1]=='\n')&&(message[i]=='\r')) { i++;i++; return i; } else if (message[i]=='\n') { i++; return i; } else { while ((message[i]!='\n')&&(i<len)) { i++; } } i++; }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif return i;}/* Modified by Dieter Fiebelkorn <dieter@fiebelkorn.net> * to support MBOX mailboxes, while keeping compatability with old TC format. */int message_save (char *folder, char *message) { int fid=folder_seek_by_name (folder); unsigned long size, hsize; unsigned long seek, ttime; Folder *fseek; FILE *fs; char *home=NULL; char *tmps=NULL; char *filename, *s; int id, slen; Message *msglst; Header_Info *headinf; struct stat st;#if DEBUG > 4 printf( "Inside of message_save().\n" ) ;#endif /* If we don't have a valid folder, use the default Inbox folder */ if (fid==-1) { fid=folder_seek_by_name ("Inbox"); } fseek=folder_seek (fid); size=strlen (message); if (message[size]!='\n') { message[size]='\n'; }#if DEBUG > 6 printf ("message:\n%s", message);#endif hsize=message_hsize (message); tmps=(char *)calloc (hsize+1, sizeof(char)); memmove (tmps, message, hsize); headinf=parse_header (tmps); free (tmps);#if DEBUG > 8 printf( "Saved data...\n" ) ; printf ("headinf->from='%s'\n", headinf->from); printf ("headinf->to='%s'\n", headinf->to); printf ("headinf->cc='%s'\n", headinf->cc); printf ("headinf->subject='%s'\n", headinf->subject); printf ("headinf->priority='%s'\n",headinf->priority); printf ("headinf->replyto='%s'\n",headinf->replyto); printf ("headinf->sensitivity='%s'\n",headinf->sensitivity); printf ("headinf->expiry='%s'\n",headinf->expiry); printf ("headinf->replyby='%s'\n",headinf->replyby); printf ("headinf->keywords='%s'\n",headinf->keywords); printf ("headinf->date='%s'\n", headinf->date); printf ("headinf->content_type='%s'\n", headinf->content_type); printf ("headinf->content_t_e='%s'\n", headinf->content_t_e); printf ("headinf->content_d='%s'\n", headinf->content_d); printf ("headinf->uidl='%s'\n", headinf->uidl);#endif#if DEBUG > 8 printf ("size=%d hsize=%d\n", (int)size, (int)hsize);#endif home=(char *)calloc (4096, sizeof(char)); snprintf (home, 1024, "$(HOME)"); env_srch_rep (home); tmps=(char *)calloc (4096, sizeof(char)); filename=(char *)calloc (4096, sizeof(char)); snprintf (filename, 4096, "%s/.tradeclient/%s",home, fseek->name); fs=fopen(filename, "a"); if (fs!=NULL) { ttime=time(NULL);#ifdef SUPP_RETRIEVED snprintf (tmps, 4096, "Retrieved -- %lu\n", ttime); fwrite (tmps, strlen(tmps), sizeof(char), fs);#else s = ctime (&ttime); slen=strlen(s) - 1; snprintf (tmps, 4096, "From %s %*.*s\n", "TradeClient", slen,slen,s); fwrite (tmps, strlen(tmps), sizeof(char), fs);#endif seek=ftell (fs); fwrite (message, strlen(message), sizeof(char), fs); id=message_add_new (fseek, seek, size, hsize, 0, ttime, fseek->name); msglst=message_seek (fseek, id); fclose (fs); if (headinf->from) msglst->from=strdup(headinf->from); if (headinf->subject) msglst->subject=strdup(headinf->subject); if (headinf->date) msglst->date=strdup(headinf->date); if (headinf->uidl) msglst->uidl=strdup(headinf->uidl); if (headinf->priority) msglst->priority=strdup(headinf->priority); if (headinf->replyto) msglst->replyto=strdup(headinf->replyto); if (headinf->sensitivity) msglst->sensitivity=strdup(headinf->sensitivity); if (headinf->replyby) msglst->replyby=strdup(headinf->replyby); if (headinf->expiry) msglst->expiry=strdup(headinf->expiry); if (headinf->keywords) msglst->keywords=strdup(headinf->keywords); destroy_hinfo (headinf); stat (filename, &st); fseek->asize=st.st_size; free (filename); free (tmps); free (home);#if DEBUG > 4 printf( "Finished message_save() when !NULL.\n" ) ;#endif return msglst->id; } else { printf ("Could not append message to folder; probably permission is denied\n"); } fclose(fs); destroy_hinfo (headinf); free (home); free (tmps);#if DEBUG > 4 printf( "Finished message_save().\n" ) ;#endif return -1;}char *message_load (Folder *fl, Message *msg) { pfile *pf=new_pfile(); char *ret=(char *)calloc(msg->size+5, sizeof(char)); char *tmps=(char *)calloc(1024, sizeof(char)); snprintf (tmps, 1024, "$(HOME)/.tradeclient/%s", fl->name); env_srch_rep (tmps); if ((msg->text) && (!fl->flags&F_NO_CACHE)) { memmove (ret, msg->text, msg->size); msg->texttime=time(NULL); free (tmps); return ret; } strcpy( pf -> name, fl -> name ) ; pf->fs=fopen (tmps, "r"); free (tmps); if (pf->fs!=NULL) { fseek (pf->fs,msg->seek,SEEK_SET); fread (ret, msg->size, sizeof(char), pf->fs); fclose (pf->fs); delete_pfile( pf ) ; if (!fl->flags&F_NO_CACHE) { msg->text=(char *)calloc(msg->size+5, sizeof(char)); memmove (msg->text, ret, msg->size); msg->texttime=time(NULL); } else if (msg->text) { free (msg->text); msg->text=NULL; } return ret; } else { printf ("Unable to load message, file permission denied.\n"); } if (pf) delete_pfile( pf ) ; free (ret); return NULL;}char *message_load_head (Folder *fl, Message *msg) { char *tmpret=message_load (fl, msg); char *ret=NULL; if (tmpret) { ret=(char *)calloc (msg->hsize+1, sizeof(char)); memmove (ret, tmpret, msg->hsize); free (tmpret); } return ret;}char *message_load_body (Folder *fl, Message *msg) { char *tmpret=message_load (fl, msg); char *ret=NULL; if (tmpret) { ret=(char *)calloc ((msg->size-msg->hsize)+1, sizeof(char)); memmove (ret, tmpret+msg->hsize, msg->size-msg->hsize); free (tmpret); } return ret;}/* Added by Dieter Fiebelkorn <dieter@fiebelkorn.net> * to support MBOX mailboxes, while keeping compatability with old TC format. */static int get_wday(char *wday){ if (strncmp (wday, "Sun", 3)==0) return 0; if (strncmp (wday, "Mon", 3)==0) return 1; if (strncmp (wday, "Tue", 3)==0) return 2; if (strncmp (wday, "Wed", 3)==0) return 3; if (strncmp (wday, "Thu", 3)==0) return 4; if (strncmp (wday, "Fri", 3)==0) return 5; if (strncmp (wday, "Sat", 3)==0) return 6; return -1;}/* Added by Dieter Fiebelkorn <dieter@fiebelkorn.net> * to support MBOX mailboxes, while keeping compatability with old TC format. */static int get_month(char *mons){ if (strncmp (mons, "Jan", 3)==0) return 0; if (strncmp (mons, "Feb", 3)==0) return 1; if (strncmp (mons, "Mar", 3)==0) return 2; if (strncmp (mons, "Apr", 3)==0) return 3; if (strncmp (mons, "May", 3)==0) return 4; if (strncmp (mons, "Jun", 3)==0) return 5; if (strncmp (mons, "Jul", 3)==0) return 6; if (strncmp (mons, "Aug", 3)==0) return 7; if (strncmp (mons, "Sep", 3)==0) return 8; if (strncmp (mons, "Oct", 3)==0) return 9; if (strncmp (mons, "Nov", 3)==0) return 10; if (strncmp (mons, "Dec", 3)==0) return 11; return -1;}/* Added by Dieter Fiebelkorn <dieter@fiebelkorn.net> * to support MBOX mailboxes, while keeping compatability with old TC format. */static int isMailHeader(char *tmps, int elns, unsigned long *tmptime){ if (tmps[0]=='R' && strncmp (tmps, "Retrieved --", 12)==0) { sscanf (tmps, "Retrieved -- %lu", tmptime); return TRUE; } /* Examples for mbox-Headers: * From uucphost!xx.yy.dom!user Wed Nov 10 11:11:50 1993 * From user@xx.yy.dom Wed Feb 17 08:52:51 1999 +0200 */ if (elns > 0 && tmps[0]=='F' && strncmp (tmps, "From ", 5)==0) { char email[1024], wdays[8], mons[8]; int day, hour, min, sec, year, mon, wday; struct tm timep; sscanf (tmps, "From %s %3s %3s %2u %2u:%2u:%2u %4u", email, wdays, mons, &day, &hour, &min, &sec, &year); if ((wday=get_wday(wdays))>=0) { if ((mon=get_month(mons))>=0) { timep.tm_mday=day; timep.tm_mon=mon; timep.tm_year=year-1900; timep.tm_hour=hour; timep.tm_min=min; timep.tm_sec=sec; timep.tm_wday=wday; timep.tm_yday=0; timep.tm_isdst=-1; *tmptime=mktime(&timep); if (*tmptime>=0) { return TRUE; } } } } return FALSE;}/* Modified by Dieter Fiebelkorn <dieter@fiebelkorn.net> * to support MBOX mailboxes, while keeping compatability with old TC format. */int folder_reindex (Folder *fl) { pfile *pf=(pfile *)calloc(1, sizeof(pfile)); char *tmps1, *tmps2, *dir, *head; unsigned long tmpoff=0, tmpsize=0, tmphsize=0, tmptime=0; Header_Info *tmphinfo; Message *msg; int mid, emptylines;#if DEBUG > 8 struct timeval tv1, tv2; float tf1,tf2; int i;#endif if (!fl) { free (pf); return FALSE; } dir=(char *)calloc(1024, sizeof(char)); snprintf (dir, 1024, "$(HOME)/.tradeclient"); env_srch_rep (dir); tmps1=(char *)calloc(strlen(fl->name)+strlen(dir)+10, sizeof(char)); snprintf (tmps1, strlen(fl->name)+strlen(dir)+10, "%s/%s", dir, fl->name); free (dir); pf->fs=fopen (tmps1, "r"); free (tmps1);/* This is going to be slow and very painfull, VERY painfull *//* im making this slower than i need to, but for now lets be pedantic */ emptylines=1; tmps2=(char *)calloc(1028, sizeof(char)); while (pf->T_EOF!=1) { getline_ (pf, tmps2); if (isMailHeader(tmps2, emptylines, &tmptime)) { emptylines = 0;#if DEBUG > 7 printf ("Next Message start, offset: %lu\n", pf->bboff+pf->bpos);#endif tmpoff=pf->bboff+pf->bpos; tmps1=(char *)calloc(1028, sizeof(char)); while (pf->T_EOF!=1) { getline_ (pf, tmps1); if (!tmps1) break; if ((tmps1[0]=='\r' && tmps1[1]=='\n') || tmps1[0]=='\n') { emptylines++; break; } else emptylines=0; } if (tmps1) free (tmps1); if (pf->T_EOF!=1) ungetline (pf); tmphsize=(pf->bboff+pf->bpos)-tmpoff; fseek (pf->fs, tmpoff, SEEK_SET); head=(char *)calloc(tmphsize+2, sizeof(char)); fread (head, sizeof(char), tmphsize, pf->fs); tmphinfo=parse_header (head); free (head); tmps1=(char *)calloc(1028, sizeof(char));#if DEBUG > 8 gettimeofday (&tv1, NULL); i=0;#endif while (pf->T_EOF!=1) { getline_ (pf, tmps1); if (!tmps1) break; if ((tmps1[0]=='\r' && tmps1[1]=='\n') || tmps1[0]=='\n') emptylines++; else { if (isMailHeader(tmps1, emptylines, &tmptime)) { break; } emptylines=0; }#if DEBUG > 8 i++; printf ("."); fflush (stdout);#endif }#if DEBUG > 8 printf ("\n"); gettimeofday (&tv2, NULL); tf1=(tv1.tv_sec&0x000000ff)+((float)tv1.tv_usec/(float)1000000); tf2=(tv2.tv_sec&0x000000ff)+((float)tv2.tv_usec/(float)1000000); printf ("Processed %d in %f seconds at %f lines/second\n", i, (tf2-tf1), ((float)i)/(tf2-tf1));#endif if (tmps1) free (tmps1); if (pf->T_EOF!=1) ungetline (pf); tmpsize=(pf->bboff+pf->bpos)-tmpoff; mid=message_add_new (fl, tmpoff, tmpsize, tmphsize, 0, tmptime, fl->name); msg=message_seek (fl, mid); msg->rfolder=strdup (fl->name); if (tmphinfo->from) msg->from=strdup(tmphinfo->from); if (tmphinfo->subject) msg->subject=strdup(tmphinfo->subject); if (tmphinfo->date) msg->date=strdup(tmphinfo->date); if (tmphinfo->uidl) msg->uidl=strdup(tmphinfo->uidl); if (tmphinfo->priority) msg->priority=strdup(tmphinfo->priority); if (tmphinfo->sensitivity) msg->sensitivity=strdup(tmphinfo->sensitivity); if (tmphinfo->replyby) msg->replyby=strdup(tmphinfo->replyby); if (tmphinfo->expiry) msg->expiry=strdup(tmphinfo->expiry); if (tmphinfo->replyto) msg->replyto=strdup(tmphinfo->replyto); if (tmphinfo->keywords) msg->keywords=strdup(tmphinfo->keywords); destroy_hinfo (tmphinfo); } } fl->flags&=~F_INDEX_MISSING; free (pf); free (tmps2); return TRUE;}/* Modified by Dieter Fiebelkorn <dieter@fiebelkorn.net> * to support MBOX mailboxes, while keeping compatability with old TC format. */int folders_compact () { Folder *fl=folder_first (); Folder *flbackup=fl; Message *msg; struct stat st; FILE *fd; int slen; char *tmps, *tmps2, *tmps3, *dir, *s; if (!fl) return FALSE;//should indicate no folders defined dir=(char *)calloc(1024, sizeof(char)); snprintf (dir, 1024, "$(HOME)/.tradeclient"); env_srch_rep (dir); tmps=(char *)calloc(strlen(dir)+258, sizeof(char)); tmps2=(char *)calloc(1028, sizeof(char)); while (fl) { msg=fl->msglst; fl->flags|=F_NO_CACHE; tmps=memset (tmps, 0, strlen(dir)+258); snprintf (tmps, strlen(dir)+strlen(fl->name)+4, "%s/%s~", dir, fl->name); fd=fopen (tmps, "a"); fclose (fd); while (msg) { tmps=memset (tmps, 0, strlen(dir)+258); snprintf (tmps, strlen(dir)+strlen(msg->folder)+4, "%s/%s~", dir, msg->folder); fd=fopen (tmps, "a"); tmps2=memset (tmps2, 0, 1028);#ifdef SUPP_RETRIEVED snprintf (tmps2, 1024, "Retrieved -- %lu\n", msg->time); fwrite (tmps2, strlen(tmps2), sizeof(char), fd);#else s = ctime (&(msg->time)); slen=strlen(s) - 1; snprintf (tmps2, 1024, "From %s %*.*s\n", "TradeClient", slen,slen,s); fwrite (tmps2, strlen(tmps2), sizeof(char), fd);#endif tmps3=message_load (fl, msg); fwrite (tmps3, strlen(tmps3), sizeof(char), fd); free (tmps3); fclose (fd); msg=msg->next; } fl=fl->next; } free (tmps); free (tmps2); fl=flbackup; while (fl) { fl->flags&=~F_NO_CACHE; tmps=(char *)calloc(strlen(dir)+strlen(fl->name)+4, sizeof(char)); tmps2=(char *)calloc(strlen(dir)+strlen(fl->name)+4, sizeof(char)); snprintf (tmps, strlen(dir)+strlen(fl->name)+3, "%s/%s~", dir, fl->name); snprintf (tmps2, strlen(dir)+strlen(fl->name)+2, "%s/%s", dir, fl->name); rename (tmps, tmps2); chmod (tmps2, S_IRUSR | S_IWUSR); stat (tmps2, &st); fl->asize=st.st_size; free (tmps); free (tmps2); fl=fl->next; } free (dir); return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -