Fossil SCM

Populate the esubject column of the emailbox table based on the subject in the email header.

drh 2018-06-29 22:54 UTC smtp
Commit dbb1ce5ff7d41b61175327f3b3e5905e6a0ce07ea707de541e3acdb4b445950f
1 file changed +51 -4
+51 -4
--- src/smtp.c
+++ src/smtp.c
@@ -594,10 +594,53 @@
594594
}
595595
596596
/*****************************************************************************
597597
** Server implementation
598598
*****************************************************************************/
599
+
600
+/*
601
+** Scan the header of the email message in pMsg looking for the
602
+** (first) occurrance of zField. Fill pValue with the content of
603
+** that field.
604
+**
605
+** This routine initializes pValue. Any prior content of pValue is
606
+** discarded (leaked).
607
+**
608
+** Return non-zero on success. Return 0 if no instance of the header
609
+** is found.
610
+*/
611
+int email_header_value(Blob *pMsg, const char *zField, Blob *pValue){
612
+ int nField = (int)strlen(zField);
613
+ Blob line;
614
+ blob_rewind(pMsg);
615
+ blob_init(pValue,0,0);
616
+ while( blob_line(pMsg, &line) ){
617
+ int n, i;
618
+ char *z;
619
+ blob_trim(&line);
620
+ n = blob_size(&line);
621
+ if( n==0 ) return 0;
622
+ if( n<nField+1 ) continue;
623
+ z = blob_buffer(&line);
624
+ if( sqlite3_strnicmp(z, zField, nField)==0 && z[nField]==':' ){
625
+ for(i=nField+1; i<n && fossil_isspace(z[i]); i++){}
626
+ blob_init(pValue, z+i, n-i);
627
+ while( blob_line(pMsg, &line) ){
628
+ blob_trim(&line);
629
+ n = blob_size(&line);
630
+ if( n==0 ) break;
631
+ z = blob_buffer(&line);
632
+ if( !fossil_isspace(z[0]) ) break;
633
+ for(i=1; i<n && fossil_isspace(z[i]); i++){}
634
+ blob_append(pValue, " ", 1);
635
+ blob_append(pValue, z+i, n-i);
636
+ }
637
+ return 1;
638
+ }
639
+ }
640
+ return 0;
641
+}
599642
600643
/*
601644
** Schema used by the email processing system.
602645
*/
603646
static const char zEmailSchema[] =
@@ -854,15 +897,19 @@
854897
blob_trim(&line);
855898
blob_token(&line, &token);
856899
blob_tail(&line, &tail);
857900
if( blob_size(&tail)==0 ) continue;
858901
if( blob_eq_str(&token, "mbox", 4) ){
902
+ Blob subj;
903
+ email_header_value(&p->msg, "subject", &subj);
859904
db_multi_exec(
860
- "INSERT INTO emailbox(euser,edate,efrom,emsgid,ets,estate)"
861
- " VALUES(%Q,now(),%Q,%lld,%lld,0)",
862
- blob_str(&tail), p->zFrom, p->idMsg, p->idTranscript
905
+ "INSERT INTO emailbox(euser,edate,efrom,emsgid,ets,estate,esubject)"
906
+ " VALUES(%Q,now(),%Q,%lld,%lld,0,%Q)",
907
+ blob_str(&tail), p->zFrom, p->idMsg, p->idTranscript,
908
+ blob_str(&subj)
863909
);
910
+ blob_reset(&subj);
864911
}
865912
if( blob_eq_str(&token, "forward", 7) ){
866913
smtp_append_to(p, fossil_strdup(blob_str(&tail)), 1);
867914
}
868915
blob_reset(&tail);
@@ -873,11 +920,11 @@
873920
** The SmtpServer object contains a complete incoming email.
874921
** Add this email to the database.
875922
*/
876923
static void smtp_server_route_incoming(SmtpServer *p, int bFinish){
877924
Stmt s;
878
- int i, j;
925
+ int i;
879926
if( p->zFrom
880927
&& p->nTo
881928
&& blob_size(&p->msg)
882929
&& (p->srvrFlags & SMTPSRV_DRYRUN)==0
883930
){
884931
--- src/smtp.c
+++ src/smtp.c
@@ -594,10 +594,53 @@
594 }
595
596 /*****************************************************************************
597 ** Server implementation
598 *****************************************************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
599
600 /*
601 ** Schema used by the email processing system.
602 */
603 static const char zEmailSchema[] =
@@ -854,15 +897,19 @@
854 blob_trim(&line);
855 blob_token(&line, &token);
856 blob_tail(&line, &tail);
857 if( blob_size(&tail)==0 ) continue;
858 if( blob_eq_str(&token, "mbox", 4) ){
 
 
859 db_multi_exec(
860 "INSERT INTO emailbox(euser,edate,efrom,emsgid,ets,estate)"
861 " VALUES(%Q,now(),%Q,%lld,%lld,0)",
862 blob_str(&tail), p->zFrom, p->idMsg, p->idTranscript
 
863 );
 
864 }
865 if( blob_eq_str(&token, "forward", 7) ){
866 smtp_append_to(p, fossil_strdup(blob_str(&tail)), 1);
867 }
868 blob_reset(&tail);
@@ -873,11 +920,11 @@
873 ** The SmtpServer object contains a complete incoming email.
874 ** Add this email to the database.
875 */
876 static void smtp_server_route_incoming(SmtpServer *p, int bFinish){
877 Stmt s;
878 int i, j;
879 if( p->zFrom
880 && p->nTo
881 && blob_size(&p->msg)
882 && (p->srvrFlags & SMTPSRV_DRYRUN)==0
883 ){
884
--- src/smtp.c
+++ src/smtp.c
@@ -594,10 +594,53 @@
594 }
595
596 /*****************************************************************************
597 ** Server implementation
598 *****************************************************************************/
599
600 /*
601 ** Scan the header of the email message in pMsg looking for the
602 ** (first) occurrance of zField. Fill pValue with the content of
603 ** that field.
604 **
605 ** This routine initializes pValue. Any prior content of pValue is
606 ** discarded (leaked).
607 **
608 ** Return non-zero on success. Return 0 if no instance of the header
609 ** is found.
610 */
611 int email_header_value(Blob *pMsg, const char *zField, Blob *pValue){
612 int nField = (int)strlen(zField);
613 Blob line;
614 blob_rewind(pMsg);
615 blob_init(pValue,0,0);
616 while( blob_line(pMsg, &line) ){
617 int n, i;
618 char *z;
619 blob_trim(&line);
620 n = blob_size(&line);
621 if( n==0 ) return 0;
622 if( n<nField+1 ) continue;
623 z = blob_buffer(&line);
624 if( sqlite3_strnicmp(z, zField, nField)==0 && z[nField]==':' ){
625 for(i=nField+1; i<n && fossil_isspace(z[i]); i++){}
626 blob_init(pValue, z+i, n-i);
627 while( blob_line(pMsg, &line) ){
628 blob_trim(&line);
629 n = blob_size(&line);
630 if( n==0 ) break;
631 z = blob_buffer(&line);
632 if( !fossil_isspace(z[0]) ) break;
633 for(i=1; i<n && fossil_isspace(z[i]); i++){}
634 blob_append(pValue, " ", 1);
635 blob_append(pValue, z+i, n-i);
636 }
637 return 1;
638 }
639 }
640 return 0;
641 }
642
643 /*
644 ** Schema used by the email processing system.
645 */
646 static const char zEmailSchema[] =
@@ -854,15 +897,19 @@
897 blob_trim(&line);
898 blob_token(&line, &token);
899 blob_tail(&line, &tail);
900 if( blob_size(&tail)==0 ) continue;
901 if( blob_eq_str(&token, "mbox", 4) ){
902 Blob subj;
903 email_header_value(&p->msg, "subject", &subj);
904 db_multi_exec(
905 "INSERT INTO emailbox(euser,edate,efrom,emsgid,ets,estate,esubject)"
906 " VALUES(%Q,now(),%Q,%lld,%lld,0,%Q)",
907 blob_str(&tail), p->zFrom, p->idMsg, p->idTranscript,
908 blob_str(&subj)
909 );
910 blob_reset(&subj);
911 }
912 if( blob_eq_str(&token, "forward", 7) ){
913 smtp_append_to(p, fossil_strdup(blob_str(&tail)), 1);
914 }
915 blob_reset(&tail);
@@ -873,11 +920,11 @@
920 ** The SmtpServer object contains a complete incoming email.
921 ** Add this email to the database.
922 */
923 static void smtp_server_route_incoming(SmtpServer *p, int bFinish){
924 Stmt s;
925 int i;
926 if( p->zFrom
927 && p->nTo
928 && blob_size(&p->msg)
929 && (p->srvrFlags & SMTPSRV_DRYRUN)==0
930 ){
931

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button