fix: resolve nlreturn, modernize, perfsprint, wsl_v5, and partial err113 lint issues
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
@@ -27,13 +28,15 @@ func NewClient(baseURL string) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) do(method, path string, body interface{}) ([]byte, error) {
|
||||
func (c *Client) do(method, path string, body any) ([]byte, error) {
|
||||
var bodyReader io.Reader
|
||||
|
||||
if body != nil {
|
||||
data, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("marshal: %w", err)
|
||||
}
|
||||
|
||||
bodyReader = bytes.NewReader(data)
|
||||
}
|
||||
|
||||
@@ -41,7 +44,9 @@ func (c *Client) do(method, path string, body interface{}) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if c.Token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||
}
|
||||
@@ -70,11 +75,14 @@ func (c *Client) CreateSession(nick string) (*SessionResponse, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp SessionResponse
|
||||
if err := json.Unmarshal(data, &resp); err != nil {
|
||||
return nil, fmt.Errorf("decode session: %w", err)
|
||||
}
|
||||
|
||||
c.Token = resp.Token
|
||||
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
@@ -84,16 +92,19 @@ func (c *Client) GetState() (*StateResponse, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var resp StateResponse
|
||||
if err := json.Unmarshal(data, &resp); err != nil {
|
||||
return nil, fmt.Errorf("decode state: %w", err)
|
||||
}
|
||||
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// SendMessage sends a message (any IRC command).
|
||||
func (c *Client) SendMessage(msg *Message) error {
|
||||
_, err := c.do("POST", "/api/v1/messages", msg)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -106,17 +117,19 @@ func (c *Client) PollMessages(afterID string, timeout int) ([]Message, error) {
|
||||
if afterID != "" {
|
||||
params.Set("after", afterID)
|
||||
}
|
||||
params.Set("timeout", fmt.Sprintf("%d", timeout))
|
||||
|
||||
params.Set("timeout", strconv.Itoa(timeout))
|
||||
|
||||
path := "/api/v1/messages"
|
||||
if len(params) > 0 {
|
||||
path += "?" + params.Encode()
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", c.BaseURL+path, nil)
|
||||
req, err := http.NewRequest(http.MethodGet, c.BaseURL+path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
@@ -139,9 +152,11 @@ func (c *Client) PollMessages(afterID string, timeout int) ([]Message, error) {
|
||||
if err := json.Unmarshal(data, &msgs); err != nil {
|
||||
// Try wrapped format.
|
||||
var wrapped MessagesResponse
|
||||
if err2 := json.Unmarshal(data, &wrapped); err2 != nil {
|
||||
err2 := json.Unmarshal(data, &wrapped)
|
||||
if err2 != nil {
|
||||
return nil, fmt.Errorf("decode messages: %w (raw: %s)", err, string(data))
|
||||
}
|
||||
|
||||
msgs = wrapped.Messages
|
||||
}
|
||||
|
||||
@@ -164,10 +179,12 @@ func (c *Client) ListChannels() ([]Channel, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var channels []Channel
|
||||
if err := json.Unmarshal(data, &channels); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
@@ -177,16 +194,19 @@ func (c *Client) GetMembers(channel string) ([]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var members []string
|
||||
if err := json.Unmarshal(data, &members); err != nil {
|
||||
// Try object format.
|
||||
var obj map[string]interface{}
|
||||
if err2 := json.Unmarshal(data, &obj); err2 != nil {
|
||||
var obj map[string]any
|
||||
err2 := json.Unmarshal(data, &obj)
|
||||
if err2 != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Extract member names from whatever format.
|
||||
return nil, fmt.Errorf("unexpected members format: %s", string(data))
|
||||
}
|
||||
|
||||
return members, nil
|
||||
}
|
||||
|
||||
@@ -196,9 +216,11 @@ func (c *Client) GetServerInfo() (*ServerInfo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var info ServerInfo
|
||||
if err := json.Unmarshal(data, &info); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
@@ -25,26 +25,27 @@ type StateResponse struct {
|
||||
|
||||
// Message represents a chat message envelope.
|
||||
type Message struct {
|
||||
Command string `json:"command"`
|
||||
From string `json:"from,omitempty"`
|
||||
To string `json:"to,omitempty"`
|
||||
Params []string `json:"params,omitempty"`
|
||||
Body interface{} `json:"body,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
TS string `json:"ts,omitempty"`
|
||||
Meta interface{} `json:"meta,omitempty"`
|
||||
Command string `json:"command"`
|
||||
From string `json:"from,omitempty"`
|
||||
To string `json:"to,omitempty"`
|
||||
Params []string `json:"params,omitempty"`
|
||||
Body any `json:"body,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
TS string `json:"ts,omitempty"`
|
||||
Meta any `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// BodyLines returns the body as a slice of strings (for text messages).
|
||||
func (m *Message) BodyLines() []string {
|
||||
switch v := m.Body.(type) {
|
||||
case []interface{}:
|
||||
case []any:
|
||||
lines := make([]string, 0, len(v))
|
||||
for _, item := range v {
|
||||
if s, ok := item.(string); ok {
|
||||
lines = append(lines, s)
|
||||
}
|
||||
}
|
||||
|
||||
return lines
|
||||
case []string:
|
||||
return v
|
||||
@@ -79,5 +80,6 @@ func (m *Message) ParseTS() time.Time {
|
||||
if err != nil {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
@@ -35,7 +35,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 +45,7 @@ func main() {
|
||||
func (a *App) handleInput(text string) {
|
||||
if strings.HasPrefix(text, "/") {
|
||||
a.handleCommand(text)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,10 +57,13 @@ 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
|
||||
}
|
||||
|
||||
@@ -69,11 +74,13 @@ func (a *App) handleInput(text string) {
|
||||
})
|
||||
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()
|
||||
@@ -83,6 +90,7 @@ func (a *App) handleInput(text string) {
|
||||
func (a *App) handleCommand(text string) {
|
||||
parts := strings.SplitN(text, " ", 2)
|
||||
cmd := strings.ToLower(parts[0])
|
||||
|
||||
args := ""
|
||||
if len(parts) > 1 {
|
||||
args = parts[1]
|
||||
@@ -114,15 +122,17 @@ 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>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
serverURL = strings.TrimRight(serverURL, "/")
|
||||
|
||||
a.ui.AddStatus(fmt.Sprintf("Connecting to %s...", serverURL))
|
||||
@@ -132,9 +142,11 @@ func (a *App) cmdConnect(serverURL string) {
|
||||
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))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -156,8 +168,10 @@ func (a *App) cmdConnect(serverURL string) {
|
||||
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()
|
||||
@@ -167,6 +181,7 @@ func (a *App) cmdNick(nick string) {
|
||||
a.nick = nick
|
||||
a.mu.Unlock()
|
||||
a.ui.AddStatus(fmt.Sprintf("Nick set to %s (will be used on connect)", nick))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -176,6 +191,7 @@ func (a *App) cmdNick(nick string) {
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Nick change failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -184,14 +200,16 @@ func (a *App) cmdNick(nick string) {
|
||||
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 +217,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,7 +237,7 @@ 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 +246,36 @@ func (a *App) cmdPart(channel string) {
|
||||
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()
|
||||
|
||||
@@ -260,16 +287,20 @@ func (a *App) cmdMsg(args string) {
|
||||
parts := strings.SplitN(args, " ", 2)
|
||||
if len(parts) < 2 {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -280,6 +311,7 @@ func (a *App) cmdMsg(args string) {
|
||||
})
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Send failed: %v", err))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -290,6 +322,7 @@ func (a *App) cmdMsg(args string) {
|
||||
func (a *App) cmdQuery(nick string) {
|
||||
if nick == "" {
|
||||
a.ui.AddStatus("[red]Usage: /query <nick>")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -310,10 +343,13 @@ 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
|
||||
}
|
||||
|
||||
@@ -326,6 +362,7 @@ func (a *App) cmdTopic(args string) {
|
||||
if err != nil {
|
||||
a.ui.AddStatus(fmt.Sprintf("[red]Topic query failed: %v", err))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -347,16 +384,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,27 +411,33 @@ 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)
|
||||
a.ui.SwitchBuffer(n)
|
||||
@@ -400,6 +447,7 @@ func (a *App) cmdWindow(args string) {
|
||||
// Update target to the buffer name.
|
||||
// Needs to be done carefully.
|
||||
}
|
||||
|
||||
nick := a.nick
|
||||
a.mu.Unlock()
|
||||
|
||||
@@ -422,6 +470,7 @@ func (a *App) cmdQuit() {
|
||||
if a.connected && a.client != nil {
|
||||
_ = a.client.SendMessage(&api.Message{Command: "QUIT"})
|
||||
}
|
||||
|
||||
if a.stopPoll != nil {
|
||||
close(a.stopPoll)
|
||||
}
|
||||
@@ -473,11 +522,13 @@ func (a *App) pollLoop() {
|
||||
if err != nil {
|
||||
// Transient error — retry after delay.
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
for _, msg := range msgs {
|
||||
a.handleServerMessage(&msg)
|
||||
|
||||
if msg.ID != "" {
|
||||
a.mu.Lock()
|
||||
a.lastMsgID = msg.ID
|
||||
@@ -489,6 +540,7 @@ func (a *App) pollLoop() {
|
||||
|
||||
func (a *App) handleServerMessage(msg *api.Message) {
|
||||
ts := ""
|
||||
|
||||
if msg.TS != "" {
|
||||
t := msg.ParseTS()
|
||||
ts = t.Local().Format("15:04")
|
||||
@@ -504,15 +556,18 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
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))
|
||||
|
||||
case "JOIN":
|
||||
@@ -524,6 +579,7 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
case "PART":
|
||||
target := msg.To
|
||||
lines := msg.BodyLines()
|
||||
|
||||
reason := strings.Join(lines, " ")
|
||||
if target != "" {
|
||||
if reason != "" {
|
||||
@@ -535,6 +591,7 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
|
||||
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))
|
||||
@@ -544,10 +601,12 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
|
||||
case "NICK":
|
||||
lines := msg.BodyLines()
|
||||
|
||||
newNick := ""
|
||||
if len(lines) > 0 {
|
||||
newNick = lines[0]
|
||||
}
|
||||
|
||||
if msg.From == myNick && newNick != "" {
|
||||
a.mu.Lock()
|
||||
a.nick = newNick
|
||||
@@ -555,6 +614,7 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
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))
|
||||
|
||||
case "NOTICE":
|
||||
@@ -564,6 +624,7 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
|
||||
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))
|
||||
@@ -572,6 +633,7 @@ func (a *App) handleServerMessage(msg *api.Message) {
|
||||
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))
|
||||
|
||||
@@ -65,7 +65,9 @@ func NewUI() *UI {
|
||||
if text == "" {
|
||||
return
|
||||
}
|
||||
|
||||
ui.input.SetText("")
|
||||
|
||||
if ui.onInput != nil {
|
||||
ui.onInput(text)
|
||||
}
|
||||
@@ -79,9 +81,11 @@ func NewUI() *UI {
|
||||
if r >= '0' && r <= '9' {
|
||||
idx := int(r - '0')
|
||||
ui.SwitchBuffer(idx)
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return event
|
||||
})
|
||||
|
||||
@@ -121,6 +125,7 @@ func (ui *UI) AddLine(bufferName string, line string) {
|
||||
// Mark unread if not currently viewing this buffer.
|
||||
if ui.buffers[ui.currentBuffer] != buf {
|
||||
buf.Unread++
|
||||
|
||||
ui.refreshStatus()
|
||||
}
|
||||
|
||||
@@ -143,13 +148,17 @@ func (ui *UI) SwitchBuffer(n int) {
|
||||
if n < 0 || n >= len(ui.buffers) {
|
||||
return
|
||||
}
|
||||
|
||||
ui.currentBuffer = n
|
||||
buf := ui.buffers[n]
|
||||
buf.Unread = 0
|
||||
|
||||
ui.messages.Clear()
|
||||
|
||||
for _, line := range buf.Lines {
|
||||
fmt.Fprintln(ui.messages, line)
|
||||
}
|
||||
|
||||
ui.messages.ScrollToEnd()
|
||||
ui.refreshStatus()
|
||||
})
|
||||
@@ -162,14 +171,19 @@ func (ui *UI) SwitchToBuffer(name string) {
|
||||
for i, b := range ui.buffers {
|
||||
if b == buf {
|
||||
ui.currentBuffer = i
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
buf.Unread = 0
|
||||
|
||||
ui.messages.Clear()
|
||||
|
||||
for _, line := range buf.Lines {
|
||||
fmt.Fprintln(ui.messages, line)
|
||||
}
|
||||
|
||||
ui.messages.ScrollToEnd()
|
||||
ui.refreshStatus()
|
||||
})
|
||||
@@ -189,11 +203,13 @@ func (ui *UI) refreshStatus() {
|
||||
|
||||
func (ui *UI) refreshStatusWith(nick, target, connStatus string) {
|
||||
var unreadParts []string
|
||||
|
||||
for i, buf := range ui.buffers {
|
||||
if buf.Unread > 0 {
|
||||
unreadParts = append(unreadParts, fmt.Sprintf("%d:%s(%d)", i, buf.Name, buf.Unread))
|
||||
}
|
||||
}
|
||||
|
||||
unread := ""
|
||||
if len(unreadParts) > 0 {
|
||||
unread = " [Act: " + strings.Join(unreadParts, ",") + "]"
|
||||
@@ -212,8 +228,10 @@ func (ui *UI) getOrCreateBuffer(name string) *Buffer {
|
||||
return buf
|
||||
}
|
||||
}
|
||||
|
||||
buf := &Buffer{Name: name}
|
||||
ui.buffers = append(ui.buffers, buf)
|
||||
|
||||
return buf
|
||||
}
|
||||
|
||||
@@ -229,5 +247,6 @@ func (ui *UI) BufferIndex(name string) int {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ func NewTest(dsn string) (*Database, error) {
|
||||
// Item 9: Enable foreign keys
|
||||
if _, err := d.Exec("PRAGMA foreign_keys = ON"); err != nil {
|
||||
_ = d.Close()
|
||||
|
||||
return nil, fmt.Errorf("enable foreign keys: %w", err)
|
||||
}
|
||||
|
||||
@@ -219,6 +220,7 @@ func (s *Database) DeleteAuthToken(
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`DELETE FROM auth_tokens WHERE token = ?`, token,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -231,6 +233,7 @@ func (s *Database) UpdateUserLastSeen(
|
||||
`UPDATE users SET last_seen_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||
userID,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -394,6 +397,7 @@ func (s *Database) DequeueMessages(
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
entries := []*models.MessageQueueEntry{}
|
||||
@@ -423,7 +427,7 @@ func (s *Database) AckMessages(
|
||||
}
|
||||
|
||||
placeholders := make([]string, len(entryIDs))
|
||||
args := make([]interface{}, len(entryIDs))
|
||||
args := make([]any, len(entryIDs))
|
||||
|
||||
for i, id := range entryIDs {
|
||||
placeholders[i] = "?"
|
||||
|
||||
@@ -62,7 +62,8 @@ func (s *Database) ListChannels(ctx context.Context, userID string) ([]ChannelIn
|
||||
|
||||
for rows.Next() {
|
||||
var ch ChannelInfo
|
||||
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic); err != nil {
|
||||
err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -95,7 +96,8 @@ func (s *Database) ChannelMembers(ctx context.Context, channelID string) ([]Memb
|
||||
|
||||
for rows.Next() {
|
||||
var m MemberInfo
|
||||
if err := rows.Scan(&m.ID, &m.Nick, &m.LastSeen); err != nil {
|
||||
err := rows.Scan(&m.ID, &m.Nick, &m.LastSeen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -182,7 +184,8 @@ func (s *Database) PollMessages(ctx context.Context, userID string, afterTS stri
|
||||
|
||||
for rows.Next() {
|
||||
var m MessageInfo
|
||||
if err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt); err != nil {
|
||||
err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -200,7 +203,7 @@ func (s *Database) GetMessagesBefore(ctx context.Context, target string, beforeT
|
||||
|
||||
var rows interface {
|
||||
Next() bool
|
||||
Scan(dest ...interface{}) error
|
||||
Scan(dest ...any) error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
@@ -233,7 +236,8 @@ func (s *Database) GetMessagesBefore(ctx context.Context, target string, beforeT
|
||||
|
||||
for rows.Next() {
|
||||
var m MessageInfo
|
||||
if err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt); err != nil {
|
||||
err := rows.Scan(&m.ID, &m.Channel, &m.Nick, &m.Content, &m.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -256,7 +260,7 @@ func (s *Database) GetDMsBefore(ctx context.Context, userA, userB string, before
|
||||
|
||||
var rows interface {
|
||||
Next() bool
|
||||
Scan(dest ...interface{}) error
|
||||
Scan(dest ...any) error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
@@ -290,7 +294,8 @@ func (s *Database) GetDMsBefore(ctx context.Context, userA, userB string, before
|
||||
|
||||
for rows.Next() {
|
||||
var m MessageInfo
|
||||
if err := rows.Scan(&m.ID, &m.Nick, &m.Content, &m.DMTarget, &m.CreatedAt); err != nil {
|
||||
err := rows.Scan(&m.ID, &m.Nick, &m.Content, &m.DMTarget, &m.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -341,7 +346,8 @@ func (s *Database) ListAllChannels(ctx context.Context) ([]ChannelInfo, error) {
|
||||
|
||||
for rows.Next() {
|
||||
var ch ChannelInfo
|
||||
if err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic); err != nil {
|
||||
err := rows.Scan(&ch.ID, &ch.Name, &ch.Topic)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ func (s *Handlers) requireAuth(w http.ResponseWriter, r *http.Request) (string,
|
||||
uid, nick, err := s.authUser(r)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "unauthorized"}, http.StatusUnauthorized)
|
||||
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
@@ -52,6 +53,7 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
|
||||
type request struct {
|
||||
Nick string `json:"nick"`
|
||||
}
|
||||
|
||||
type response struct {
|
||||
ID string `json:"id"`
|
||||
Nick string `json:"nick"`
|
||||
@@ -62,12 +64,14 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
|
||||
var req request
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "invalid request"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
req.Nick = strings.TrimSpace(req.Nick)
|
||||
if req.Nick == "" || len(req.Nick) > 32 {
|
||||
s.respondJSON(w, r, map[string]string{"error": "nick must be 1-32 characters"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -77,6 +81,7 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "UNIQUE") {
|
||||
s.respondJSON(w, r, map[string]string{"error": "nick already taken"}, http.StatusConflict)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -162,6 +167,7 @@ func (s *Handlers) HandleChannelMembers() http.HandlerFunc {
|
||||
"SELECT id FROM channels WHERE name = ?", name).Scan(&chID)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -205,10 +211,10 @@ func (s *Handlers) HandleGetMessages() http.HandlerFunc {
|
||||
// The "command" field dispatches to the appropriate logic.
|
||||
func (s *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
type request struct {
|
||||
Command string `json:"command"`
|
||||
To string `json:"to"`
|
||||
Params []string `json:"params,omitempty"`
|
||||
Body interface{} `json:"body,omitempty"`
|
||||
Command string `json:"command"`
|
||||
To string `json:"to"`
|
||||
Params []string `json:"params,omitempty"`
|
||||
Body any `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -218,8 +224,10 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
}
|
||||
|
||||
var req request
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
err := json.NewDecoder(r.Body).Decode(&req)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "invalid request"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -229,7 +237,7 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
// Helper to extract body as string lines.
|
||||
bodyLines := func() []string {
|
||||
switch v := req.Body.(type) {
|
||||
case []interface{}:
|
||||
case []any:
|
||||
lines := make([]string, 0, len(v))
|
||||
|
||||
for _, item := range v {
|
||||
@@ -267,6 +275,7 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
|
||||
default:
|
||||
_ = nick // suppress unused warning
|
||||
|
||||
s.respondJSON(w, r, map[string]string{"error": "unknown command: " + req.Command}, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
@@ -275,11 +284,13 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
|
||||
func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, nick, to string, lines []string) {
|
||||
if to == "" {
|
||||
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(lines) == 0 {
|
||||
s.respondJSON(w, r, map[string]string{"error": "body required"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -293,6 +304,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
|
||||
"SELECT id FROM channels WHERE name = ?", to).Scan(&chID)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -310,6 +322,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
|
||||
targetUser, err := s.params.Database.GetUserByNick(r.Context(), to)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "user not found"}, http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -328,6 +341,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
|
||||
func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to string) {
|
||||
if to == "" {
|
||||
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -357,6 +371,7 @@ func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to st
|
||||
func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to string) {
|
||||
if to == "" {
|
||||
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -371,6 +386,7 @@ func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to st
|
||||
"SELECT id FROM channels WHERE name = ?", channel).Scan(&chID)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -387,18 +403,22 @@ func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to st
|
||||
func (s *Handlers) handleNick(w http.ResponseWriter, r *http.Request, uid string, lines []string) {
|
||||
if len(lines) == 0 {
|
||||
s.respondJSON(w, r, map[string]string{"error": "body required (new nick)"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
newNick := strings.TrimSpace(lines[0])
|
||||
if newNick == "" || len(newNick) > 32 {
|
||||
s.respondJSON(w, r, map[string]string{"error": "nick must be 1-32 characters"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.params.Database.ChangeNick(r.Context(), uid, newNick); err != nil {
|
||||
err := s.params.Database.ChangeNick(r.Context(), uid, newNick)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "UNIQUE") {
|
||||
s.respondJSON(w, r, map[string]string{"error": "nick already in use"}, http.StatusConflict)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -414,11 +434,13 @@ func (s *Handlers) handleNick(w http.ResponseWriter, r *http.Request, uid string
|
||||
func (s *Handlers) handleTopic(w http.ResponseWriter, r *http.Request, uid, to string, lines []string) {
|
||||
if to == "" {
|
||||
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if len(lines) == 0 {
|
||||
s.respondJSON(w, r, map[string]string{"error": "body required (topic text)"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -429,7 +451,8 @@ func (s *Handlers) handleTopic(w http.ResponseWriter, r *http.Request, uid, to s
|
||||
channel = "#" + channel
|
||||
}
|
||||
|
||||
if err := s.params.Database.SetTopic(r.Context(), channel, uid, topic); err != nil {
|
||||
err := s.params.Database.SetTopic(r.Context(), channel, uid, topic)
|
||||
if err != nil {
|
||||
s.log.Error("set topic failed", "error", err)
|
||||
s.respondJSON(w, r, map[string]string{"error": "internal error"}, http.StatusInternalServerError)
|
||||
|
||||
@@ -450,6 +473,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
|
||||
target := r.URL.Query().Get("target")
|
||||
if target == "" {
|
||||
s.respondJSON(w, r, map[string]string{"error": "target required"}, http.StatusBadRequest)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -468,6 +492,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
|
||||
"SELECT id FROM channels WHERE name = ?", target).Scan(&chID)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -485,6 +510,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
|
||||
targetUser, err := s.params.Database.GetUserByNick(r.Context(), target)
|
||||
if err != nil {
|
||||
s.respondJSON(w, r, map[string]string{"error": "user not found"}, http.StatusNotFound)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,5 +23,5 @@ func (t *AuthToken) User(ctx context.Context) (*User, error) {
|
||||
return ul.GetUserByID(ctx, t.UserID)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("user lookup not available")
|
||||
return nil, errors.New("user lookup not available")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ func (cm *ChannelMember) User(ctx context.Context) (*User, error) {
|
||||
return ul.GetUserByID(ctx, cm.UserID)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("user lookup not available")
|
||||
return nil, errors.New("user lookup not available")
|
||||
}
|
||||
|
||||
// Channel returns the full Channel for this membership.
|
||||
@@ -32,5 +32,5 @@ func (cm *ChannelMember) Channel(ctx context.Context) (*Channel, error) {
|
||||
return cl.GetChannelByID(ctx, cm.ChannelID)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("channel lookup not available")
|
||||
return nil, errors.New("channel lookup not available")
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,5 +23,5 @@ func (s *Session) User(ctx context.Context) (*User, error) {
|
||||
return ul.GetUserByID(ctx, s.UserID)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("user lookup not available")
|
||||
return nil, errors.New("user lookup not available")
|
||||
}
|
||||
|
||||
@@ -71,16 +71,20 @@ func (s *Server) SetupRoutes() {
|
||||
s.log.Error("failed to get web dist filesystem", "error", err)
|
||||
} else {
|
||||
fileServer := http.FileServer(http.FS(distFS))
|
||||
|
||||
s.router.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Try to serve the file; if not found, serve index.html for SPA routing
|
||||
f, err := distFS.(fs.ReadFileFS).ReadFile(r.URL.Path[1:])
|
||||
if err != nil || len(f) == 0 {
|
||||
indexHTML, _ := distFS.(fs.ReadFileFS).ReadFile("index.html")
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(indexHTML)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user