Skip to content

Adding global timestamp option to messages. #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion chat/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions chat/message/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions cmd/ssh-chat/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down