/* StreamRA.rx by Sigbjørn (CISC) Skjæret ** $VER: StreamRA.rx v2.3.1 (19.12.01) ** ** Tries to stream a RealAudio file via a file containing URL(s) (like a .ram file)... */ options results options failat 21 signal on halt signal on break_c parse arg args /*** Edit these options ***/ ahiunit = '0' /* Which AHI-unit to play audio on */ raprog = 'C:RAPlay' /* Which decoder to run */ rxprog = 'SYS:RexxC/RX' /* The ARexx interpreter */ bfsize = '8192' /* Initial playback buffer (will revert to "pbsize" variable after first read) */ pbsize = '1024' /* Playback buffer (keep this low unless you have a very fast connection to the stream) */ savepath = '' /* Leave this empty unless you wish to save file in this dir while streaming */ comment = '1' /* Save URL as comment to the file saved? (1 for yes, 0 for no) */ reqtools = '1' /* Display ReqTools requester? (1 for yes, 0 for no) */ pubscreen = 'Workbench' /* The public screen you want the requester on */ /*** <-end-> ***/ if ~show('L','rxsocket.library') then do if ~addlib('rxsocket.library',0,-30,0) then do say 'ARexx socket library not available, exiting' exit 10 end end if(reqtools) then if ~show('L','rexxreqtools.library') then if ~addlib('rexxreqtools.library',0,-30,0) then reqtools=0 if(~exists(raprog) | ~exists(rxprog)) then do if ~exists(rxprog) then notfound=rxprog else notfound=raprog say 'Sorry, "'notfound'" does not exist, please edit path in script.' exit 10 end if(upper(word(args,1))='URL') then do url=word(args,2) ratmp=word(args,3) call Stream() call close(pipe) call CloseSocket(sock) if(save) then do call close(file) if(comment) then address command 'FileNote "'savepath''filename'" http://'url end exit end else if(upper(word(args,1))='BREAK') then do pidtmp='pid'time('S') address command 'Status >T:'pidtmp call open(pid,'T:'pidtmp,R) do while ~eof(pid) line=readln(pid) if(pos(raprog,line)>0) then do id=strip(word(line,2),'T',':');cmd=subword(line,6) address command 'Break >NIL: 'id say 'Sent Ctrl-C to process 'id': 'cmd end end call close(pid) address command 'Delete >NIL: T:'pidtmp' QUIET' say 'Done.' exit end args=strip(args,'B','"') parse source . . . pname;pname=subword(pname,1,words(pname)-2);found=0 if(pos(' ',pname)>0) then do say 'Sorry, due to restrictions in RX, you''ll have to place "'pname'" somewhere where the path does not resolve with a space in it.' exit 10 end if(args='') then do if(reqtools) then url=rtgetstring(,'Enter URL:','Enter URL') else do;say 'Enter URL:';parse pull url;end if(url='') then do say 'No URL entered .. exiting!' exit end if(pos('://',url)=0) then url='http://'url call Play() end else if ~open(file,args,R) then do say 'Unable to open file containing URL!!!' exit 10 end else do x=1 while ~eof(file) url=readln(file);url=strip(url,'T',d2c(13)) if(url='') then iterate x if(~abbrev(url,'http://',7) & ~abbrev(url,'pnm://',6)) then iterate x call Play() end if ~found then do say 'No supported URLs found!!!' exit 10 end exit /*** PROCEDURES ***/ Play: if(upper(right(url,4))='.RAM' | upper(right(url,4))='.RPM') then do if(pos('http://',url)=1) then url=substr(url,8);if(pos('pnm://',url)=1) then url=substr(url,7);port='80';crlf=d2c(13)''d2c(10);lookup=1 if(pos('/',url)>0) then do;host=substr(url,1,pos('/',url)-1);path=substr(url,pos('/',url));end else do;host=url;path='/';end if(pos(':',host)>0) then do;port=substr(host,pos(':',host)+1);host=substr(host,1,pos(':',host)-1);end if ~GetHost('HOST',host) then;if(datatype(compress(host,'.'),'N')) then do;host.HOSTADDRLIST.0=host;lookup=0;end;else do;say 'Unable to lookup host!!!';iterate x;end sock=Socket('INET','STREAM','IP');if sock<0 then do;say 'Unable to create socket!!!';iterate x;end conn.ADDRFAMILY='INET';conn.ADDRPORT=port;conn.ADDRADDR=host.HOSTADDRLIST.0 if Connect(sock,'CONN')<0 then do;if(lookup) then say 'Sorry, server is down, unable to stream data!';else say 'Host does not exist!!!';iterate x;end req='GET 'path' HTTP/1.0'crlf'Host: 'host''crlf''crlf call Send(sock,req) len=1 do y=1 while len>0 len=recvline(sock,'BUF',1024) if(buf=crlf) then do z=1 while len>0 len=recvline(sock,'BUF',1024) url=strip(strip(strip(buf,'T',d2c(10)),'T',d2c(13)),'B') if(url='') then iterate z if(~abbrev(url,'http://',7) & ~abbrev(url,'pnm://',6)) then iterate z leave y end end call CloseSocket(sock) end if(url='') then return 0 ratmp='ra'time('S');setclip(ratmp);found=1 address command 'Run >NIL: 'rxprog' 'pname' URL 'url' 'ratmp do i=1 to 15 address command 'Wait 5' clip=getclip(ratmp) if(clip='PLAY') then leave i if(clip~='') then do say 'Failed: 'clip setclip(ratmp) return 0 end if(i=15) then do say 'Operation timed out...' setclip(ratmp,'DIE') /* Just in case */ return 0 end end pragma('STACK',65536) /* address command ''raprog' AUDIO:B/16/F/8000/T/SIGNED/BUFFER/32768' */ address command ''raprog' PIPE:'ratmp' UNIT='ahiunit if(rc=20) then setclip(ratmp,'DIE') return 0 Stream: filename='';size='' if(pos('http://',url)=1) then url=substr(url,8);if(pos('pnm://',url)=1) then url=substr(url,7);port='80';save=0 host=substr(url,1,pos('/',url)-1);path=substr(url,pos('/',url)) if(pos(':',host)>0) then do;port=substr(host,pos(':',host)+1);host=substr(host,1,pos(':',host)-1);end if(savepath~='') then do if(right(savepath,1)~=':' & right(savepath,1)~='/') then savepath=savepath'/' filename=substr(path,lastpos('/',path)+1);save=1 end crlf=d2c(13)''d2c(10);lookup=1 if ~GetHost('HOST',host) then;if(datatype(compress(host,'.'),'N')) then do;host.HOSTADDRLIST.0=host;lookup=0;end;else do;setclip(ratmp,'Unable to lookup host!!!');return 10;end sock=Socket('INET','STREAM','IP');if sock<0 then do;setclip(ratmp,'Unable to create socket!!!');return 10;end conn.ADDRFAMILY='INET';conn.ADDRPORT=port;conn.ADDRADDR=host.HOSTADDRLIST.0 if Connect(sock,'CONN')<0 then do;if(lookup) then setclip(ratmp,'Sorry, server is down, unable to stream data!');else setclip(ratmp,'Host does not exist!!!');return 10;end req='GET 'path' HTTP/1.0'crlf'Host: 'host''crlf''crlf call Send(sock,req) call open(pipe,'PIPE:'ratmp,W) if(save) then call open(file,savepath''filename,W) len=1;start=0 do y=1 while len>0 if(start) then do len=recv(sock,'BUF',bfsize);bfsize=pbsize if(len>0) then do call writech(pipe,buf) if(save) then call writech(file,buf) end if(getclip(ratmp)='DIE') then do;setclip(ratmp);return 0;end end else do len=recvline(sock,'BUF',1024) if(y=1 & word(buf,2)~='200') then do;setclip(ratmp,strip(subword(buf,3),'T',crlf));return 10;end if(word(buf,1)='Content-Length:') then size=strip(word(buf,2),'T',crlf) if(buf=crlf) then do;start=1;setclip(ratmp,'PLAY');call OpenReq();end end end return 0 OpenReq: if(reqtools) then do if(size='') then size='Unknown' else size=size' bytes' if(save=0) then svfl='No' else svfl=savepath''filename if(pubscreen='') then reqtags="'rtez_flags=ezreqf_noreturnkey'" else reqtags="'rt_pubscrname="pubscreen" rtez_flags=ezreqf_noreturnkey'" req="'Host: "host"'d2c(10)'File: "path"'d2c(10)'Save: "svfl"'d2c(10)'Size: "size"','_Close|_Break','StreamRA...',"reqtags address command 'Run >NIL: 'rxprog' "call rtezrequest('req');if(rtresult=0) then setclip('''ratmp''',''DIE'')"' end return 0 Halt: Break_C: setclip(ratmp,'DIE') exit