- add service file\n- add config file system

This commit is contained in:
Mewrry the Kitty 2026-02-13 09:56:09 -06:00
parent 74542329f2
commit 7988bb936e
3 changed files with 159 additions and 32 deletions

104
mewny.rb
View file

@ -18,6 +18,14 @@ VERSION = {
:FIL_TXT => '0.01', :FIL_TXT => '0.01',
:FIL_CUP => '0.02', :FIL_CUP => '0.02',
:FIL_CO8 => '0.30', :FIL_CO8 => '0.30',
:CFL_PORT => 'port',
:CFL_SV_F => 'save-file',
:CFL_SV_T => 'save-interval',
:CFL_ROWS => 'rows',
:CFL_COLS => 'cols',
:CFL_DUMB => 'call-me-stupid',
:CFL_PRIV => 'private',
} }
options = { options = {
@ -29,8 +37,47 @@ options = {
:cols => 40, :cols => 40,
:file => nil, :file => nil,
:file_lockout => false, :file_lockout => false,
:file_interval => 600, :file_interval => 600.0,
} }
def parse_svfile f, options
return options if options[:file_lockout]
options[:file_lockout] = true
file_stuff = {
:name => f,
:text => '',
:color => [],
:cursor => 0,
:writeme => false
}
unless File.exist? f
file_stuff[:writeme] = true
options[:file] = file_stuff
options[:file_lockout] = false
return options
end
File.readlines(f, chomp: true).each do |msg|
ver, msg = msg.chomp.split('|',2)
next if msg.nil?
case ver
when VERSION[:FIL_TXT]
file_stuff[:text] = msg
when VERSION[:FIL_CO8]
msg.each_char.with_index do |c, i|
next unless c =~ /[0-7]/
file_stuff[:color] << {
:co8 => 7
} until file_stuff[:color].length > i
file_stuff[:color][i][:co8] = c.to_i.clamp(0,7)
end
when VERSION[:FIL_CUP]
file_stuff[:cursor] = msg.to_i
end
end
options[:file] = file_stuff
options
end
OptionParser.new do |opt| OptionParser.new do |opt|
opt.on('-pPORT', '--port PORT', Integer, opt.on('-pPORT', '--port PORT', Integer,
'Port to serve/listen on (defaults to 7570)') do |o| 'Port to serve/listen on (defaults to 7570)') do |o|
@ -70,6 +117,7 @@ OptionParser.new do |opt|
'(server only) Serve publicly, rather than privately. Please ONLY add this option if you entierly know what you\'re doing and have permission from/are the server owner. Say please to expose.' '(server only) Serve publicly, rather than privately. Please ONLY add this option if you entierly know what you\'re doing and have permission from/are the server owner. Say please to expose.'
) do |o| ) do |o|
if o == "please" if o == "please"
puts 'I_AM_STUPID enabled'
options[:server_host] = PUBLIC_HOST options[:server_host] = PUBLIC_HOST
end end
end end
@ -82,40 +130,34 @@ OptionParser.new do |opt|
opt.on('-f FILE', '--save-file FILE', opt.on('-f FILE', '--save-file FILE',
'(server only) File to read initial state from and save state to.' '(server only) File to read initial state from and save state to.'
) do |f| ) do |f|
break if options[:file_lockout] options = parse_svfile(f, options)
options[:file_lockout] = true
file_stuff = {
:name => f,
:text => '',
:color => [],
:cursor => 0,
:writeme => false
}
unless File.exist? f
file_stuff[:writeme] = true
options[:file] = file_stuff
options[:file_lockout] = false
next
end end
opt.on('-C FILE', '--config-file FILE',
'(server only) File to set all other config options from.'
) do |f|
File.readlines(f, chomp: true).each do |msg| File.readlines(f, chomp: true).each do |msg|
next if msg.start_with? '#'
ver, msg = msg.chomp.split('|',2) ver, msg = msg.chomp.split('|',2)
next unless msg != nil next if msg.nil?
case ver case ver
when VERSION[:FIL_TXT] when VERSION[:CFL_PORT]
file_stuff[:text] = msg options[:port] = msg.to_i
when VERSION[:FIL_CO8] when VERSION[:CFL_SV_F]
msg.each_char.with_index do |c, i| options = parse_svfile(msg, options)
next unless c =~ /[0-7]/ when VERSION[:CFL_SV_T]
file_stuff[:color] << { options[:file_interval] = msg.to_f
:co8 => 7 when VERSION[:CFL_ROWS]
} until file_stuff[:color].length > i options[:rows] = msg.to_i
file_stuff[:color][i][:co8] = c.to_i.clamp(0,7) when VERSION[:CFL_COLS]
end options[:cols] = msg.to_i
when VERSION[:FIL_CUP] when VERSION[:CFL_DUMB]
file_stuff[:cursor] = msg.to_i next unless msg == 'please'
puts 'I_AM_STUPID enabled'
options[:server_host] = PUBLIC_HOST
when VERSION[:CFL_PRIV]
options[:server_host] = PRIVATE_HOST
end end
end end
options[:file] = file_stuff
end end
opt.on('-h', '--help', 'Get help with this command') do opt.on('-h', '--help', 'Get help with this command') do
puts opt puts opt
@ -306,7 +348,7 @@ def do_server(file_opts)
next next
end end
ver, msg = msg.chomp.split('|',2) ver, msg = msg.chomp.split('|',2)
next unless msg != nil next if msg.nil?
case ver case ver
when VERSION[:CLN_TXT] when VERSION[:CLN_TXT]
newtext << clean_chars(msg) newtext << clean_chars(msg)
@ -441,7 +483,7 @@ def do_client
msg = socket.gets msg = socket.gets
exit 0 unless msg exit 0 unless msg
ver, msg = msg.split('|',2) ver, msg = msg.split('|',2)
next unless msg != nil next if msg.nil?
case ver case ver
when VERSION[:SRV_TXT] when VERSION[:SRV_TXT]
row,col,chars = msg.split('|',3) row,col,chars = msg.split('|',3)

