mirror of
https://github.com/IllusionMan1212/gorc.git
synced 2026-10-24 18:12:33 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3ddd238552
|
@@ -0,0 +1,11 @@
|
||||
# progress
|
||||
it works nicely when there isn't spamming going on.
|
||||
|
||||
if spamming:
|
||||
when pressing g while a message is being received the calculation messes up and the first shown message is actually the 2nd or 3rd
|
||||
when pressing G while a message is being received a message or two will be missing, im suspecting this happens because of the ToInsert gets written to the db but we already fetched from the db so ToInsert is empty even tho we haven't appened it yet. (same thing happens when going down with d)
|
||||
-- potential fixes
|
||||
- save stuff to db message by message (this is slow as shit, but maybe goroutines could do something here ???)
|
||||
- same way of throttling the message receiving when a key is pressed such as g, G, d or etc..
|
||||
- maybe there's a way to optimize the readfromdb cmd, maybe the pointer stuff is slowing it down. idk
|
||||
- last resort is making this whole feature an option for people that know they have slow chats and want to keep the program open for days (idk)
|
||||
+139
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/illusionman1212/gorc/irc"
|
||||
"github.com/illusionman1212/gorc/irc/commands"
|
||||
)
|
||||
|
||||
type ConnectMsg struct{}
|
||||
@@ -40,6 +41,15 @@ func Quit(client *irc.Client) tea.Cmd {
|
||||
}
|
||||
}
|
||||
|
||||
type TickMsg struct{}
|
||||
|
||||
func Tick(client *irc.Client) tea.Cmd {
|
||||
return tea.Tick(time.Second/60, func(time.Time) tea.Msg {
|
||||
client.SendCommand(commands.PRIVMSG, "#hell", fmt.Sprintf("%d", client.Ticks))
|
||||
return TickMsg{}
|
||||
})
|
||||
}
|
||||
|
||||
type SendPrivMsgMsg struct {
|
||||
Msg string
|
||||
Timestamp string
|
||||
@@ -74,3 +84,132 @@ type UpdateTabBarMsg struct{}
|
||||
func UpdateTabBar() tea.Msg {
|
||||
return UpdateTabBarMsg{}
|
||||
}
|
||||
|
||||
type ReadHistoryFromDBMsg struct {
|
||||
LinesRead uint64
|
||||
Direction string
|
||||
}
|
||||
|
||||
func ReadHistoryFromDB(client *irc.Client, linesToRead int, direction string) tea.Cmd {
|
||||
hash := client.Channels[client.ActiveChannelIndex].Hash
|
||||
scrollBackBufFirstLine := client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine
|
||||
|
||||
messages := make([]string, 0)
|
||||
|
||||
switch direction {
|
||||
case "top":
|
||||
client.DB.
|
||||
Table("histories").
|
||||
Select("message").
|
||||
Where("hash = ?", hash).
|
||||
Limit(linesToRead).
|
||||
Scan(&messages).
|
||||
Order("line ASC")
|
||||
|
||||
// set the new messages
|
||||
copy(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, messages)
|
||||
// adjust the scrollback buf first line
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine = 1
|
||||
|
||||
return func() tea.Msg {
|
||||
return ReadHistoryFromDBMsg{
|
||||
LinesRead: 0,
|
||||
Direction: "top",
|
||||
}
|
||||
}
|
||||
case "up":
|
||||
limit := linesToRead
|
||||
linesToGet := 0
|
||||
|
||||
if linesToRead > int(scrollBackBufFirstLine) {
|
||||
limit = int(scrollBackBufFirstLine) - 1
|
||||
linesToGet = int(scrollBackBufFirstLine) - limit
|
||||
} else {
|
||||
linesToGet = int(scrollBackBufFirstLine) - linesToRead
|
||||
}
|
||||
|
||||
client.DB.
|
||||
Table("histories").
|
||||
Select("message").
|
||||
Where("hash = ? AND line >= ?", hash, linesToGet).
|
||||
Limit(limit).
|
||||
Scan(&messages).
|
||||
Order("line ASC")
|
||||
|
||||
if len(messages) != 0 {
|
||||
// prepend to the scroll back buf
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(messages, client.Channels[client.ActiveChannelIndex].ScrollBackBuf...)
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = client.Channels[client.ActiveChannelIndex].ScrollBackBuf[:irc.SCROLLBACK_MAX]
|
||||
// adjust the scrollback buf first line
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine -= uint64(limit)
|
||||
|
||||
return func() tea.Msg {
|
||||
return ReadHistoryFromDBMsg{
|
||||
LinesRead: uint64(limit),
|
||||
Direction: "up",
|
||||
}
|
||||
}
|
||||
}
|
||||
case "down":
|
||||
client.DB.
|
||||
Table("histories").
|
||||
Select("message").
|
||||
Where("hash = ? AND line >= ?", hash, client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine+irc.SCROLLBACK_MAX).
|
||||
Limit(linesToRead).
|
||||
Scan(&messages).
|
||||
Order("line ASC")
|
||||
linesGot := len(messages)
|
||||
if len(messages) != 0 {
|
||||
extraMessagesLen := 0
|
||||
// append to the scroll back buf
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, messages...)
|
||||
// if we reached the end of the messages in the db. try appending the messages in the ToInsert buffer
|
||||
if linesGot < linesToRead {
|
||||
for _, history := range client.Channels[client.ActiveChannelIndex].ToInsert {
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, history.Message)
|
||||
}
|
||||
extraMessagesLen = len(client.Channels[client.ActiveChannelIndex].ToInsert)
|
||||
}
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = client.Channels[client.ActiveChannelIndex].ScrollBackBuf[len(client.Channels[client.ActiveChannelIndex].ScrollBackBuf)-irc.SCROLLBACK_MAX:]
|
||||
// adjust the scrollback buf first line
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine += uint64(linesGot) + uint64(extraMessagesLen)
|
||||
|
||||
return func() tea.Msg {
|
||||
return ReadHistoryFromDBMsg{
|
||||
LinesRead: uint64(linesGot) + uint64(extraMessagesLen),
|
||||
Direction: "down",
|
||||
}
|
||||
}
|
||||
}
|
||||
case "bottom":
|
||||
client.DB.Raw("SELECT t.message FROM (SELECT line, message FROM histories WHERE hash = ? ORDER BY line DESC LIMIT ?) t ORDER BY t.line ASC;", hash, linesToRead).
|
||||
Scan(&messages)
|
||||
|
||||
toInsert := make([]string, 0)
|
||||
|
||||
for _, history := range client.Channels[client.ActiveChannelIndex].ToInsert {
|
||||
toInsert = append(toInsert, history.Message)
|
||||
}
|
||||
|
||||
// set the new messages
|
||||
copy(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, messages)
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = append(client.Channels[client.ActiveChannelIndex].ScrollBackBuf, toInsert...)
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBuf = client.Channels[client.ActiveChannelIndex].ScrollBackBuf[len(toInsert):]
|
||||
// adjust the scrollback buf first line
|
||||
client.Channels[client.ActiveChannelIndex].ScrollBackBufFirstLine = client.Channels[client.ActiveChannelIndex].TotalLines - irc.SCROLLBACK_MAX + uint64(len(toInsert))
|
||||
|
||||
return func() tea.Msg {
|
||||
return ReadHistoryFromDBMsg{
|
||||
LinesRead: 0,
|
||||
Direction: "bottom",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return func() tea.Msg {
|
||||
return ReadHistoryFromDBMsg{
|
||||
LinesRead: 0,
|
||||
Direction: "unknown",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
type History struct {
|
||||
Hash string `gorm:"index"`
|
||||
Line uint64 `gorm:"index" gorm:"unique"`
|
||||
Message string
|
||||
}
|
||||
|
||||
func ConnectToDB() (*gorm.DB, error) {
|
||||
db, err := gorm.Open(sqlite.Open("gorc.db"), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
AllowGlobalUpdate: true,
|
||||
})
|
||||
|
||||
db.AutoMigrate(&History{})
|
||||
db.Delete(&History{})
|
||||
|
||||
return db, err
|
||||
}
|
||||
@@ -1,30 +1,19 @@
|
||||
module github.com/illusionman1212/gorc
|
||||
|
||||
go 1.19
|
||||
go 1.16
|
||||
|
||||
replace github.com/charmbracelet/bubbles => /home/illusion/Desktop/repos/bubbles
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/bubbles v0.15.0
|
||||
github.com/charmbracelet/bubbletea v0.23.2
|
||||
github.com/charmbracelet/lipgloss v0.6.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aymanbagabas/go-osc52 v1.2.1 // indirect
|
||||
github.com/charmbracelet/bubbles v0.10.2
|
||||
github.com/charmbracelet/bubbletea v0.19.3
|
||||
github.com/charmbracelet/lipgloss v0.4.0
|
||||
github.com/containerd/console v1.0.3 // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
||||
github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a // indirect
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/muesli/reflow v0.3.0 // indirect
|
||||
github.com/muesli/termenv v0.14.0 // indirect
|
||||
github.com/rivo/uniseg v0.4.3 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.5.0 // indirect
|
||||
golang.org/x/term v0.5.0 // indirect
|
||||
golang.org/x/text v0.7.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.13 // indirect
|
||||
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||
gorm.io/driver/sqlite v1.3.4
|
||||
gorm.io/gorm v1.23.6 // indirect
|
||||
)
|
||||
|
||||
@@ -1,62 +1,66 @@
|
||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
|
||||
github.com/aymanbagabas/go-osc52 v1.2.1 h1:q2sWUyDcozPLcLabEMd+a+7Ea2DitxZVN9hTxab9L4E=
|
||||
github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
|
||||
github.com/charmbracelet/bubbles v0.15.0 h1:c5vZ3woHV5W2b8YZI1q7v4ZNQaPetfHuoHzx+56Z6TI=
|
||||
github.com/charmbracelet/bubbles v0.15.0/go.mod h1:Y7gSFbBzlMpUDR/XM9MhZI374Q+1p1kluf1uLl8iK74=
|
||||
github.com/charmbracelet/bubbletea v0.23.1/go.mod h1:JAfGK/3/pPKHTnAS8JIE2u9f61BjWTQY57RbT25aMXU=
|
||||
github.com/charmbracelet/bubbletea v0.23.2 h1:vuUJ9HJ7b/COy4I30e8xDVQ+VRDUEFykIjryPfgsdps=
|
||||
github.com/charmbracelet/bubbletea v0.23.2/go.mod h1:FaP3WUivcTM0xOKNmhciz60M6I+weYLF76mr1JyI7sM=
|
||||
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
||||
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
|
||||
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
|
||||
github.com/charmbracelet/bubbletea v0.19.3 h1:OKeO/Y13rQQqt4snX+lePB0QrnW80UdrMNolnCcmoAw=
|
||||
github.com/charmbracelet/bubbletea v0.19.3/go.mod h1:VuXF2pToRxDUHcBUcPmCRUHRvFATM4Ckb/ql1rBl3KA=
|
||||
github.com/charmbracelet/harmonica v0.1.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
|
||||
github.com/charmbracelet/lipgloss v0.4.0 h1:768h64EFkGUr8V5yAKV7/Ta0NiVceiPaV+PphaW1K9g=
|
||||
github.com/charmbracelet/lipgloss v0.4.0/go.mod h1:vmdkHvce7UzX6xkyf4cca8WlwdQ5RQr8fzta+xl7BOM=
|
||||
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
|
||||
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
|
||||
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
|
||||
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
|
||||
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I=
|
||||
github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
|
||||
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
||||
github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a h1:jlDOeO5TU0pYlbc/y6PFguab5IjANI0Knrpg3u/ton4=
|
||||
github.com/muesli/ansi v0.0.0-20221106050444-61f0cd9a192a/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 h1:kMlmsLSbjkikxQJ1IPwaM+7LJ9ltFu/fi8CRzvSnQmA=
|
||||
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
||||
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
|
||||
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
|
||||
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
|
||||
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
|
||||
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
|
||||
github.com/muesli/termenv v0.14.0 h1:8x9NFfOe8lmIWK4pgy3IfVEy47f+ppe3tUqdPZG2Uy0=
|
||||
github.com/muesli/termenv v0.14.0/go.mod h1:kG/pF1E7fh949Xhe156crRUrHNyK221IuGO7Ez60Uc8=
|
||||
github.com/muesli/termenv v0.9.0 h1:wnbOaGz+LUR3jNT0zOzinPnyDaCZUQRZj9GxK8eRVl8=
|
||||
github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
|
||||
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
|
||||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/sqlite v1.3.4 h1:NnFOPVfzi4CPsJPH4wXr6rMkPb4ElHEqKMvrsx9c9Fk=
|
||||
gorm.io/driver/sqlite v1.3.4/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U=
|
||||
gorm.io/gorm v1.23.4 h1:1BKWM67O6CflSLcwGQR7ccfmC4ebOxQrTfOQGRE9wjg=
|
||||
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||
gorm.io/gorm v1.23.6 h1:KFLdNgri4ExFFGTRGGFWON2P1ZN28+9SJRN8voOoYe0=
|
||||
gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||
|
||||
+66
-14
@@ -26,8 +26,10 @@ import (
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/illusionman1212/gorc/db"
|
||||
"github.com/illusionman1212/gorc/irc/commands"
|
||||
"github.com/illusionman1212/gorc/ui"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
@@ -42,8 +44,26 @@ type Channel struct {
|
||||
// Channel topic
|
||||
Topic string
|
||||
|
||||
// Channel messages history
|
||||
History string
|
||||
// Scrollback buffer for the channel message history
|
||||
ScrollBackBuf []string
|
||||
|
||||
// Whethere the viewport is at the bottom or not
|
||||
AtBottom bool
|
||||
|
||||
// Total amount of lines that have been received from the tcp conn
|
||||
// and written to the history db
|
||||
TotalLines uint64
|
||||
|
||||
// Buffer of messages that need to be inserted into the db
|
||||
ToInsert []db.History
|
||||
|
||||
// Position/Index of first line in the scrollback buffer as mapped
|
||||
// to TotalLines.
|
||||
// Last line is ScrollBackBufFirstLine + SCROLLBACK_MAX - 1
|
||||
ScrollBackBufFirstLine uint64
|
||||
|
||||
// Unique sha1 hash for the channel made from server hostname and channel name
|
||||
Hash string
|
||||
|
||||
// Users in this channel
|
||||
// The map key is the user's nickname
|
||||
@@ -77,18 +97,19 @@ type Client struct {
|
||||
// Reference to the bubbletea program
|
||||
Tea *tea.Program
|
||||
|
||||
// TODO: remove this
|
||||
Ticks int
|
||||
|
||||
// DB reference
|
||||
DB *gorm.DB
|
||||
|
||||
// Index of the first visible tab in the tab bar
|
||||
FirstTabIndexInTabBar int
|
||||
|
||||
// Index of the last visible tab in the tab bar
|
||||
LastTabIndexInTabBar int
|
||||
|
||||
// The acknowledged capabilities
|
||||
EnabledCapabilities Capabilities
|
||||
}
|
||||
|
||||
type Capabilities map[string]string
|
||||
|
||||
type MsgFmtOpts struct {
|
||||
WithTimestamp bool
|
||||
AsServerMsg bool
|
||||
@@ -101,9 +122,12 @@ var serverMsgStyle = lipgloss.NewStyle().Foreground(ui.ServerMsgColor)
|
||||
var timestampStyle = serverMsgStyle.Copy()
|
||||
var dateStyle = lipgloss.NewStyle().Foreground(ui.DateColor)
|
||||
|
||||
const CRLF = "\r\n"
|
||||
const (
|
||||
CRLF = "\r\n"
|
||||
SCROLLBACK_MAX = 400
|
||||
)
|
||||
|
||||
func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) {
|
||||
func (c *Channel) AppendMsg(host string, database *gorm.DB, timestamp string, fullMsg string, opts MsgFmtOpts) {
|
||||
prefixes := ""
|
||||
style := ui.DefaultStyle
|
||||
|
||||
@@ -124,7 +148,32 @@ func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) {
|
||||
style = dateStyle
|
||||
}
|
||||
|
||||
c.History += prefixes + style.Render(fullMsg) + CRLF
|
||||
finalMsg := prefixes + style.Render(fullMsg)
|
||||
|
||||
c.TotalLines++
|
||||
|
||||
history := db.History{Hash: c.Hash, Line: c.TotalLines, Message: finalMsg}
|
||||
c.ToInsert = append(c.ToInsert, history)
|
||||
|
||||
if c.TotalLines%10 == 0 {
|
||||
database.Create(c.ToInsert)
|
||||
c.ToInsert = nil
|
||||
}
|
||||
|
||||
if len(c.ScrollBackBuf) < SCROLLBACK_MAX {
|
||||
c.ScrollBackBuf = append(c.ScrollBackBuf, prefixes+style.Render(fullMsg))
|
||||
// We haven't reached the bottom yet so the first line is still 1
|
||||
c.ScrollBackBufFirstLine = 1
|
||||
} else if c.AtBottom && len(c.ScrollBackBuf) >= SCROLLBACK_MAX {
|
||||
// TODO: when pressing 'g' in viewport, we need to set this to 1
|
||||
// TODO: when pressing 'G' in viewport, we need to set this to TotalLines - 1000 + 1
|
||||
// TODO: when going up in viewport, we need to decrement the amount of lines by how many lines we need to prepend to the scrollbackbuf
|
||||
// TODO: when going down in viewport, we need to increment the amount of lines by how many lines we need to append to the scrollbackbuf
|
||||
// If we're at bottom and we're appending a line, then we increment the first line
|
||||
c.ScrollBackBufFirstLine++
|
||||
c.ScrollBackBuf = append(c.ScrollBackBuf, prefixes+style.Render(fullMsg))
|
||||
c.ScrollBackBuf = c.ScrollBackBuf[1:]
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Client) Initialize(host string, port string, tlsEnabled bool) {
|
||||
@@ -143,7 +192,7 @@ func (s *Client) Initialize(host string, port string, tlsEnabled bool) {
|
||||
s.Port = port
|
||||
s.ActiveChannel = host
|
||||
s.Channels = make([]Channel, 0)
|
||||
s.EnabledCapabilities = make(Capabilities, 0)
|
||||
s.Ticks = 1
|
||||
return
|
||||
}
|
||||
|
||||
@@ -162,7 +211,7 @@ func (s *Client) Initialize(host string, port string, tlsEnabled bool) {
|
||||
s.Port = port
|
||||
s.ActiveChannel = host
|
||||
s.Channels = make([]Channel, 0)
|
||||
s.EnabledCapabilities = make(Capabilities, 0)
|
||||
s.Ticks = 1
|
||||
}
|
||||
|
||||
func (c *Client) Register(nick string, password string, channel string) {
|
||||
@@ -171,14 +220,17 @@ func (c *Client) Register(nick string, password string, channel string) {
|
||||
Users: make(map[string]User),
|
||||
})
|
||||
|
||||
c.SendCommand(commands.CAP, "LS", "302")
|
||||
c.SendCommand(commands.CAP, "LS")
|
||||
if password != "" {
|
||||
c.SendCommand(commands.PASS, password)
|
||||
}
|
||||
// TODO: check if nickname has spaces and remove them
|
||||
c.SendCommand(commands.NICK, nick)
|
||||
// set user-wanted nickname
|
||||
c.Nickname = nick
|
||||
c.SendCommand(commands.USER, nick, "0", "*", nick)
|
||||
// TODO: CAP REQ :whatever capability the client recognizes and supports
|
||||
c.SendCommand(commands.CAP, "END")
|
||||
// joining a channel when registering is optional
|
||||
if channel != "" {
|
||||
c.SendCommand(commands.JOIN, channel)
|
||||
@@ -192,7 +244,7 @@ func (c *Client) SetDay() {
|
||||
|
||||
now := time.Now()
|
||||
msg := fmt.Sprintf("————— %s %d —————", now.Month().String(), now.Day())
|
||||
c.Channels[0].AppendMsg("", msg, msgOpts)
|
||||
c.Channels[0].AppendMsg(c.Host, c.DB, "", msg, msgOpts)
|
||||
|
||||
// TODO: append new day to each channel whenever the day changes
|
||||
}
|
||||
|
||||
@@ -116,7 +116,6 @@ const (
|
||||
RPL_MYINFO = "004" // RFC2812 - Implemented
|
||||
RPL_ISUPPORT = "005" // ??????? - Not Implemented (TODO:)
|
||||
RPL_BOUNCE = "010" // ??????? - Not Implemented (TODO:)
|
||||
RPL_REMOTEISUPPORT = "105" // ??????? - Not Implemented (TODO:) // Exact same as RPL_ISUPPORT but for remote servers
|
||||
RPL_TRACELINK = "200" // RFC1459 - Not Implemented (TODO:)
|
||||
RPL_TRACECONNECTING = "201" // RFC1459 - Not Implemented (TODO:)
|
||||
RPL_TRACEHANDSHAKE = "202" // RFC1459 - Not Implemented (TODO:)
|
||||
@@ -241,7 +240,7 @@ const (
|
||||
ERR_NOTOPLEVEL = "413" // RFC1459 - Not Implemented (TODO:)
|
||||
ERR_WILDTOPLEVEL = "414" // RFC1459 - Not Implemented (TODO:)
|
||||
ERR_BADMASK = "415" // RFC2812 - Not Implemented (TODO:)
|
||||
ERR_UNKNOWNCOMMAND = "421" // RFC1459 - Implemented
|
||||
ERR_UNKNOWNCOMMAND = "421" // RFC1459 - Not Implemented (TODO:)
|
||||
ERR_NOMOTD = "422" // RFC1459 - Not Implemented (TODO:)
|
||||
ERR_NOADMININFO = "423" // RFC1459 - Not Implemented (TODO:)
|
||||
ERR_FILEERROR = "424" // RFC1459 - Not Implemented (TODO:)
|
||||
@@ -313,7 +312,7 @@ var Features = map[string]bool{
|
||||
"CHANLIMIT": true,
|
||||
"CHANMODES": true,
|
||||
"CHANNELLEN": true,
|
||||
"CHANTYPES": true, // Channel Types. Default is #. Available are #&
|
||||
"CHANTYPES": true,
|
||||
"CHARSET": true, // Deprecated but might still be used
|
||||
"CLIENTVER": true, // Deprecated but might still be used
|
||||
"CNOTICE": true,
|
||||
@@ -365,42 +364,7 @@ var Features = map[string]bool{
|
||||
"WHOX": true,
|
||||
}
|
||||
|
||||
var Capabilities = map[string]bool{
|
||||
// Twitch-specific capabilities
|
||||
"twitch.tv/membership": false,
|
||||
"twitch.tv/commands": false,
|
||||
"twitch.tv/tags": false,
|
||||
// Messages MUST end in CRLF and SHOULD be encoded and decoded using UTF-8 (with fallbacks such as Latin-1).
|
||||
// Names are casemapped, read the casemapping from the RPL_ISUPPORT the server sends when registration is completed.
|
||||
|
||||
// Solanum-specific capabilities
|
||||
"solanum.chat/identify-msg": false,
|
||||
"solanum.chat/oper": false,
|
||||
"solanum.chat/realhost": false,
|
||||
|
||||
// IRCv3 capabilities
|
||||
"account-notify": false,
|
||||
"account-registration": false, // Draft
|
||||
"account-tag": false,
|
||||
"away-notify": false,
|
||||
"batch": false,
|
||||
"cap-notify": true,
|
||||
"channel-rename": false, // Draft
|
||||
"chathistory": false, // Draft
|
||||
"chghost": false,
|
||||
"echo-message": false,
|
||||
"event-playback": false, // Draft
|
||||
"extended-join": false,
|
||||
"extended-monitor": false,
|
||||
"invite-notify": false,
|
||||
"labeled-response": false,
|
||||
"message-tags": false,
|
||||
"metadata": false,
|
||||
"monitor": false,
|
||||
"multi-prefix": false,
|
||||
"multiline": false, // Draft
|
||||
"read-marker": false, // Draft
|
||||
"sasl": false,
|
||||
"server-time": true,
|
||||
"setname": false,
|
||||
"tls": false, // Deprecated
|
||||
"userhost-in-names": false,
|
||||
}
|
||||
// When receiving a numeric reply, client MUST be able to handle any number of parameters on a numeric reply.
|
||||
|
||||
+43
-127
@@ -18,6 +18,7 @@ package handler
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -69,7 +70,7 @@ func handlePrivMsg(msg parser.IRCMessage, client *irc.Client) {
|
||||
|
||||
for i, c := range client.Channels {
|
||||
if c.Name == channel {
|
||||
client.Channels[i].AppendMsg(msg.Timestamp, privMsg, msgOpts)
|
||||
client.Channels[i].AppendMsg(client.Host, client.DB, msg.Timestamp, privMsg, msgOpts)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,13 +87,24 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
|
||||
}
|
||||
|
||||
if nick == client.Nickname {
|
||||
for _, regChannel := range client.Channels {
|
||||
if regChannel.Name == channel {
|
||||
client.Tea.Send(cmds.SwitchChannels())
|
||||
return
|
||||
}
|
||||
}
|
||||
hash := sha1.New()
|
||||
hash.Write([]byte(client.Host + " " + channel))
|
||||
hashStr := fmt.Sprintf("%x", hash.Sum(nil))
|
||||
|
||||
client.Channels = append(client.Channels, irc.Channel{
|
||||
Name: channel,
|
||||
Users: make(map[string]irc.User),
|
||||
Hash: hashStr,
|
||||
})
|
||||
client.ActiveChannelIndex = len(client.Channels) - 1
|
||||
client.ActiveChannel = channel
|
||||
client.Channels[client.ActiveChannelIndex].AppendMsg(msg.Timestamp, joinMsg, msgOpts)
|
||||
client.Channels[client.ActiveChannelIndex].AppendMsg(client.Host, client.DB, msg.Timestamp, joinMsg, msgOpts)
|
||||
if _, exists := client.Channels[client.ActiveChannelIndex].Users[nick]; !exists {
|
||||
client.Channels[client.ActiveChannelIndex].Users[nick] = irc.User{}
|
||||
}
|
||||
@@ -100,7 +112,7 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
|
||||
} else {
|
||||
for i, c := range client.Channels {
|
||||
if c.Name == channel {
|
||||
client.Channels[i].AppendMsg(msg.Timestamp, joinMsg, msgOpts)
|
||||
client.Channels[i].AppendMsg(client.Host, client.DB, msg.Timestamp, joinMsg, msgOpts)
|
||||
if _, exists := client.Channels[i].Users[nick]; !exists {
|
||||
client.Channels[i].Users[nick] = irc.User{}
|
||||
}
|
||||
@@ -128,7 +140,7 @@ func handleQuit(msg parser.IRCMessage, client *irc.Client) {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
client.Channels[i].AppendMsg(msg.Timestamp, quitMsg, msgOpts)
|
||||
client.Channels[i].AppendMsg(client.Host, client.DB, msg.Timestamp, quitMsg, msgOpts)
|
||||
delete(client.Channels[i].Users, nick)
|
||||
}
|
||||
|
||||
@@ -160,7 +172,7 @@ func handlePart(msg parser.IRCMessage, client *irc.Client) {
|
||||
}
|
||||
client.LastTabIndexInTabBar--
|
||||
} else {
|
||||
client.Channels[i].AppendMsg(msg.Timestamp, partMsg, msgOpts)
|
||||
client.Channels[i].AppendMsg(client.Host, client.DB, msg.Timestamp, partMsg, msgOpts)
|
||||
delete(client.Channels[i].Users, nick)
|
||||
}
|
||||
}
|
||||
@@ -169,87 +181,6 @@ func handlePart(msg parser.IRCMessage, client *irc.Client) {
|
||||
client.Tea.Send(cmds.SwitchChannels())
|
||||
}
|
||||
|
||||
func handleCAP(msg parser.IRCMessage, client *irc.Client) {
|
||||
switch msg.Parameters[1] {
|
||||
case "LIST":
|
||||
fallthrough
|
||||
case "NEW":
|
||||
fallthrough
|
||||
case "LS":
|
||||
caps := strings.Split(msg.Parameters[2], " ")
|
||||
recognizedCaps := make([]irc.Capabilities, 0)
|
||||
capabilities := make(irc.Capabilities, 0)
|
||||
length := 0
|
||||
|
||||
for _, capability := range caps {
|
||||
// NOTE: not sure if I should store the cap value the server advertises or use my own
|
||||
parts := strings.Split(capability, "=")
|
||||
key := parts[0]
|
||||
value := ""
|
||||
if len(parts) > 1 {
|
||||
value = parts[1]
|
||||
}
|
||||
|
||||
if commands.Capabilities[key] {
|
||||
// When the final parameter approaches 510 bytes,
|
||||
// we send multiple REQ commands
|
||||
// (https://ircv3.net/specs/extensions/capability-negotiation.html#the-cap-req-subcommand)
|
||||
if length+len(capability) >= 500 {
|
||||
recognizedCaps = append(recognizedCaps, capabilities)
|
||||
capabilities = make(irc.Capabilities, 0)
|
||||
length = 0
|
||||
}
|
||||
|
||||
capabilities[key] = value
|
||||
length += len(capability)
|
||||
} else {
|
||||
log.Println("Unknown capability:", capability)
|
||||
}
|
||||
}
|
||||
recognizedCaps = append(recognizedCaps, capabilities)
|
||||
|
||||
for _, capsList := range recognizedCaps {
|
||||
list := make([]string, 0)
|
||||
for key, value := range capsList {
|
||||
if len(value) > 0 {
|
||||
list = append(list, key+"="+value)
|
||||
} else {
|
||||
list = append(list, key)
|
||||
}
|
||||
}
|
||||
client.SendCommand(commands.CAP, "REQ", strings.Join(list, " "))
|
||||
}
|
||||
client.SendCommand(commands.CAP, "END")
|
||||
case "ACK":
|
||||
caps := strings.Split(msg.Parameters[2], " ")
|
||||
for _, capability := range caps {
|
||||
if capability[0] == '-' {
|
||||
delete(client.EnabledCapabilities, capability[1:])
|
||||
} else {
|
||||
parts := strings.Split(capability, "=")
|
||||
key := parts[0]
|
||||
value := ""
|
||||
if len(parts) > 1 {
|
||||
value = parts[1]
|
||||
}
|
||||
client.EnabledCapabilities[key] = value
|
||||
}
|
||||
}
|
||||
case "NAK":
|
||||
caps := strings.Split(msg.Parameters[2], " ")
|
||||
msgOpts := irc.MsgFmtOpts{
|
||||
WithTimestamp: true,
|
||||
AsServerMsg: true,
|
||||
}
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, "Unrecognized capabilities: "+strings.Join(caps, " "), msgOpts)
|
||||
case "DEL":
|
||||
caps := strings.Split(msg.Parameters[2], " ")
|
||||
for _, capability := range caps {
|
||||
delete(client.EnabledCapabilities, capability)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleWELCOME(msg parser.IRCMessage, client *irc.Client) {
|
||||
nick := msg.Parameters[0]
|
||||
welcomeMsg := msg.Parameters[1]
|
||||
@@ -261,7 +192,7 @@ func handleWELCOME(msg parser.IRCMessage, client *irc.Client) {
|
||||
|
||||
// set server-registered nickname
|
||||
client.Nickname = nick
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, welcomeMsg, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, welcomeMsg, msgOpts)
|
||||
}
|
||||
|
||||
func handleYOURHOST(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -272,7 +203,7 @@ func handleYOURHOST(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, host, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, host, msgOpts)
|
||||
}
|
||||
|
||||
func handleCREATED(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -283,7 +214,7 @@ func handleCREATED(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, created, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, created, msgOpts)
|
||||
}
|
||||
|
||||
func handleMYINFO(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -294,7 +225,7 @@ func handleMYINFO(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, info, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, info, msgOpts)
|
||||
}
|
||||
|
||||
func handleLUSERCLIENT(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -305,7 +236,7 @@ func handleLUSERCLIENT(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleLUSEROP(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -316,7 +247,7 @@ func handleLUSEROP(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleLUSERUNKNOWN(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -327,7 +258,7 @@ func handleLUSERUNKNOWN(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleLUSERCHANNELS(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -338,7 +269,7 @@ func handleLUSERCHANNELS(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleLUSERME(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -349,7 +280,7 @@ func handleLUSERME(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleLOCALUSERS(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -360,7 +291,7 @@ func handleLOCALUSERS(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleGLOBALUSERS(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -371,7 +302,7 @@ func handleGLOBALUSERS(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleWHOISUSER(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -393,8 +324,8 @@ func handleWHOISUSER(msg parser.IRCMessage, client *irc.Client) {
|
||||
realName,
|
||||
)
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, "WHOIS Information", msgOpts)
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, "WHOIS Information", msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleWHOISSERVER(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -412,7 +343,7 @@ func handleWHOISSERVER(msg parser.IRCMessage, client *irc.Client) {
|
||||
serverInfo,
|
||||
)
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleWHOISIDLE(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -436,7 +367,7 @@ func handleWHOISIDLE(msg parser.IRCMessage, client *irc.Client) {
|
||||
idleSeconds,
|
||||
since.Format(time.ANSIC),
|
||||
)
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleENDOFWHOIS(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -447,7 +378,7 @@ func handleENDOFWHOIS(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleWHOISCHANNELS(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -460,7 +391,7 @@ func handleWHOISCHANNELS(msg parser.IRCMessage, client *irc.Client) {
|
||||
|
||||
message := fmt.Sprintf("channels: %s", chans)
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleNAMREPLY(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -501,7 +432,7 @@ func handleMOTDStart(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleMOTD(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -512,7 +443,7 @@ func handleMOTD(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, messageLine, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, messageLine, msgOpts)
|
||||
}
|
||||
|
||||
func handleWHOISHOST(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -523,7 +454,7 @@ func handleWHOISHOST(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleWHOISMODES(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -534,7 +465,7 @@ func handleWHOISMODES(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleNOSUCHSERVER(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -545,18 +476,7 @@ func handleNOSUCHSERVER(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleUNKNOWNCOMMAND(msg parser.IRCMessage, client *irc.Client) {
|
||||
message := msg.Parameters[1] + " " + msg.Parameters[2]
|
||||
|
||||
msgOpts := irc.MsgFmtOpts{
|
||||
WithTimestamp: true,
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleNONICKNAMEGIVEN(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -567,7 +487,7 @@ func handleNONICKNAMEGIVEN(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleNEEDMOREPARAMS(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -578,7 +498,7 @@ func handleNEEDMOREPARAMS(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func handleALREADYREGISTERED(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -589,7 +509,7 @@ func handleALREADYREGISTERED(msg parser.IRCMessage, client *irc.Client) {
|
||||
AsServerMsg: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, message, msgOpts)
|
||||
}
|
||||
|
||||
func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
|
||||
@@ -605,8 +525,6 @@ func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
|
||||
handleQuit(msg, client)
|
||||
case commands.PART:
|
||||
handlePart(msg, client)
|
||||
case commands.CAP:
|
||||
handleCAP(msg, client)
|
||||
case commands.RPL_WELCOME:
|
||||
handleWELCOME(msg, client)
|
||||
case commands.RPL_YOURHOST:
|
||||
@@ -663,8 +581,6 @@ func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
|
||||
handleWHOISMODES(msg, client)
|
||||
case commands.ERR_NOSUCHSERVER:
|
||||
handleNOSUCHSERVER(msg, client)
|
||||
case commands.ERR_UNKNOWNCOMMAND:
|
||||
handleUNKNOWNCOMMAND(msg, client)
|
||||
case commands.ERR_NONICKNAMEGIVEN:
|
||||
handleNONICKNAMEGIVEN(msg, client)
|
||||
case commands.ERR_NEEDMOREPARAMS:
|
||||
@@ -685,7 +601,7 @@ func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
|
||||
NotImpl: true,
|
||||
}
|
||||
|
||||
client.Channels[0].AppendMsg(msg.Timestamp, fullMsg, msgOpts)
|
||||
client.Channels[0].AppendMsg(client.Host, client.DB, msg.Timestamp, fullMsg, msgOpts)
|
||||
}
|
||||
|
||||
// send a receivedIRCmsg tea message so the ui can update
|
||||
|
||||
+6
-19
@@ -17,6 +17,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
@@ -25,24 +26,6 @@ import (
|
||||
"github.com/illusionman1212/gorc/irc/commands"
|
||||
)
|
||||
|
||||
func handleSlashJoin(params []string, client *irc.Client) tea.Cmd {
|
||||
channel := ""
|
||||
if len(params) > 0 {
|
||||
channel = params[0]
|
||||
}
|
||||
|
||||
for i, c := range client.Channels {
|
||||
if c.Name == channel {
|
||||
client.ActiveChannelIndex = i
|
||||
client.ActiveChannel = channel
|
||||
return cmds.SwitchChannels
|
||||
}
|
||||
}
|
||||
client.SendCommand(commands.JOIN, params...)
|
||||
|
||||
return cmds.SwitchChannels
|
||||
}
|
||||
|
||||
func HandleSlashCommand(msg string, client *irc.Client) tea.Cmd {
|
||||
substrs := strings.Fields(msg[1:])
|
||||
command := strings.ToUpper(substrs[0])
|
||||
@@ -53,10 +36,14 @@ func HandleSlashCommand(msg string, client *irc.Client) tea.Cmd {
|
||||
|
||||
switch command {
|
||||
case commands.JOIN:
|
||||
return handleSlashJoin(params, client)
|
||||
client.SendCommand(commands.JOIN, params...)
|
||||
return cmds.SwitchChannels
|
||||
case commands.QUIT:
|
||||
client.SendCommand(command, params...)
|
||||
return cmds.Quit(client)
|
||||
case "TEST":
|
||||
log.Println("running the spammer")
|
||||
return cmds.Tick(client)
|
||||
default:
|
||||
client.SendCommand(command, params...)
|
||||
return nil
|
||||
|
||||
+3
-56
@@ -18,7 +18,6 @@ package parser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -35,44 +34,9 @@ type IRCMessage struct {
|
||||
type Tags map[string]string
|
||||
|
||||
func (m *IRCMessage) setTimestamp() {
|
||||
if serverTime, ok := m.Tags["time"]; ok {
|
||||
t, err := time.Parse("2006-01-02T15:04:05.000Z", serverTime)
|
||||
if err != nil {
|
||||
// TODO: properly handle error
|
||||
log.Fatalln(err)
|
||||
}
|
||||
m.Timestamp = fmt.Sprintf("[%02d:%02d]", t.Local().Hour(), t.Local().Minute())
|
||||
} else {
|
||||
// TODO: get timestamp from time tag
|
||||
now := time.Now()
|
||||
m.Timestamp = fmt.Sprintf("[%02d:%02d]", now.Hour(), now.Minute())
|
||||
}
|
||||
}
|
||||
|
||||
func escapeCharacters(char rune) string {
|
||||
switch char {
|
||||
case 's':
|
||||
return " "
|
||||
case 'n':
|
||||
return "\n"
|
||||
case 'r':
|
||||
return "\r"
|
||||
case 'b':
|
||||
return "\b"
|
||||
case 't':
|
||||
return "\t"
|
||||
case 'f':
|
||||
return "\f"
|
||||
case ':':
|
||||
return ";"
|
||||
case '\\':
|
||||
fallthrough
|
||||
case '\'':
|
||||
fallthrough
|
||||
case '"':
|
||||
fallthrough
|
||||
default:
|
||||
return string(char)
|
||||
}
|
||||
m.Timestamp = fmt.Sprintf("[%02d:%02d:%02d.%03d]", now.Hour(), now.Minute(), now.Second(), now.Nanosecond()/1000000)
|
||||
}
|
||||
|
||||
func parseTags(rawTags string) Tags {
|
||||
@@ -87,24 +51,7 @@ func parseTags(rawTags string) Tags {
|
||||
|
||||
// we need to make sure a "value" exists before accessing str[1]
|
||||
if len(str) > 1 {
|
||||
rawValue := str[1]
|
||||
for idx := 0; idx < len(rawValue); {
|
||||
// if we have a backslash at the end of the string
|
||||
// we need to ignore it
|
||||
if idx == len(rawValue)-1 && rawValue[idx] == '\\' {
|
||||
idx += 2
|
||||
continue
|
||||
}
|
||||
|
||||
if rawValue[idx] == '\\' {
|
||||
value += escapeCharacters(rune(rawValue[idx+1]))
|
||||
idx += 2
|
||||
continue
|
||||
}
|
||||
|
||||
value += string(rawValue[idx])
|
||||
idx++
|
||||
}
|
||||
value = str[1]
|
||||
}
|
||||
|
||||
tags[key] = value
|
||||
|
||||
@@ -341,6 +341,7 @@ func TestParser(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: fails
|
||||
t.Run("Test escaped tags", func(t *testing.T) {
|
||||
testMessage := "@a=b\\\\and\\nk;c=72\\s45;d=gh\\:764 foo"
|
||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||
@@ -400,6 +401,7 @@ func TestParser(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: FAIL
|
||||
t.Run("Test single param with preceeding :", func(t *testing.T) {
|
||||
testMessage := ":src JOIN :#chan"
|
||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||
@@ -650,6 +652,7 @@ func TestParser(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: FAIL
|
||||
t.Run("Test yaml encoding with slashes", func(t *testing.T) {
|
||||
testMessage := "@foo=\\\\\\\\\\:\\\\s\\s\\r\\n COMMAND"
|
||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||
@@ -746,6 +749,7 @@ func TestParser(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: FAIL
|
||||
t.Run("Test tags. they should be parsed char-at-a-time", func(t *testing.T) {
|
||||
testMessage := "@tag1=value\\\\ntest COMMAND"
|
||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||
@@ -767,6 +771,7 @@ func TestParser(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: FAIl
|
||||
t.Run("Test tag escape for char that doesn't need it", func(t *testing.T) {
|
||||
testMessage := "@tag1=value\\1 COMMAND"
|
||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||
@@ -784,6 +789,7 @@ func TestParser(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
// TODO: FAIL
|
||||
t.Run("Test slash at end of tag", func(t *testing.T) {
|
||||
testMessage := "@tag1=value1\\ COMMAND"
|
||||
ircMessage, valid := ParseIRCMessage(testMessage)
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"log"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/illusionman1212/gorc/db"
|
||||
"github.com/illusionman1212/gorc/ui/app"
|
||||
)
|
||||
|
||||
@@ -32,7 +33,13 @@ func main() {
|
||||
tea.WithMouseCellMotion(),
|
||||
)
|
||||
|
||||
db, err := db.ConnectToDB()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
gorc.Client.Tea = p
|
||||
gorc.Client.DB = db
|
||||
|
||||
f, err := tea.LogToFile("gorc.log", "gorc")
|
||||
defer f.Close()
|
||||
|
||||
+1
-4
@@ -61,21 +61,18 @@ func NewLogin() State {
|
||||
t.Focus()
|
||||
t.CharLimit = 40
|
||||
t.TextStyle = FocusedStyle
|
||||
t.Validate = NoSpacesValidation
|
||||
case 1:
|
||||
t.Placeholder = "Port"
|
||||
t.CharLimit = 5
|
||||
t.Validate = ValidatePort
|
||||
case 2:
|
||||
t.Placeholder = "Channel"
|
||||
t.CharLimit = 32
|
||||
case 3:
|
||||
t.Placeholder = "Nickname"
|
||||
t.CharLimit = 32
|
||||
t.Validate = NoSpacesValidation
|
||||
case 4:
|
||||
t.Placeholder = "Password"
|
||||
t.CharLimit = 64
|
||||
t.CharLimit = 32
|
||||
t.EchoMode = textinput.EchoPassword
|
||||
t.EchoCharacter = '•'
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
// gorc project
|
||||
// Copyright (C) 2023 IllusionMan1212
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, see https://www.gnu.org/licenses.
|
||||
|
||||
package login
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func NoSpacesValidation(input string) error {
|
||||
if strings.Contains(input, " ") {
|
||||
return errors.New("Field cannot contain spaces")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidatePort(input string) error {
|
||||
pNum, err := strconv.ParseUint(input, 10, 16)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if pNum < 1 || pNum > 65535 {
|
||||
return errors.New("Invalid port number")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+57
-10
@@ -57,7 +57,9 @@ type State struct {
|
||||
|
||||
func NewMainScreen(client *irc.Client) State {
|
||||
newViewport := viewport.New(0, 0)
|
||||
newViewport.Wrap = viewport.Wrap
|
||||
newViewport.Style = MessagesStyle.Copy()
|
||||
newViewport.MouseWheelEnabled = false
|
||||
|
||||
return State{
|
||||
Client: client,
|
||||
@@ -76,9 +78,25 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case cmds.ReceivedIRCMsgMsg:
|
||||
wasAtBottom := s.Viewport.AtBottom()
|
||||
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History)
|
||||
s.Viewport.SetContent(strings.Join(s.Client.Channels[s.Client.ActiveChannelIndex].ScrollBackBuf, "\n"))
|
||||
|
||||
if wasAtBottom {
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AtBottom = true
|
||||
s.Viewport.GotoBottom()
|
||||
} else {
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AtBottom = false
|
||||
}
|
||||
return s, nil
|
||||
case cmds.ReadHistoryFromDBMsg:
|
||||
s.Viewport.SetContent(strings.Join(s.Client.Channels[s.Client.ActiveChannelIndex].ScrollBackBuf, "\n"))
|
||||
switch msg.Direction {
|
||||
case "top":
|
||||
s.Viewport.GotoTop()
|
||||
case "up":
|
||||
s.Viewport.SetYOffset(s.Viewport.YOffset + int(msg.LinesRead))
|
||||
case "down":
|
||||
s.Viewport.SetYOffset(s.Viewport.YOffset - int(msg.LinesRead))
|
||||
case "bottom":
|
||||
s.Viewport.GotoBottom()
|
||||
}
|
||||
return s, nil
|
||||
@@ -93,17 +111,17 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
|
||||
WithTimestamp: true,
|
||||
}
|
||||
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AppendMsg(msg.Timestamp, fullMsg, msgOpts)
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AppendMsg(s.Client.Host, s.Client.DB, msg.Timestamp, fullMsg, msgOpts)
|
||||
// TODO: make sure to only append the message to the history if server sends back no errors
|
||||
s.Client.SendCommand(commands.PRIVMSG, s.Client.ActiveChannel, msg.Msg)
|
||||
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History)
|
||||
s.Viewport.SetContent(strings.Join(s.Client.Channels[s.Client.ActiveChannelIndex].ScrollBackBuf, "\n"))
|
||||
s.Viewport.GotoBottom()
|
||||
}
|
||||
}
|
||||
|
||||
return s, nil
|
||||
case cmds.SwitchChannelsMsg:
|
||||
s.Viewport.SetContent(s.Client.Channels[s.Client.ActiveChannelIndex].History)
|
||||
s.Viewport.SetContent(strings.Join(s.Client.Channels[s.Client.ActiveChannelIndex].ScrollBackBuf, "\n"))
|
||||
s.Viewport.GotoBottom()
|
||||
|
||||
*s.SidePanel, cmd = s.SidePanel.Update(msg)
|
||||
@@ -112,6 +130,9 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
|
||||
s.Client.LastTabIndexInTabBar = len(s.Client.Channels) - 1
|
||||
s.TabRenderingDirection = Left
|
||||
return s, nil
|
||||
case cmds.TickMsg:
|
||||
s.Client.Ticks++
|
||||
return s, cmds.Tick(s.Client)
|
||||
case tea.KeyMsg:
|
||||
key := msg.String()
|
||||
switch key {
|
||||
@@ -179,11 +200,37 @@ func (s State) Update(msg tea.Msg) (State, tea.Cmd) {
|
||||
return s, cmds.SwitchChannels
|
||||
case "g":
|
||||
if s.FocusIndex == Viewport {
|
||||
s.Viewport.GotoTop()
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AtBottom = false
|
||||
if !s.Viewport.AtTop() {
|
||||
cmdsToProcess = append(cmdsToProcess, cmds.ReadHistoryFromDB(s.Client, irc.SCROLLBACK_MAX, "top"))
|
||||
}
|
||||
}
|
||||
case "G":
|
||||
if s.FocusIndex == Viewport {
|
||||
s.Viewport.GotoBottom()
|
||||
if !s.Viewport.AtBottom() {
|
||||
cmdsToProcess = append(cmdsToProcess, cmds.ReadHistoryFromDB(s.Client, irc.SCROLLBACK_MAX, "bottom"))
|
||||
}
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AtBottom = true
|
||||
}
|
||||
case "j", "k", "d", "u", "f", "b", "up", "down":
|
||||
// TODO: what???
|
||||
// if s.Viewport.AtBottom() {
|
||||
// break
|
||||
// }
|
||||
|
||||
if key == "j" || key == "d" || key == "f" || key == "down" {
|
||||
if s.Viewport.ScrollPercent() > 0.5 {
|
||||
excess := s.Viewport.ScrollPercent() - 0.5 + 0.1
|
||||
linesToRead := int(math.Floor(excess * float64(irc.SCROLLBACK_MAX)))
|
||||
cmdsToProcess = append(cmdsToProcess, cmds.ReadHistoryFromDB(s.Client, linesToRead, "down"))
|
||||
}
|
||||
} else {
|
||||
s.Client.Channels[s.Client.ActiveChannelIndex].AtBottom = false
|
||||
if s.Viewport.ScrollPercent() < 0.5 && s.Viewport.ScrollPercent() > 0.0 {
|
||||
excess := 0.5 - s.Viewport.ScrollPercent() + 0.1
|
||||
linesToRead := int(math.Floor(excess * float64(irc.SCROLLBACK_MAX)))
|
||||
cmdsToProcess = append(cmdsToProcess, cmds.ReadHistoryFromDB(s.Client, linesToRead, "up"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -226,13 +273,13 @@ func (s *State) Blur() {
|
||||
|
||||
func (s *State) SetSize(width, height int) {
|
||||
s.InputBox.SetSize(width)
|
||||
s.SidePanel.SetSize(width, height, s.InputBox.Style.GetVerticalPadding())
|
||||
s.SidePanel.SetSize(width, height, s.InputBox.Style.GetVerticalFrameSize())
|
||||
|
||||
// We floor because width is an int and some fractions are lost when casting
|
||||
// and also because we ceil the sidepanel's width
|
||||
// -3 for the tab bar height
|
||||
newWidth := int(math.Floor(float64(width) * 8 / 10))
|
||||
newHeight := height - s.InputBox.Style.GetVerticalFrameSize() - 3
|
||||
newWidth := int(math.Floor(float64(width)*8/10) - float64(s.Viewport.Style.GetHorizontalFrameSize()))
|
||||
newHeight := height - s.InputBox.Style.GetVerticalFrameSize() - s.Viewport.Style.GetVerticalFrameSize() - 3
|
||||
|
||||
s.Viewport.Width = newWidth
|
||||
s.Viewport.Height = newHeight
|
||||
@@ -243,7 +290,7 @@ func (s *State) SetSize(width, height int) {
|
||||
// we need this to render an empty viewport
|
||||
history := ""
|
||||
if len(s.Client.Channels) != 0 {
|
||||
history = s.Client.Channels[s.Client.ActiveChannelIndex].History
|
||||
history = strings.Join(s.Client.Channels[s.Client.ActiveChannelIndex].ScrollBackBuf, "\n")
|
||||
}
|
||||
|
||||
// we need to re-set the content because words wrap differently on different sizes
|
||||
|
||||
@@ -39,7 +39,7 @@ func (s *SidePanelState) getHeader() string {
|
||||
if len(s.Client.Channels) != 0 {
|
||||
usersCount = len(s.Client.Channels[s.Client.ActiveChannelIndex].Users)
|
||||
}
|
||||
separator := strings.Repeat("—", s.Viewport.Width-s.Viewport.Style.GetHorizontalFrameSize()) + "\n"
|
||||
separator := strings.Repeat("—", s.Viewport.Width) + "\n"
|
||||
header := fmt.Sprintf("Users in this channel (%d)\n", usersCount) + separator
|
||||
|
||||
return header
|
||||
@@ -61,6 +61,7 @@ func (s *SidePanelState) getLatestNicks() []string {
|
||||
func NewSidePanel(client *irc.Client) *SidePanelState {
|
||||
newViewport := viewport.New(0, 0)
|
||||
newViewport.Style = SidePanelStyle.Copy()
|
||||
newViewport.Wrap = viewport.Wrap
|
||||
|
||||
return &SidePanelState{
|
||||
Client: client,
|
||||
@@ -118,7 +119,7 @@ func (s *SidePanelState) UpdateNicks() {
|
||||
func (s *SidePanelState) SetSize(width, height, inputboxHeight int) {
|
||||
// We ceil here because width is an int and some fractions are lost
|
||||
// -1 is for some extra invisible margin or padding between the sidepanel and the inputbox
|
||||
newWidth := int(math.Ceil((float64(width) * 2 / 10)))
|
||||
newWidth := int(math.Ceil(math.Ceil((float64(width) * 2 / 10)) - float64(s.Viewport.Style.GetHorizontalFrameSize())))
|
||||
newHeight := height - inputboxHeight - s.Viewport.Style.GetVerticalFrameSize() - 1
|
||||
|
||||
s.Viewport.Style = s.Viewport.Style.Width(newWidth)
|
||||
|
||||
Reference in New Issue
Block a user