/*
	MR/2 ICE - MAILLIST.CMD

	Copyright (c) 1996, Nick Knight
	All Rights Reserved.

	Author: 	Nick Knight
	Created:	02/26/96
	Usage:		maillist addressFile MessageFile
	Purpose:	maillist.cmd will parse message to an automated mailing
				list and handle adding and removing email addresses
				from the list via email command.  Subjects should
				start with either "SUBSCRIBE" or "UNSUBSCRIBE" where
				case of commands is not important.

	US Mail:	Nick Knight, PO Box 22366, Beachwood, Ohio 44122
	Fidonet:	1:157/2 or 1:/157/200
	Internet:	nick@secant.com
	Compuserve: 76066,1240
	BBS:		Private messages on Nerd's Nook (216) 356-1772 or 1872
*/

'@echo off'
parse arg address_list msg_filename

status = stream(msg_filename,'c','open read')

replyto = ''
from = ''
tag = ''
subject = ''
value = ''
tmp_lst = 'adr$$_$.$$$'
mode = 0

ctr = 0
do while ((ctr < 100) & (lines(msg_filename) > 0))
	iline = linein(msg_filename)
	Parse VALUE iline with tag ': ' value
	Parse UPPER VALUE iline with tag ': '
	say tag
	
	if (tag == 'REPLY-TO') then
		replyto = value
	else if (tag == 'FROM') then
		from = value
	else if (tag == 'SUBJECT') then
		subject = value
	
	ctr = ctr + 1
end

status = stream(msg_filename,'c','close')

/* for debugging, dump info to screen */
say 'Reply to: '||replyto||', From: '||from

if Length(replyto) > 0 then
	from = replyto
	
PARSE UPPER value subject with subject .
if (subject == 'UNSUBSCRIBE') then
	mode = 2
else if (subject == 'SUBSCRIBE') then
	mode = 1

say 'Subject: '||subject||' ('||mode||')'

if ((Length(replyto) > 0) | (Length(from) > 0)) then do

	if (mode == 2) then
		status = stream(tmp_lst,'c','open write')
	
	status = stream(address_list,'c','open read')
	fFound = 0
	do while ((fFound == 0) & (lines(address_list) > 0))
		iline = linein(address_list)
		if (iline == from) then
			fFound = 1
		else if (mode == 2) then
			rc = lineout(tmp_lst,iline)
	end
	status = stream(address_list,'c','close')
	
	if (mode == 2) then do
		status = stream(tmp_lst,'c','close')
		if (fFound <> 0) then do
		
			'copy '||address_list||' '||address_list||'-bak >nul'
			'copy '||tmp_lst||' '||address_list||' >nul'
			'del '||tmp_lst||' >nul'
		end
		else do
			Say 'Error ... not in list!!'
		end
	end
	else if (mode == 1) then do
		if (fFound == 0) then do
			status = stream(address_list,'c','open')
			status = stream(address_list,'c','seek <0')
			rc = lineout(address_list,from)
			status = stream(address_list,'c','close')
		end
		else do
			Say 'Error ... already in list!!'
		end
	end
end

