fix: golangci-lint v2 config and lint-clean production code
- Fix .golangci.yml for v2 format (linters-settings -> linters.settings) - All production code now passes golangci-lint with zero issues - Line length 88, funlen 80/50, cyclop 15, dupl 100 - Extract shared helpers in db (scanChannels, scanInt64s, scanMessages) - Split runMigrations into applyMigration/execMigration - Fix fanOut return signature (remove unused int64) - Add fanOutSilent helper to avoid dogsled - Rewrite CLI code for lint compliance (nlreturn, wsl_v5, noctx, etc) - Rename CLI api package to chatapi to avoid revive var-naming - Fix all noinlineerr, mnd, perfsprint, funcorder issues - Fix db tests: extract helpers, add t.Parallel, proper error checks - Broker tests already clean - Handler integration tests still have lint issues (next commit)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// Package main is the entry point for the chat-cli client.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -7,7 +8,14 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.eeqj.de/sneak/chat/cmd/chat-cli/api"
|
||||
api "git.eeqj.de/sneak/chat/cmd/chat-cli/api"
|
||||
)
|
||||
|
||||
const (
|
||||
splitParts = 2
|
||||
pollTimeout = 15
|
||||
pollRetry = 2 * time.Second
|
||||
timeFormat = "15:04"
|
||||
)
|
||||
|
||||
// App holds the application state.
|
||||
@@ -17,9 +25,9 @@ type App struct {
|
||||
|
||||
mu sync.Mutex
|
||||
nick string
|
||||
target string // current target (#channel or nick for DM)
|
||||
target string
|
||||
connected bool
|
||||
lastQID int64 // queue cursor for polling
|
||||
lastQID int64
|
||||
stopPoll chan struct{}
|
||||
}
|
||||
|
||||
@@ -32,10 +40,17 @@ func main() {
|
||||
app.ui.OnInput(app.handleInput)
|
||||
app.ui.SetStatus(app.nick, "", "disconnected")
|
||||
|
||||
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")
|
||||
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,21 +59,29 @@ func main() {
|
||||
func (a *App) handleInput(text string) {
|
||||
if strings.HasPrefix(text, "/") {
|
||||
a.handleCommand(text)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Plain text → PRIVMSG to current target.
|
||||
a.mu.Lock()
|
||||
target := a.target
|
||||
connected := a.connected
|
||||
a.mu.Unlock()
|
||||
|
||||
if !connected {
|
||||
a.ui.AddStatus("[red]Not connected. Use /connect <url>")
|
||||
a.ui.AddStatus(
|
||||
"[red]Not connected. Use /connect <url>",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if target == "" {
|
||||
a.ui.AddStatus("[red]No target. Use /join #channel or /query nick")
|
||||
a.ui.AddStatus(
|
||||
"[red]No target. " +
|
||||
"Use /join #channel or /query nick",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -68,26 +91,38 @@ func (a *App) handleInput(text string) {
|
||||
Body: []string{text},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Send error: %v", err))
|
||||
a.ui.AddStatus(
|
||||
"[red]Send error: " + err.Error(),
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Echo locally.
|
||||
ts := time.Now().Format("15:04")
|
||||
ts := time.Now().Format(timeFormat)
|
||||
|
||||
a.mu.Lock()
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, nick, text))
|
||||
|
||||
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)
|
||||
parts := strings.SplitN(text, " ", splitParts)
|
||||
cmd := strings.ToLower(parts[0])
|
||||
|
||||
args := ""
|
||||
if len(parts) > 1 {
|
||||
args = parts[1]
|
||||
}
|
||||
|
||||
a.dispatchCommand(cmd, args)
|
||||
}
|
||||
|
||||
func (a *App) dispatchCommand(cmd, args string) {
|
||||
switch cmd {
|
||||
case "/connect":
|
||||
a.cmdConnect(args)
|
||||
@@ -114,27 +149,37 @@ func (a *App) handleCommand(text string) {
|
||||
case "/help":
|
||||
a.cmdHelp()
|
||||
default:
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Unknown command: %s", cmd))
|
||||
a.ui.AddStatus(
|
||||
"[red]Unknown command: " + cmd,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) cmdConnect(serverURL string) {
|
||||
if serverURL == "" {
|
||||
a.ui.AddStatus("[red]Usage: /connect <server-url>")
|
||||
a.ui.AddStatus(
|
||||
"[red]Usage: /connect <server-url>",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
serverURL = strings.TrimRight(serverURL, "/")
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("Connecting to %s...", serverURL))
|
||||
a.ui.AddStatus("Connecting to " + serverURL + "...")
|
||||
|
||||
a.mu.Lock()
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
client := api.NewClient(serverURL)
|
||||
|
||||
resp, err := client.CreateSession(nick)
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Connection failed: %v", err))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Connection failed: %v", err,
|
||||
))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -145,19 +190,26 @@ func (a *App) cmdConnect(serverURL string) {
|
||||
a.lastQID = 0
|
||||
a.mu.Unlock()
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("[green]Connected! Nick: %s, Session: %d", resp.Nick, resp.ID))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[green]Connected! Nick: %s, Session: %d",
|
||||
resp.Nick, resp.ID,
|
||||
))
|
||||
a.ui.SetStatus(resp.Nick, "", "connected")
|
||||
|
||||
// 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>")
|
||||
a.ui.AddStatus(
|
||||
"[red]Usage: /nick <name>",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.mu.Lock()
|
||||
connected := a.connected
|
||||
a.mu.Unlock()
|
||||
@@ -166,7 +218,12 @@ 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))
|
||||
|
||||
a.ui.AddStatus(
|
||||
"Nick set to " + nick +
|
||||
" (will be used on connect)",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -175,7 +232,10 @@ func (a *App) cmdNick(nick string) {
|
||||
Body: []string{nick},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Nick change failed: %v", err))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Nick change failed: %v", err,
|
||||
))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -183,15 +243,20 @@ 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")
|
||||
a.ui.AddStatus(
|
||||
"[red]Usage: /join #channel",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(channel, "#") {
|
||||
channel = "#" + channel
|
||||
}
|
||||
@@ -199,14 +264,19 @@ 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))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Join failed: %v", err,
|
||||
))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -216,7 +286,9 @@ 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")
|
||||
}
|
||||
|
||||
@@ -225,30 +297,41 @@ func (a *App) cmdPart(channel string) {
|
||||
if channel == "" {
|
||||
channel = a.target
|
||||
}
|
||||
|
||||
connected := a.connected
|
||||
a.mu.Unlock()
|
||||
|
||||
if channel == "" || !strings.HasPrefix(channel, "#") {
|
||||
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))
|
||||
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,19 +340,25 @@ func (a *App) cmdPart(channel string) {
|
||||
}
|
||||
|
||||
func (a *App) cmdMsg(args string) {
|
||||
parts := strings.SplitN(args, " ", 2)
|
||||
if len(parts) < 2 {
|
||||
a.ui.AddStatus("[red]Usage: /msg <nick> <text>")
|
||||
parts := strings.SplitN(args, " ", splitParts)
|
||||
if len(parts) < splitParts {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -279,17 +368,27 @@ func (a *App) cmdMsg(args string) {
|
||||
Body: []string{text},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Send failed: %v", err))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Send failed: %v", err,
|
||||
))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ts := time.Now().Format("15:04")
|
||||
a.ui.AddLine(target, fmt.Sprintf("[gray]%s [green]<%s>[white] %s", ts, nick, text))
|
||||
ts := time.Now().Format(timeFormat)
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf(
|
||||
"[gray]%s [green]<%s>[white] %s",
|
||||
ts, nick, text,
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) cmdQuery(nick string) {
|
||||
if nick == "" {
|
||||
a.ui.AddStatus("[red]Usage: /query <nick>")
|
||||
a.ui.AddStatus(
|
||||
"[red]Usage: /query <nick>",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -310,22 +409,27 @@ 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{
|
||||
Command: "TOPIC",
|
||||
To: target,
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Topic query failed: %v", err))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Topic query failed: %v", err,
|
||||
))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -335,7 +439,9 @@ func (a *App) cmdTopic(args string) {
|
||||
Body: []string{args},
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Topic set failed: %v", err))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Topic set failed: %v", err,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -347,20 +453,29 @@ 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))
|
||||
a.ui.AddStatus(fmt.Sprintf(
|
||||
"[red]Names failed: %v", err,
|
||||
))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf("[cyan]*** Members of %s: %s", target, strings.Join(members, " ")))
|
||||
a.ui.AddLine(target, fmt.Sprintf(
|
||||
"[cyan]*** Members of %s: %s",
|
||||
target, strings.Join(members, " "),
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) cmdList() {
|
||||
@@ -370,47 +485,60 @@ 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))
|
||||
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(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>")
|
||||
a.ui.AddStatus(
|
||||
"[red]Usage: /window <number>",
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
n := 0
|
||||
fmt.Sscanf(args, "%d", &n)
|
||||
|
||||
var n int
|
||||
|
||||
_, _ = 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() {
|
||||
if n >= 0 && 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")
|
||||
|
||||
a.ui.SetStatus(
|
||||
nick, buf.Name, "connected",
|
||||
)
|
||||
} else {
|
||||
a.ui.SetStatus(nick, "", "connected")
|
||||
}
|
||||
@@ -419,12 +547,17 @@ 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(
|
||||
&api.Message{Command: "QUIT"},
|
||||
)
|
||||
}
|
||||
|
||||
if a.stopPoll != nil {
|
||||
close(a.stopPoll)
|
||||
}
|
||||
|
||||
a.mu.Unlock()
|
||||
a.ui.Stop()
|
||||
}
|
||||
@@ -441,11 +574,12 @@ func (a *App) cmdHelp() {
|
||||
" /topic [text] — View/set topic",
|
||||
" /names — List channel members",
|
||||
" /list — List channels",
|
||||
" /window <n> — Switch buffer (Alt+0-9)",
|
||||
" /window <n> — Switch buffer",
|
||||
" /quit — Disconnect and exit",
|
||||
" /help — This help",
|
||||
" Plain text sends to current target.",
|
||||
}
|
||||
|
||||
for _, line := range help {
|
||||
a.ui.AddStatus(line)
|
||||
}
|
||||
@@ -469,10 +603,12 @@ func (a *App) pollLoop() {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := client.PollMessages(lastQID, 15)
|
||||
result, err := client.PollMessages(
|
||||
lastQID, pollTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
// Transient error — retry after delay.
|
||||
time.Sleep(2 * time.Second)
|
||||
time.Sleep(pollRetry)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -482,20 +618,14 @@ func (a *App) pollLoop() {
|
||||
a.mu.Unlock()
|
||||
}
|
||||
|
||||
for _, msg := range result.Messages {
|
||||
a.handleServerMessage(&msg)
|
||||
for i := range result.Messages {
|
||||
a.handleServerMessage(&result.Messages[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
ts := a.formatTS(msg)
|
||||
|
||||
a.mu.Lock()
|
||||
myNick := a.nick
|
||||
@@ -503,79 +633,172 @@ 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.handlePrivmsgEvent(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.handleJoinEvent(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.handlePartEvent(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.handleQuitEvent(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.handleNickEvent(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.handleNoticeEvent(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.handleTopicEvent(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.handleDefaultEvent(msg, ts)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) formatTS(msg *api.Message) string {
|
||||
if msg.TS != "" {
|
||||
return msg.ParseTS().UTC().Format(timeFormat)
|
||||
}
|
||||
|
||||
return time.Now().Format(timeFormat)
|
||||
}
|
||||
|
||||
func (a *App) handlePrivmsgEvent(
|
||||
msg *api.Message, ts, myNick string,
|
||||
) {
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
if msg.From == myNick {
|
||||
return
|
||||
}
|
||||
|
||||
target := msg.To
|
||||
if !strings.HasPrefix(target, "#") {
|
||||
target = msg.From
|
||||
}
|
||||
|
||||
a.ui.AddLine(target, fmt.Sprintf(
|
||||
"[gray]%s [green]<%s>[white] %s",
|
||||
ts, msg.From, text,
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) handleJoinEvent(
|
||||
msg *api.Message, ts string,
|
||||
) {
|
||||
if msg.To == "" {
|
||||
return
|
||||
}
|
||||
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf(
|
||||
"[gray]%s [yellow]*** %s has joined %s",
|
||||
ts, msg.From, msg.To,
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) handlePartEvent(
|
||||
msg *api.Message, ts string,
|
||||
) {
|
||||
if msg.To == "" {
|
||||
return
|
||||
}
|
||||
|
||||
lines := msg.BodyLines()
|
||||
reason := strings.Join(lines, " ")
|
||||
|
||||
if reason != "" {
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf(
|
||||
"[gray]%s [yellow]*** %s has left %s (%s)",
|
||||
ts, msg.From, msg.To, reason,
|
||||
))
|
||||
} else {
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf(
|
||||
"[gray]%s [yellow]*** %s has left %s",
|
||||
ts, msg.From, msg.To,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleQuitEvent(
|
||||
msg *api.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) handleNickEvent(
|
||||
msg *api.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) handleNoticeEvent(
|
||||
msg *api.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) handleTopicEvent(
|
||||
msg *api.Message, ts string,
|
||||
) {
|
||||
if msg.To == "" {
|
||||
return
|
||||
}
|
||||
|
||||
lines := msg.BodyLines()
|
||||
text := strings.Join(lines, " ")
|
||||
|
||||
a.ui.AddLine(msg.To, fmt.Sprintf(
|
||||
"[gray]%s [cyan]*** %s set topic: %s",
|
||||
ts, msg.From, text,
|
||||
))
|
||||
}
|
||||
|
||||
func (a *App) handleDefaultEvent(
|
||||
msg *api.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