Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b992353b02 | |||
| 0a889f38d4 | |||
| 4bf639f080 | |||
| a10b632f9d | |||
| 3e9e17323d | |||
| 5b0ef3e596 | |||
| 5f7ce723ae | |||
| bba0bc1a29 | |||
| e5a7257758 | |||
| f062efc176 | |||
| bf95cadeac |
@@ -65,7 +65,11 @@ The client uses the following flags
|
||||
-ip <ip:port> - specify alternate server destination
|
||||
-l - list all quotes
|
||||
-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
|
||||
-v - display version and exit
|
||||
-vv - display version and about info and exit
|
||||
```
|
||||
The server uses the following flags
|
||||
``` sh
|
||||
@@ -79,8 +83,6 @@ The server uses the following flags
|
||||
- Log all uses/transaction data each day
|
||||
- Request, send, and receive the logfile
|
||||
- User-specific quotes/quotefiles
|
||||
- Colored printing and coloring quotes
|
||||
- Edit quotes
|
||||
|
||||
[golang]: <https://go.dev/doc/install>
|
||||
[just]: <https://github.com/casey/just>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// =============================================================================
|
||||
// Auth: alex
|
||||
// File: client.go
|
||||
// Revn: 09-06-2026 3.0.0
|
||||
// Revn: 09-16-2026 4.0.0
|
||||
// Func: ask server for a message of the day quote
|
||||
//
|
||||
// TODO: remove ioutil import
|
||||
@@ -43,23 +43,34 @@
|
||||
// 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
|
||||
|
||||
import (
|
||||
"bufio" // NewReader, ReadString
|
||||
"io" // io.EOF
|
||||
"io/ioutil" // ReadFile
|
||||
"flag" // BoolVar, StringVar, Parse
|
||||
"fmt" // Println
|
||||
"math/rand" // NewSource, New, Intn, *rand.Rand
|
||||
"net" // ResolveTCPAddr, DialTCP, conn.Write,Read
|
||||
"os" // Exit, Create, Stdin
|
||||
"strconv" // Atoi, Itoa
|
||||
"strings" // Split
|
||||
"time" // Now, UnixNano
|
||||
"bufio" // NewReader, ReadString
|
||||
"io" // io.EOF
|
||||
"io/ioutil" // ReadFile
|
||||
"flag" // BoolVar, StringVar, Parse
|
||||
"fmt" // Println
|
||||
"math/rand" // NewSource, New, Intn, *rand.Rand
|
||||
"net" // ResolveTCPAddr, DialTCP, conn.Write,Read
|
||||
"os" // Exit, Create, Stdin
|
||||
"strconv" // Atoi, Itoa
|
||||
"strings" // Split
|
||||
"time" // Now, UnixNano
|
||||
"github.com/fatih/color" // color.New, Printf, Println
|
||||
)
|
||||
|
||||
|
||||
@@ -169,7 +180,75 @@ func sizeAnswer( subcom []string ) string {
|
||||
|
||||
// receive quote, print
|
||||
func quoteAnswer( subcom []string ) string {
|
||||
fmt.Println( subcom[0] ) // whole-ass just print quote
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -243,7 +322,8 @@ func listAnswer( subcom []string ) string {
|
||||
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 )
|
||||
fmt.Print( p + 1, "\t" )
|
||||
quoteAnswer( []string{ v } )
|
||||
}
|
||||
resp = "te@success" // set transaction end message
|
||||
}
|
||||
@@ -315,16 +395,18 @@ func secondEdit( subcom []string ) string {
|
||||
return "te@success"
|
||||
}
|
||||
|
||||
// edit command is a removal and an add
|
||||
// edit command is an add and then a removal
|
||||
var resp string
|
||||
resp = "cr@r2@" + subcom[0] + "@ar@"
|
||||
// server response is text to be edited
|
||||
resp = "cr@ar@"
|
||||
|
||||
fmt.Println( "as is: " + subcom[1] )
|
||||
fmt.Print( "preview: " ) // won't be useful until pretty print
|
||||
fmt.Print( "preview: " ) // show raw of current quote
|
||||
_ = quoteAnswer( subcom[1:] ) // display quote properly
|
||||
|
||||
// get user input for new quote and send
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -480,12 +562,12 @@ func handle( conn net.Conn ) {
|
||||
//var qsize int // keep track of num of quotes
|
||||
var conn net.Conn // keep track of connection
|
||||
|
||||
|
||||
// global flag variables
|
||||
var add bool // add new quotes to quote file ( on server )
|
||||
var remove bool // remove quotes from quote file ( on 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 tsize int // size of transmission buffer
|
||||
var VERS string // version
|
||||
@@ -496,18 +578,19 @@ func main() {
|
||||
var vv bool // big version flag
|
||||
|
||||
// define flags
|
||||
flag.BoolVar( &add, "a", false, "add quote to list" )
|
||||
flag.BoolVar( &remove, "r", false, "remove quote from list" )
|
||||
flag.BoolVar( &list, "l", false, "print quote list" )
|
||||
flag.BoolVar( &edit, "e", false, "edit quote from list" )
|
||||
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.BoolVar( &add, "a", false, "add quote to list" )
|
||||
flag.BoolVar( &remove, "r", false, "remove quote from list" )
|
||||
flag.BoolVar( &list, "l", false, "print quote list" )
|
||||
flag.BoolVar( &edit, "e", false, "edit quote from list" )
|
||||
flag.BoolVar( &co, "c", false, "list color codes with examples" )
|
||||
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 = "3.0.0"
|
||||
VERS = "4.0.0"
|
||||
|
||||
if vv { // display info and exit
|
||||
fmt.Println( "ekho client v" + VERS )
|
||||
@@ -520,6 +603,54 @@ func main() {
|
||||
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
|
||||
|
||||
// create address object
|
||||
|
||||
@@ -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=
|
||||
@@ -1,9 +1 @@
|
||||
@user:
|
||||
shmuley
|
||||
floglin
|
||||
@all:
|
||||
blingo
|
||||
no but like actually blingo shmingo
|
||||
i would prefer not to
|
||||
@default:
|
||||
thank you for using ekho
|
||||
thank you for using $1ekho
|
||||
@@ -1,14 +1,13 @@
|
||||
// =============================================================================
|
||||
// Auth: alex
|
||||
// File: server.go
|
||||
// Revn: 09-06-2026 3.0.0
|
||||
// Revn: 09-15-2026 3.0.2
|
||||
// Func: host MOTD connection
|
||||
//
|
||||
// TODO: add user-specific prints
|
||||
// write usage and errors to logfile
|
||||
// remove ioutil import
|
||||
// send logfile to client on request
|
||||
// add color printing to qfile
|
||||
// add flags?
|
||||
// add more /codes/ to handle
|
||||
// =============================================================================
|
||||
@@ -52,6 +51,9 @@
|
||||
// 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
|
||||
//
|
||||
// =============================================================================
|
||||
|
||||
@@ -151,27 +153,18 @@ func receive( conn net.Conn ) {
|
||||
n, err := conn.Read( buffer[:] )
|
||||
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 @
|
||||
command := strings.Split( string( buffer[:n] ), "@" )
|
||||
|
||||
// if command is to end transaction
|
||||
if command[0] == "te" {
|
||||
conn.Close()
|
||||
return
|
||||
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
|
||||
@@ -195,7 +188,7 @@ func handle( command []string ) string {
|
||||
case "qr": // /qr/, quote request
|
||||
// convert message argument to string to make sure it's a
|
||||
// valid number
|
||||
num, nerr := strconv.Atoi( command[0] )
|
||||
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
|
||||
@@ -213,7 +206,7 @@ func handle( command []string ) string {
|
||||
// why bad? num is -
|
||||
default:
|
||||
// good num, get quote, send off
|
||||
resp = "qa@" + qlist[num - 1] // quote answer
|
||||
resp = "qa@" + qlist[num] // quote answer
|
||||
}
|
||||
}
|
||||
case "lr": // /lr/, list request
|
||||
@@ -438,7 +431,6 @@ func handle( command []string ) string {
|
||||
|
||||
// globals
|
||||
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
|
||||
@@ -462,7 +454,7 @@ func main() {
|
||||
flag.Parse()
|
||||
|
||||
// FIXME get better versioning method
|
||||
VERS = "3.0.0"
|
||||
VERS = "3.0.2"
|
||||
|
||||
if vv { // display info and exit
|
||||
fmt.Println( "ekho server v" + VERS )
|
||||
|
||||
Reference in New Issue
Block a user