/
command_chat_pm.go
90 lines (77 loc) · 2.24 KB
/
command_chat_pm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"html"
"time"
)
// commandChatPM is sent when a user sends a private message
//
// Example data:
// {
// msg: 'i secretly adore you',
// recipient: 'Alice',
// }
func commandChatPM(ctx context.Context, s *Session, d *CommandData) {
// Check to see if their IP has been muted
if s != nil && s.Muted {
s.Warning("You have been muted by an administrator.")
return
}
// Sanitize and validate the chat message
if v, valid := sanitizeChatInput(s, d.Msg, false); !valid {
return
} else {
d.Msg = v
}
// Sanitize and validate the private message recipient
if v, valid := sanitizeChatInput(s, d.Recipient, false); !valid {
return
} else {
d.Recipient = v
}
// Validate that they are not sending a private message to themselves
normalizedUsername := normalizeString(d.Recipient)
if normalizedUsername == normalizeString(s.Username) {
s.Warning("You cannot send a private message to yourself.")
return
}
// Validate that the recipient is online
sessionList := sessions.GetList()
var recipientSession *Session
for _, s2 := range sessionList {
if normalizeString(s2.Username) == normalizedUsername {
recipientSession = s2
break
}
}
if recipientSession == nil {
s.Warning("User \"" + d.Recipient + "\" is not currently online.")
return
}
// Escape all HTML special characters (to stop various attacks against other players)
d.Msg = html.EscapeString(d.Msg)
chatPM(s, d, recipientSession)
}
func chatPM(s *Session, d *CommandData, recipientSession *Session) {
// Log the message
logger.Info("PM <" + s.Username + "> --> <" + recipientSession.Username + "> " + d.Msg)
// Add the message to the database
if err := models.ChatLogPM.Insert(s.UserID, d.Msg, recipientSession.UserID); err != nil {
logger.Error("Failed to insert a private message into the database: " + err.Error())
s.Error(DefaultErrorMsg)
return
}
chatMessage := &ChatMessage{
Msg: d.Msg,
Who: s.Username,
Discord: false,
Server: false,
Datetime: time.Now(),
Room: "",
Recipient: recipientSession.Username,
}
// Echo the private message back to the person who sent it
s.Emit("chat", chatMessage)
// Send the private message to the recipient
recipientSession.Emit("chat", chatMessage)
}