feat: message timestamps

* made it easier to styles and prefix messages
* added timestamps to messages
* added prefix and styles to server messages
* append current date to host channel
* fixed some styles issues
This commit is contained in:
2022-02-19 16:24:39 +02:00
parent 6c538cc352
commit f4dd0066c8
11 changed files with 222 additions and 51 deletions
+59 -6
View File
@@ -22,8 +22,12 @@ import (
"log"
"net"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/irc/commands"
"github.com/illusionman1212/gorc/ui"
)
type User struct {
@@ -80,8 +84,44 @@ type Client struct {
LastTabIndexInTabBar int
}
type MsgFmtOpts struct {
WithTimestamp bool
AsServerMsg bool
NotImpl bool
AsDate bool
}
var notImpl = lipgloss.NewStyle().Foreground(ui.ErrorColor).Render("[NOT IMPL]")
var serverMsgStyle = lipgloss.NewStyle().Foreground(ui.ServerMsgColor)
var timestampStyle = serverMsgStyle.Copy()
var dateStyle = lipgloss.NewStyle().Foreground(ui.DateColor)
const CRLF = "\r\n"
func (c *Channel) AppendMsg(timestamp string, fullMsg string, opts MsgFmtOpts) {
prefixes := ""
style := ui.DefaultStyle
if opts.WithTimestamp {
prefixes += timestampStyle.Render(timestamp) + " "
}
if opts.NotImpl {
prefixes += notImpl + " "
}
if opts.AsServerMsg {
prefixes += serverMsgStyle.Render("==") + " "
style = serverMsgStyle
}
if opts.AsDate {
style = dateStyle
}
c.History += prefixes + style.Render(fullMsg) + CRLF
}
func (s *Client) Initialize(host string, port string, tlsEnabled bool) {
addr := fmt.Sprintf("%s:%s", host, port)
@@ -124,22 +164,35 @@ func (c *Client) Register(nick string, password string, channel string) {
Users: make(map[string]User),
})
c.SendCommand("CAP", "LS")
c.SendCommand(commands.CAP, "LS")
if password != "" {
c.SendCommand("PASS", password)
c.SendCommand(commands.PASS, password)
}
// TODO: check if nickname has spaces and remove them
c.SendCommand("NICK", nick)
c.SendCommand(commands.NICK, nick)
// set user-wanted nickname
c.Nickname = nick
c.SendCommand("USER", nick, "0", "*", nick)
c.SendCommand(commands.USER, nick, "0", "*", nick)
// TODO: CAP REQ :whatever capability the client recognizes and supports
c.SendCommand("CAP", "END")
c.SendCommand(commands.CAP, "END")
// joining a channel when registering is optional
if channel != "" {
c.SendCommand("JOIN", channel)
c.SendCommand(commands.JOIN, channel)
}
}
func (c *Client) SetDay() {
msgOpts := MsgFmtOpts{
AsDate: true,
}
now := time.Now()
msg := fmt.Sprintf("————— %s %d —————", now.Month().String(), now.Day())
c.Channels[0].AppendMsg("", msg, msgOpts)
// TODO: append new day to each channel whenever the day changes
}
func (c Client) SendCommand(cmd string, params ...string) {
if c.TcpConn == nil {
// TODO: properly handle the error instead of Fatal-ing
+116 -29
View File
@@ -23,16 +23,12 @@ import (
"log"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/illusionman1212/gorc/cmds"
"github.com/illusionman1212/gorc/irc"
"github.com/illusionman1212/gorc/irc/commands"
"github.com/illusionman1212/gorc/irc/parser"
"github.com/illusionman1212/gorc/ui"
)
var notImpl = lipgloss.NewStyle().Foreground(ui.Red).Render("[NOT IMPL]")
func ReadLoop(client *irc.Client) {
// 512 bytes as a base + 8192 additional bytes for tags
r := bufio.NewReaderSize(client.TcpConn, 8192+512)
@@ -65,9 +61,13 @@ func handlePrivMsg(msg parser.IRCMessage, client *irc.Client) {
msgContent := msg.Parameters[1]
privMsg := fmt.Sprintf("%s: %s", nick, msgContent)
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
}
for i, c := range client.Channels {
if c.Name == channel {
client.Channels[i].History += privMsg + irc.CRLF
client.Channels[i].AppendMsg(msg.Timestamp, privMsg, msgOpts)
}
}
}
@@ -76,7 +76,12 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
nick := strings.SplitN(msg.Source, "!", 2)[0]
channel := msg.Parameters[0]
joinMsg := fmt.Sprintf("== %s has joined", nick)
joinMsg := fmt.Sprintf("%s has joined", nick)
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
if nick == client.Nickname {
client.Channels = append(client.Channels, irc.Channel{
@@ -85,7 +90,7 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
})
client.ActiveChannelIndex = len(client.Channels) - 1
client.ActiveChannel = channel
client.Channels[client.ActiveChannelIndex].History += joinMsg + irc.CRLF
client.Channels[client.ActiveChannelIndex].AppendMsg(msg.Timestamp, joinMsg, msgOpts)
if _, exists := client.Channels[client.ActiveChannelIndex].Users[nick]; !exists {
client.Channels[client.ActiveChannelIndex].Users[nick] = irc.User{}
}
@@ -93,7 +98,7 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
} else {
for i, c := range client.Channels {
if c.Name == channel {
client.Channels[i].History += joinMsg + irc.CRLF
client.Channels[i].AppendMsg(msg.Timestamp, joinMsg, msgOpts)
if _, exists := client.Channels[i].Users[nick]; !exists {
client.Channels[i].Users[nick] = irc.User{}
}
@@ -109,14 +114,19 @@ func handleJoin(msg parser.IRCMessage, client *irc.Client) {
func handleQuit(msg parser.IRCMessage, client *irc.Client) {
nick := strings.SplitN(msg.Source, "!", 2)[0]
reason := msg.Parameters[0]
quitMsg := fmt.Sprintf("== %s has quit (%s)", nick, reason)
quitMsg := fmt.Sprintf("%s has quit (%s)", nick, reason)
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
for i := range client.Channels {
// skip the server "channel"
if i == 0 {
continue
}
client.Channels[i].History += quitMsg + irc.CRLF
client.Channels[i].AppendMsg(msg.Timestamp, quitMsg, msgOpts)
delete(client.Channels[i].Users, nick)
}
@@ -131,7 +141,12 @@ func handlePart(msg parser.IRCMessage, client *irc.Client) {
reason = msg.Parameters[1]
}
partMsg := fmt.Sprintf("== %s has left %s (%s)", nick, channel, reason)
partMsg := fmt.Sprintf("%s has left %s (%s)", nick, channel, reason)
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
for i, c := range client.Channels {
if c.Name == channel {
@@ -143,7 +158,7 @@ func handlePart(msg parser.IRCMessage, client *irc.Client) {
}
client.LastTabIndexInTabBar--
} else {
client.Channels[i].History += partMsg + irc.CRLF
client.Channels[i].AppendMsg(msg.Timestamp, partMsg, msgOpts)
delete(client.Channels[i].Users, nick)
}
}
@@ -153,69 +168,127 @@ func handlePart(msg parser.IRCMessage, client *irc.Client) {
}
func handleWELCOME(msg parser.IRCMessage, client *irc.Client) {
nick := msg.Parameters[0]
welcomeMsg := msg.Parameters[1]
client.Channels[0].History += welcomeMsg + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
// set server-registered nickname
client.Nickname = nick
client.Channels[0].AppendMsg(msg.Timestamp, welcomeMsg, msgOpts)
}
func handleYOURHOST(msg parser.IRCMessage, client *irc.Client) {
host := msg.Parameters[1]
client.Channels[0].History += host + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, host, msgOpts)
}
func handleCREATED(msg parser.IRCMessage, client *irc.Client) {
created := msg.Parameters[1]
client.Channels[0].History += created + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, created, msgOpts)
}
func handleMYINFO(msg parser.IRCMessage, client *irc.Client) {
info := strings.Join(msg.Parameters[1:], " ")
client.Channels[0].History += info + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, info, msgOpts)
}
func handleLUSERCLIENT(msg parser.IRCMessage, client *irc.Client) {
message := msg.Parameters[1]
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleLUSEROP(msg parser.IRCMessage, client *irc.Client) {
message := strings.Join(msg.Parameters[1:], " ")
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleLUSERUNKNOWN(msg parser.IRCMessage, client *irc.Client) {
message := strings.Join(msg.Parameters[1:], " ")
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleLUSERCHANNELS(msg parser.IRCMessage, client *irc.Client) {
message := strings.Join(msg.Parameters[1:], " ")
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleLUSERME(msg parser.IRCMessage, client *irc.Client) {
message := msg.Parameters[1]
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleLOCALUSERS(msg parser.IRCMessage, client *irc.Client) {
message := msg.Parameters[1]
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleGLOBALUSERS(msg parser.IRCMessage, client *irc.Client) {
message := msg.Parameters[1]
client.Channels[0].History += message + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleNAMREPLY(msg parser.IRCMessage, client *irc.Client) {
@@ -249,15 +322,25 @@ func handleNAMREPLY(msg parser.IRCMessage, client *irc.Client) {
}
func handleMOTDStart(msg parser.IRCMessage, client *irc.Client) {
params := strings.Join(msg.Parameters[1:], " ")
message := strings.Join(msg.Parameters[1:], " ")
client.Channels[0].History += params + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, message, msgOpts)
}
func handleMOTD(msg parser.IRCMessage, client *irc.Client) {
messageLine := msg.Parameters[1]
client.Channels[0].History += messageLine + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
AsServerMsg: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, messageLine, msgOpts)
}
func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
@@ -315,15 +398,19 @@ func HandleCommand(msg parser.IRCMessage, client *irc.Client) {
// and log an error if timeout ends without receiving this command.
default:
fullMsg := fmt.Sprintf(
"%s %s %s %s %s",
notImpl,
"%s %s %s %s",
msg.Tags,
msg.Source,
msg.Command,
strings.Join(msg.Parameters, " "),
)
client.Channels[0].History += fullMsg + irc.CRLF
msgOpts := irc.MsgFmtOpts{
WithTimestamp: true,
NotImpl: true,
}
client.Channels[0].AppendMsg(msg.Timestamp, fullMsg, msgOpts)
}
// send a receivedIRCmsg tea message so the ui can update
+11 -1
View File
@@ -17,12 +17,14 @@
package parser
import (
"fmt"
"regexp"
"strings"
"time"
)
type IRCMessage struct {
// TODO: prefix: that will hold the timestamp AND the server message prefix (==)
Timestamp string
Tags Tags // starts with @ | Optional
Source string // starts with : | Optional
Command string // can either be a string or a numeric value | Required
@@ -31,6 +33,12 @@ type IRCMessage struct {
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())
}
func parseTags(rawTags string) Tags {
tags := make(Tags, 0)
rawTagsSlice := strings.Split(rawTags, ";")
@@ -108,5 +116,7 @@ func ParseIRCMessage(line string) (IRCMessage, bool) {
ircMessage.Parameters = finalParams
}
ircMessage.setTimestamp()
return ircMessage, true
}