forked from percipia/eslgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendmsg.go
53 lines (48 loc) · 1.3 KB
/
sendmsg.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
/*
* Copyright (c) 2020 Percipia
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* Contributor(s):
* Andrew Querol <aquerol@percipia.com>
*/
package command
import (
"fmt"
"net/textproto"
"strconv"
)
type SendMessage struct {
UUID string
Headers textproto.MIMEHeader
Body string
Sync bool
SyncPri bool
}
func (s *SendMessage) BuildMessage() string {
if s.Headers == nil {
s.Headers = make(textproto.MIMEHeader)
}
// Waits for this event to finish before continuing even in async mode
if s.Sync {
s.Headers.Set("event-lock", "true")
}
// No documentation on this flag, I assume it takes priority over the other flag?
if s.SyncPri {
s.Headers.Set("event-lock-pri", "true")
}
// Ensure the correct content length is set in the header
if len(s.Body) > 0 {
s.Headers.Set("Content-Length", strconv.Itoa(len(s.Body)))
} else {
delete(s.Headers, "Content-Length")
}
// Format the headers
headerString := FormatHeaderString(s.Headers)
if _, ok := s.Headers["Content-Length"]; ok {
return fmt.Sprintf("sendmsg %s\r\n%s\r\n\r\n%s", s.UUID, headerString, s.Body)
}
return fmt.Sprintf("sendmsg %s\r\n%s", s.UUID, headerString)
}