|
|
@@ -1,14 +1,17 @@
|
|
|
|
// =============================================================================
|
|
|
|
// =============================================================================
|
|
|
|
// Auth: alex
|
|
|
|
// Auth: alex
|
|
|
|
// File: server.go
|
|
|
|
// File: server.go
|
|
|
|
// Revn: 04-28-2026 6.0
|
|
|
|
// Revn: 05-12-2026 2.1.2
|
|
|
|
// Func: host MOTD connection
|
|
|
|
// Func: host MOTD connection
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// TODO: remove ioutil import
|
|
|
|
// TODO:
|
|
|
|
// add more /codes/ to handle
|
|
|
|
// add user-specific prints
|
|
|
|
// add flags, like specify port
|
|
|
|
|
|
|
|
// write usage and errors to logfile
|
|
|
|
// write usage and errors to logfile
|
|
|
|
|
|
|
|
// remove ioutil import
|
|
|
|
// send logfile to client on request
|
|
|
|
// send logfile to client on request
|
|
|
|
|
|
|
|
// add color printing to qfile
|
|
|
|
|
|
|
|
// add flags?
|
|
|
|
|
|
|
|
// add more /codes/ to handle
|
|
|
|
// =============================================================================
|
|
|
|
// =============================================================================
|
|
|
|
// CHANGE LOG
|
|
|
|
// CHANGE LOG
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
@@ -31,7 +34,7 @@
|
|
|
|
// fixed bug in /r2/ where removing quote at final
|
|
|
|
// fixed bug in /r2/ where removing quote at final
|
|
|
|
// position would break
|
|
|
|
// position would break
|
|
|
|
// removed deadcode from /r2/, unused "quit" bool?
|
|
|
|
// removed deadcode from /r2/, unused "quit" bool?
|
|
|
|
//*10-17-2023: fixed bug where quotes are read from and saved to two
|
|
|
|
//#10-17-2023: fixed bug where quotes are read from and saved to two
|
|
|
|
// different files
|
|
|
|
// different files
|
|
|
|
// removed implicit newline at end of qfile to stop ghost
|
|
|
|
// removed implicit newline at end of qfile to stop ghost
|
|
|
|
// line bug in /la/
|
|
|
|
// line bug in /la/
|
|
|
@@ -43,9 +46,11 @@
|
|
|
|
// commented
|
|
|
|
// commented
|
|
|
|
//*06-14-2024: byte count-based PWFT rewrite
|
|
|
|
//*06-14-2024: byte count-based PWFT rewrite
|
|
|
|
//*06-17-2024: catch keyboard interrupts with os/signal and syscall
|
|
|
|
//*06-17-2024: catch keyboard interrupts with os/signal and syscall
|
|
|
|
//*01-09-2025: copied lr/la PWFT updates into r1/r2
|
|
|
|
//#01-09-2025: copied lr/la PWFT updates into r1/r2
|
|
|
|
//*04-28-2026: removed implicit newline at end of qfile to stop ghost
|
|
|
|
//*04-28-2026: removed implicit newline at end of qfile to stop ghost
|
|
|
|
// line bug in /la/ AGAIN
|
|
|
|
// line bug in /la/ AGAIN
|
|
|
|
|
|
|
|
// 05-11-2026: updated fread() to handle users in qfile
|
|
|
|
|
|
|
|
// 05-12-2026: added v and vv input flags
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// =============================================================================
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
|
@@ -118,8 +123,13 @@ func fread() string {
|
|
|
|
// really exist
|
|
|
|
// really exist
|
|
|
|
qfile = qfile[:len( qfile ) - 1]
|
|
|
|
qfile = qfile[:len( qfile ) - 1]
|
|
|
|
// cast bytes to string, split string over newline into array
|
|
|
|
// cast bytes to string, split string over newline into array
|
|
|
|
qlist = strings.Split( string( qfile ), "\n" )
|
|
|
|
//qlist = strings.Split( string( qfile ), "\n" )
|
|
|
|
// deal with the trailing
|
|
|
|
profiles := strings.Split( string( qfile ), "@" )
|
|
|
|
|
|
|
|
for _, p := range profiles {
|
|
|
|
|
|
|
|
quotes := strings.Split( p, ":" )
|
|
|
|
|
|
|
|
qlist[quotes[0]] = strings.Split( quotes[1], "\n" )
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return string( qfile ) // return file as string
|
|
|
|
return string( qfile ) // return file as string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@@ -145,6 +155,17 @@ func handle( conn net.Conn ) {
|
|
|
|
n, err := conn.Read( buffer[:] )
|
|
|
|
n, err := conn.Read( buffer[:] )
|
|
|
|
checkN( err, conn ) // make sure read worked
|
|
|
|
checkN( err, conn ) // make sure read worked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// message anatomy
|
|
|
|
|
|
|
|
// command@number[@user]
|
|
|
|
|
|
|
|
// @ @ delimiter
|
|
|
|
|
|
|
|
// command command, like list request or quote request
|
|
|
|
|
|
|
|
// number sequence or quote number
|
|
|
|
|
|
|
|
// user quote username if applicable, default if not give
|
|
|
|
|
|
|
|
// add request message anatomy
|
|
|
|
|
|
|
|
// command@quote[@user]
|
|
|
|
|
|
|
|
// quote quote to be added
|
|
|
|
|
|
|
|
// user quote username, default if not given
|
|
|
|
|
|
|
|
|
|
|
|
// cast message to string, take first n bytes, split over @
|
|
|
|
// cast message to string, take first n bytes, split over @
|
|
|
|
command := strings.Split( string( buffer[:n] ), "@" )
|
|
|
|
command := strings.Split( string( buffer[:n] ), "@" )
|
|
|
|
|
|
|
|
|
|
|
@@ -158,9 +179,20 @@ func handle( conn net.Conn ) {
|
|
|
|
// concat and send
|
|
|
|
// concat and send
|
|
|
|
resp = "sa@" + num // size answer
|
|
|
|
resp = "sa@" + num // size answer
|
|
|
|
case "qr": // /qr/, quote request
|
|
|
|
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
|
|
|
|
// convert message argument to string to make sure it's a
|
|
|
|
// valid number
|
|
|
|
// valid number
|
|
|
|
num, nerr := strconv.Atoi( command[1] )
|
|
|
|
num, nerr := strconv.Atoi( subcommands[0] )
|
|
|
|
// nerr will not be nil if comm[1] is not a number
|
|
|
|
// nerr will not be nil if comm[1] is not a number
|
|
|
|
if nerr != nil {
|
|
|
|
if nerr != nil {
|
|
|
|
// formulate an error message
|
|
|
|
// formulate an error message
|
|
|
@@ -168,7 +200,7 @@ func handle( conn net.Conn ) {
|
|
|
|
resp = "ba@qr@!" // bad answer
|
|
|
|
resp = "ba@qr@!" // bad answer
|
|
|
|
} else { // comm[1] was a number, but...
|
|
|
|
} else { // comm[1] was a number, but...
|
|
|
|
switch { // how do we know it's a valid number
|
|
|
|
switch { // how do we know it's a valid number
|
|
|
|
case num > len( qlist ):
|
|
|
|
case num > len( qlist[name] ):
|
|
|
|
// bad answer in quote request
|
|
|
|
// bad answer in quote request
|
|
|
|
resp = "ba@qr@>"
|
|
|
|
resp = "ba@qr@>"
|
|
|
|
// why bad? num is >
|
|
|
|
// why bad? num is >
|
|
|
@@ -178,7 +210,7 @@ func handle( conn net.Conn ) {
|
|
|
|
// why bad? num is -
|
|
|
|
// why bad? num is -
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
// good num, get quote, send off
|
|
|
|
// good num, get quote, send off
|
|
|
|
resp = "qa@" + qlist[num] // quote answer
|
|
|
|
resp = "qa@" + qlist[name][num] // quote answer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "lr": // /lr/, list request
|
|
|
|
case "lr": // /lr/, list request
|
|
|
@@ -220,7 +252,7 @@ func handle( conn net.Conn ) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "ar": // /ar/, add request
|
|
|
|
case "ar": // /ar/, add request
|
|
|
|
// append new quote to list
|
|
|
|
// append new quote to list
|
|
|
|
qlist = append( qlist, command[1] )
|
|
|
|
qlist["f"] = append( qlist["f"], command[1] )
|
|
|
|
// also append new quote to file variable
|
|
|
|
// also append new quote to file variable
|
|
|
|
qfile = qfile + "\n" + command[1]
|
|
|
|
qfile = qfile + "\n" + command[1]
|
|
|
|
writeFile( qfile )
|
|
|
|
writeFile( qfile )
|
|
|
@@ -287,14 +319,14 @@ func handle( conn net.Conn ) {
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
// replace the quote at that position with
|
|
|
|
// replace the quote at that position with
|
|
|
|
// final quote
|
|
|
|
// final quote
|
|
|
|
qlist[num] = qlist[len( qlist ) - 1]
|
|
|
|
qlist["f"][num] = qlist["f"][len( qlist ) - 1]
|
|
|
|
// remove second copy of quote
|
|
|
|
// remove second copy of quote
|
|
|
|
qlist = qlist[:len( qlist ) - 1]
|
|
|
|
qlist["f"] = qlist["f"][:len( qlist ) - 1]
|
|
|
|
// O(1) remove from list at any position
|
|
|
|
// O(1) remove from list at any position
|
|
|
|
// must entirely recreate quote string
|
|
|
|
// must entirely recreate quote string
|
|
|
|
var f string = "" // init empty string
|
|
|
|
var f string = "" // init empty string
|
|
|
|
// iterate over list, disregard index
|
|
|
|
// iterate over list, disregard index
|
|
|
|
for _, v := range qlist {
|
|
|
|
for _, v := range qlist["f"] {
|
|
|
|
// add next quote to file and separate
|
|
|
|
// add next quote to file and separate
|
|
|
|
// quotes with newline
|
|
|
|
// quotes with newline
|
|
|
|
f = f + v + "\n"
|
|
|
|
f = f + v + "\n"
|
|
|
@@ -334,10 +366,11 @@ func handle( conn net.Conn ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// globals
|
|
|
|
// globals
|
|
|
|
var qlist []string // list of quotes
|
|
|
|
//var qlist []string // list of quotes
|
|
|
|
|
|
|
|
var qlist = make( map[string][]string )
|
|
|
|
var qfile string // file object
|
|
|
|
var qfile string // file object
|
|
|
|
var tsize int // size of transmission buffer
|
|
|
|
var tsize int // size of transmission buffer
|
|
|
|
|
|
|
|
var VERS string // version
|
|
|
|
|
|
|
|
|
|
|
|
// main, create port and wait for connection
|
|
|
|
// main, create port and wait for connection
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
@@ -347,12 +380,32 @@ func main() {
|
|
|
|
// set interrupt and terminate signals to notify channel
|
|
|
|
// set interrupt and terminate signals to notify channel
|
|
|
|
signal.Notify( sigs, syscall.SIGINT, syscall.SIGTERM )
|
|
|
|
signal.Notify( sigs, syscall.SIGINT, syscall.SIGTERM )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// flag vars
|
|
|
|
|
|
|
|
var v bool // simple version
|
|
|
|
|
|
|
|
var vv bool // verbose version
|
|
|
|
|
|
|
|
|
|
|
|
// flag for default transmission size
|
|
|
|
// flag for default transmission size
|
|
|
|
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
|
|
|
|
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
|
|
|
|
|
|
|
|
flag.BoolVar( &v, "v", false, "display version and exit" )
|
|
|
|
|
|
|
|
flag.BoolVar( &vv, "vv", false, "display version and about info and exit" )
|
|
|
|
flag.Parse()
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
|
|
qfile = fread() // read quote file, init ( global ) list of quotes
|
|
|
|
// FIXME get better versioning method
|
|
|
|
|
|
|
|
VERS = "2.1.2"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if vv { // display info and exit
|
|
|
|
|
|
|
|
fmt.Println( "ekho server v" + VERS )
|
|
|
|
|
|
|
|
fmt.Println( "ekho is named after the Echo the Oread" )
|
|
|
|
|
|
|
|
fmt.Println( "in Greek mythology, Echo was cursed by Hera to only be able to say the last words spoken to her" )
|
|
|
|
|
|
|
|
fmt.Println( "i found this to be quite fitting for a MOTD server" )
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
} else if v { // display info and exit
|
|
|
|
|
|
|
|
fmt.Println( VERS )
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qfile = fread() // read quote file, init ( global ) list of quotes
|
|
|
|
service := ":1300" // create service on ip and port
|
|
|
|
service := ":1300" // create service on ip and port
|
|
|
|
|
|
|
|
|
|
|
|
// resolve ip address and port
|
|
|
|
// resolve ip address and port
|
|
|
|