2019-03-20 20:31:40 +01:00
|
|
|
#!/usr/bin/env retro
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
# Atua: A Gopher Server
|
|
|
|
|
|
|
|
Atua is a gopher server written in Retro.
|
|
|
|
|
|
|
|
This will get run as an inetd service, which keeps things simple as it
|
|
|
|
prevents needing to handle socket I/O directly.
|
|
|
|
|
|
|
|
# Features
|
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
Atua is a minimal server targetting the the basic Gopher0 protocol. It
|
2017-10-17 03:32:30 +02:00
|
|
|
supports a minimal gophermap format for indexes and transfer of static
|
|
|
|
content.
|
|
|
|
|
|
|
|
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|
|
|
|
# Script Prelude
|
|
|
|
|
|
|
|
Atua uses Retro's *rre* interface layer. Designed to run a single
|
|
|
|
program then exit, this makes using Retro as a scripting language
|
|
|
|
possible.
|
|
|
|
|
|
|
|
# Configuration
|
|
|
|
|
|
|
|
Atua needs to know:
|
|
|
|
|
|
|
|
- the path to the files to serve
|
|
|
|
- the name of the index file
|
|
|
|
- The maximum length of a selector
|
|
|
|
- The maximum file size
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2019-01-03 22:54:21 +01:00
|
|
|
'/home/crc/atua 'PATH s:const
|
|
|
|
'/gophermap 'DEFAULT-INDEX s:const
|
|
|
|
'forthworks.com 'SERVER s:const
|
|
|
|
'70 'PORT s:const
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
# I/O Words
|
|
|
|
|
|
|
|
Retro only supports basic output by default. The RRE interface that
|
|
|
|
Atua uses adds support for files and stdin, so we use these and
|
|
|
|
provide some other helpers.
|
|
|
|
|
|
|
|
*Console Output*
|
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
The Gopher protocol uses tabs and cr/lf for signficant things. To aid
|
2017-10-17 03:32:30 +02:00
|
|
|
in this, I define output words for tabs and end of line.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2018-05-07 18:24:36 +02:00
|
|
|
:eol (-) ASCII:CR c:put ASCII:LF c:put ;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
*Console Input*
|
|
|
|
|
|
|
|
Input lines end with a cr, lf, or tab. The `eol?` checks for this.
|
|
|
|
The `gets` word could easily be made more generic in terms of what
|
|
|
|
it checks for. This suffices for a Gopher server though.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:eol? (c-f)
|
|
|
|
[ ASCII:CR eq? ] [ ASCII:LF eq? ] [ ASCII:HT eq? ] tri or or ;
|
2018-05-07 18:24:36 +02:00
|
|
|
:s:get (a-)
|
2017-10-17 03:32:30 +02:00
|
|
|
buffer:set
|
2019-01-02 17:38:33 +01:00
|
|
|
#0 [ n:inc c:get dup buffer:add eol? not over #255 lt? and ] while drop ;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
# Gopher Namespace
|
|
|
|
|
|
|
|
Atua uses a `gopher:` namespace to group the server related words.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
{{
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
First up are buffers for the selector string and the file buffer. The
|
|
|
|
variables and buffers are kept private.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Selector d:create
|
2019-01-03 22:54:21 +01:00
|
|
|
#255 n:inc allot
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
:buffer here ;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
Next up, variables to track information related to the requested
|
|
|
|
selector. Atua will construct filenames based on these.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Requested-File var
|
|
|
|
'Requested-Index var
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
`FID`, the file id, tracks the open file handle that Atua uses
|
|
|
|
when reading in a file. The `Size` variable will hold the size of
|
|
|
|
the file (in bytes).
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'FID var
|
|
|
|
'Size var
|
|
|
|
'Mode var
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
I use a `Server-Info` variable to decide whether or not to display
|
|
|
|
the index footer. This will become a configurable option in the
|
|
|
|
future.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Server-Info var
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:with-path (-s)
|
|
|
|
PATH &Selector s:chop s:append ;
|
|
|
|
|
|
|
|
:construct-filenames (-)
|
|
|
|
with-path s:keep !Requested-File
|
|
|
|
with-path '/gophermap s:append s:keep !Requested-Index
|
|
|
|
;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
A *gophermap* is a file that makes it easier to handle Gopher menus.
|
|
|
|
Atua's gophermap support covers:
|
|
|
|
|
|
|
|
- comment lines
|
|
|
|
|
|
|
|
Comment lines are static text without any tabs. They will be
|
|
|
|
reformatted according to protocol and sent.
|
|
|
|
|
|
|
|
- selector lines
|
|
|
|
|
|
|
|
Any line with a tab is treated as a selector line and is transferred
|
|
|
|
without changing.
|
|
|
|
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Tab var
|
|
|
|
:eol? [ ASCII:LF eq? ] [ ASCII:CR eq? ] bi or ;
|
|
|
|
:tab? @Tab ;
|
|
|
|
:check-tab
|
|
|
|
dup ASCII:HT eq? [ &Tab v:on ] if ;
|
|
|
|
:gopher:gets (a-)
|
|
|
|
&Tab v:off
|
|
|
|
buffer:set
|
|
|
|
[ @FID file:read dup buffer:add check-tab eol? not ] while
|
|
|
|
buffer:get drop
|
|
|
|
;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
The internal helpers are now defined, so switch to the part of the
|
|
|
|
namespace that'll be left exposed to the world.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
---reveal---
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
An information line s:get a format like:
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
i...text...<tab><tab>null.host<tab>port<cr,lf>
|
|
|
|
|
|
|
|
The `gopher:i` displays a string in this format. It's used later for
|
|
|
|
the index footer.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:i (s-)
|
2018-05-07 18:24:36 +02:00
|
|
|
'i%s\t\tnull.host\t1 s:format s:put eol ;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:get-selector (-)
|
2018-05-07 18:24:36 +02:00
|
|
|
&Selector s:get ;
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
:gopher:file-requested? (-f)
|
|
|
|
&Selector s:chop s:length n:-zero? ;
|
|
|
|
|
|
|
|
:gopher:valid-file? (-f)
|
|
|
|
[ @Requested-File file:R file:open
|
|
|
|
file:size n:strictly-positive? ]
|
|
|
|
[ FALSE ] choose ;
|
|
|
|
|
|
|
|
:gopher:get-index (-s)
|
|
|
|
@Requested-Index file:exists?
|
|
|
|
[ @Requested-Index &Server-Info v:on ]
|
|
|
|
[ PATH '/empty.index s:append ] choose ;
|
|
|
|
|
|
|
|
:gopher:file-for-request (-s)
|
|
|
|
&Mode v:off
|
|
|
|
construct-filenames
|
|
|
|
gopher:file-requested?
|
|
|
|
[ @Requested-File file:exists? gopher:valid-file?
|
|
|
|
[ @Requested-File ]
|
|
|
|
[ gopher:get-index ] choose
|
|
|
|
]
|
|
|
|
[ PATH DEFAULT-INDEX s:append &Server-Info v:on ] choose
|
|
|
|
;
|
|
|
|
|
|
|
|
:gopher:read-file (f-s)
|
|
|
|
file:R file:open !FID
|
|
|
|
@FID file:size !Size
|
2018-12-19 02:05:13 +01:00
|
|
|
@Size [ @FID file:read c:put ] times
|
2017-10-17 03:32:30 +02:00
|
|
|
@FID file:close
|
|
|
|
;
|
|
|
|
|
|
|
|
:gopher:line (s-)
|
|
|
|
dup [ ASCII:HT eq? ] s:filter s:length #1 gt?
|
2018-05-07 18:24:36 +02:00
|
|
|
[ s:put ]
|
|
|
|
[ s:put tab SERVER s:put tab PORT s:put ] choose
|
2017-10-17 03:32:30 +02:00
|
|
|
eol ;
|
|
|
|
|
|
|
|
:gopher:generate-index (f-)
|
|
|
|
file:R file:open !FID
|
|
|
|
@FID file:size !Size
|
|
|
|
[ buffer gopher:gets
|
|
|
|
buffer tab? [ gopher:line ] [ gopher:i ] choose
|
|
|
|
@FID file:tell @Size lt? ] while
|
|
|
|
@FID file:close
|
|
|
|
;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
The only thing left is the top level server.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:server
|
|
|
|
gopher:get-selector
|
|
|
|
gopher:file-for-request
|
|
|
|
@Server-Info
|
|
|
|
[ gopher:generate-index
|
|
|
|
'------------------------------------------- gopher:i
|
|
|
|
'forthworks.com:70_/_atua_/_running_on_retro gopher:i
|
2018-05-07 18:24:36 +02:00
|
|
|
'. s:put eol ]
|
2018-12-19 02:05:13 +01:00
|
|
|
[ gopher:read-file ] choose
|
2017-10-17 03:32:30 +02:00
|
|
|
;
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
Close off the helper portion of the namespace.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
}}
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
And run the `gopher:server`.
|
|
|
|
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
gopher:server
|
|
|
|
reset
|
2018-04-25 18:51:46 +02:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|