66
various/mewny.conf Normal file
View file

@ -0,0 +1,66 @@
# This is an example config file for
# a mewny server. I warn against
# putting spaces at the end of lines
# as that may cause confusion to you
# or the program. Note that despite
# being formatted mostly like the
# mewny protocol, this is unlike
# that protocol in that
# 1. This file is NOT necessarily
# forwards compatible with future
# versions of this software.
# 2. This file uses octothorpes (#) to
# denote comments.
# 3. This file uses human-readable
# 'version' strings before the
# initial bar.
# I hope you find this configuration
# file useful :3
# The paragraph below will describe
# how this document is formatted.
# This is a comment describing an option
# The option is placed below with a
# sensable or its default value like
# so...
#option|sensible-or-default-value
# Set the port
#port|7570
# Number of rows in the grid
#rows|24
# Number of columns in the grid
#cols|40
# Expose this instance to the world
# Only do this if you are very stupid
# or entirely know what you are doing.
# Set the value past the bar to 'please'
# (without quotes) to make this work
# as described.
# The command line option for this
# is --expose-this-instance-for-the
# -world-to-see-because-I-know-what
# -I'm-doing and accepts an argument
# which preferably says please.
#call-me-stupid|if-you-*Have*-to
# Do not expose this instance to the
# world (Opposite of call-me-stupid)
# Any value past the bar will enable
# this, and it is enabled by default
# unless the stupid 'call-me-stupid'
# option is enabled.
#private|makes-the-dev-happy
# Save file to write the grid to
# This has no default value but is
# confgiured to match the service
# file below:
save-file|/var/lib/mewny/mewny_savefile
# Save file autosave interval in seconds
# 600 seconds is 10m btw
#save-interval|600

19
various/mewny.service Normal file
View file

@ -0,0 +1,19 @@
[Unit]
Description=Mewny Server
After=network.target
Documentation=https://git.linuxposting.xyz/mewrrythekibby/mewny
[Service]
Type=simple
ExecStart=mewny --server --config-file /etc/mewny/mewny.conf
User=mewny
Group=mewny
WorkingDirectory=/var/lib/mewny
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target