3 Commits

Author SHA1 Message Date
oracle 3e9e17323d rewrote quoteAnswer 2026-09-12 23:02:44 -04:00
oracle 5b0ef3e596 minor comments 2026-09-12 23:01:21 -04:00
oracle 5f7ce723ae added fatih/color 2026-09-12 22:55:07 -04:00
4 changed files with 114 additions and 31 deletions
+86 -14
View File
@@ -1,7 +1,7 @@
// ============================================================================= // =============================================================================
// Auth: alex // Auth: alex
// File: client.go // File: client.go
// Revn: 09-06-2026 3.0.0 // Revn: 09-12-2026 3.0.2
// 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
@@ -43,23 +43,28 @@
// 09-04-2026: rewrote in() with bufio instead of scanln // 09-04-2026: rewrote in() with bufio instead of scanln
//#09-06-2026: fixed blank message error in secondEdit() //#09-06-2026: fixed blank message error in secondEdit()
// comments // comments
// 09-07-2026: added fatih/color import
// rewrote quoteAnswer() to handle coloring escape
// sequences
// 09-12-2026: commented quoteAnswer()
// //
// ============================================================================= // =============================================================================
package main package main
import ( import (
"bufio" // NewReader, ReadString "bufio" // NewReader, ReadString
"io" // io.EOF "io" // io.EOF
"io/ioutil" // ReadFile "io/ioutil" // ReadFile
"flag" // BoolVar, StringVar, Parse "flag" // BoolVar, StringVar, Parse
"fmt" // Println "fmt" // Println
"math/rand" // NewSource, New, Intn, *rand.Rand "math/rand" // NewSource, New, Intn, *rand.Rand
"net" // ResolveTCPAddr, DialTCP, conn.Write,Read "net" // ResolveTCPAddr, DialTCP, conn.Write,Read
"os" // Exit, Create, Stdin "os" // Exit, Create, Stdin
"strconv" // Atoi, Itoa "strconv" // Atoi, Itoa
"strings" // Split "strings" // Split
"time" // Now, UnixNano "time" // Now, UnixNano
"github.com/fatih/color" // color.New, Printf, Println
) )
@@ -169,6 +174,74 @@ func sizeAnswer( subcom []string ) string {
// receive quote, print // receive quote, print
func quoteAnswer( subcom []string ) string { 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 fmt.Println( subcom[0] ) // whole-ass just print quote
return "te@success" // set transaction end message return "te@success" // set transaction end message
} }
@@ -480,7 +553,6 @@ func handle( conn net.Conn ) {
//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 )
@@ -507,7 +579,7 @@ func main() {
flag.Parse() flag.Parse()
// FIXME get better versioning method // FIXME get better versioning method
VERS = "3.0.0" VERS = "3.0.2"
if vv { // display info and exit if vv { // display info and exit
fmt.Println( "ekho client v" + VERS ) fmt.Println( "ekho client v" + VERS )
+11
View File
@@ -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
)
+9
View File
@@ -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=
+7 -16
View File
@@ -1,7 +1,7 @@
// ============================================================================= // =============================================================================
// Auth: alex // Auth: alex
// File: server.go // File: server.go
// Revn: 09-06-2026 3.0.0 // Revn: 09-12-2026 3.0.1
// Func: host MOTD connection // Func: host MOTD connection
// //
// TODO: add user-specific prints // TODO: add user-specific prints
@@ -52,6 +52,7 @@
// 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-05-2026: buncha shit happened, dropped the commit
//#09-06-2026: added /e1/, /e2/, and /cr/, got edit working //#09-06-2026: added /e1/, /e2/, and /cr/, got edit working
// 09-12-2026: minor comments in receive()
// //
// ============================================================================= // =============================================================================
@@ -151,25 +152,16 @@ func receive( 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" { if command[0] == "te" {
conn.Close() conn.Close() // end transaction
return return // and bail
} }
// process command get response
resp := handle( command ) resp := handle( command )
// write response to client // write response to client
@@ -438,7 +430,6 @@ func handle( command []string ) string {
// 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
@@ -462,7 +453,7 @@ func main() {
flag.Parse() flag.Parse()
// FIXME get better versioning method // FIXME get better versioning method
VERS = "3.0.0" VERS = "3.0.1"
if vv { // display info and exit if vv { // display info and exit
fmt.Println( "ekho server v" + VERS ) fmt.Println( "ekho server v" + VERS )