WIP: offloading to disk

This commit is contained in:
2023-02-19 00:58:42 +02:00
parent 3c04345560
commit 3ddd238552
11 changed files with 372 additions and 64 deletions
+62 -6
View File
@@ -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,6 +97,12 @@ 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
@@ -96,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
@@ -119,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) {
@@ -138,6 +192,7 @@ func (s *Client) Initialize(host string, port string, tlsEnabled bool) {
s.Port = port
s.ActiveChannel = host
s.Channels = make([]Channel, 0)
s.Ticks = 1
return
}
@@ -156,6 +211,7 @@ func (s *Client) Initialize(host string, port string, tlsEnabled bool) {
s.Port = port
s.ActiveChannel = host
s.Channels = make([]Channel, 0)
s.Ticks = 1
}
func (c *Client) Register(nick string, password string, channel string) {
@@ -188,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
}
+43 -31
View File
@@ -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)
}
}
@@ -180,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) {
@@ -191,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) {
@@ -202,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) {
@@ -213,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) {
@@ -224,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) {
@@ -235,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) {
@@ -246,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) {
@@ -257,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) {
@@ -268,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) {
@@ -279,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) {
@@ -290,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) {
@@ -312,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) {
@@ -331,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) {
@@ -355,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) {
@@ -366,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) {
@@ -379,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) {
@@ -420,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) {
@@ -431,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) {
@@ -442,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) {
@@ -453,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) {
@@ -464,7 +476,7 @@ func handleNOSUCHSERVER(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 handleNONICKNAMEGIVEN(msg parser.IRCMessage, client *irc.Client) {
@@ -475,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) {
@@ -486,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) {
@@ -497,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) {
@@ -589,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
View File
@@ -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
+1 -1
View File
@@ -36,7 +36,7 @@ type Tags map[string]string
func (m *IRCMessage) setTimestamp() {
// TODO: get timestamp from time tag
now := time.Now()
m.Timestamp = fmt.Sprintf("[%02d:%02d]", now.Hour(), now.Minute())
m.Timestamp = fmt.Sprintf("[%02d:%02d:%02d.%03d]", now.Hour(), now.Minute(), now.Second(), now.Nanosecond()/1000000)
}
func parseTags(rawTags string) Tags {