-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
209 lines (183 loc) · 6.65 KB
/
message.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package chadango
import (
"html"
"regexp"
"strconv"
"strings"
"time"
"github.com/n0h4rt/chadango/models"
"github.com/n0h4rt/chadango/utils"
)
var (
AnonSeedRe = regexp.MustCompile(`<n\d{4}/>`)
// Go does not support negative lookahead `<(?!br\s*\/?>).*?>`.
// This alternative will match either the `<br>` and `<br/>` tags (captured in group 1)
// or any other HTML tags (captured in group 2).
// Then the `ReplaceAllString(text, "$1")` method will then keep the content matched by group 1
// and remove the content matched by group 2.
HtmlTagRe = regexp.MustCompile(`(<br\s*\/?>)|(<[^>]+>)`)
)
type Message struct {
Group *Group // Group represents the group chat where the message originated (nil if private chat).
Private *Private // Private represents the private chat where the message originated (nil if group chat).
models.Message
}
// Reply sends a reply to the message with the specified text.
//
// If the message is from a group, it calls the [SendMessage] method on the associated [Group] to send the reply.
// If the message is private, it calls the [SendMessage] method on the associated [Private] to send the reply to the user.
// The text can include formatting placeholders (%s, %d, etc.), and optional arguments can be provided to fill in these placeholders.
//
// Args:
// - text: The text of the reply message.
// - a: Optional arguments to fill in placeholders in the message text.
//
// Returns:
// - *Message: A pointer to the sent message (nil if the message is private).
// - error: An error if any occurs during the message sending process.
func (m *Message) Reply(text string, a ...any) (*Message, error) {
if !m.IsPrivate {
return m.Group.SendMessage(text, a...)
}
return nil, m.Private.SendMessage(m.User.Name, text, a...)
}
// Delete deletes the message.
//
// If the message is from a group, it calls the [Delete] method on the associated [Group] to remove the message from the group chat.
// If the message is private, this method does nothing and returns nil.
//
// Returns:
// - error: An error if any occurs during the deletion process.
func (m *Message) Delete() error {
if !m.IsPrivate {
return m.Group.Delete(m)
}
return nil
}
// DeleteAll deletes all messages from the sender of the current message.
//
// If the message is from a group, it calls the [DeleteAll] method on the associated [Group] to remove all messages from the sender in the group chat.
// If the message is private, this method does nothing and returns nil.
//
// Returns:
// - error: An error if any occurs during the deletion process.
func (m *Message) DeleteAll() error {
if !m.IsPrivate {
return m.Group.DeleteAll(m)
}
return nil
}
// BanUser bans the user who sent the current message from the group.
//
// If the message is from a group, it calls the [BanUser] method on the associated [Group] to perform the banning action.
// If the message is private, this method does nothing and returns nil.
//
// Returns:
// - error: An error if any occurs during the banning process.
func (m *Message) BanUser() error {
if !m.IsPrivate {
return m.Group.BanUser(m)
}
return nil
}
// ParseGroupMessage parses a group message data.
//
// It extracts information about the sender, the content, the time of sending, and the channel flags from the provided data.
//
// Args:
// - data: The group message data.
// - group: The group chat where the message originated.
//
// Returns:
// - *Message: A pointer to the parsed [Message] object.
func ParseGroupMessage(data string, group *Group) *Message {
var (
user *models.User
reResult []string
)
fields := strings.SplitN(data, ":", 10)
msg := &Message{Group: group}
msg.ReceivedTime = time.Now()
msg.Time, _ = utils.ParseTime(fields[0])
msg.UserID, _ = strconv.Atoi(fields[3])
if fields[1] != "" {
user = &models.User{Name: fields[1]}
} else if fields[2] != "" {
user = &models.User{Name: fields[2], IsAnon: true}
msg.FromAnon = true
} else {
if reResult = AnonSeedRe.FindAllString(fields[9], 1); len(reResult) > 0 {
msg.AnonSeed, _ = strconv.Atoi(reResult[0][2:6])
}
user = &models.User{Name: utils.GetAnonName(msg.AnonSeed, msg.UserID), IsAnon: true}
msg.FromAnon = true
}
msg.User = user
// Apparently, the [Group.SessionID] can be a zero (uninitialized),
// as this function is called before the "OK" event is received.
// As a temporary workaround, the condition fields[3] == Group.SessionID[:8]
// will be treated as `true` to handle this situation.
if group.UserID == 0 {
user.IsSelf = strings.EqualFold(user.Name, group.LoginName)
} else {
user.IsSelf = msg.UserID == group.UserID && strings.EqualFold(user.Name, group.LoginName)
}
msg.FromSelf = user.IsSelf
msg.ModerationID = fields[4]
msg.ID = fields[5]
msg.UserIP = fields[6]
flag, _ := strconv.ParseInt(fields[7], 10, 64)
msg.Flag = models.MessageChannel(flag)
// _ = fields[8] // Omitted for now
msg.RawText = fields[9]
text := HtmlTagRe.ReplaceAllString(fields[9], "$1")
text = strings.ReplaceAll(text, "<br/>", "\n")
msg.Text = html.UnescapeString(text)
return msg
}
// ParsePrivateMessage parses a private message data.
//
// It extracts information about the sender, the content, the time of sending, and the channel flags from the provided data.
//
// Args:
// - data: The private message data.
// - private: The private chat where the message originated.
//
// Returns:
// - *Message: A pointer to the parsed [Message] object.
func ParsePrivateMessage(data string, private *Private) *Message {
fields := strings.SplitN(data, ":", 6)
msg := &Message{Private: private}
msg.IsPrivate = true
// It should not be possible to send a private message to oneself.
// isSelf := strings.EqualFold(fields[0], private.LoginName)
msg.User = &models.User{Name: fields[0]}
msg.Time, _ = utils.ParseTime(fields[3])
msg.ID, _, _ = strings.Cut(fields[3], ".") // A fake ID used as the key in [Private.Messages].
flag, _ := strconv.ParseInt(fields[4], 10, 64)
msg.Flag = models.MessageChannel(flag)
msg.RawText = fields[5]
text := HtmlTagRe.ReplaceAllString(fields[5], "$1")
msg.Text = html.UnescapeString(text)
return msg
}
// ParseAnnouncement parses a group announcement data.
//
// It extracts the announcement text from the provided data.
//
// Args:
// - data: The group announcement data.
// - group: The group chat where the announcement originated.
//
// Returns:
// - *Message: A pointer to the parsed [Message] object.
func ParseAnnouncement(data string, group *Group) *Message {
fields := strings.SplitN(data, ":", 3)
text := HtmlTagRe.ReplaceAllString(fields[2], "$1")
text = html.UnescapeString(text)
msg := &Message{Group: group}
msg.ReceivedTime = time.Now()
msg.RawText = fields[2]
msg.Text = text
return msg
}