1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// Copyright (c) 2024, Přemysl Eric Janouch <p@janouch.name>
// SPDX-License-Identifier: 0BSD
package main
import (
"bufio"
"context"
"encoding/binary"
"flag"
"fmt"
"image/color"
"io"
"log"
"net"
"net/url"
"os"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var (
debug = flag.Bool("debug", false, "enable debug output")
projectName = "xA"
projectVersion = "?"
)
// --- Theme -------------------------------------------------------------------
type customTheme struct{}
func convertColor(c int) color.Color {
base16 := []uint16{
0x000, 0x800, 0x080, 0x880, 0x008, 0x808, 0x088, 0xccc,
0x888, 0xf00, 0x0f0, 0xff0, 0x00f, 0xf0f, 0x0ff, 0xfff,
}
if c < 16 {
r := 0xf & uint8(base16[c]>>8)
g := 0xf & uint8(base16[c]>>4)
b := 0xf & uint8(base16[c])
return color.RGBA{r * 0x11, g * 0x11, b * 0x11, 0xff}
}
if c >= 216 {
return color.Gray{8 + uint8(c-216)*10}
}
var (
i = uint8(c - 16)
r = i / 36 >> 0
g = (i / 6 >> 0) % 6
b = i % 6
)
if r != 0 {
r = 55 + 40*r
}
if g != 0 {
g = 55 + 40*g
}
if b != 0 {
b = 55 + 40*b
}
return color.RGBA{r, g, b, 0xff}
}
var ircColors = make(map[fyne.ThemeColorName]color.Color)
func init() {
for color := 0; color < 256; color++ {
ircColors[fyne.ThemeColorName(
fmt.Sprintf("irc%02x", color))] = convertColor(color)
}
}
func (t *customTheme) Color(
name fyne.ThemeColorName, variant fyne.ThemeVariant) color.Color {
// Fuck this low contrast shit, text must be black.
if name == theme.ColorNameForeground &&
variant == theme.VariantLight {
return color.Black
}
// TODO(p): Consider constants for stuff like timestamps.
if c, ok := ircColors[name]; ok {
return c
}
return theme.DefaultTheme().Color(name, variant)
}
func (t *customTheme) Font(style fyne.TextStyle) fyne.Resource {
return theme.DefaultTheme().Font(style)
}
func (t *customTheme) Icon(i fyne.ThemeIconName) fyne.Resource {
return theme.DefaultTheme().Icon(i)
}
func (t *customTheme) Size(s fyne.ThemeSizeName) float32 {
return theme.DefaultTheme().Size(s)
}
// --- Relay state -------------------------------------------------------------
type server struct {
state RelayServerState
user string
userModes string
}
type bufferLineItem struct {
format fyne.TextStyle
// For RichTextStyle.ColorName.
// XXX: Fyne's RichText doesn't support background colours.
color string
text string
}
type bufferLine struct {
/// Leaked from another buffer, but temporarily staying in another one.
leaked bool
isUnimportant bool
isHighlight bool
rendition RelayRendition
when time.Time
items []bufferLineItem
}
type buffer struct {
bufferName string
hideUnimportant bool
kind RelayBufferKind
serverName string
lines []bufferLine
// TODO(p): Server by name or by pointer?
// Channel:
topic []bufferLineItem
modes string
// Stats:
newMessages int
newUnimportantMessages int
highlighted bool
// Input:
input string
inputStart, inputEnd int
history []string
historyAt int
}
type callback func(err error, response *RelayResponseData)
var (
backendAddress string
backendContext context.Context
backendCancel context.CancelFunc
// Connection state:
commandSeq uint32
commandCallbacks map[uint32]callback
buffers []buffer
bufferCurrent string
bufferLast string
servers map[string]server
// Widgets:
wRichText *widget.RichText
wRichScroll *container.Scroll
wEntry *widget.Entry
)
// -----------------------------------------------------------------------------
func relayReadMessage(r io.Reader) (m RelayEventMessage, ok bool) {
var length uint32
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
log.Println("Event receive failed: " + err.Error())
return
}
b := make([]byte, length)
if _, err := io.ReadFull(r, b); err != nil {
log.Println("Event receive failed: " + err.Error())
return
}
if after, ok2 := m.ConsumeFrom(b); !ok2 {
log.Println("Event deserialization failed")
return
} else if len(after) != 0 {
log.Println("Event deserialization failed: trailing data")
return
}
if *debug {
log.Printf("<? %v\n", b)
j, err := m.MarshalJSON()
if err != nil {
log.Println("Event marshalling failed: " + err.Error())
return
}
log.Printf("<- %s\n", j)
}
return m, true
}
func relayMakeReceiver(
ctx context.Context, conn net.Conn) <-chan RelayEventMessage {
// The usual event message rarely gets above 1 kilobyte,
// thus this is set to buffer up at most 1 megabyte or so.
p := make(chan RelayEventMessage, 1000)
r := bufio.NewReaderSize(conn, 65536)
go func() {
defer close(p)
for {
m, ok := relayReadMessage(r)
if !ok {
return
}
select {
case p <- m:
case <-ctx.Done():
return
}
}
}()
return p
}
func relayWriteMessage(conn net.Conn, commandData any) bool {
m := RelayCommandMessage{
CommandSeq: commandSeq,
Data: RelayCommandData{commandData},
}
commandSeq++
b, ok := m.AppendTo(make([]byte, 4))
if !ok {
log.Println("Command serialization failed")
return false
}
binary.BigEndian.PutUint32(b[:4], uint32(len(b)-4))
if _, err := conn.Write(b); err != nil {
log.Println("Command send failed: " + err.Error())
return false
}
if *debug {
log.Printf("-> %v\n", b)
}
return true
}
func relayProcessMessage(m *RelayEventMessage) {
switch data := m.Data.Interface.(type) {
case RelayEventDataError:
// TODO(p): Process callbacks.
_ = data.CommandSeq
_ = data.Error
case RelayEventDataResponse:
// TODO(p): Process callbacks.
_ = data.CommandSeq
_ = data.Data
case RelayEventDataPing:
// TODO(p): Send the command.
_ = RelayCommandDataPingResponse{
Command: RelayCommandPingResponse,
EventSeq: m.EventSeq,
}
// TODO(p): Process all remaining message kinds.
case RelayEventDataBufferLine:
case RelayEventDataBufferUpdate:
case RelayEventDataBufferStats:
case RelayEventDataBufferRename:
case RelayEventDataBufferRemove:
case RelayEventDataBufferActivate:
case RelayEventDataBufferInput:
case RelayEventDataBufferClear:
case RelayEventDataServerUpdate:
case RelayEventDataServerRename:
case RelayEventDataServerRemove:
}
}
func relayRun() {
// TODO(p): Maybe reset state, and indicate in the UI that we're connecting.
backendContext, backendCancel = context.WithCancel(context.Background())
defer backendCancel()
conn, err := net.Dial("tcp", backendAddress)
if err != nil {
log.Println("Connection failed: " + err.Error())
// TODO(p): Display errors to the user.
return
}
defer conn.Close()
// TODO(p): How to send messages?
// - It would probably make the most sense to have either a chan
// (which makes code synchronize), or a locked slice.
// - But I also need to wake the sender up somehow,
// so maybe use a channel after all.
// - Or maybe use a channel just for the signalling.
// - Sending (semi-)synchronously is also an option, perhaps.
// TODO(p): Handle any errors here.
_ = relayWriteMessage(conn, &RelayCommandDataHello{
Command: RelayCommandHello,
Version: RelayVersion,
})
relayMessages := relayMakeReceiver(backendContext, conn)
for {
select {
case m, ok := <-relayMessages:
if !ok {
break
}
relayProcessMessage(&m)
default:
break
}
}
// TODO(p): Indicate in the UI that we're no longer connected.
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(),
"Usage: %s [OPTION...] CONNECT\n\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() < 1 || flag.NArg() > 1 {
flag.Usage()
os.Exit(1)
}
backendAddress = flag.Arg(0)
a := app.New()
a.Settings().SetTheme(&customTheme{})
w := a.NewWindow(projectName)
// TODO(p): There should also be a widget.NewLabel() next to the entry.
// - Probably another Border, even though this seems odd.
wRichText = widget.NewRichText()
wRichScroll = container.NewVScroll(wRichText)
wEntry = widget.NewMultiLineEntry()
w.SetContent(container.NewBorder(nil, wEntry, nil, nil, wRichScroll))
testURL, _ := url.Parse("https://x.com")
wRichText.Segments = []widget.RichTextSegment{
&widget.ParagraphSegment{Texts: []widget.RichTextSegment{
&widget.TextSegment{Text: "Test"},
&widget.HyperlinkSegment{Text: "X", URL: testURL},
&widget.TextSegment{
Text: " is a website, certainly",
Style: widget.RichTextStyleInline,
},
}},
&widget.TextSegment{Style: widget.RichTextStyleParagraph},
&widget.SeparatorSegment{},
&widget.TextSegment{Text: "Paragraph"},
}
wRichText.Wrapping = fyne.TextWrapWord
wRichText.Refresh()
go relayRun()
w.ShowAndRun()
}
|