-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
250 lines (203 loc) · 6.08 KB
/
main.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
_ "embed"
"fmt"
"net/http"
"os"
"runtime"
"strings"
"sync"
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
"github.com/rs/zerolog"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/bridge"
"maunium.net/go/mautrix/bridge/commands"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/util/configupgrade"
"go.mau.fi/mautrix-deltachat/config"
"go.mau.fi/mautrix-deltachat/database"
)
// Information to find out exactly which commit the bridge was built from.
// These are filled at build time with the -X linker flag.
var (
Tag = "unknown"
Commit = "unknown"
BuildTime = "unknown"
)
//go:embed example-config.yaml
var ExampleConfig string
type DeltaChatBridge struct {
bridge.Bridge
Config *config.Config
DB *database.Database
RPC *deltachat.RpcIO
AccountManager *deltachat.AccountManager
//provisioning *ProvisioningAPI
usersByMXID map[id.UserID]*User
usersByAccountID map[deltachat.AccountId]*User
usersLock sync.Mutex
managementRooms map[id.RoomID]*User
managementRoomsLock sync.Mutex
portalsByMXID map[id.RoomID]*Portal
portalsByID map[database.PortalID]*Portal
portalsLock sync.Mutex
puppets map[database.PuppetID]*Puppet
puppetsByCustomMXID map[id.UserID]*Puppet
puppetsLock sync.Mutex
//attachmentTransfers *util.SyncMap[attachmentKey, *util.ReturnableOnce[*database.File]]
}
func (br *DeltaChatBridge) GetExampleConfig() string {
return ExampleConfig
}
func (br *DeltaChatBridge) GetConfigPtr() interface{} {
br.Config = &config.Config{
BaseConfig: &br.Bridge.Config,
}
br.Config.BaseConfig.Bridge = &br.Config.Bridge
return br.Config
}
func (br *DeltaChatBridge) Init() {
br.RPC = deltachat.NewRpcIO()
br.RPC.Stderr = ZStderr(br.ZLog.With().Str("component", "deltachat-core").Logger())
br.AccountManager = &deltachat.AccountManager{Rpc: br.RPC}
br.CommandProcessor = commands.NewProcessor(&br.Bridge)
br.RegisterCommands()
//matrixHTMLParser.PillConverter = br.pillConverter
br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
//deltaChatLog = br.ZLog.With().Str("component", "deltachat").Logger()
// TODO move this to mautrix-go?
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
files := strings.Split(file, "/")
file = files[len(files)-1]
name := runtime.FuncForPC(pc).Name()
fns := strings.Split(name, ".")
name = fns[len(fns)-1]
return fmt.Sprintf("%s:%d:%s()", file, line, name)
}
}
func (br *DeltaChatBridge) Start() {
if err := br.RPC.Start(); err != nil {
br.ZLog.Fatal().Err(err).Msg("Failed to communicate with Delta Chat core")
}
// for each user we already know, import anything we've might've missed
accounts, err := br.AccountManager.Accounts()
if err != nil {
br.ZLog.Err(err).Msg("Failed to get accounts on init")
return
}
for _, acct := range accounts {
user := br.GetUserByAccountID(acct.Id)
if user == nil {
br.ZLog.Warn().Uint64("account_id", uint64(acct.Id)).Msg("Account found from Delta Chat database not mapped?")
continue
}
err := user.Import()
if err != nil {
br.ZLog.Err(err).Msg("Failed to import user data")
} else {
br.ZLog.Info().Str("user_id", string(user.GetMXID())).Msg("User imported successfully!")
}
err = user.Connect()
if err != nil {
br.ZLog.Err(err).Msg("Failed to connect user")
}
}
/*
if br.Config.Bridge.Provisioning.SharedSecret != "disable" {
br.provisioning = newProvisioningAPI(br)
}
*/
//go br.startUsers()
}
func (br *DeltaChatBridge) Stop() {
br.RPC.Stop()
for _, user := range br.usersByMXID {
if !user.Connected() {
continue
}
br.Log.Debugln("Disconnecting", user.MXID)
user.Disconnect()
}
}
func (br *DeltaChatBridge) GetAllIPortals() (iportals []bridge.Portal) {
/*
portals := br.GetAllPortals()
iportals = make([]bridge.Portal, len(portals))
for i, portal := range portals {
iportals[i] = portal
}
return iportals
*/
return []bridge.Portal{}
}
func (br *DeltaChatBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
p := br.GetPortalByMXID(mxid)
if p == nil {
return nil
}
return p
}
func (br *DeltaChatBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
p := br.GetUserByMXID(mxid)
if p == nil {
return nil
}
return p
}
func (br *DeltaChatBridge) IsGhost(mxid id.UserID) bool {
_, isGhost := br.ParsePuppetMXID(mxid)
return isGhost
}
func (br *DeltaChatBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
return br.GetPuppetByMXID(mxid)
}
func (br *DeltaChatBridge) CreatePrivatePortal(id id.RoomID, user bridge.User, ghost bridge.Ghost) {
//TODO implement
}
func main() {
br := &DeltaChatBridge{
usersByMXID: make(map[id.UserID]*User),
usersByAccountID: make(map[deltachat.AccountId]*User),
managementRooms: make(map[id.RoomID]*User),
portalsByMXID: make(map[id.RoomID]*Portal),
portalsByID: make(map[database.PortalID]*Portal),
puppets: make(map[database.PuppetID]*Puppet),
puppetsByCustomMXID: make(map[id.UserID]*Puppet),
}
br.Bridge = bridge.Bridge{
Name: "mautrix-deltachat",
URL: "https://github.com/mautrix/deltachat",
Description: "A Matrix-DeltaChat puppeting bridge.",
Version: "0.0.0",
ProtocolName: "Delta Chat",
CryptoPickleKey: "maunium.net/go/mautrix-deltachat",
ConfigUpgrader: &configupgrade.StructUpgrader{
SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
Blocks: config.SpacedBlocks,
Base: ExampleConfig,
},
Child: br,
}
br.InitVersion(Tag, Commit, BuildTime)
br.Main()
}
func (br *DeltaChatBridge) UploadBlobWithName(path string, fileName string) (id.ContentURI, error) {
data, err := os.ReadFile(path)
if err != nil {
return id.ContentURI{}, err
}
req := mautrix.ReqUploadMedia{
ContentBytes: data,
ContentLength: int64(len(data)),
ContentType: http.DetectContentType(data),
FileName: fileName,
}
resp, err := br.Bot.UploadMedia(req)
if err != nil {
return id.ContentURI{}, err
}
return resp.ContentURI, nil
}
func (br *DeltaChatBridge) UploadBlob(path string) (id.ContentURI, error) {
return br.UploadBlobWithName(path, "")
}