/* UUEncode.thor by Troels Walsted Hansen
** $VER: UUEncode.thor 2.1 (6.5.96)
**
** An ARexx script that uuencodes a file and either places it in the
** clipboard or posts a message for you containing the file. Using
** LhA, this script will archive the file if it isn't already.
**
** Utilises LhA by Stefan Boberg for archiving.
** The script may use either of the following for uuencoding:
**	· UUFast v1.25 by Jørn Halonen
**	· uuIn v1.03 by Nicolas Dade
**	· UUxT v3.0 by Asher Feldman
**
** New: · Added userdatabase lookup code from ForwardMsg.thor
**        "Post" now works on fido/tcp/uucp/etc. systemtypes.
*/

/* some user variables. edit to your hearts content */

tmpdir = "T:"		/* Work dir for the encoding    */
uuencoder = "uuin"	/* may be: uuin, uufast or uuxt */

/* needs THOR and bbsread.library functions */

options results
options failat 31

p = ' ' || address() || ' ' || show('P',,)
thorport = pos(' THOR.',p)

if thorport > 0 then thorport = word(substr(p,thorport+1),1)
else
do
	say 'No THOR port found!'
	exit 10
end

if ~show('p', 'BBSREAD') then
do
	address command
		"run >nil: `GetEnv THOR/THORPath`bin/LoadBBSRead"
		"WaitForPort BBSREAD"
end

/* get filename */

address(bbsread)
GETGLOBALDATA STEM GLOBALDATA

address(thorport)
THORTOFRONT
REQUESTFILE TITLE '"Select file to encode:"' ID '"'GLOBALDATA.UPLOADPATH'"' FP PAT '"#?"'
if(rc ~= 0) then exit

filetoencode = result
lastchar = right(filetoencode,1)

if(lastchar = "/" | lastchar = ":" | filetoencode = "") then
do
	REQUESTNOTIFY TEXT '"No file selected!"' BT '"_Ok"'
	call CleanUp()
end

posi = lastpos("/",FileToEncode)
if(posi = 0) then posi = lastpos(":",FileToEncode)
WithoutPath = substr(FileToEncode,posi+1)

/* if file isn't LhA'ed -- do it ourselves */

extension = upper(substr(FileToEncode, lastpos(".",FileToEncode)+1))

if~(extension = "LHA" | extension = "LZH") then
do
	REQUESTNOTIFY TEXT '"Do you want to LhA the file?"' BT '"_Yes|_No"'
	if(result) then
	do
		address command "LhA >nil: -y -q a "||'"'tmpdir||WithoutPath'"'||" "||'"'FileToEncode'"'

		if(rc ~= 0) then REQUESTNOTIFY TEXT '"LhA''ing did not succeed."' BT '"_Ok"'
		else FileToEncode = tmpdir||WithoutPath||".lha"
	end
end

/* select correct uuencoder commandstring */

select
	when(uuencoder = 'uufast') then cmd = "UUFast >nil: " || '"' || filetoencode || '"' || " TO " || tmpdir || "Message.uu E"
	when(uuencoder = 'uuin') then cmd = "uuIn >nil: INFILE=" || '"' || filetoencode || '"' || " OUTFILE=" || tmpdir || "Message.uu"
	when(uuencoder = 'uuxt') then cmd = 'UUxT >nil: a ' || tmpdir || 'Message.uu ' || '"'filetoencode'"'
	otherwise
	do
		REQUESTNOTIFY TEXT '"UUEncoder not configured correctly."' BT '"_Ok"'
		call CleanUp()
	end
end
address command cmd

if ~exists(tmpdir"Message.uu") then
do
	REQUESTNOTIFY TEXT '"Something went wrong during uuencoding."' BT '"_Ok"'
	call CleanUp()
end

REQUESTNOTIFY TEXT '"Place in clipboard or post as a message?"' BT '"Clip_board|_Post|_Cancel"'
choice = result

if(choice = 1) then
do
	PUTCLIP unit 0 file '"'tmpdir'Message.uu"'
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
		exit 5
	end
