/*******************************************************************/
/*                    Twit script for Spot                         */
/*                                                                 */
/* This script will go through all your unread messages looking    */
/* for messages to or from unwanted users or containing trivial    */
/* subjects. See enclosed Twit.cfg for examples of setup.          */
/* These messages will either be marked as READ or DELETED.        */
/*                                                                 */
/* Place Twit.cfg in MAIL: or update TwitFile variable to reflect  */
/* position.                                                       */
/*                                                                 */
/*                                                                 */
/* Twit.spot knows two switches:                                   */
/*                                                                 */
/* DELETE   All matching messages will be deleted                  */
/*          (Default is to mark messages as READ)                  */
/*                                                                 */
/* QUIET    You will not be presented with a BailOut requester.    */
/*          (Default is to ask if you're sure you want to twit)    */
/*                                                                 */
/* WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! WARNING!  */
/*                                                                 */
/* This script is potentionally dangerous, therefore I DO          */
/* NOT recommend you to use arguments DELETE or QUIET until you    */
/* are familiar with the inner workings of this script!!!          */
/*                                                                 */
/* I take no responsibility for any damage done to your            */
/* message-base. Use at your own RISK!!!                           */
/*                                                                 */
/* 'nuff serious stuff, let's Twit :-)                             */
/*                                                                 */
/*                 $VER: Twit.spot 1.0 (7.8.93)                    */
/*                                                                 */
/*       Author: Brian Jacobsen   FidoNet: 2:230/311.17            */
/*******************************************************************/

address 'SPOT'
signal on syntax
options results
PARSE UPPER ARG arg1 arg2

Twitfile = 'MAIL:Twit.cfg'								/* Name of config     */
Writers. = 0											/* Stem for writers   */
Receivers. = 0											/* Stem for receivers */
Subjects. = 0											/* Stem for subjects  */

IF arg1 = 'DELETE' | arg2 = 'DELETE' THEN				/* Delete....         */
	DO
		DeleteMessages = 1								/* At your own risk   */
		Action = 'delete'
	END
ELSE													/* Mark as READ       */
	DO
		DeleteMessages = 0
		Action = 'clear UNREAD flag for'
	END

IF arg1 = 'QUIET' | arg2 = 'QUIET' THEN					/* Be quiet...        */
	Quiet = 1
ELSE													/* Accept requester   */
	Quiet = 0


CALL ReadCfg Twitfile									/* Read config        */
'lockgui'												/* Lock Spot's GUI    */

'arealist'												/* Goto arealist      */
'unreadarea'
'messagelist'
'getareaname'
FirstArea = result
DO UNTIL FirstArea = result
	DO i = 1 TO Writers.0
		SpotArgs = '"'Writers.i'" FROM UNREAD NOGUI NOREQ'
		'include' SpotArgs
	END
	DO i = 1 TO Receivers.0
		SpotArgs = '"'Receivers.i'" TO UNREAD NOGUI NOREQ'
		'include' SpotArgs
	END
	DO i = 1 TO Subjects.0
		SpotArgs = '"'Subjects.i'" SUBJECT UNREAD NOGUI NOREQ'
		'include' SpotArgs
	END

	'getnumselected'
	SelectedCount = result
	IF SelectedCount > 0 THEN							/* Did we find msgs?  */
		DO
			BailOut = 0
			IF Quiet = 0 THEN
				DO
					requestdata = 'TITLE "Last chance..." PROMPT "Okay to' action SelectedCount 'messages?"'
					'requestresponse' requestdata
					IF rc ~= 0 THEN BailOut = 1
				END

			IF ~BailOut THEN
				IF DeleteMessages THEN
					'deletemessage NOREQ'
				ELSE
					'clearflags UNREAD'
		END
	'unreadarea'
	'getareaname'
END
'arealist'
'unlockgui'
EXIT

ReadCfg: PROCEDURE EXPOSE Writers. Receivers. Subjects.
ARG CfgFileName
SubjectCounter  = 0
WriterCounter   = 0
ReceiverCounter = 0

IF open('cfghandle',CfgFileName,'Read') ~= 1 THEN
	DO
		requestdata = 'PROMPT "Could not open' CfgFileName 'for reading.."'
		'requestnotify' requestdata
		SIGNAL exit
	END

inline = READLN('cfghandle')

DO WHILE ~EOF('cfghandle')
	PARSE VAR inline keyword argument
	argument = STRIP(argument,'B')
	SELECT
		WHEN keyword = 'FROM' THEN								/* Twit from */
			DO
				WriterCounter = WriterCounter + 1
				Writers.0 = WriterCounter
				Writers.WriterCounter = argument
			END
		WHEN keyword = 'TO' THEN								/* Twit to   */
			DO
				ReceiverCounter = ReceiverCounter + 1
				Receivers.0 = ReceiverCounter
				Receivers.ReceiverCounter = argument
			END
		WHEN keyword = 'SUBJECT' THEN							/* Twit subj */
			DO
				SubjectCounter = SubjectCounter + 1
				Subjects.0 = SubjectCounter
				Subjects.SubjectCounter = argument
				
			END
		WHEN keyword = '' | keyword = ';' THEN NOP
		OTHERWISE
			DO
				requestdata = 'PROMPT "Unknown keyword' keyword 'in' CfgFileName'"'
				'requestnotify' requestdata
				SIGNAL exit
			END
	END
	inline = READLN('cfghandle')
END
CALL CLOSE('cfghandle')
RETURN

syntax:
say rc errortext(rc) 'in line' SIGL
exit:
'unlockgui'
EXIT
