- 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

106
mewny.rb
View file

@ -18,6 +18,14 @@ VERSION = {
:FIL_TXT => '0.01',
:FIL_CUP => '0.02',
: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 = {
@ -29,8 +37,47 @@ options = {
:cols => 40,
:file => nil,
: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|
opt.on('-pPORT', '--port PORT', Integer,
'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.'
) do |o|
if o == "please"
puts 'I_AM_STUPID enabled'
options[:server_host] = PUBLIC_HOST
end
end
@ -82,40 +130,34 @@ OptionParser.new do |opt|
opt.on('-f FILE', '--save-file FILE',
'(server only) File to read initial state from and save state to.'
) do |f|
break 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
next
end
options = parse_svfile(f, options)
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|
next if msg.start_with? '#'
ver, msg = msg.chomp.split('|',2)
next unless msg != nil
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
when VERSION[:CFL_PORT]
options[:port] = msg.to_i
when VERSION[:CFL_SV_F]
options = parse_svfile(msg, options)
when VERSION[:CFL_SV_T]
options[:file_interval] = msg.to_f
when VERSION[:CFL_ROWS]
options[:rows] = msg.to_i
when VERSION[:CFL_COLS]
options[:cols] = msg.to_i
when VERSION[:CFL_DUMB]
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
options[:file] = file_stuff
end
opt.on('-h', '--help', 'Get help with this command') do
puts opt
@ -306,7 +348,7 @@ def do_server(file_opts)
next
end
ver, msg = msg.chomp.split('|',2)
next unless msg != nil
next if msg.nil?
case ver
when VERSION[:CLN_TXT]
newtext << clean_chars(msg)
@ -441,7 +483,7 @@ def do_client
msg = socket.gets
exit 0 unless msg
ver, msg = msg.split('|',2)
next unless msg != nil
next if msg.nil?
case ver
when VERSION[:SRV_TXT]
row,col,chars = msg.split('|',3)