end
else if(choice = 2) then
do
	drop EVENT.
	address(bbsread)

	GETBBSLIST stem BBSLIST
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		exit 5
	end

	address(thorport)

	REQUESTLIST instem BBSLIST title '"Select BBS:"' SIZEGADGET
	if(rc ~= 0) then break
	else bbs = result

	address(bbsread)
        GETCONFLIST '"'bbs'"' CONFLIST
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		exit 5
	end

	address(thorport)

	REQUESTLIST instem CONFLIST title '"Select conf:"' SIZEGADGET
	if(rc ~= 0) then break
	else EVENT.CONFERENCE = result

	REQUESTSTRING TITLE '"Please enter subject of message:"' BT '"_Ok|_Cancel"' ID '"'WithoutPath'"' MAXCHARS 100
	EVENT.SUBJECT = result
	if(rc ~= 0 | EVENT.SUBJECT = "") then break

	do forever
		REQUESTSTRING TITLE '"Please enter receiver name:"' BT '" _Ok | _Cancel "' MAXCHARS 200
		if(rc ~= 0) then exit
		forwardto = result


		if(upper(forwardto) ~= "ALL") then
		do
			address(bbsread)

			/*
				The message was not addressed to 'ALL',
				let's search for the user on the system
				the message is to be forwarded to.
				Both name, addresses and aliases will be searched
				for to find a match.
			*/

			SEARCHBRUSER bbsname '"'bbs'"' stem USERS search '"'forwardto'"' name address alias suggestusersstem SUG
			if(rc ~= 0) then goto quit

			if(result > 0) then
			do
				/* Ok, we found one or more users matching the data entered exactly */

				address(bbsread)

				drop LIST.
				drop USERTAGS.

				LIST.COUNT = USERS.COUNT

				/*
					We must build a list of user names suitable to
					be viewed with the REQUESTLIST command.
				*/

				do n = 1 to USERS.COUNT
					LIST.n.USERNR = USERS.n.USERNR
					READBRUSER bbsname '"'bbs'"' usernr USERS.n.USERNR tagsstem USERTAGS
					if(rc ~= 0) then goto quit
					LIST.n = USERTAGS.NAME

					if(symbol("USERTAGS.ADDRESS") = "VAR") then
						LIST.n.ADDRESS = USERTAGS.ADDRESS
				end

				/* Select a user */

				address(thorport)
				REQUESTLIST instem LIST title '"Select user:"'
				if(rc ~= 0) then
				do
					if(rc ~= 5) then REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
					exit
				end

				forwardto = result

				/* We might have an address (email address/Fido address etc.) */

				do n = 1 to LIST.COUNT
					if(LIST.n = forwardto) then
						forwardaddress = LIST.n.ADDRESS
				end
				leave
			end
			else
			do
				/*
					No users matching the given data exactly were found,
					let's prosess the suggestions.
				*/

				if(symbol("SUG.COUNT") = "VAR") then do
					address(thorport)
					REQUESTLIST instem SUG title '"Select user:"'
					if(rc ~= 0) then
					do
						if(rc ~= 5) then REQUESTNOTIFY TEXT '"'THOR.LASTERROR'"' BT '"_Ok"'
						exit
					end

					forwardto = result

					/* Let's find the user number of the user selected */

					do n = 1 to SUG.COUNT
						if(SUG.n = forwardto) then
							usernumber = SUG.n.USERNR
					end

					drop USERTAGS.

					address(bbsread)

					/* Get some data about the user selected */

					READBRUSER bbsname '"'bbs'"' usernr usernumber tagsstem USERTAGS
					if(rc ~= 0) then goto quit

					if(symbol("USERTAGS.ADDRESS") = "VAR") then
						forwardaddress = USERTAGS.ADDRESS

					leave
				end
				else
				do
					/* No users were found in the search */

					address(thorport)
					REQUESTNOTIFY TEXT '"No matching users found, try again?"' BT '"_Ok | _Cancel"'
					if(rc ~= 0) then exit
					if(result = 0) then exit
				end
			end
		end
		else
			leave
	end

	EVENT.TONAME = forwardto
	EVENT.TOADDR = forwardaddress

	address(bbsread)
	UNIQUEMSGFILE bbsname '"'bbs'"' stem UNIQUEFILE
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		exit
	end

	address command 'copy >nil: ' || tmpdir || 'Message.uu TO ' || UNIQUEFILE.NAME
	EVENT.MSGFILE = UNIQUEFILE.FILEPART

	WRITEBREVENT bbsname '"'bbs'"' event 0 stem EVENT
	if(rc ~= 0) then
	do
		address(thorport)
		REQUESTNOTIFY TEXT '"'BBSREAD.LASTERROR'"' BT '"_Ok"'
		exit 5
	end
	else
	do
		address(thorport)
		PACKEVENTS '"'bbs'"'
		RESCAN
	end
end

call CleanUp()

/* cleanup and exit */

CleanUp:
	if(exists(tmpdir || WithoutPath || ".lha")) then address command 'delete >nil: ' || '"' || tmpdir || WithoutPath || '.lha' || '"'
	if(exists(tmpdir || "Message.uu")) then address command 'delete >nil: ' || tmpdir || 'Message.uu'
	exit
return
