-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfallacy.go
114 lines (93 loc) · 2.56 KB
/
fallacy.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
// Copyright 2021 The fallacy Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package fallacy implements the fallacy bot library.
package fallacy
import (
"sync"
"time"
"github.com/jackc/pgx/v4/pgxpool"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
)
type Config struct {
// the name of the bot
Name string
// Device ID, use for avoiding flooding sessions with device IDs.
DeviceID id.DeviceID `toml:"device_id"`
// the homeserver to connect to, e.g., https://matrix-client.matrix.org
Homeserver string
// the username (mxid) to connect with, e.g., @fallacy:matrix.org
Username id.UserID
// the password to the account
Password string
// the rooms the bot responds in, omit to allow all rooms
PermittedRooms []id.RoomID `toml:"permitted_rooms"`
}
// New initializes the library and should be called before any other functions.
// It is safe to call more than once, as it is only initialized once.
func (c Config) New() error {
lock.Lock()
defer lock.Unlock()
if !once {
client, err := mautrix.NewClient(c.Homeserver, c.Username, "")
if err != nil {
return err
}
Client = client
once = true
permittedRooms = c.PermittedRooms
}
return nil
}
func (c Config) Login() error {
_, err := Client.Login(&mautrix.ReqLogin{
DeviceID: c.DeviceID,
Identifier: mautrix.UserIdentifier{
User: string(c.Username),
Type: mautrix.IdentifierTypeUser,
},
InitialDeviceDisplayName: c.Name,
Password: c.Password,
StoreCredentials: true,
Type: mautrix.AuthTypePassword,
})
return err
}
func enableFirefox(b bool) {
lock.Lock()
defer lock.Unlock()
firefox = b
}
func enableWelcome(b bool) {
lock.Lock()
defer lock.Unlock()
welcome = b
}
var defaultHandles = map[string][]Callback{
"ban": {{BanUser, 1}},
"import": {{ImportList, 1}},
"mute": {{MuteUser, 1}},
"pin": {{Function: PinMessage}},
"purge": {{Function: CommandPurge}},
"say": {{SayMessage, 1}},
"umute": {{UnmuteUser, 1}},
}
var (
// mutex protecting this block
lock sync.RWMutex
// initialize this block one time only
once bool
// Client is the currently connected Client
Client *mautrix.Client
// handles are the current handlers
handles = defaultHandles
// limit are the allowed requests/second
limit = time.NewTicker(time.Millisecond * 200).C
pool pgxpool.Pool
// some room specific settings, should be migrated...
firefox, welcome bool
// this is supposed to be join rules
rules []string
permittedRooms []id.RoomID
)