From 5482edc5b2177522dadc9e88f7b750180b13e893 Mon Sep 17 00:00:00 2001 From: rswindell <> Date: Tue, 21 Oct 2003 23:57:23 +0000 Subject: [PATCH] Created parse_msg_header() convenience function (in mailproc_util.js) to create an object (associative array) that contains all the RFC822 header fields as string properties. --- exec/load/mailproc_util.js | 22 ++++++++++++++++++++++ exec/mailproc_example.js | 18 ++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 exec/load/mailproc_util.js diff --git a/exec/load/mailproc_util.js b/exec/load/mailproc_util.js new file mode 100644 index 0000000000..639c8b88d6 --- /dev/null +++ b/exec/load/mailproc_util.js @@ -0,0 +1,22 @@ +// mailproc_util.js + +// Utility functions for Synchronet external mail processors + +// $Id$ + +// Parses raw RFC822-formatted messages for use with SMTP Mail Processors +// Returns an array of header fields parsed from the msgtxt +// msgtxt is an array of lines from the source (RFC822) message text +function parse_msg_header(msgtxt) +{ + var hdr={}; + + for(i in msgtxt) { + if(msgtxt[i].length==0) /* Header delimiter */ + break; + var match = msgtxt[i].match(/\s*(\S+)\s*:\s*(.*)/); + hdr[match[0]]=match[1]; + } + + return(hdr); +} \ No newline at end of file diff --git a/exec/mailproc_example.js b/exec/mailproc_example.js index 2ac8bcf423..69e6f2c3a1 100644 --- a/exec/mailproc_example.js +++ b/exec/mailproc_example.js @@ -5,6 +5,8 @@ // $Id$ +load("mailproc_util.js"); + // Set to false at any time to indicate a processing failure var success=true; @@ -25,19 +27,27 @@ var recipient=rcptlst.iniGetAllObjects("number"); // These lines open the message text file in append mode (writing to the end) var msgtxt = new File(message_text_filename); -if(!msgtxt.open("a")) // Change thie mode to "r+" for "read/update" access +if(!msgtxt.open("a+")) // Change the mode to "r+" for "read/update" access exit(); +// Create an object (associative array) of header field strings +var header = parse_msg_header(msgtxt.readAll()); + // This an example of modifying the message text. // In this case, we're adding some text (a dump of the recipient object array) // to the end of the message. -msgtxt.writeln("\r\nHello from mailproc_example.js\r\n"); -msgtxt.writeln("Array of recipient objects (message envelopes):\r\n"); +msgtxt.writeln("\r\nHello from mailproc_example.js"); // Dump recipient object array +msgtxt.writeln("\r\nArray of recipient objects (message envelopes):\r\n"); for(i in recipient) // For each recipient object... for(j in recipient[i]) // For each property... - msgtxt.writeln("recipient[" +i+ "]." +j+ "=" + recipient[i][j]); + msgtxt.writeln("recipient[" +i+ "]." +j+ " = " + recipient[i][j]); + +// Dump header field strings +msgtxt.writeln("\r\nArray of RFC822 header fields:\r\n"); +for(i in header) + msgtxt.writeln("header." +i+ " = " + header[i]); // If there were any processing errors... reject the message if(!success) -- GitLab