@@ -27,37 +27,47 @@ export class MessageData {
2727 return str . toLowerCase ( ) . split ( ',' ) . map ( address => address . trim ( ) ) ;
2828 }
2929
30- private static parseListId ( raw : string ) : string {
31- // Parsing would be limited to headers only
32- const raw_header_str = raw . split ( '\r\n\r\n' ) [ 0 ] ;
33- // const match = raw_header_str.match(/^\s*list-id:[^<]*<([^>]*)>\s*$/im);
34- // if (match == null || !match[1]) {
35- // return '';
36- // }
37- // const raw_list_id = match[1];
38- // const raw_list_id_at_index = raw_list_id.lastIndexOf('.', raw_list_id.lastIndexOf('.') - 1);
39- // const listId = raw_list_id.substr(0, raw_list_id_at_index) + '@' + raw_list_id.substr(raw_list_id_at_index + 1, raw_list_id.length);
40- // return listId.toLowerCase().replace(/[^a-z0-9@\.\/+]+/g, '-');
41-
42- // E.x. Mailing-list: list [email protected] ; contact [email protected] 43- const match = raw_header_str . match ( / ^ \s * m a i l i n g - l i s t : ( .* ) $ / im) ;
44- if ( match == null || ! match [ 1 ] ) {
45- return '' ;
46- }
47- const parts = match [ 1 ] . trim ( ) . split ( ';' ) ;
48- if ( parts . length === 0 ) {
49- return '' ;
30+ private static parseMailingList ( message : GoogleAppsScript . Gmail . GmailMessage ) : string {
31+ const mailing_list = message . getHeader ( 'Mailing-list' ) . trim ( ) ;
32+ if ( ! ! mailing_list ) {
33+ 34+ const parts = mailing_list . split ( ';' ) ;
35+ for ( const part of parts ) {
36+ const [ type , address ] = part . trim ( ) . split ( / \s + / ) ;
37+ Utils . assert ( typeof address !== 'undefined' , `Unexpected mailing list: ${ mailing_list } ` ) ;
38+ if ( type . trim ( ) === 'list' ) {
39+ return address ;
40+ }
41+ }
5042 }
51- for ( const part of parts ) {
52- const [ type , address ] = part . trim ( ) . split ( / \s + / ) ;
53- Utils . assert ( typeof address !== 'undefined' , `Unexpected mailing list: ${ match [ 1 ] . trim ( ) } ` ) ;
54- if ( type . trim ( ) === 'list' ) {
55- return address ;
43+ const list_id = message . getHeader ( 'List-ID' ) . trim ( ) ;
44+ if ( ! ! list_id ) {
45+ // E.x. "<mygroup.gmail.com>"
46+ let address = list_id ;
47+ if ( address . length > 0 && address . charAt ( 0 ) == '<' ) {
48+ address = address . substring ( 1 ) ;
49+ }
50+ if ( address . length > 0 && address . charAt ( - 1 ) ) {
51+ address = address . slice ( 0 , - 1 ) ;
5652 }
53+ return address ;
5754 }
5855 return '' ;
5956 }
6057
58+ private static parseSenders ( message : GoogleAppsScript . Gmail . GmailMessage ) : string [ ] {
59+ const original_sender = message . getHeader ( 'X-Original-Sender' ) . trim ( ) ;
60+ const sender = message . getHeader ( 'Sender' ) . trim ( ) ;
61+ const senders : string [ ] = [ ] ;
62+ if ( ! ! original_sender ) {
63+ senders . push ( original_sender ) ;
64+ }
65+ if ( ! ! sender ) {
66+ senders . push ( sender ) ;
67+ }
68+ return senders ;
69+ }
70+
6171 public readonly from : string ;
6272 public readonly to : string [ ] ;
6373 public readonly cc : string [ ] ;
@@ -74,17 +84,18 @@ export class MessageData {
7484 this . to = MessageData . parseAddresses ( message . getTo ( ) ) ;
7585 this . cc = MessageData . parseAddresses ( message . getCc ( ) ) ;
7686 this . bcc = MessageData . parseAddresses ( message . getBcc ( ) ) ;
77- this . list = MessageData . parseListId ( message . getRawContent ( ) ) ;
87+ this . list = MessageData . parseMailingList ( message ) ;
7888 this . reply_to = MessageData . parseAddresses ( message . getReplyTo ( ) ) ;
79- this . sender = ( [ ] as string [ ] ) . concat ( this . from , this . reply_to ) ;
89+ this . sender = ( [ ] as string [ ] ) . concat (
90+ this . from , this . reply_to , ...MessageData . parseSenders ( message ) ) ;
8091 this . receivers = ( [ ] as string [ ] ) . concat ( this . to , this . cc , this . bcc , this . list ) ;
8192 this . subject = message . getSubject ( ) ;
8293 // Potentially could be HTML, Plain, or RAW. But doesn't seem very useful other than Plain.
8394 let body = message . getPlainBody ( ) ;
8495 // Truncate and log long messages.
8596 if ( body . length > MAX_BODY_PROCESSING_LENGTH ) {
8697 Logger . log ( `Ignoring the end of long message with subject "${ this . subject } "` ) ;
87- body = body . substr ( 0 , MAX_BODY_PROCESSING_LENGTH ) ;
98+ body = body . substring ( 0 , MAX_BODY_PROCESSING_LENGTH ) ;
8899 }
89100 this . body = body ;
90101 }
0 commit comments