mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-07-01 19:24:34 +02:00
chat: /away tweaks
This commit is contained in:
parent
0338cb824d
commit
9329227403
@ -461,26 +461,19 @@ func InitCommands(c *Commands) {
|
|||||||
|
|
||||||
c.Add(Command{
|
c.Add(Command{
|
||||||
Prefix: "/away",
|
Prefix: "/away",
|
||||||
PrefixHelp: "[AWAY MESSAGE]",
|
PrefixHelp: "[REASON]",
|
||||||
|
Help: "Set away reason, or empty to unset.",
|
||||||
Handler: func(room *Room, msg message.CommandMsg) error {
|
Handler: func(room *Room, msg message.CommandMsg) error {
|
||||||
awayMsg := strings.TrimSpace(strings.TrimLeft(msg.Body(), "/away"))
|
awayMsg := strings.TrimSpace(strings.TrimLeft(msg.Body(), "/away"))
|
||||||
isAway, _, _ := msg.From().GetAway()
|
isAway, _, _ := msg.From().GetAway()
|
||||||
if awayMsg == "" {
|
|
||||||
if isAway {
|
|
||||||
msg.From().SetActive()
|
|
||||||
room.Send(message.NewSystemMsg("You are marked as active, welcome back!", msg.From()))
|
|
||||||
room.Send(message.NewEmoteMsg("is back", msg.From()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
room.Send(message.NewSystemMsg("Not away. Add an away message to set away.", msg.From()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.From().SetAway(awayMsg)
|
msg.From().SetAway(awayMsg)
|
||||||
room.Send(message.NewSystemMsg("You are marked as away, enjoy your excursion!", msg.From()))
|
if awayMsg != "" {
|
||||||
|
room.Send(message.NewEmoteMsg("has gone away: "+awayMsg, msg.From()))
|
||||||
room.Send(message.NewEmoteMsg("has gone away: "+awayMsg, msg.From()))
|
} else if !isAway {
|
||||||
|
room.Send(message.NewSystemMsg("Not away. Append a reason message to set away.", msg.From()))
|
||||||
|
} else {
|
||||||
|
room.Send(message.NewEmoteMsg("is back.", msg.From()))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
@ -33,13 +33,12 @@ type User struct {
|
|||||||
screen io.WriteCloser
|
screen io.WriteCloser
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
config UserConfig
|
config UserConfig
|
||||||
replyTo *User // Set when user gets a /msg, for replying.
|
replyTo *User // Set when user gets a /msg, for replying.
|
||||||
|
lastMsg time.Time // When the last message was rendered.
|
||||||
lastMsg time.Time // When the last message was rendered
|
awayReason string // Away reason, "" when not away.
|
||||||
awayStatus string // user's away status
|
awaySince time.Time // When away was set, 0 when not away.
|
||||||
awaySince time.Time
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUser(identity Identifier) *User {
|
func NewUser(identity Identifier) *User {
|
||||||
@ -74,31 +73,24 @@ func (u *User) LastMsg() time.Time {
|
|||||||
return u.lastMsg
|
return u.lastMsg
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetAway sets the users availablity state
|
// SetAway sets the users away reason and state.
|
||||||
func (u *User) SetAway(msg string) {
|
func (u *User) SetAway(msg string) {
|
||||||
u.mu.Lock()
|
u.mu.Lock()
|
||||||
defer u.mu.Unlock()
|
defer u.mu.Unlock()
|
||||||
u.awayStatus = msg
|
u.awayReason = msg
|
||||||
if msg != "" {
|
if msg == "" {
|
||||||
|
u.awaySince = time.Time{}
|
||||||
|
} else {
|
||||||
|
// Reset away timer even if already away
|
||||||
u.awaySince = time.Now()
|
u.awaySince = time.Now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetActive sets the users as active
|
// GetAway returns if the user is away, when they went away, and the reason.
|
||||||
func (u *User) SetActive() {
|
|
||||||
u.mu.Lock()
|
|
||||||
defer u.mu.Unlock()
|
|
||||||
u.awayStatus = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAway returns if the user is away, when they went away and if they set a message
|
|
||||||
func (u *User) GetAway() (bool, time.Time, string) {
|
func (u *User) GetAway() (bool, time.Time, string) {
|
||||||
u.mu.Lock()
|
u.mu.Lock()
|
||||||
defer u.mu.Unlock()
|
defer u.mu.Unlock()
|
||||||
if u.awayStatus == "" {
|
return u.awayReason != "", u.awaySince, u.awayReason
|
||||||
return false, time.Time{}, ""
|
|
||||||
}
|
|
||||||
return true, u.awaySince, u.awayStatus
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *User) Config() UserConfig {
|
func (u *User) Config() UserConfig {
|
||||||
|
@ -63,11 +63,13 @@ func (i Identity) Whois(room *chat.Room) string {
|
|||||||
if i.PublicKey() != nil {
|
if i.PublicKey() != nil {
|
||||||
fingerprint = sshd.Fingerprint(i.PublicKey())
|
fingerprint = sshd.Fingerprint(i.PublicKey())
|
||||||
}
|
}
|
||||||
|
// TODO: Rewrite this using strings.Builder like WhoisAdmin
|
||||||
|
|
||||||
awayMsg := ""
|
awayMsg := ""
|
||||||
if m, ok := room.MemberByID(i.ID()); ok {
|
if m, ok := room.MemberByID(i.ID()); ok {
|
||||||
isAway, awaySince, awayMessage := m.GetAway()
|
isAway, awaySince, awayMessage := m.GetAway()
|
||||||
if isAway {
|
if isAway {
|
||||||
awayMsg = fmt.Sprintf("%s > away since: (%s) %s", message.Newline, humantime.Since(awaySince), awayMessage)
|
awayMsg = fmt.Sprintf("%s > away: (%s ago) %s", message.Newline, humantime.Since(awaySince), awayMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "name: " + i.Name() + message.Newline +
|
return "name: " + i.Name() + message.Newline +
|
||||||
|
Loading…
x
Reference in New Issue
Block a user