diff --git a/chat/message/message.go b/chat/message/message.go index 5b4f76a..53ca527 100644 --- a/chat/message/message.go +++ b/chat/message/message.go @@ -108,7 +108,9 @@ func (m PublicMsg) Render(t *Theme) string { if t == nil { return m.String() } - + if (m.From().GetTimeStampVisible()) { + return fmt.Sprintf("[%s] %s: %s", time.Now().Format("15:04:05"), t.ColorName(m.from), m.body) + } return fmt.Sprintf("%s: %s", t.ColorName(m.from), m.body) } @@ -125,10 +127,16 @@ func (m PublicMsg) RenderFor(cfg UserConfig) string { if cfg.Bell { body += Bel } + if (m.From().GetTimeStampVisible()) { + return fmt.Sprintf("[%s] %s: %s", time.Now().Format("15:04:05"), cfg.Theme.ColorName(m.from), m.body) + } return fmt.Sprintf("%s: %s", cfg.Theme.ColorName(m.from), body) } func (m PublicMsg) String() string { + if (m.From().GetTimeStampVisible()) { + return fmt.Sprintf("[%s] %s: %s", time.Now().Format("15:04:05"), m.from.Name(), m.body) + } return fmt.Sprintf("%s: %s", m.from.Name(), m.body) } @@ -151,6 +159,9 @@ func NewEmoteMsg(body string, from *User) *EmoteMsg { } func (m EmoteMsg) Render(t *Theme) string { + if (m.from.GetTimeStampVisible()) { + return fmt.Sprintf("[%s] ** %s %s", time.Now().Format("15:04:05"), m.from.Name(), m.body) + } return fmt.Sprintf("** %s %s", m.from.Name(), m.body) } @@ -177,6 +188,9 @@ func (m PrivateMsg) To() *User { func (m PrivateMsg) Render(t *Theme) string { s := fmt.Sprintf("[PM from %s] %s", m.from.Name(), m.body) + if (m.from.GetTimeStampVisible()) { + s = fmt.Sprintf("[%s][PM from %s] %s", time.Now().Format("15:04:05"), m.from.Name(), m.body) + } if t == nil { return s } diff --git a/chat/message/user.go b/chat/message/user.go index d6f97fa..6f9add2 100644 --- a/chat/message/user.go +++ b/chat/message/user.go @@ -33,6 +33,7 @@ type User struct { mu sync.Mutex config UserConfig replyTo *User // Set when user gets a /msg, for replying. + TimeStamp bool } func NewUser(identity Identifier) *User { @@ -43,6 +44,7 @@ func NewUser(identity Identifier) *User { msg: make(chan Message, messageBuffer), done: make(chan struct{}), Ignored: set.New(), + TimeStamp: false, } u.setColorIdx(rand.Int()) @@ -74,6 +76,20 @@ func (u *User) SetID(id string) { u.setColorIdx(rand.Int()) } +// Sets visiblity of timestamp. +func (u *User) SetTimeStampVisible(TSvis bool) { + u.mu.Lock() + defer u.mu.Unlock() + u.TimeStamp = TSvis +} + +// Gets visibility status of timestamp. +func (u *User) GetTimeStampVisible() bool { + u.mu.Lock() + defer u.mu.Unlock() + return u.TimeStamp +} + // ReplyTo returns the last user that messaged this user. func (u *User) ReplyTo() *User { u.mu.Lock() diff --git a/cmd/ssh-chat/cmd.go b/cmd/ssh-chat/cmd.go index 7c7d000..a5eb251 100644 --- a/cmd/ssh-chat/cmd.go +++ b/cmd/ssh-chat/cmd.go @@ -36,6 +36,7 @@ type Options struct { Motd string `long:"motd" description:"Optional Message of the Day file."` Log string `long:"log" description:"Write chat log to this file."` Pprof int `long:"pprof" description:"Enable pprof http server for profiling."` + TStamps bool `long:"timestamps" description:"Enable"` } var logLevels = []log.Level{ @@ -122,6 +123,7 @@ func main() { host := sshchat.NewHost(s, auth) host.SetTheme(message.Themes[0]) host.Version = Version + host.SetTimeStamp(options.TStamps) err = fromFile(options.Admin, func(line []byte) error { key, _, _, _, err := ssh.ParseAuthorizedKey(line) diff --git a/host.go b/host.go index 752cf2f..2beb488 100644 --- a/host.go +++ b/host.go @@ -25,6 +25,12 @@ func GetPrompt(user *message.User) string { if cfg.Theme != nil { name = cfg.Theme.ColorName(user) } + + // user timestamp visibility prompt format + if (user.GetTimeStampVisible()) { + return fmt.Sprintf("[%s][%s] ", time.Now().Format("15:04:05"), name) + } + return fmt.Sprintf("[%s] ", name) } @@ -45,6 +51,7 @@ type Host struct { mu sync.Mutex motd string count int + tstamp bool } // NewHost creates a Host on top of an existing listener. @@ -66,6 +73,13 @@ func NewHost(listener *sshd.SSHListener, auth *Auth) *Host { return &h } +// SetTimeStamp sets the visibility of the timestamp globally in messages. +func (h *Host) SetTimeStamp(vis bool) { + h.mu.Lock() + h.tstamp = vis + h.mu.Unlock() +} + // SetTheme sets the default theme for the host. func (h *Host) SetTheme(theme message.Theme) { h.mu.Lock() @@ -123,6 +137,12 @@ func (h *Host) Connect(term *sshd.Terminal) { return } + // Set global timestamp visibility in messages if the option was + // specified on the command line. + if (h.tstamp) { + user.SetTimeStampVisible(true) + } + // Successfully joined. term.SetPrompt(GetPrompt(user)) term.AutoCompleteCallback = h.AutoCompleteFunction(user)