much to late initial commit

Signed-off-by: Sarmonsiill <sarmonsiill@tilde.guru>
This commit is contained in:
Sarmonsiill 2023-03-19 15:41:34 +01:00
commit f1035a7457
7 changed files with 151 additions and 0 deletions

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright 2023 Sarmonsiill [sarmonsiill@tilde.guru]
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2
config/server.pgons Normal file
View File

@ -0,0 +1,2 @@
host:0.0.0.0
port:1978

8
resources/banner.txt Normal file
View File

@ -0,0 +1,8 @@
### ###
## ##
###### ### ## #### ##### ##### ## ## #####
## ## ## ## ## ## ## ## ## ##### ##### ##
## ## ## ## ## ## ## ## ##### ## ## ## ## #####
##### ##### ## ## ## ## ## ## ## ## ## ##
## ## #### ## ## ###### ###### ###### ######
#### #####

1
run_dev Executable file
View File

@ -0,0 +1 @@
clear && PGONS_DEBUG=true v run ./src/.

63
src/config/config.v Normal file
View File

@ -0,0 +1,63 @@
module config
import os
import utils
pub struct Config {
pub:
host string
port int
}
pub fn get_conf() Config {
return populate_config()
}
fn populate_config() Config {
home := os.getenv('HOME')
possible_paths := [
'./server.pgons',
'./config/server.pgons',
'../server.pgons',
'../config/server.pgons'
'${home}/.server.pgons',
'${home}/server.pgons',
'${home}/.config/server.pgons'
]
for ppath in possible_paths {
if os.exists(ppath) {
return read_config_file(ppath)
}
}
utils.dbg('could not find a config file in either possible places: ${possible_paths}')
return Config{
host: utils.genv_string('PGONS_HOST', '0.0.0.0')
port: utils.genv_string('PGONS_PORT', '1978').int()
}
}
fn read_config_file(file string) Config {
utils.dbg('found config file: ${file}')
lines := os.read_lines(file) or {
panic(err)
}
/* FIXME: config fields allowed. should be solved in a nicer way i think */
mut t_host := utils.genv_string('PGONS_HOST', '0.0.0.0')
mut t_port := utils.genv_string('PGONS_PORT', '1978').int()
utils.dbg('parsing ${lines} into our config structure')
for line in lines {
sp := line.split(':')
match true {
sp[0] == 'host' { t_host = sp[1] }
sp[0] == 'port' { t_port = sp[1].int() }
else { utils.dbg('${sp[0]} is not a known config key') }
}
}
return Config{
host: t_host
port: t_port
}
}

52
src/pgons_bbs.v Normal file
View File

@ -0,0 +1,52 @@
module main
import io
import net
import utils
import config
const pgons_host = utils.genv_string('PGONS_HOST', '0.0.0.0')
const pgons_port = utils.genv_string('PGONS_PORT', '1978').int()
const pgons_config = config.get_conf()
fn main() {
mut server := net.listen_tcp(.ip, '${pgons_host}:${pgons_port}') or {
panic(err)
}
addr := server.addr() or {
panic(err)
}
println('listening on ${addr}')
for {
mut socket := server.accept() or {
panic('socket failed: ${err}')
}
spawn handle_client(mut socket)
}
}
fn handle_client(mut socket net.TcpConn) {
defer {
socket.close() or { panic(err) }
}
client_addr := socket.peer_addr() or { return }
eprintln('> new client: ${client_addr}')
mut reader := io.new_buffered_reader(reader: socket)
defer {
unsafe {
reader.free()
}
}
socket.write_string('server: hello\n') or { return }
for {
received_line := reader.read_line() or { return }
if received_line == '' {
return
}
println('client ${client_addr}: ${received_line}')
socket.write_string('server: ${received_line}\n') or { return }
}
}

18
src/utils/utils.v Normal file
View File

@ -0,0 +1,18 @@
module utils
import os
import term
const debug = genv_string('PGONS_DEBUG', 'false').bool()
pub fn genv_string(key string, def string) string {
val := os.getenv(key)
if val == '' { return def }
return val
}
pub fn dbg(input string) {
if debug {
println('${term.bright_blue('DEBUG')} >> ${term.magenta(input)}')
}
}