/
cmd_sync.go
139 lines (129 loc) · 3.04 KB
/
cmd_sync.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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/spf13/cobra"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
type syncCommand struct {
globalOpts *globalOptions
presence bool
once bool
syncTimeout int
}
func (c *syncCommand) printEventJSON(roomID id.RoomID, e *event.Event) {
type outData struct {
Body string `json:"body,omitempty"`
Error string `json:"error,omitempty"`
EventID string `json:"event_id"`
EventType string `json:"event_type"`
RoomID string `json:"room_id,omitempty"`
Sender string `json:"sender,omitempty"`
Timestamp time.Time `json:"timestamp"`
}
var (
s = e.Timestamp / 1000
ns = (e.Timestamp - s*1000) * 1000000
outEvent = outData{
EventID: e.ID.String(),
EventType: e.Type.String(),
RoomID: roomID.String(),
Sender: e.Sender.String(),
Timestamp: time.Unix(s, ns),
}
)
switch e.Type {
case event.EventMessage:
if val, ok := e.Content.Raw["body"]; ok {
outEvent.Body = fmt.Sprintf("%s", val)
}
case event.EventEncrypted:
fmt.Printf("%+v\n", e)
decrypted, err := c.globalOpts.client.olmMachine.DecryptMegolmEvent(e)
if err != nil {
outEvent.Error = err.Error()
} else {
fmt.Println(decrypted)
if val, ok := decrypted.Content.Raw["body"]; ok {
outEvent.Body = fmt.Sprintf("%s", val)
}
}
default:
outEvent.Error = fmt.Sprintf("event type %s not implemented", e.Type.String())
}
if c.globalOpts.json {
out, _ := json.Marshal(outEvent)
fmt.Println(string(out))
} else {
if outEvent.Error != "" {
fmt.Printf(
"!%s|%s|%s|%s|%s|%s\n",
outEvent.Timestamp.Format(time.StampMilli),
outEvent.EventType,
outEvent.EventID,
outEvent.RoomID,
outEvent.Sender,
outEvent.Error,
)
} else {
fmt.Printf(
"%s|%s|%s|%s|%s|%s\n",
outEvent.Timestamp.Format(time.StampMilli),
outEvent.EventType,
outEvent.EventID,
outEvent.RoomID,
outEvent.Sender,
outEvent.Body,
)
}
}
}
func (c *syncCommand) printSync(s *mautrix.RespSync) {
if len(s.Rooms.Join) > 0 {
for roomID, roomEvent := range s.Rooms.Join {
for _, event_ := range roomEvent.Timeline.Events {
c.printEventJSON(roomID, event_)
}
}
}
}
func (c *syncCommand) run(cmd *cobra.Command, args []string) error {
var (
err error
resp *mautrix.RespSync
client = c.globalOpts.client
nextBatch = ""
filterID = ""
presence = event.PresenceOffline
)
for {
if c.globalOpts.roomID != "" {
filter := &mautrix.Filter{
Room: mautrix.RoomFilter{
Rooms: []id.RoomID{id.RoomID(c.globalOpts.roomID)},
},
}
resp, err := client.CreateFilter(filter)
if err != nil {
return err
}
filterID = resp.FilterID
}
if c.presence {
presence = event.PresenceOnline
}
resp, err = client.SyncRequest(c.syncTimeout, nextBatch, filterID, false, presence, context.Background())
if err != nil {
return err
}
nextBatch = resp.NextBatch
c.printSync(resp)
if c.once {
return nil
}
}
}