Skip to content
Snippets Groups Projects
Commit 602a2d0e authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Updated to be suitable as a /usr/bin/mail[x] replacement too

Migrated rcpt() to mailutil.js parse_mail_recipient()
parent dfad2d1a
No related branches found
No related tags found
No related merge requests found
#!/sbbs/exec/jsexec -x -c/sbbs/ctrl
// Change previous line to correct jsexec and ctrl paths!
#!/sbbs/exec/jsexec -x
// Change previous line to correct jsexec path!
// Suitable as a replacement for /usr/sbin/sendmail
// and/or /usr/bin/mail
// and/or /usr/bin/mailx
var log = new File(system.temp_dir + "sendmail.log");
//log.open('a');
log.writeln(system.timestr() + ' ' + js.exec_file + ' argv=' + argv);
load("sbbsdefs.js");
load("mailutil.js");
/* This script emulates the "sendmail -i -t" or "sendmail -oi -t"
* *nix command, sending a message from stdin to recipients specified
/* This script emulates the "sendmail" or "mail" *nix command,
* sending a message from stdin to recipients specified
* in the message headers and not using . on a line by itself
* to terminate a message.
*/
var line;
var verbosity = 0;
var done_headers=false;
var hdr = { from: 'sendmail' };
var hdr = { from: 'sendmail', subject: 'no subject specified' };
var sendmail_mode = (js.exec_file != 'mail' && js.exec_file != 'mailx');
var done_headers = !sendmail_mode;
var body = '';
var rcpt_list = [];
function rcpt(str)
{
return {
to: mail_get_name(str),
to_net_addr: mail_get_address(str),
to_net_type: NET_INTERNET
};
}
var parse_options = true;
var parse_recipients = false;
for(var i = 0; i < argv.length; i++) {
if(argv[i][0] == '-') {
if(parse_options && argv[i][0] == '-') {
switch(argv[i][1]) {
case '-':
// "Stop processing command flags
// and use the rest of the arguments as addresses."
parse_options = false;
break;
case 'v':
verbosity++;
break;
case 'F':
hdr.from = argv[++i];
if(sendmail_mode) {
if(argv[i].length > 2)
hdr.from = argv[i].substring(2);
else
hdr.from = argv[++i];
}
break;
case 'r':
case 'f':
if(argv[i].length > 2)
hdr.from_net_addr = argv[i].substring(2);
else
hdr.from_net_addr = argv[++i];
break;
case 's':
if(!sendmail_mode)
hdr.subject = argv[++i];
break;
case 't':
parse_recipients = true;
done_headers = false;
break;
}
} else {
rcpt_list.push(rcpt(argv[i]));
rcpt_list.push(parse_mail_recipient(argv[i]));
}
}
if(parse_recipients && !sendmail_mode) {
// "Recipients specified on the command line are ignored."
rcpt_list.length = 0;
}
while((line=readln()) != undefined) {
log.writeln(line);
if(!done_headers) {
if(line == '') {
done_headers=true;
......@@ -53,23 +86,23 @@ while((line=readln()) != undefined) {
if(m != undefined && m.index>-1) {
switch(m[1].toLowerCase()) {
case 'to':
if(!parse_recipients)
break;
addys=m[2].split(',');
for (addy in addys) {
rcpt_list.push(rcpt(addys[addy]));
rcpt_list.push(parse_mail_recipient(addys[addy]));
}
break;
case 'from':
hdr.from=mail_get_name(m[2]);
hdr.from_net_type=NET_INTERNET;
hdr.from_net_addr=mail_get_address(m[2]);
hdr.from = mail_get_name(m[2]);
hdr.from_net_addr = mail_get_address(m[2]);
break;
case 'subject':
hdr.subject=m[2];
break;
case 'reply-to':
hdr.replyto=mail_get_name(m[2]);
hdr.replyto_net_type=NET_INTERNET;
hdr.replyto_net_addr=mail_get_address(m[2]);
hdr.replyto = mail_get_name(m[2]);
hdr.replyto_net_addr = mail_get_address(m[2]);
break;
case 'message-id':
hdr.id=m[2];
......@@ -81,7 +114,8 @@ while((line=readln()) != undefined) {
hdr.date=m[2];
break;
}
}
} else
print("Non-header line received: " + line);
}
else {
body += line + "\r\n";
......@@ -97,3 +131,8 @@ if(!msgbase.save_msg(hdr, body, rcpt_list)) {
writeln("Cannot send email: " + msgbase.error);
exit();
}
log.writeln("---");
log.writeln("hdr = " + JSON.stringify(hdr, null, 4));
log.writeln("rcpt_list = " + JSON.stringify(rcpt_list, null, 4));
log.writeln("===");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment