-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages.go
98 lines (83 loc) · 2.33 KB
/
messages.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
package idec
import (
"encoding/base64"
"errors"
"strconv"
"strings"
"time"
)
// Message IDEC message structure
type Message struct {
From string `json:"from"`
To string `json:"to"`
Address string `json:"address"`
Echo string `json:"echo"`
Subg string `json:"subg"`
ID string `json:"id"`
Timestamp int `json:"timestamp"`
Body string `json:"body"`
Tags Tags `json:"tags"`
Repto string `json:"repto"`
}
// PointMessage
type PointMessage struct {
Echo string `json:"echo"`
To string `json:"to"`
Subg string `json:"subg"`
EmptyLine string `json:"empty_line"`
Repto string `json:"repto"`
Body string `json:"body"`
}
// Tags IDEC message tags
type Tags struct {
II string `json:"ii"`
Repto string `json:"repto"`
}
// CollectTags make ii/ok message from Tags
func (t Tags) CollectTags() (string, error) {
var tagstring string
if t.II == "" {
e := errors.New("Wrong ii/ok tag")
return "", e
}
if t.Repto != "" {
tagstring = strings.Join([]string{"ii", t.II}, "/")
} else {
tagstring = strings.Join([]string{"ii", t.II, "repto", t.Repto}, "/")
}
return tagstring, nil
}
// Make bundle from point message
func (p PointMessage) MakeBundleMessage(from, address string) (*Message, string, error) {
var result string
var message *Message
nodeTime := strconv.Itoa(int(time.Now().Unix()))
tags := Tags{"ok", p.Repto}
message.Tags = tags
message.Echo = p.Echo
message.Timestamp = int(time.Now().Unix())
message.From = from
message.Address = address
message.To = p.To
message.Subg = p.Subg
message.Body = p.Body
strTags, err := tags.CollectTags()
if err != nil {
return message, "", err
}
rawMessage := strings.Join([]string{strTags, p.Echo,
nodeTime, from, address, p.To, p.Subg, p.EmptyLine, p.Body}, "\n")
result = base64.StdEncoding.EncodeToString([]byte(rawMessage))
return message, result, nil
}
// PrepareMessageForSend Make base64 encoded message. Client.
func (p PointMessage) PrepareMessageForSend() string {
var result string
var rawMessage string
if p.Repto != "" {
rawMessage = strings.Join([]string{p.Echo, p.To, p.Subg, p.EmptyLine, p.Repto, p.Body}, "\n")
}
rawMessage = strings.Join([]string{p.Echo, p.To, p.Subg, p.EmptyLine, p.Body}, "\n")
result = base64.StdEncoding.EncodeToString([]byte(rawMessage))
return result
}