added quote-editting flag and functions
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
// =============================================================================
|
||||
// Auth: alex
|
||||
// File: server.go
|
||||
// Revn: 05-12-2026 2.1.2
|
||||
// Revn: 09-06-2026 3.0.0
|
||||
// Func: host MOTD connection
|
||||
//
|
||||
// TODO:
|
||||
// TODO: FUCK
|
||||
// add user-specific prints
|
||||
// write usage and errors to logfile
|
||||
// remove ioutil import
|
||||
@@ -51,6 +51,8 @@
|
||||
// line bug in /la/ AGAIN
|
||||
// 05-11-2026: updated fread() to handle users in qfile
|
||||
// 05-12-2026: added v and vv input flags
|
||||
// 09-05-2026: buncha shit happened, dropped the commit
|
||||
//#09-06-2026: added /e1/, /e2/, and /cr/, got edit working
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
@@ -121,14 +123,9 @@ func fread() string {
|
||||
// bugs where a blank item was introduced into qlist and calling
|
||||
// /la/ would produce a blank line on the client side that didn't
|
||||
// really exist
|
||||
qfile = qfile[:len( qfile ) - 1]
|
||||
qfile = qfile[:len( qfile )]
|
||||
// cast bytes to string, split string over newline into array
|
||||
//qlist = strings.Split( string( qfile ), "\n" )
|
||||
profiles := strings.Split( string( qfile ), "@" )
|
||||
for _, p := range profiles {
|
||||
quotes := strings.Split( p, ":" )
|
||||
qlist[quotes[0]] = strings.Split( quotes[1], "\n" )
|
||||
}
|
||||
qlist = strings.Split( string( qfile ), "\n" )
|
||||
|
||||
return string( qfile ) // return file as string
|
||||
}
|
||||
@@ -145,9 +142,9 @@ func writeFile( f string ) {
|
||||
}
|
||||
|
||||
|
||||
// handle connections ( new and improved )
|
||||
func handle( conn net.Conn ) {
|
||||
|
||||
// receive from connection, send to get processed, send out response
|
||||
func receive( conn net.Conn ) {
|
||||
|
||||
// create buffer to hold read message
|
||||
buffer := make( []byte, tsize )
|
||||
//var buffer [256]byte
|
||||
@@ -169,6 +166,24 @@ func handle( conn net.Conn ) {
|
||||
// cast message to string, take first n bytes, split over @
|
||||
command := strings.Split( string( buffer[:n] ), "@" )
|
||||
|
||||
if command[0] == "te" {
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
resp := handle( command )
|
||||
|
||||
// write response to client
|
||||
_, err = conn.Write( []byte( resp ) )
|
||||
checkN( err, conn ) // make sure write worked
|
||||
|
||||
receive( conn ) // call handle() again to get response from client
|
||||
}
|
||||
|
||||
|
||||
// handle connections ( new and improved )
|
||||
func handle( command []string ) string {
|
||||
|
||||
// declare response string
|
||||
var resp string
|
||||
switch command[0] { // switch/case over opcode
|
||||
@@ -179,20 +194,9 @@ func handle( conn net.Conn ) {
|
||||
// concat and send
|
||||
resp = "sa@" + num // size answer
|
||||
case "qr": // /qr/, quote request
|
||||
// see if user was sent
|
||||
subcommands := strings.Split( command[1], ":" )
|
||||
|
||||
// keep track of name, or default if need be
|
||||
var name string
|
||||
if len( subcommands ) == 1 {
|
||||
name = "default"
|
||||
} else {
|
||||
name = subcommands[1]
|
||||
}
|
||||
|
||||
// convert message argument to string to make sure it's a
|
||||
// valid number
|
||||
num, nerr := strconv.Atoi( subcommands[0] )
|
||||
num, nerr := strconv.Atoi( command[0] )
|
||||
// nerr will not be nil if comm[1] is not a number
|
||||
if nerr != nil {
|
||||
// formulate an error message
|
||||
@@ -200,7 +204,7 @@ func handle( conn net.Conn ) {
|
||||
resp = "ba@qr@!" // bad answer
|
||||
} else { // comm[1] was a number, but...
|
||||
switch { // how do we know it's a valid number
|
||||
case num > len( qlist[name] ):
|
||||
case num > len( qlist ):
|
||||
// bad answer in quote request
|
||||
resp = "ba@qr@>"
|
||||
// why bad? num is >
|
||||
@@ -210,7 +214,7 @@ func handle( conn net.Conn ) {
|
||||
// why bad? num is -
|
||||
default:
|
||||
// good num, get quote, send off
|
||||
resp = "qa@" + qlist[name][num] // quote answer
|
||||
resp = "qa@" + qlist[num - 1] // quote answer
|
||||
}
|
||||
}
|
||||
case "lr": // /lr/, list request
|
||||
@@ -252,11 +256,83 @@ func handle( conn net.Conn ) {
|
||||
}
|
||||
case "ar": // /ar/, add request
|
||||
// append new quote to list
|
||||
qlist["f"] = append( qlist["f"], command[1] )
|
||||
qlist = append( qlist, command[1] )
|
||||
// also append new quote to file variable
|
||||
qfile = qfile + "\n" + command[1]
|
||||
writeFile( qfile )
|
||||
resp = "aa@success" // add answer
|
||||
case "cr":
|
||||
if len( command ) != 5 {
|
||||
return "ba@bad request"
|
||||
}
|
||||
|
||||
//mes1 := strings.Join( command[1:3], "@" )
|
||||
//mes1 := strings.Join( command[3:], "@" )
|
||||
_ = handle( command[1:3] )
|
||||
_ = handle( command[3:] )
|
||||
|
||||
resp = "e2@success"
|
||||
case "e1":
|
||||
resp = "e1@" // begin crafting response
|
||||
// file too big for single transmission
|
||||
// initial header size is 5, e1@1@, so counting that into
|
||||
// the transmission size, the file must be longer than 251
|
||||
// ( default ) bytes to reach this block of code
|
||||
if len( qfile ) > ( tsize - 5 ) {
|
||||
if command[1] == "init" { // first request
|
||||
// add message number, delim, and the first
|
||||
// transmittable bytes
|
||||
resp = resp + "0@" + qfile[:( tsize - 5 )]
|
||||
} else { // subsequent requests
|
||||
// convert byte count to int
|
||||
bytes, err := strconv.Atoi( command[1] )
|
||||
check( err ) // make sure convert worked
|
||||
// replace tx'd bytes and delim to new header
|
||||
resp = resp + command[1] + "@"
|
||||
rlen := len( resp ) // capture header length
|
||||
// slices are always of size "txsize - header
|
||||
// length", so find end byte
|
||||
end := bytes + ( tsize - rlen )
|
||||
// if end goes beyond the bounds of the list
|
||||
if end > len( qfile ) {
|
||||
// replace byte count with X to signal
|
||||
// final message, concat the rest of the file,
|
||||
// beginning with the start index
|
||||
resp = "e1@X@" + qfile[bytes:]
|
||||
} else {
|
||||
// get slice from start index to end, concat
|
||||
// with response
|
||||
resp = resp + qfile[bytes:end]
|
||||
}
|
||||
}
|
||||
} else { // file is smaller than the tx size
|
||||
// just concat entire list to send
|
||||
resp = resp + qfile // list answer
|
||||
}
|
||||
case "e2":
|
||||
// convert message argument to string to make sure it's a
|
||||
// valid number
|
||||
num, nerr := strconv.Atoi( command[1] )
|
||||
// nerr will not be nil if comm[1] is not a number
|
||||
if nerr != nil {
|
||||
// formulate an error message
|
||||
// TODO flesh this out more
|
||||
resp = "ba@qr@!" // bad answer
|
||||
} else { // comm[1] was a number, but...
|
||||
switch { // how do we know it's a valid number
|
||||
case num > len( qlist ):
|
||||
// bad answer in quote request
|
||||
resp = "ba@qr@>"
|
||||
// why bad? num is >
|
||||
case num < 0:
|
||||
// bad answer in quote request
|
||||
resp = "ba@qr@-"
|
||||
// why bad? num is -
|
||||
default:
|
||||
// good num, get quote, send off
|
||||
resp = "e2@" + command[1] + "@" + qlist[num - 1]
|
||||
}
|
||||
}
|
||||
case "r1": // /r1/, remove request 1
|
||||
resp = "r1@" // begin crafting response
|
||||
// file too big for single transmission
|
||||
@@ -319,14 +395,14 @@ func handle( conn net.Conn ) {
|
||||
default:
|
||||
// replace the quote at that position with
|
||||
// final quote
|
||||
qlist["f"][num] = qlist["f"][len( qlist ) - 1]
|
||||
qlist[num] = qlist[len( qlist ) - 1]
|
||||
// remove second copy of quote
|
||||
qlist["f"] = qlist["f"][:len( qlist ) - 1]
|
||||
qlist = qlist[:len( qlist ) - 1]
|
||||
// O(1) remove from list at any position
|
||||
// must entirely recreate quote string
|
||||
var f string = "" // init empty string
|
||||
// iterate over list, disregard index
|
||||
for _, v := range qlist["f"] {
|
||||
for _, v := range qlist {
|
||||
// add next quote to file and separate
|
||||
// quotes with newline
|
||||
f = f + v + "\n"
|
||||
@@ -349,25 +425,21 @@ func handle( conn net.Conn ) {
|
||||
case "te": // /te/, transaction end
|
||||
// TODO should I be sending something?
|
||||
// TODO is it better to close now or outside of handle()
|
||||
conn.Close() // close connection and leave
|
||||
return // return from handle(), finish connection
|
||||
//conn.Close() // close connection and leave
|
||||
return "" // return from handle(), finish connection
|
||||
default: // opcode not recognized
|
||||
// TODO there should be more being down, here, right?
|
||||
return // return from handle(), finish connection
|
||||
return "" // return from handle(), finish connection
|
||||
}
|
||||
|
||||
// write response to client
|
||||
_, err = conn.Write( []byte( resp ) )
|
||||
checkN( err, conn ) // make sure write worked
|
||||
|
||||
handle( conn ) // call handle() again to get response from client
|
||||
return resp
|
||||
|
||||
}
|
||||
|
||||
|
||||
// globals
|
||||
//var qlist []string // list of quotes
|
||||
var qlist = make( map[string][]string )
|
||||
var qlist []string // list of quotes
|
||||
//var qlist = make( map[string][]string )
|
||||
var qfile string // file object
|
||||
var tsize int // size of transmission buffer
|
||||
var VERS string // version
|
||||
@@ -391,7 +463,7 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
// FIXME get better versioning method
|
||||
VERS = "2.1.2"
|
||||
VERS = "3.0.0"
|
||||
|
||||
if vv { // display info and exit
|
||||
fmt.Println( "ekho server v" + VERS )
|
||||
@@ -427,7 +499,7 @@ func main() {
|
||||
// wait, create connection when found
|
||||
conn, err := listener.Accept()
|
||||
check( err ) // make sure connection works
|
||||
go handle( conn ) // handle connection
|
||||
go receive( conn ) // handle connection
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user