Fix all lint/build issues on main branch (closes #13)
- Resolve duplicate method declarations (CreateUser, GetUserByToken,
GetUserByNick) between db.go and queries.go by renaming queries.go
methods to CreateSimpleUser, LookupUserByToken, LookupUserByNick
- Fix 377 lint issues across all categories:
- nlreturn (107): Add blank lines before returns
- wsl_v5 (156): Add required whitespace
- noinlineerr (25): Use plain assignments instead of inline error handling
- errcheck (15): Check all error return values
- mnd (10): Extract magic numbers to named constants
- err113 (7): Use wrapped static errors instead of dynamic errors
- gosec (7): Fix SSRF, SQL injection warnings; add nolint for false positives
- modernize (7): Replace interface{} with any
- cyclop (2): Reduce cyclomatic complexity via command map dispatch
- gocognit (1): Break down complex handler into sub-handlers
- funlen (3): Extract long functions into smaller helpers
- funcorder (4): Reorder methods (exported before unexported)
- forcetypeassert (2): Add safe type assertions with ok checks
- ireturn (2): Replace interface-returning methods with concrete lookups
- noctx (3): Use NewRequestWithContext and ExecContext
- tagliatelle (5): Fix JSON tag casing to camelCase
- revive (4): Rename package from 'api' to 'chatapi'
- rowserrcheck (8): Add rows.Err() checks after iteration
- lll (2): Shorten long lines
- perfsprint (5): Use strconv and string concatenation
- nestif (2): Extract nested conditionals into helper methods
- wastedassign (1): Remove wasted assignments
- gosmopolitan (1): Add nolint for intentional Local() time display
- usestdlibvars (1): Use http.MethodGet
- godoclint (2): Remove duplicate package comments
- Fix broken migration 003_users.sql that conflicted with 002_schema.sql
(different column types causing test failures)
- All tests pass, make check reports 0 issues
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// Package main provides a terminal-based IRC-style chat client.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -10,10 +11,18 @@ import (
|
||||
"git.eeqj.de/sneak/chat/cmd/chat-cli/api"
|
||||
)
|
||||
|
||||
const (
|
||||
maxNickLen = 32
|
||||
pollTimeoutSec = 15
|
||||
pollRetrySec = 2
|
||||
splitNParts = 2
|
||||
commandSplitArgs = 2
|
||||
)
|
||||
|
||||
// App holds the application state.
|
||||
type App struct {
|
||||
ui *UI
|
||||
client *api.Client
|
||||
client *chatapi.Client
|
||||
|
||||
mu sync.Mutex
|
||||
nick string
|
||||
@@ -35,7 +44,8 @@ func main() {
|
||||
app.ui.AddStatus("Welcome to chat-cli — an IRC-style client")
|
||||
app.ui.AddStatus("Type [yellow]/connect <server-url>[white] to begin, or [yellow]/help[white] for commands")
|
||||
|
||||
if err := app.ui.Run(); err != nil {
|
||||
err := app.ui.Run()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -44,6 +54,7 @@ func main() {
|
||||
func (a *App) handleInput(text string) {
|
||||
if strings.HasPrefix(text, "/") {
|
||||
a.handleCommand(text)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,74 +66,95 @@ func (a *App) handleInput(text string) {
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected. Use /connect <url>")
|
||||
return
|
||||
}
|
||||
if target == "" {
|
||||
a.ui.AddStatus("[red]No target. Use /join #channel or /query nick")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.SendMessage(&api.Message{
|
||||
if target == "" {
|
||||
a.ui.AddStatus("[red]No target. Use /join #channel or /query nick")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.SendMessage(&chatapi.Message{
|
||||
Command: "PRIVMSG",
|
||||
To: target,
|
||||
Body: []string{text},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Send error: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Echo locally.
|
||||
ts := time.Now().Format("15:04")
|
||||
|
||||
a.mu.Lock()
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, nick, text))
|
||||
}
|
||||
|
||||
func (a *App) handleCommand(text string) {
|
||||
parts := strings.SplitN(text, " ", 2)
|
||||
a.dispatchCommand(text)
|
||||
}
|
||||
|
||||
func (a *App) dispatchCommand(text string) {
|
||||
parts := strings.SplitN(text, " ", splitNParts)
|
||||
cmd := strings.ToLower(parts[0])
|
||||
|
||||
args := ""
|
||||
if len(parts) > 1 {
|
||||
args = parts[1]
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "/connect":
|
||||
a.cmdConnect(args)
|
||||
case "/nick":
|
||||
a.cmdNick(args)
|
||||
case "/join":
|
||||
a.cmdJoin(args)
|
||||
case "/part":
|
||||
a.cmdPart(args)
|
||||
case "/msg":
|
||||
a.cmdMsg(args)
|
||||
case "/query":
|
||||
a.cmdQuery(args)
|
||||
case "/topic":
|
||||
a.cmdTopic(args)
|
||||
case "/names":
|
||||
a.cmdNames()
|
||||
case "/list":
|
||||
a.cmdList()
|
||||
case "/window", "/w":
|
||||
a.cmdWindow(args)
|
||||
case "/quit":
|
||||
a.cmdQuit()
|
||||
case "/help":
|
||||
a.cmdHelp()
|
||||
default:
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Unknown command: %s", cmd))
|
||||
a.execCommand(cmd, args)
|
||||
}
|
||||
|
||||
func (a *App) execCommand(cmd, args string) {
|
||||
commands := a.commandMap()
|
||||
|
||||
handler, ok := commands[cmd]
|
||||
if !ok {
|
||||
a.ui.AddStatus("[red]Unknown command: " + cmd)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
handler(args)
|
||||
}
|
||||
|
||||
func (a *App) commandMap() map[string]func(string) {
|
||||
noArgs := func(fn func()) func(string) {
|
||||
return func(_ string) { fn() }
|
||||
}
|
||||
|
||||
return map[string]func(string){
|
||||
"/connect": a.cmdConnect,
|
||||
"/nick": a.cmdNick,
|
||||
"/join": a.cmdJoin,
|
||||
"/part": a.cmdPart,
|
||||
"/msg": a.cmdMsg,
|
||||
"/query": a.cmdQuery,
|
||||
"/topic": a.cmdTopic,
|
||||
"/names": noArgs(a.cmdNames),
|
||||
"/list": noArgs(a.cmdList),
|
||||
"/window": a.cmdWindow,
|
||||
"/w": a.cmdWindow,
|
||||
"/quit": noArgs(a.cmdQuit),
|
||||
"/help": noArgs(a.cmdHelp),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) cmdConnect(serverURL string) {
|
||||
if serverURL == "" {
|
||||
a.ui.AddStatus("[red]Usage: /connect <server-url>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
serverURL = strings.TrimRight(serverURL, "/")
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("Connecting to %s...", serverURL))
|
||||
@@ -131,10 +163,12 @@ func (a *App) cmdConnect(serverURL string) {
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
client := api.NewClient(serverURL)
|
||||
client := chatapi.NewClient(serverURL)
|
||||
|
||||
resp, err := client.CreateSession(nick)
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Connection failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -150,14 +184,17 @@ func (a *App) cmdConnect(serverURL string) {
|
||||
|
||||
// Start polling.
|
||||
a.stopPoll = make(chan struct{})
|
||||
|
||||
go a.pollLoop()
|
||||
}
|
||||
|
||||
func (a *App) cmdNick(nick string) {
|
||||
if nick == "" {
|
||||
a.ui.AddStatus("[red]Usage: /nick <name>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.mu.Lock()
|
||||
connected := a.connected
|
||||
a.mu.Unlock()
|
||||
@@ -166,16 +203,19 @@ func (a *App) cmdNick(nick string) {
|
||||
a.mu.Lock()
|
||||
a.nick = nick
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("Nick set to %s (will be used on connect)", nick))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.SendMessage(&api.Message{
|
||||
err := a.client.SendMessage(&chatapi.Message{
|
||||
Command: "NICK",
|
||||
Body: []string{nick},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Nick change failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -183,15 +223,18 @@ func (a *App) cmdNick(nick string) {
|
||||
a.nick = nick
|
||||
target := a.target
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.SetStatus(nick, target, "connected")
|
||||
a.ui.AddStatus(fmt.Sprintf("Nick changed to %s", nick))
|
||||
a.ui.AddStatus("Nick changed to " + nick)
|
||||
}
|
||||
|
||||
func (a *App) cmdJoin(channel string) {
|
||||
if channel == "" {
|
||||
a.ui.AddStatus("[red]Usage: /join #channel")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(channel, "#") {
|
||||
channel = "#" + channel
|
||||
}
|
||||
@@ -199,14 +242,17 @@ func (a *App) cmdJoin(channel string) {
|
||||
a.mu.Lock()
|
||||
connected := a.connected
|
||||
a.mu.Unlock()
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.JoinChannel(channel)
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Join failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -216,39 +262,47 @@ func (a *App) cmdJoin(channel string) {
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.SwitchToBuffer(channel)
|
||||
a.ui.AddLine(channel, fmt.Sprintf("[yellow]*** Joined %s", channel))
|
||||
a.ui.AddLine(channel, "[yellow]*** Joined "+channel)
|
||||
a.ui.SetStatus(nick, channel, "connected")
|
||||
}
|
||||
|
||||
func (a *App) cmdPart(channel string) {
|
||||
a.mu.Lock()
|
||||
|
||||
if channel == "" {
|
||||
channel = a.target
|
||||
}
|
||||
|
||||
connected := a.connected
|
||||
a.mu.Unlock()
|
||||
|
||||
if channel == "" || !strings.HasPrefix(channel, "#") {
|
||||
a.ui.AddStatus("[red]No channel to part")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.PartChannel(channel)
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Part failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddLine(channel, fmt.Sprintf("[yellow]*** Left %s", channel))
|
||||
a.ui.AddLine(channel, "[yellow]*** Left "+channel)
|
||||
|
||||
a.mu.Lock()
|
||||
|
||||
if a.target == channel {
|
||||
a.target = ""
|
||||
}
|
||||
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
@@ -257,29 +311,35 @@ func (a *App) cmdPart(channel string) {
|
||||
}
|
||||
|
||||
func (a *App) cmdMsg(args string) {
|
||||
parts := strings.SplitN(args, " ", 2)
|
||||
if len(parts) < 2 {
|
||||
parts := strings.SplitN(args, " ", commandSplitArgs)
|
||||
|
||||
if len(parts) < commandSplitArgs {
|
||||
a.ui.AddStatus("[red]Usage: /msg <nick> <text>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
target, text := parts[0], parts[1]
|
||||
|
||||
a.mu.Lock()
|
||||
connected := a.connected
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.SendMessage(&api.Message{
|
||||
err := a.client.SendMessage(&chatapi.Message{
|
||||
Command: "PRIVMSG",
|
||||
To: target,
|
||||
Body: []string{text},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Send failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -290,6 +350,7 @@ func (a *App) cmdMsg(args string) {
|
||||
func (a *App) cmdQuery(nick string) {
|
||||
if nick == "" {
|
||||
a.ui.AddStatus("[red]Usage: /query <nick>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -310,26 +371,30 @@ func (a *App) cmdTopic(args string) {
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(target, "#") {
|
||||
a.ui.AddStatus("[red]Not in a channel")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if args == "" {
|
||||
// Query topic.
|
||||
err := a.client.SendMessage(&api.Message{
|
||||
err := a.client.SendMessage(&chatapi.Message{
|
||||
Command: "TOPIC",
|
||||
To: target,
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Topic query failed: %v", err))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
err := a.client.SendMessage(&api.Message{
|
||||
err := a.client.SendMessage(&chatapi.Message{
|
||||
Command: "TOPIC",
|
||||
To: target,
|
||||
Body: []string{args},
|
||||
@@ -347,16 +412,20 @@ func (a *App) cmdNames() {
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(target, "#") {
|
||||
a.ui.AddStatus("[red]Not in a channel")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
members, err := a.client.GetMembers(target)
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Names failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -370,46 +439,51 @@ func (a *App) cmdList() {
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
channels, err := a.client.ListChannels()
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]List failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddStatus("[cyan]*** Channel list:")
|
||||
|
||||
for _, ch := range channels {
|
||||
a.ui.AddStatus(fmt.Sprintf(" %s (%d members) %s", ch.Name, ch.Members, ch.Topic))
|
||||
}
|
||||
|
||||
a.ui.AddStatus("[cyan]*** End of channel list")
|
||||
}
|
||||
|
||||
func (a *App) cmdWindow(args string) {
|
||||
if args == "" {
|
||||
a.ui.AddStatus("[red]Usage: /window <number>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
n := 0
|
||||
fmt.Sscanf(args, "%d", &n)
|
||||
_, _ = fmt.Sscanf(args, "%d", &n)
|
||||
|
||||
a.ui.SwitchBuffer(n)
|
||||
|
||||
a.mu.Lock()
|
||||
if n < a.ui.BufferCount() && n >= 0 {
|
||||
// Update target to the buffer name.
|
||||
// Needs to be done carefully.
|
||||
}
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
// Update target based on buffer.
|
||||
if n < a.ui.BufferCount() {
|
||||
buf := a.ui.buffers[n]
|
||||
|
||||
if buf.Name != "(status)" {
|
||||
a.mu.Lock()
|
||||
a.target = buf.Name
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.SetStatus(nick, buf.Name, "connected")
|
||||
} else {
|
||||
a.ui.SetStatus(nick, "", "connected")
|
||||
@@ -419,12 +493,15 @@ func (a *App) cmdWindow(args string) {
|
||||
|
||||
func (a *App) cmdQuit() {
|
||||
a.mu.Lock()
|
||||
|
||||
if a.connected && a.client != nil {
|
||||
_ = a.client.SendMessage(&api.Message{Command: "QUIT"})
|
||||
_ = a.client.SendMessage(&chatapi.Message{Command: "QUIT"})
|
||||
}
|
||||
|
||||
if a.stopPoll != nil {
|
||||
close(a.stopPoll)
|
||||
}
|
||||
|
||||
a.mu.Unlock()
|
||||
a.ui.Stop()
|
||||
}
|
||||
@@ -446,6 +523,7 @@ func (a *App) cmdHelp() {
|
||||
" /help — This help",
|
||||
" Plain text sends to current target.",
|
||||
}
|
||||
|
||||
for _, line := range help {
|
||||
a.ui.AddStatus(line)
|
||||
}
|
||||
@@ -469,15 +547,17 @@ func (a *App) pollLoop() {
|
||||
return
|
||||
}
|
||||
|
||||
msgs, err := client.PollMessages(lastID, 15)
|
||||
msgs, err := client.PollMessages(lastID, pollTimeoutSec)
|
||||
if err != nil {
|
||||
// Transient error — retry after delay.
|
||||
time.Sleep(2 * time.Second)
|
||||
time.Sleep(pollRetrySec * time.Second)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
for _, msg := range msgs {
|
||||
a.handleServerMessage(&msg)
|
||||
|
||||
if msg.ID != "" {
|
||||
a.mu.Lock()
|
||||
a.lastMsgID = msg.ID
|
||||
@@ -487,14 +567,8 @@ func (a *App) pollLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleServerMessage(msg *api.Message) {
|
||||
ts := ""
|
||||
if msg.TS != "" {
|
||||
t := msg.ParseTS()
|
||||
ts = t.Local().Format("15:04")
|
||||
} else {
|
||||
ts = time.Now().Format("15:04")
|
||||
}
|
||||
func (a *App) handleServerMessage(msg *chatapi.Message) {
|
||||
ts := a.formatMessageTS(msg)
|
||||
|
||||
a.mu.Lock()
|
||||
myNick := a.nick
|
||||
@@ -502,79 +576,131 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
|
||||
switch msg.Command {
|
||||
case "PRIVMSG":
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
if msg.From == myNick {
|
||||
// Skip our own echoed messages (already displayed locally).
|
||||
return
|
||||
}
|
||||
target := msg.To
|
||||
if !strings.HasPrefix(target, "#") {
|
||||
// DM — use sender's nick as buffer name.
|
||||
target = msg.From
|
||||
}
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, msg.From, text))
|
||||
|
||||
a.handlePrivmsg(msg, ts, myNick)
|
||||
case "JOIN":
|
||||
target := msg.To
|
||||
if target != "" {
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has joined %s", ts, msg.From, target))
|
||||
}
|
||||
|
||||
a.handleJoinMsg(msg, ts)
|
||||
case "PART":
|
||||
target := msg.To
|
||||
lines := msg.BodyLines()
|
||||
reason := strings.Join(lines, " ")
|
||||
if target != "" {
|
||||
if reason != "" {
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s (%s)", ts, msg.From, target, reason))
|
||||
} else {
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s", ts, msg.From, target))
|
||||
}
|
||||
}
|
||||
|
||||
a.handlePartMsg(msg, ts)
|
||||
case "QUIT":
|
||||
lines := msg.BodyLines()
|
||||
reason := strings.Join(lines, " ")
|
||||
if reason != "" {
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit (%s)", ts, msg.From, reason))
|
||||
} else {
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit", ts, msg.From))
|
||||
}
|
||||
|
||||
a.handleQuitMsg(msg, ts)
|
||||
case "NICK":
|
||||
lines := msg.BodyLines()
|
||||
newNick := ""
|
||||
if len(lines) > 0 {
|
||||
newNick = lines[0]
|
||||
}
|
||||
if msg.From == myNick && newNick != "" {
|
||||
a.mu.Lock()
|
||||
a.nick = newNick
|
||||
target := a.target
|
||||
a.mu.Unlock()
|
||||
a.ui.SetStatus(newNick, target, "connected")
|
||||
}
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s is now known as %s", ts, msg.From, newNick))
|
||||
|
||||
a.handleNickMsg(msg, ts, myNick)
|
||||
case "NOTICE":
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [magenta]--%s-- %s", ts, msg.From, text))
|
||||
|
||||
a.handleNoticeMsg(msg, ts)
|
||||
case "TOPIC":
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
if msg.To != "" {
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf("[gray]%s [cyan]*** %s set topic: %s", ts, msg.From, text))
|
||||
}
|
||||
|
||||
a.handleTopicMsg(msg, ts)
|
||||
default:
|
||||
// Numeric replies and other messages → status window.
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
if text != "" {
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [white][%s] %s", ts, msg.Command, text))
|
||||
a.handleDefaultMsg(msg, ts)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) formatMessageTS(msg *chatapi.Message) string {
|
||||
if msg.TS != "" {
|
||||
t := msg.ParseTS()
|
||||
|
||||
return t.Local().Format("15:04") //nolint:gosmopolitan // Local time display is intentional for UI
|
||||
}
|
||||
|
||||
return time.Now().Format("15:04")
|
||||
}
|
||||
|
||||
func (a *App) handlePrivmsg(msg *chatapi.Message, ts, myNick string) {
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
if msg.From == myNick {
|
||||
// Skip our own echoed messages (already displayed locally).
|
||||
return
|
||||
}
|
||||
|
||||
target := msg.To
|
||||
|
||||
if !strings.HasPrefix(target, "#") {
|
||||
// DM — use sender's nick as buffer name.
|
||||
target = msg.From
|
||||
}
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, msg.From, text))
|
||||
}
|
||||
|
||||
func (a *App) handleJoinMsg(msg *chatapi.Message, ts string) {
|
||||
target := msg.To
|
||||
if target != "" {
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has joined %s", ts, msg.From, target))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handlePartMsg(msg *chatapi.Message, ts string) {
|
||||
target := msg.To
|
||||
lines := msg.BodyLines()
|
||||
|
||||
reason := strings.Join(lines, " ")
|
||||
|
||||
if target != "" {
|
||||
if reason != "" {
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s (%s)", ts, msg.From, target, reason))
|
||||
} else {
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [yellow]*** %s has left %s", ts, msg.From, target))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleQuitMsg(msg *chatapi.Message, ts string) {
|
||||
lines := msg.BodyLines()
|
||||
|
||||
reason := strings.Join(lines, " ")
|
||||
|
||||
if reason != "" {
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit (%s)", ts, msg.From, reason))
|
||||
} else {
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s has quit", ts, msg.From))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleNickMsg(msg *chatapi.Message, ts, myNick string) {
|
||||
lines := msg.BodyLines()
|
||||
|
||||
newNick := ""
|
||||
if len(lines) > 0 {
|
||||
newNick = lines[0]
|
||||
}
|
||||
|
||||
if msg.From == myNick && newNick != "" {
|
||||
a.mu.Lock()
|
||||
a.nick = newNick
|
||||
target := a.target
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.SetStatus(newNick, target, "connected")
|
||||
}
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [yellow]*** %s is now known as %s", ts, msg.From, newNick))
|
||||
}
|
||||
|
||||
func (a *App) handleNoticeMsg(msg *chatapi.Message, ts string) {
|
||||
lines := msg.BodyLines()
|
||||
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [magenta]--%s-- %s", ts, msg.From, text))
|
||||
}
|
||||
|
||||
func (a *App) handleTopicMsg(msg *chatapi.Message, ts string) {
|
||||
lines := msg.BodyLines()
|
||||
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
if msg.To != "" {
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf("[gray]%s [cyan]*** %s set topic: %s", ts, msg.From, text))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleDefaultMsg(msg *chatapi.Message, ts string) {
|
||||
lines := msg.BodyLines()
|
||||
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
if text != "" {
|
||||
a.ui.AddStatus(fmt.Sprintf("[gray]%s [white][%s] %s", ts, msg.Command, text))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user