Compare commits
16 Commits
justfile
...
850efbe00f
| Author | SHA1 | Date | |
|---|---|---|---|
| 850efbe00f | |||
| b992353b02 | |||
| 0a889f38d4 | |||
| 4bf639f080 | |||
| a10b632f9d | |||
| a7183cd560 | |||
| 3e9e17323d | |||
| 5b0ef3e596 | |||
| 5f7ce723ae | |||
| bba0bc1a29 | |||
| e5a7257758 | |||
| f062efc176 | |||
| bf95cadeac | |||
| cd8ac36097 | |||
| da3b784b05 | |||
| b93aff00d9 |
@@ -65,7 +65,11 @@ The client uses the following flags
|
|||||||
-ip <ip:port> - specify alternate server destination
|
-ip <ip:port> - specify alternate server destination
|
||||||
-l - list all quotes
|
-l - list all quotes
|
||||||
-r [i] - remove; may be run with or without argument
|
-r [i] - remove; may be run with or without argument
|
||||||
|
-e [i] - edit; may be run with or without argument
|
||||||
|
-c - list color format specifiers with examples
|
||||||
-s <i> - specify the size of the receipt buffer
|
-s <i> - specify the size of the receipt buffer
|
||||||
|
-v - display version and exit
|
||||||
|
-vv - display version and about info and exit
|
||||||
```
|
```
|
||||||
The server uses the following flags
|
The server uses the following flags
|
||||||
``` sh
|
``` sh
|
||||||
@@ -79,8 +83,6 @@ The server uses the following flags
|
|||||||
- Log all uses/transaction data each day
|
- Log all uses/transaction data each day
|
||||||
- Request, send, and receive the logfile
|
- Request, send, and receive the logfile
|
||||||
- User-specific quotes/quotefiles
|
- User-specific quotes/quotefiles
|
||||||
- Colored printing and coloring quotes
|
|
||||||
- Edit quotes
|
|
||||||
|
|
||||||
[golang]: <https://go.dev/doc/install>
|
[golang]: <https://go.dev/doc/install>
|
||||||
[just]: <https://github.com/casey/just>
|
[just]: <https://github.com/casey/just>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// =============================================================================
|
// =============================================================================
|
||||||
// Auth: alex
|
// Auth: alex
|
||||||
// File: client.go
|
// File: client.go
|
||||||
// Revn: 01-09-2025 4.0
|
// Revn: 09-16-2026 4.0.0
|
||||||
// Func: ask server for a message of the day quote
|
// Func: ask server for a message of the day quote
|
||||||
//
|
//
|
||||||
// TODO: remove ioutil import
|
// TODO: remove ioutil import
|
||||||
@@ -24,32 +24,53 @@
|
|||||||
// imported check() from soary
|
// imported check() from soary
|
||||||
// 09-29-2023: commented handle()
|
// 09-29-2023: commented handle()
|
||||||
//*10-02-2023: commented main(), and friends
|
//*10-02-2023: commented main(), and friends
|
||||||
//*10-05-2023: /remove/ works
|
//#10-05-2023: /remove/ works
|
||||||
// removed -d flag
|
// removed -d flag
|
||||||
// commented
|
// commented
|
||||||
// 06-12-2024: began added PWFT to /la/
|
// 06-12-2024: began added PWFT to /la/
|
||||||
// 06-13-2024: finished adding PWFT to /la/
|
// 06-13-2024: finished adding PWFT to /la/
|
||||||
// commented PWFT stuff
|
// commented PWFT stuff
|
||||||
// STARTED BYTE-BASED PWFT REWRITE
|
// STARTED BYTE-BASED PWFT REWRITE
|
||||||
//*06-14-2024: finished byte-based PWFT rewrite
|
//#06-14-2024: finished byte-based PWFT rewrite
|
||||||
// commented
|
// commented
|
||||||
//*01-09-2025: basically just copy/pasted from /la/ into /r1/, /r2/
|
//*01-09-2025: basically just copy/pasted from /la/ into /r1/, /r2/
|
||||||
|
// 06-21-2026: copied v and vv flags from server
|
||||||
|
// copied /edit/ switch case from /remove/ in main()
|
||||||
|
// pulled /sa/ and /qa/ into separate functions
|
||||||
|
// 09-03-2026: renamed in() -> inputNumber()
|
||||||
|
// copied in() and removed all logic
|
||||||
|
// added firstEdit() and secondEdit() and /e1/ and /e2/
|
||||||
|
// 09-04-2026: rewrote in() with bufio instead of scanln
|
||||||
|
//#09-06-2026: fixed blank message error in secondEdit()
|
||||||
|
// comments
|
||||||
|
// 09-07-2026: added fatih/color import
|
||||||
|
// rewrote quoteAnswer() to handle coloring escape
|
||||||
|
// sequences
|
||||||
|
//*09-12-2026: commented quoteAnswer()
|
||||||
|
//*09-15-2026: added -c flag and color code example printing
|
||||||
|
// changed edit command to be add and then remove instead
|
||||||
|
// of remove and then add, fixing server OOB error when
|
||||||
|
// editting only quote in list
|
||||||
|
//#09-15-2026: finished pretty print implementation
|
||||||
|
// made listAnswer() print list with color print
|
||||||
//
|
//
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io" // io.EOF
|
"bufio" // NewReader, ReadString
|
||||||
"io/ioutil" // ReadFile
|
"io" // io.EOF
|
||||||
"flag" // BoolVar, StringVar, Parse
|
"io/ioutil" // ReadFile
|
||||||
"fmt" // Println
|
"flag" // BoolVar, StringVar, Parse
|
||||||
"math/rand" // NewSource, New, Intn, *rand.Rand
|
"fmt" // Println
|
||||||
"net" // ResolveTCPAddr, DialTCP, conn.Write,Read
|
"math/rand" // NewSource, New, Intn, *rand.Rand
|
||||||
"os" // Exit, Create
|
"net" // ResolveTCPAddr, DialTCP, conn.Write,Read
|
||||||
"strconv" // Atoi, Itoa
|
"os" // Exit, Create, Stdin
|
||||||
"strings" // Split
|
"strconv" // Atoi, Itoa
|
||||||
"time" // Now, UnixNano
|
"strings" // Split
|
||||||
|
"time" // Now, UnixNano
|
||||||
|
"github.com/fatih/color" // color.New, Printf, Println
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -85,20 +106,37 @@ func check( err error ) {
|
|||||||
|
|
||||||
// get user input
|
// get user input
|
||||||
func in( prompt string ) string {
|
func in( prompt string ) string {
|
||||||
|
// instantiate reader
|
||||||
|
reader := bufio.NewReader( os.Stdin )
|
||||||
fmt.Print( prompt ) // print user prompt
|
fmt.Print( prompt ) // print user prompt
|
||||||
var input string // declare var to hold user input
|
// get user input, read until newline
|
||||||
fmt.Scanln( &input ) // get user input
|
input, _ := reader.ReadString( '\n' )
|
||||||
|
// strip trailing newline before returning
|
||||||
|
return input[:len( input ) - 1]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// get user input, must be a number
|
||||||
|
func inputNumber( prompt string ) string {
|
||||||
|
// get user input
|
||||||
|
//input := in( prompt )
|
||||||
|
fmt.Print( prompt )
|
||||||
|
var input string
|
||||||
|
fmt.Scanln( &input )
|
||||||
|
|
||||||
// try to convert to an int, discard results
|
// try to convert to an int, discard results
|
||||||
_, err := strconv.Atoi( input )
|
_, err := strconv.Atoi( input )
|
||||||
// if there was an error, user didn't input an int
|
// if there was an error, user didn't input an int
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// warn user against this
|
// warn user against this
|
||||||
fmt.Println( "Input must be a number" )
|
fmt.Println( "Input must be a number" )
|
||||||
input = in( prompt ) // try to get user input again
|
// try to get user input again
|
||||||
|
//XXX input = in( prompt )
|
||||||
|
fmt.Println( "err: ", err.Error() )
|
||||||
// this, like handle() calling handle(), seems like a super
|
// this, like handle() calling handle(), seems like a super
|
||||||
// bad idea, stack-wise...
|
// bad idea, stack-wise...
|
||||||
}
|
}
|
||||||
// once user has properly inputted a number, return string of num
|
// once user has properly inputted a number, return string of num
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,7 +151,7 @@ func seed() *rand.Rand {
|
|||||||
|
|
||||||
|
|
||||||
// get a random number
|
// get a random number
|
||||||
func draw() string {
|
func draw( qsize int ) string {
|
||||||
rn := seed() // get a random seed
|
rn := seed() // get a random seed
|
||||||
r := rn.Intn( qsize ) // get a random number using seed
|
r := rn.Intn( qsize ) // get a random number using seed
|
||||||
// convert random number to ascii, because converting
|
// convert random number to ascii, because converting
|
||||||
@@ -123,6 +161,344 @@ func draw() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// respond to size answer
|
||||||
|
func sizeAnswer( subcom []string ) string {
|
||||||
|
// subcom should contain only a size
|
||||||
|
// convert to int, catch error
|
||||||
|
size, err := strconv.Atoi( subcom[0] )
|
||||||
|
|
||||||
|
if err != nil { // if int conversion failed ( for some reason )
|
||||||
|
os.Exit( 1 ) // just quit
|
||||||
|
// TODO make exit codes unique, document them
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw a random number, add to quote request
|
||||||
|
return "qr@" + draw( size )
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// receive quote, print
|
||||||
|
func quoteAnswer( subcom []string ) string {
|
||||||
|
|
||||||
|
// split subcommand over spaces
|
||||||
|
words := strings.Split( subcom[0], " " )
|
||||||
|
|
||||||
|
// default text is white
|
||||||
|
c := color.New( color.FgWhite )
|
||||||
|
for _, w := range words {
|
||||||
|
// if first char is normal color escape character
|
||||||
|
if string( w[0] ) == "$" {
|
||||||
|
// see which color is requested
|
||||||
|
switch string( w[1] ) {
|
||||||
|
case "1": // 1 -> red
|
||||||
|
c = color.New( color.FgRed )
|
||||||
|
case "2": // 2 -> yellow
|
||||||
|
c = color.New( color.FgYellow )
|
||||||
|
case "3": // 3 -> green
|
||||||
|
c = color.New( color.FgGreen )
|
||||||
|
case "4": // 4 -> blue
|
||||||
|
c = color.New( color.FgBlue )
|
||||||
|
case "5": // 5 -> cyan
|
||||||
|
c = color.New( color.FgCyan )
|
||||||
|
case "6": // 6 -> magenta
|
||||||
|
c = color.New( color.FgMagenta )
|
||||||
|
case "7": // 7 -> black
|
||||||
|
c = color.New( color.FgBlack )
|
||||||
|
case "0": // 0 -> white again
|
||||||
|
c = color.New( color.FgWhite )
|
||||||
|
default:
|
||||||
|
// none of these, continue with the last color
|
||||||
|
}
|
||||||
|
// color chosen, print string with trailing space, minus
|
||||||
|
// first two characters
|
||||||
|
c.Printf( "%s ", w[2:] )
|
||||||
|
// if first char is bold color escape character
|
||||||
|
} else if string( w[0] ) == "#" {
|
||||||
|
// see which color is requested
|
||||||
|
switch string( w[1] ) {
|
||||||
|
case "1": // 1 -> red
|
||||||
|
c = color.New( color.FgRed, color.Bold )
|
||||||
|
case "2": // 2 -> yellow
|
||||||
|
c = color.New( color.FgYellow, color.Bold )
|
||||||
|
case "3": // 3 -> green
|
||||||
|
c = color.New( color.FgGreen, color.Bold )
|
||||||
|
case "4": // 4 -> blue
|
||||||
|
c = color.New( color.FgBlue, color.Bold )
|
||||||
|
case "5": // 5 -> cyan
|
||||||
|
c = color.New( color.FgCyan, color.Bold )
|
||||||
|
case "6": // 6 -> magenta
|
||||||
|
c = color.New( color.FgMagenta, color.Bold )
|
||||||
|
case "7": // 7 -> black
|
||||||
|
c = color.New( color.FgBlack, color.Bold )
|
||||||
|
case "0": // 0 -> white again
|
||||||
|
c = color.New( color.FgWhite, color.Bold )
|
||||||
|
default:
|
||||||
|
// none of these, continue with the last color
|
||||||
|
}
|
||||||
|
// color chosen, print string with trailing space, minus
|
||||||
|
// first two characters
|
||||||
|
c.Printf( "%s ", w[2:] )
|
||||||
|
} else { // TODO add if for if w[0] is "\"
|
||||||
|
// using the currently selected color, print the next work
|
||||||
|
// and a trailing space
|
||||||
|
c.Printf( "%s ", w )
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Println() // print final newline
|
||||||
|
|
||||||
|
//fmt.Println( subcom[0] ) // whole-ass just print quote
|
||||||
|
return "te@success" // set transaction end message
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// add answer
|
||||||
|
func addAnswer( subcom []string ) string {
|
||||||
|
|
||||||
|
var resp string = "te@"
|
||||||
|
|
||||||
|
// if server says add succeeded
|
||||||
|
if subcom[0] == "success" {
|
||||||
|
fmt.Println( "Add successful" ) // print success
|
||||||
|
resp = resp + "success" // set transaction end message
|
||||||
|
// if server says add failed
|
||||||
|
} else if subcom[0] == "error" {
|
||||||
|
fmt.Println( "Add failed" ) // print fail
|
||||||
|
resp = resp + "error" // set transaction end message
|
||||||
|
// unknown status message
|
||||||
|
} else {
|
||||||
|
// print unknown status
|
||||||
|
fmt.Println( "Unknown status code: " + subcom[0] )
|
||||||
|
// TODO should this be unknown instead of error?
|
||||||
|
resp = resp + "error" // set transaction end message
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// receive answer to list
|
||||||
|
func listAnswer( subcom []string ) string {
|
||||||
|
|
||||||
|
var resp string
|
||||||
|
|
||||||
|
if len( subcom ) > 1 { // multistage send condition
|
||||||
|
if subcom[0] == "X" { // final message
|
||||||
|
// TODO update
|
||||||
|
// read entire temp storage file as byte array
|
||||||
|
tfile, err := ioutil.ReadFile( "temp.q" )
|
||||||
|
check( err ) // check read for errors
|
||||||
|
// cast file to string, concat with recv'd string
|
||||||
|
tstr := string( tfile ) + subcom[1]
|
||||||
|
// split concat over newlines, separating quotes
|
||||||
|
tlist := strings.Split( tstr, "\n" )
|
||||||
|
// iterate over array, printing each quote
|
||||||
|
for p, v := range tlist {
|
||||||
|
fmt.Println( p + 1, "\t", v )
|
||||||
|
}
|
||||||
|
// delete temp storage file
|
||||||
|
err = os.Remove( "temp.q" )
|
||||||
|
check( err ) // check rm for errors
|
||||||
|
resp = "te@X" // successful response message
|
||||||
|
} else { // first or intermediate message
|
||||||
|
// open temp quote storage file, creating if
|
||||||
|
// necessary, with write-only permissions, in
|
||||||
|
// append mode, wr u perm, r g/w perm
|
||||||
|
f, err := os.OpenFile( "temp.q", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644 )
|
||||||
|
check( err ) // check file open for errors
|
||||||
|
// write received message to file, keeping track
|
||||||
|
// of bytes written
|
||||||
|
n, err := f.WriteString( subcom[1] )
|
||||||
|
check( err ) // check file write for errors
|
||||||
|
// cast bytes already written field to int
|
||||||
|
b, err := strconv.Atoi( subcom[0] )
|
||||||
|
check( err ) // check cast for errors
|
||||||
|
// add bytes just written to bytes already written, cast
|
||||||
|
// back to string, and append to response variable
|
||||||
|
resp = "lr@" + strconv.Itoa( b + n )
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// split recv'd message over \n, create quote list
|
||||||
|
quotes := strings.Split( subcom[0], "\n" )
|
||||||
|
for p, v := range quotes { // iterate over quotes
|
||||||
|
// print num of quote, followed by that quote
|
||||||
|
fmt.Print( p + 1, "\t" )
|
||||||
|
quoteAnswer( []string{ v } )
|
||||||
|
}
|
||||||
|
resp = "te@success" // set transaction end message
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// first round edit request
|
||||||
|
func firstEdit( subcom []string ) string {
|
||||||
|
|
||||||
|
var resp string
|
||||||
|
|
||||||
|
if len( subcom ) > 1 { // multistage send condition
|
||||||
|
if subcom[0] == "X" { // final message
|
||||||
|
// TODO update
|
||||||
|
// read entire temp storage file as byte array
|
||||||
|
tfile, err := ioutil.ReadFile( "temp.q" )
|
||||||
|
check( err ) // check read for errors
|
||||||
|
// cast file to string, concat with recv'd string
|
||||||
|
tstr := string( tfile ) + subcom[1]
|
||||||
|
// split concat over newlines, separating quotes
|
||||||
|
tlist := strings.Split( tstr, "\n" )
|
||||||
|
// iterate over array, printing each quote
|
||||||
|
for p, v := range tlist {
|
||||||
|
fmt.Println( p + 1, "\t", v )
|
||||||
|
}
|
||||||
|
// delete temp storage file
|
||||||
|
err = os.Remove( "temp.q" )
|
||||||
|
check( err ) // check rm for errors
|
||||||
|
// ask user which quote to remove
|
||||||
|
resp = "e2@" + inputNumber( "\n-> " )
|
||||||
|
} else { // first or intermediate message
|
||||||
|
// open temp quote storage file, creating if
|
||||||
|
// necessary, with write-only permissions, in
|
||||||
|
// append mode, wr u perm, r g/w perm
|
||||||
|
f, err := os.OpenFile( "temp.q", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644 )
|
||||||
|
check( err ) // check file open for errors
|
||||||
|
// write received message to file, keeping track
|
||||||
|
// of bytes written
|
||||||
|
n, err := f.WriteString( subcom[1] )
|
||||||
|
check( err ) // check file write for errors
|
||||||
|
// cast bytes already written field to int
|
||||||
|
b, err := strconv.Atoi( subcom[0] )
|
||||||
|
check( err ) // check cast for errors
|
||||||
|
// add bytes just written to bytes already written, cast
|
||||||
|
// back to string, and append to response variable
|
||||||
|
resp = "e1@" + strconv.Itoa( b + n )
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// split recv'd message over \n, create quote list
|
||||||
|
quotes := strings.Split( subcom[0], "\n" )
|
||||||
|
for p, v := range quotes { // iterate over quotes
|
||||||
|
// print num of quote, followed by that quote
|
||||||
|
fmt.Println( p + 1, "\t", v )
|
||||||
|
}
|
||||||
|
// ask user which quote to edit
|
||||||
|
resp = "e2@" + inputNumber( "\n-> " )
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// second round edit request ( and also third, technically )
|
||||||
|
func secondEdit( subcom []string ) string {
|
||||||
|
// success message and edit request pt2 come to the same place
|
||||||
|
if subcom[0] == "success" {
|
||||||
|
// print success and end transaction
|
||||||
|
fmt.Println( "Edit successful" )
|
||||||
|
return "te@success"
|
||||||
|
}
|
||||||
|
|
||||||
|
// edit command is an add and then a removal
|
||||||
|
var resp string
|
||||||
|
resp = "cr@ar@"
|
||||||
|
|
||||||
|
fmt.Println( "as is: " + subcom[1] )
|
||||||
|
fmt.Print( "preview: " ) // show raw of current quote
|
||||||
|
_ = quoteAnswer( subcom[1:] ) // display quote properly
|
||||||
|
|
||||||
|
// server response is user input for new quote
|
||||||
|
resp = resp + in( "\nupdate -> " )
|
||||||
|
// and position of old quote to be removed
|
||||||
|
resp = resp + "@r2@" + subcom[0]
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// list of all quotes, pick which to remove
|
||||||
|
func firstRemove( subcom []string ) string {
|
||||||
|
// the idea is that if the user doesn't specify which quote to
|
||||||
|
// remove, list all quotes and let the user choose the quote
|
||||||
|
// to remove. There was probably a way to handle this a little
|
||||||
|
// more simply, but this, I feel, has some nuance to it that I
|
||||||
|
// actually like
|
||||||
|
|
||||||
|
var resp string
|
||||||
|
|
||||||
|
if len( subcom ) > 1 { // multistage send condition
|
||||||
|
if subcom[0] == "X" { // final message
|
||||||
|
// TODO update
|
||||||
|
// read entire temp storage file as byte array
|
||||||
|
tfile, err := ioutil.ReadFile( "temp.q" )
|
||||||
|
check( err ) // check read for errors
|
||||||
|
// cast file to string, concat with recv'd string
|
||||||
|
tstr := string( tfile ) + subcom[1]
|
||||||
|
// split concat over newlines, separating quotes
|
||||||
|
tlist := strings.Split( tstr, "\n" )
|
||||||
|
// iterate over array, printing each quote
|
||||||
|
for p, v := range tlist {
|
||||||
|
fmt.Println( p + 1, "\t", v )
|
||||||
|
}
|
||||||
|
// delete temp storage file
|
||||||
|
err = os.Remove( "temp.q" )
|
||||||
|
check( err ) // check rm for errors
|
||||||
|
// ask user which quote to remove
|
||||||
|
resp = "r2@" + inputNumber( "\n-> " )
|
||||||
|
} else { // first or intermediate message
|
||||||
|
// open temp quote storage file, creating if
|
||||||
|
// necessary, with write-only permissions, in
|
||||||
|
// append mode, wr u perm, r g/w perm
|
||||||
|
f, err := os.OpenFile( "temp.q", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644 )
|
||||||
|
check( err ) // check file open for errors
|
||||||
|
// write received message to file, keeping track
|
||||||
|
// of bytes written
|
||||||
|
n, err := f.WriteString( subcom[1] )
|
||||||
|
check( err ) // check file write for errors
|
||||||
|
// cast bytes already written field to int
|
||||||
|
b, err := strconv.Atoi( subcom[0] )
|
||||||
|
check( err ) // check cast for errors
|
||||||
|
// add bytes just written to bytes already written, cast
|
||||||
|
// back to string, and append to response variable
|
||||||
|
resp = "r1@" + strconv.Itoa( b + n )
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// split recv'd message over \n, create quote list
|
||||||
|
quotes := strings.Split( subcom[0], "\n" )
|
||||||
|
for p, v := range quotes { // iterate over quotes
|
||||||
|
// print num of quote, followed by that quote
|
||||||
|
fmt.Println( p + 1, "\t", v )
|
||||||
|
}
|
||||||
|
// ask user which quote to remove
|
||||||
|
resp = "r2@" + inputNumber( "\n-> " )
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// second remove, determine if success or failure
|
||||||
|
func secondRemove( subcom []string ) string {
|
||||||
|
var resp string
|
||||||
|
// if server says remove succeeded
|
||||||
|
if subcom[0] == "success" {
|
||||||
|
fmt.Println( "Remove successful\n" ) // success
|
||||||
|
// list all quotes to validate remove succeeded
|
||||||
|
// TODO should /aa/ also make /lr/ before /te/?
|
||||||
|
resp = "lr@init" // set transaction end message
|
||||||
|
} else if subcom[0] == "error" { // if server says remove failed
|
||||||
|
fmt.Println( "Remove failed" ) // print failure
|
||||||
|
resp = "te@error" // set transaction end message
|
||||||
|
} else { // if unknown status message
|
||||||
|
// TODO there's a known issue where if the buffer
|
||||||
|
// size ( -s ) is less than 10, on the server side,
|
||||||
|
// 'success' will get cut off ( r2@success )
|
||||||
|
// and /r2/ will end up down here despite having
|
||||||
|
// actually worked correctly, and will not display the
|
||||||
|
// list, as it should on success
|
||||||
|
// print unknown status
|
||||||
|
fmt.Println( "Unknown error code: " + subcom[0] )
|
||||||
|
// TODO should this be unknown instead of error?
|
||||||
|
resp = "te@error" // set transaction end message
|
||||||
|
}
|
||||||
|
return resp
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// get server response and print
|
// get server response and print
|
||||||
func handle( conn net.Conn ) {
|
func handle( conn net.Conn ) {
|
||||||
|
|
||||||
@@ -135,176 +511,37 @@ func handle( conn net.Conn ) {
|
|||||||
check( err ) // check for errors
|
check( err ) // check for errors
|
||||||
|
|
||||||
// split recv'd message over @, separate header from message
|
// split recv'd message over @, separate header from message
|
||||||
msg := strings.Split( string( buffer[:n] ), "@" )
|
message := strings.Split( string( buffer[:n] ), "@" )
|
||||||
|
command := message[0]
|
||||||
|
|
||||||
var resp string // declare response variable
|
var resp string // declare response variable
|
||||||
switch msg[0] { // switch/case over the header
|
switch command { // switch/case over the header
|
||||||
// if message is...
|
// if message is...
|
||||||
case "sa": // /sa/, size answer
|
case "sa": // /sa/, size answer
|
||||||
// convert back half to int, catch error
|
resp = sizeAnswer( message[1:] )
|
||||||
size, nerr := strconv.Atoi( msg[1] )
|
|
||||||
if nerr == nil { // if there was no error
|
|
||||||
qsize = size // set size global to recv'd size
|
|
||||||
} else {
|
|
||||||
// if int conversion failed, just quit
|
|
||||||
// TODO make more robust
|
|
||||||
os.Exit( 1 )
|
|
||||||
}
|
|
||||||
req := draw() // draw a random number
|
|
||||||
resp = "qr@" + req // create quote query with rng
|
|
||||||
case "ba": // /ba/, bad answer
|
|
||||||
// TODO create server-side, and handle client-side
|
|
||||||
fmt.Println( "error in " + msg[1] + ": " + msg[2] )
|
|
||||||
resp = "te@error" // set transaction end message
|
|
||||||
case "qa": // /qa/, quote answer
|
case "qa": // /qa/, quote answer
|
||||||
fmt.Println( msg[1] ) // whole-ass just print quote
|
resp = quoteAnswer( message[1:] )
|
||||||
resp = "te@success" // set transaction end message
|
|
||||||
case "la": // /la/, list answer
|
|
||||||
if len( msg ) > 2 { // multistage send condition
|
|
||||||
if msg[1] == "X" { // final message
|
|
||||||
// TODO update
|
|
||||||
// read entire temp storage file as byte array
|
|
||||||
tfile, err := ioutil.ReadFile( "temp.q" )
|
|
||||||
check( err ) // check read for errors
|
|
||||||
// cast file to string, concat with recv'd string
|
|
||||||
tstr := string( tfile ) + msg[2]
|
|
||||||
// split concat over newlines, separating quotes
|
|
||||||
tlist := strings.Split( tstr, "\n" )
|
|
||||||
// iterate over array, printing each quote
|
|
||||||
for p, v := range tlist {
|
|
||||||
fmt.Println( p + 1, "\t", v )
|
|
||||||
}
|
|
||||||
// delete temp storage file
|
|
||||||
err = os.Remove( "temp.q" )
|
|
||||||
check( err ) // check rm for errors
|
|
||||||
resp = "te@X" // successful response message
|
|
||||||
} else { // first or intermediate message
|
|
||||||
// open temp quote storage file, creating if
|
|
||||||
// necessary, with write-only permissions, in
|
|
||||||
// append mode, wr u perm, r g/w perm
|
|
||||||
f, err := os.OpenFile( "temp.q", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644 )
|
|
||||||
check( err ) // check file open for errors
|
|
||||||
// write received message to file, keeping track
|
|
||||||
// of bytes written
|
|
||||||
n, err = f.WriteString( msg[2] )
|
|
||||||
check( err ) // check file write for errors
|
|
||||||
// cast bytes already written field to int
|
|
||||||
b, err := strconv.Atoi( msg[1] )
|
|
||||||
check( err ) // check cast for errors
|
|
||||||
// add bytes just written to bytes already
|
|
||||||
// written, cast back to string, and append to
|
|
||||||
// response variable
|
|
||||||
resp = "lr@" + strconv.Itoa( b + n )
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// split recv'd message over \n, create quote list
|
|
||||||
quotes := strings.Split( msg[1], "\n" )
|
|
||||||
for p, v := range quotes { // iterate over quotes
|
|
||||||
// print num of quote, followed by that quote
|
|
||||||
fmt.Println( p + 1, "\t", v )
|
|
||||||
}
|
|
||||||
resp = "te@success" // set transaction end message
|
|
||||||
}
|
|
||||||
case "aa": // /aa/, add answer
|
case "aa": // /aa/, add answer
|
||||||
// if server says add succeeded
|
resp = addAnswer( message[1:] )
|
||||||
if msg[1] == "success" {
|
case "la": // /la/, list answer
|
||||||
fmt.Println( "Add successful" ) // print success
|
resp = listAnswer( message[1:] )
|
||||||
resp = "te@success" // set transaction end message
|
case "e1": // /e1/, edit quote ( first transaction )
|
||||||
// if server says add failed
|
resp = firstEdit( message[1:] )
|
||||||
} else if msg[1] == "error" {
|
case "e2": // /e2/, edit quote ( second transaction )
|
||||||
fmt.Println( "Add failed" ) // print fail
|
resp = secondEdit( message[1:] )
|
||||||
resp = "te@error" // set transaction end message
|
|
||||||
// unknown status message
|
|
||||||
} else {
|
|
||||||
// print unknown status
|
|
||||||
fmt.Println( "Unknown status code: " + msg[1] )
|
|
||||||
// TODO should this be unknown instead of error?
|
|
||||||
resp = "te@error" // set transaction end message
|
|
||||||
}
|
|
||||||
case "r1": // /r1/, remove ( first transaction )
|
case "r1": // /r1/, remove ( first transaction )
|
||||||
// the idea is that if the user doesn't specify which quote to
|
resp = firstRemove( message[1:] )
|
||||||
// remove, list all quotes and let the user choose the quote
|
|
||||||
// to remove. There was probably a way to handle this a little
|
|
||||||
// more simply, but this, I feel, has some nuance to it that I
|
|
||||||
// actually like
|
|
||||||
// redo /r1/, list all quotes
|
|
||||||
// split recv'd message over newline to create quote list
|
|
||||||
if len( msg ) > 2 { // multistage send condition
|
|
||||||
if msg[1] == "X" { // final message
|
|
||||||
// TODO update
|
|
||||||
// read entire temp storage file as byte array
|
|
||||||
tfile, err := ioutil.ReadFile( "temp.q" )
|
|
||||||
check( err ) // check read for errors
|
|
||||||
// cast file to string, concat with recv'd string
|
|
||||||
tstr := string( tfile ) + msg[2]
|
|
||||||
// split concat over newlines, separating quotes
|
|
||||||
tlist := strings.Split( tstr, "\n" )
|
|
||||||
// iterate over array, printing each quote
|
|
||||||
for p, v := range tlist {
|
|
||||||
fmt.Println( p + 1, "\t", v )
|
|
||||||
}
|
|
||||||
// delete temp storage file
|
|
||||||
err = os.Remove( "temp.q" )
|
|
||||||
check( err ) // check rm for errors
|
|
||||||
// ask user which quote to remove
|
|
||||||
resp = "r2@" + in( "\n-> " )
|
|
||||||
} else { // first or intermediate message
|
|
||||||
// open temp quote storage file, creating if
|
|
||||||
// necessary, with write-only permissions, in
|
|
||||||
// append mode, wr u perm, r g/w perm
|
|
||||||
f, err := os.OpenFile( "temp.q", os.O_WRONLY | os.O_APPEND | os.O_CREATE, 0644 )
|
|
||||||
check( err ) // check file open for errors
|
|
||||||
// write received message to file, keeping track
|
|
||||||
// of bytes written
|
|
||||||
n, err = f.WriteString( msg[2] )
|
|
||||||
check( err ) // check file write for errors
|
|
||||||
// cast bytes already written field to int
|
|
||||||
b, err := strconv.Atoi( msg[1] )
|
|
||||||
check( err ) // check cast for errors
|
|
||||||
// add bytes just written to bytes already
|
|
||||||
// written, cast back to string, and append to
|
|
||||||
// response variable
|
|
||||||
resp = "r1@" + strconv.Itoa( b + n )
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// split recv'd message over \n, create quote list
|
|
||||||
quotes := strings.Split( msg[1], "\n" )
|
|
||||||
for p, v := range quotes { // iterate over quotes
|
|
||||||
// print num of quote, followed by that quote
|
|
||||||
fmt.Println( p + 1, "\t", v )
|
|
||||||
}
|
|
||||||
// ask user which quote to remove
|
|
||||||
resp = "r2@" + in( "\n-> " )
|
|
||||||
}
|
|
||||||
case "r2": // /r2/, remove ( second transaction )
|
case "r2": // /r2/, remove ( second transaction )
|
||||||
// if server says remove succeeded
|
resp = secondRemove( message[1:] )
|
||||||
if msg[1] == "success" {
|
|
||||||
fmt.Println( "Remove successful\n" ) // success
|
|
||||||
// list all quotes to validate remove succeeded
|
|
||||||
// TODO should /aa/ also make /lr/ before /te/?
|
|
||||||
resp = "lr@init" // set transaction end message
|
|
||||||
// if server says remove failed
|
|
||||||
} else if msg[1] == "error" {
|
|
||||||
fmt.Println( "Remove failed" ) // print failure
|
|
||||||
resp = "te@error" // set transaction end message
|
|
||||||
// if unknown status message
|
|
||||||
} else {
|
|
||||||
// TODO there's a known issue where if the buffer
|
|
||||||
// size ( -s ) is less than 10, on the server side,
|
|
||||||
// 'success' will get cut off ( r2@success )
|
|
||||||
// and /r2/ will end up down here despite having
|
|
||||||
// actually worked correctly, and with not display the
|
|
||||||
// list, as it should on success
|
|
||||||
// print unknown status
|
|
||||||
fmt.Println( "Unknown error code: " + msg[1] )
|
|
||||||
// TODO should this be unknown instead of error?
|
|
||||||
resp = "te@error" // set transaction end message
|
|
||||||
}
|
|
||||||
case "br": // /br/, bad request
|
case "br": // /br/, bad request
|
||||||
// client has asked for something nonsensical
|
// client has asked for something nonsensical
|
||||||
// TODO write out to file
|
// TODO write out to file
|
||||||
fmt.Println( "Client sent a bad request" )
|
fmt.Println( "Client sent a bad request" )
|
||||||
resp = "te@error" // set transaction end message
|
resp = "te@error" // set transaction end message
|
||||||
|
case "ba": // /ba/, bad answer
|
||||||
|
// TODO create server-side, and handle client-side
|
||||||
|
fmt.Println( "error in " + message[1] + ": " + message[2] )
|
||||||
|
resp = "te@error" // set transaction end message
|
||||||
default: // something undefined entirely
|
default: // something undefined entirely
|
||||||
// TODO write out to file
|
// TODO write out to file
|
||||||
//fmt.Println( "Message header not recognized" )
|
//fmt.Println( "Message header not recognized" )
|
||||||
@@ -322,26 +559,97 @@ func handle( conn net.Conn ) {
|
|||||||
|
|
||||||
|
|
||||||
// globals
|
// globals
|
||||||
var qsize int // keep track of num of quotes
|
//var qsize int // keep track of num of quotes
|
||||||
var conn net.Conn // keep track of connection
|
var conn net.Conn // keep track of connection
|
||||||
|
|
||||||
|
|
||||||
// global flag variables
|
// global flag variables
|
||||||
var add bool // add new quotes to quote file ( on server )
|
var add bool // add new quotes to quote file ( on server )
|
||||||
var remove bool // remove quotes from quote file ( on server )
|
var remove bool // remove quotes from quote file ( on server )
|
||||||
var list bool // request entire quote file from server
|
var list bool // request entire quote file from server
|
||||||
|
var edit bool // edit a pre-existing quote from quote file ( on server )
|
||||||
|
var co bool // list color codes
|
||||||
var dest string // provide a new destination ip and port
|
var dest string // provide a new destination ip and port
|
||||||
var tsize int // size of transmission buffer
|
var tsize int // size of transmission buffer
|
||||||
|
var VERS string // version
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
var v bool // version flag
|
||||||
|
var vv bool // big version flag
|
||||||
|
|
||||||
// define flags
|
// define flags
|
||||||
flag.BoolVar( &add, "a", false, "add quote to list" )
|
flag.BoolVar( &add, "a", false, "add quote to list" )
|
||||||
flag.BoolVar( &remove, "r", false, "remove quote from list" )
|
flag.BoolVar( &remove, "r", false, "remove quote from list" )
|
||||||
flag.BoolVar( &list, "l", false, "print quote list" )
|
flag.BoolVar( &list, "l", false, "print quote list" )
|
||||||
flag.StringVar( &dest, "ip", "127.0.0.1:1300", "ip:port of server" )
|
flag.BoolVar( &edit, "e", false, "edit quote from list" )
|
||||||
flag.IntVar( &tsize, "s", 256, "transmission size ( bytes )" )
|
flag.BoolVar( &co, "c", false, "list color codes with examples" )
|
||||||
flag.Parse() // process flags
|
flag.StringVar( &dest, "ip", "127.0.0.1:1300", "ip:port of server" )
|
||||||
|
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()
|
||||||
|
|
||||||
|
// FIXME get better versioning method
|
||||||
|
VERS = "4.0.0"
|
||||||
|
|
||||||
|
if vv { // display info and exit
|
||||||
|
fmt.Println( "ekho client v" + VERS )
|
||||||
|
fmt.Println( "ekho is named after 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
|
||||||
|
}
|
||||||
|
|
||||||
|
if co { // display color codes and examples and quit
|
||||||
|
// print color code and color example on same line
|
||||||
|
// color.XYZ print inserts newline
|
||||||
|
fmt.Print( "$1: " )
|
||||||
|
color.Red( "red" )
|
||||||
|
fmt.Print( "$2: " )
|
||||||
|
color.Yellow( "yellow" )
|
||||||
|
fmt.Print( "$3: " )
|
||||||
|
color.Green( "green" )
|
||||||
|
fmt.Print( "$4: " )
|
||||||
|
color.Blue( "blue" )
|
||||||
|
fmt.Print( "$5: " )
|
||||||
|
color.Cyan( "cyan" )
|
||||||
|
fmt.Print( "$6: " )
|
||||||
|
color.Magenta( "magenta" )
|
||||||
|
fmt.Print( "$7: " )
|
||||||
|
color.Black( "black" )
|
||||||
|
fmt.Print( "$0: " )
|
||||||
|
color.White( "white (reset)" )
|
||||||
|
// print bold codes
|
||||||
|
fmt.Print( "\n#1: " )
|
||||||
|
// create bold color object, change out actual color and print
|
||||||
|
bold := color.New( color.Bold, color.FgRed )
|
||||||
|
bold.Println( "bold red" )
|
||||||
|
fmt.Print( "#2: " )
|
||||||
|
bold.Add( color.FgYellow )
|
||||||
|
bold.Println( "bold yellow" )
|
||||||
|
fmt.Print( "#3: " )
|
||||||
|
bold.Add( color.FgGreen )
|
||||||
|
bold.Println( "bold green" )
|
||||||
|
fmt.Print( "#4: " )
|
||||||
|
bold.Add( color.FgBlue )
|
||||||
|
bold.Println( "bold blue" )
|
||||||
|
fmt.Print( "#5: " )
|
||||||
|
bold.Add( color.FgCyan )
|
||||||
|
bold.Println( "bold Cyan" )
|
||||||
|
fmt.Print( "#6: " )
|
||||||
|
bold.Add( color.FgMagenta )
|
||||||
|
bold.Println( "bold magenta" )
|
||||||
|
fmt.Print( "#7: " )
|
||||||
|
bold.Add( color.FgBlack )
|
||||||
|
bold.Println( "bold black" )
|
||||||
|
fmt.Print( "#0: " )
|
||||||
|
bold.Add( color.FgWhite )
|
||||||
|
bold.Println( "bold white" )
|
||||||
|
return // bail
|
||||||
|
}
|
||||||
|
|
||||||
service := dest // declare server ip:port
|
service := dest // declare server ip:port
|
||||||
|
|
||||||
@@ -375,6 +683,27 @@ func main() {
|
|||||||
fmt.Println( "/a/ flag must be used with an arugment" )
|
fmt.Println( "/a/ flag must be used with an arugment" )
|
||||||
os.Exit( 1 )
|
os.Exit( 1 )
|
||||||
}
|
}
|
||||||
|
case edit: // if edit flag is active
|
||||||
|
// if arguments were specified
|
||||||
|
if len( flag.Args() ) > 0 {
|
||||||
|
// argument is supposed to the number of the quote to
|
||||||
|
// be edited, get first arg
|
||||||
|
// cast to int just to make sure it's a valid number
|
||||||
|
// assuming the user knows what they're doing
|
||||||
|
_, rerr := strconv.Atoi( flag.Args()[0] )
|
||||||
|
// if there was an error, just pretend there's no args
|
||||||
|
if rerr != nil {
|
||||||
|
command = "e1@init" // remove request 1
|
||||||
|
} else {
|
||||||
|
// if user knows the number, take care of it
|
||||||
|
// arg[0] is a string, so concat is cool
|
||||||
|
// jump right into edit request 2
|
||||||
|
command = "e2@" + flag.Args()[0]
|
||||||
|
}
|
||||||
|
} else { // if no argument was specified for edit
|
||||||
|
// jump right into edit request 1
|
||||||
|
command = "e1@init"
|
||||||
|
}
|
||||||
case remove: // if remove flag is active
|
case remove: // if remove flag is active
|
||||||
// if arguments were specified
|
// if arguments were specified
|
||||||
if len( flag.Args() ) > 0 {
|
if len( flag.Args() ) > 0 {
|
||||||
@@ -386,7 +715,6 @@ func main() {
|
|||||||
// if there was an error, just pretend there's no args
|
// if there was an error, just pretend there's no args
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
command = "r1@init" // remove request 1
|
command = "r1@init" // remove request 1
|
||||||
// TODO come up w/ useful argument, replace dumvar
|
|
||||||
} else {
|
} else {
|
||||||
// if user knows the number, take care of it
|
// if user knows the number, take care of it
|
||||||
// arg[0] is a string, so concat is cool
|
// arg[0] is a string, so concat is cool
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
module earth-1.net/oracle/ekho
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
|
|
||||||
|
require github.com/fatih/color v1.19.0
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/mattn/go-colorable v0.1.14 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
golang.org/x/sys v0.42.0 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
github.com/fatih/color v1.19.0 h1:Zp3PiM21/9Ld6FzSKyL5c/BULoe/ONr9KlbYVOfG8+w=
|
||||||
|
github.com/fatih/color v1.19.0/go.mod h1:zNk67I0ZUT1bEGsSGyCZYZNrHuTkJJB+r6Q9VuMi0LE=
|
||||||
|
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||||
|
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
|
||||||
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||||
|
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
@@ -53,7 +53,6 @@ ins_server:
|
|||||||
echo "systemctl status ekho"
|
echo "systemctl status ekho"
|
||||||
systemctl status ekho
|
systemctl status ekho
|
||||||
|
|
||||||
|
|
||||||
# install client
|
# install client
|
||||||
ins_client:
|
ins_client:
|
||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
@@ -67,3 +66,57 @@ ins_client:
|
|||||||
mv ekho /usr/local/bin/
|
mv ekho /usr/local/bin/
|
||||||
echo "add 'ekho' to shell config (config.fish, .bashrc, etc)"
|
echo "add 'ekho' to shell config (config.fish, .bashrc, etc)"
|
||||||
|
|
||||||
|
# test server
|
||||||
|
stest:
|
||||||
|
@echo "creating server test session"
|
||||||
|
@tmux new-session -Ad -s "server test"
|
||||||
|
@tmux split-window -h -t "server test:1" -l 10%
|
||||||
|
@tmux split-window -v -t "server test:1.2"
|
||||||
|
@tmux send-keys -t "server test:1.1" "clear" Enter
|
||||||
|
@tmux send-keys -t "server test:1.2" "clear" Enter
|
||||||
|
@tmux send-keys -t "server test:1.3" "clear" Enter
|
||||||
|
@tmux rename-window "server test"
|
||||||
|
@tmux send-keys -t "server test:1.1" "vim server.go" Enter
|
||||||
|
@tmux send-keys -t "server test:1.2" "./server"
|
||||||
|
@tmux send-keys -t "server test:1.3" "./client"
|
||||||
|
@tmux attach -t "server test"
|
||||||
|
|
||||||
|
# test client
|
||||||
|
ctest:
|
||||||
|
@echo "creating client test session"
|
||||||
|
@tmux new-session -Ad -s "client test"
|
||||||
|
@tmux split-window -h -t "client test:1" -l 10%
|
||||||
|
@tmux split-window -v -t "client test:1.2"
|
||||||
|
@tmux send-keys -t "client test:1.1" "clear" Enter
|
||||||
|
@tmux send-keys -t "client test:1.2" "clear" Enter
|
||||||
|
@tmux send-keys -t "client test:1.3" "clear" Enter
|
||||||
|
@tmux rename-window "client test"
|
||||||
|
@tmux send-keys -t "client test:1.1" "vim client.go" Enter
|
||||||
|
@tmux send-keys -t "client test:1.2" "./server" Enter
|
||||||
|
@tmux send-keys -t "client test:1.3" "./client"
|
||||||
|
@tmux attach -t "client test"
|
||||||
|
|
||||||
|
# dev work
|
||||||
|
dev:
|
||||||
|
#!/usr/bin/bash
|
||||||
|
REPO=$(basename $(git rev-parse --show-toplevel))
|
||||||
|
BRANCH=$(git branch --show-current)
|
||||||
|
tmux has-session -t "$REPO->$BRANCH"
|
||||||
|
if [[ $? -eq 0 ]]; then
|
||||||
|
tmux attach-session -t "$REPO->$BRANCH"
|
||||||
|
else
|
||||||
|
tmux new-session -Ad -s "$REPO->$BRANCH"
|
||||||
|
tmux rename-window "write"
|
||||||
|
tmux new-window -a -t 1 -n "test"
|
||||||
|
tmux new-window -a -t 2 -n "git" lazygit
|
||||||
|
tmux new-window -a -t 3 -n "man"
|
||||||
|
tmux select-window -t 2
|
||||||
|
tmux select-window -t 1
|
||||||
|
tmux attach -t "$REPO->$BRANCH"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# prep repo to be committed
|
||||||
|
prep:
|
||||||
|
mv justfile test_justfile
|
||||||
|
mv prod_justfile justfile
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1 @@
|
|||||||
@user:
|
thank you for using $1ekho
|
||||||
shmuley
|
|
||||||
floglin
|
|
||||||
@all:
|
|
||||||
blingo
|
|
||||||
no but like actually blingo shmingo
|
|
||||||
i would prefer not to
|
|
||||||
@default:
|
|
||||||
thank you for using ekho
|
|
||||||
@@ -1,15 +1,13 @@
|
|||||||
// =============================================================================
|
// =============================================================================
|
||||||
// Auth: alex
|
// Auth: alex
|
||||||
// File: server.go
|
// File: server.go
|
||||||
// Revn: 05-12-2026 2.1.2
|
// Revn: 09-15-2026 3.0.2
|
||||||
// Func: host MOTD connection
|
// Func: host MOTD connection
|
||||||
//
|
//
|
||||||
// TODO:
|
// TODO: add user-specific prints
|
||||||
// add user-specific prints
|
|
||||||
// write usage and errors to logfile
|
// write usage and errors to logfile
|
||||||
// remove ioutil import
|
// remove ioutil import
|
||||||
// send logfile to client on request
|
// send logfile to client on request
|
||||||
// add color printing to qfile
|
|
||||||
// add flags?
|
// add flags?
|
||||||
// add more /codes/ to handle
|
// add more /codes/ to handle
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
@@ -51,6 +49,11 @@
|
|||||||
// line bug in /la/ AGAIN
|
// line bug in /la/ AGAIN
|
||||||
// 05-11-2026: updated fread() to handle users in qfile
|
// 05-11-2026: updated fread() to handle users in qfile
|
||||||
// 05-12-2026: added v and vv input flags
|
// 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
|
||||||
|
// 09-12-2026: minor comments in receive()
|
||||||
|
//*09-15-2026: fixed OOB error in /qa/ for perhaps the last time
|
||||||
|
// fixed bad Atoi conversion bug for perhaps the last time
|
||||||
//
|
//
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
||||||
@@ -121,14 +124,9 @@ func fread() string {
|
|||||||
// bugs where a blank item was introduced into qlist and calling
|
// bugs where a blank item was introduced into qlist and calling
|
||||||
// /la/ would produce a blank line on the client side that didn't
|
// /la/ would produce a blank line on the client side that didn't
|
||||||
// really exist
|
// really exist
|
||||||
qfile = qfile[:len( qfile ) - 1]
|
qfile = qfile[:len( qfile )]
|
||||||
// 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" )
|
||||||
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,9 +143,9 @@ func writeFile( f string ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// handle connections ( new and improved )
|
// receive from connection, send to get processed, send out response
|
||||||
func handle( conn net.Conn ) {
|
func receive( conn net.Conn ) {
|
||||||
|
|
||||||
// create buffer to hold read message
|
// create buffer to hold read message
|
||||||
buffer := make( []byte, tsize )
|
buffer := make( []byte, tsize )
|
||||||
//var buffer [256]byte
|
//var buffer [256]byte
|
||||||
@@ -155,20 +153,29 @@ 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] ), "@" )
|
||||||
|
|
||||||
|
// if command is to end transaction
|
||||||
|
if command[0] == "te" {
|
||||||
|
conn.Close() // end transaction
|
||||||
|
return // and bail
|
||||||
|
}
|
||||||
|
|
||||||
|
// process command get response
|
||||||
|
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
|
// declare response string
|
||||||
var resp string
|
var resp string
|
||||||
switch command[0] { // switch/case over opcode
|
switch command[0] { // switch/case over opcode
|
||||||
@@ -179,20 +186,9 @@ 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( subcommands[0] )
|
num, nerr := strconv.Atoi( command[1] )
|
||||||
// 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
|
||||||
@@ -200,7 +196,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[name] ):
|
case num > len( qlist ):
|
||||||
// bad answer in quote request
|
// bad answer in quote request
|
||||||
resp = "ba@qr@>"
|
resp = "ba@qr@>"
|
||||||
// why bad? num is >
|
// why bad? num is >
|
||||||
@@ -210,7 +206,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[name][num] // quote answer
|
resp = "qa@" + qlist[num] // quote answer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "lr": // /lr/, list request
|
case "lr": // /lr/, list request
|
||||||
@@ -252,11 +248,83 @@ 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["f"] = append( qlist["f"], command[1] )
|
qlist = append( qlist, 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 )
|
||||||
resp = "aa@success" // add answer
|
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
|
case "r1": // /r1/, remove request 1
|
||||||
resp = "r1@" // begin crafting response
|
resp = "r1@" // begin crafting response
|
||||||
// file too big for single transmission
|
// file too big for single transmission
|
||||||
@@ -319,14 +387,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["f"][num] = qlist["f"][len( qlist ) - 1]
|
qlist[num] = qlist[len( qlist ) - 1]
|
||||||
// remove second copy of quote
|
// 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
|
// 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["f"] {
|
for _, v := range qlist {
|
||||||
// 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"
|
||||||
@@ -349,25 +417,20 @@ func handle( conn net.Conn ) {
|
|||||||
case "te": // /te/, transaction end
|
case "te": // /te/, transaction end
|
||||||
// TODO should I be sending something?
|
// TODO should I be sending something?
|
||||||
// TODO is it better to close now or outside of handle()
|
// TODO is it better to close now or outside of handle()
|
||||||
conn.Close() // close connection and leave
|
//conn.Close() // close connection and leave
|
||||||
return // return from handle(), finish connection
|
return "" // return from handle(), finish connection
|
||||||
default: // opcode not recognized
|
default: // opcode not recognized
|
||||||
// TODO there should be more being down, here, right?
|
// 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
|
return resp
|
||||||
_, err = conn.Write( []byte( resp ) )
|
|
||||||
checkN( err, conn ) // make sure write worked
|
|
||||||
|
|
||||||
handle( conn ) // call handle() again to get response from client
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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
|
var VERS string // version
|
||||||
@@ -391,7 +454,7 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// FIXME get better versioning method
|
// FIXME get better versioning method
|
||||||
VERS = "2.1.2"
|
VERS = "3.0.2"
|
||||||
|
|
||||||
if vv { // display info and exit
|
if vv { // display info and exit
|
||||||
fmt.Println( "ekho server v" + VERS )
|
fmt.Println( "ekho server v" + VERS )
|
||||||
@@ -427,7 +490,7 @@ func main() {
|
|||||||
// wait, create connection when found
|
// wait, create connection when found
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
check( err ) // make sure connection works
|
check( err ) // make sure connection works
|
||||||
go handle( conn ) // handle connection
|
go receive( conn ) // handle connection
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user