This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
config.py
183 lines (163 loc) · 7.68 KB
/
config.py
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
# mautrix-facebook - A Matrix-Facebook Messenger puppeting bridge.
# Copyright (C) 2023 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import Any
import os
from mautrix.bridge.config import BaseBridgeConfig
from mautrix.types import UserID
from mautrix.util.config import ConfigUpdateHelper, ForbiddenDefault, ForbiddenKey
class Config(BaseBridgeConfig):
@property
def forbidden_defaults(self) -> list[ForbiddenDefault]:
return [
*super().forbidden_defaults,
ForbiddenDefault("appservice.database", "postgres:https://username:password@hostname/db"),
ForbiddenDefault(
"appservice.public.external",
"https://example.com/public",
condition="appservice.public.enabled",
),
ForbiddenDefault("bridge.permissions", ForbiddenKey("example.com")),
]
def do_update(self, helper: ConfigUpdateHelper) -> None:
super().do_update(helper)
copy, copy_dict, base = helper
if self["appservice.bot_avatar"] == "mxc:https://maunium.net/ddtNPZSKMNqaUzqrHuWvUADv":
base["appservice.bot_avatar"] = "mxc:https://maunium.net/ygtkteZsXnGJLJHRchUwYWak"
copy("appservice.public.enabled")
copy("appservice.public.prefix")
copy("appservice.public.external")
if self["appservice.public.shared_secret"] == "generate":
base["appservice.public.shared_secret"] = self._new_token()
else:
copy("appservice.public.shared_secret")
copy("appservice.public.allow_matrix_login")
copy("analytics.host")
if "appservice.provisioning.segment_key" in self:
base["analytics.token"] = self["appservice.provisioning.segment_key"]
else:
copy("analytics.token")
if "appservice.provisioning.segment_user_id" in self:
base["analytics.user_id"] = self["appservice.provisioning.segment_user_id"]
else:
copy("analytics.user_id")
copy("metrics.enabled")
copy("metrics.listen_port")
copy("bridge.username_template")
copy("bridge.displayname_template")
copy("bridge.displayname_preference")
copy("bridge.command_prefix")
copy("bridge.invite_own_puppet_to_pm")
copy("bridge.sync_with_custom_puppets")
copy("bridge.sync_direct_chat_list")
copy("bridge.double_puppet_server_map")
copy("bridge.double_puppet_allow_discovery")
if "bridge.login_shared_secret" in self:
base["bridge.login_shared_secret_map"] = {
base["homeserver.domain"]: self["bridge.login_shared_secret"]
}
else:
copy("bridge.login_shared_secret_map")
copy("bridge.presence_from_facebook")
copy("bridge.update_avatar_initial_sync")
copy("bridge.delivery_receipts")
copy("bridge.delivery_error_reports")
copy("bridge.message_status_events")
copy("bridge.federate_rooms")
copy("bridge.allow_invites")
copy("bridge.backfill.enable")
copy("bridge.backfill.msc2716")
copy("bridge.backfill.double_puppet_backfill")
if "bridge.initial_chat_sync" in self:
initial_chat_sync = self["bridge.initial_chat_sync"]
base["bridge.backfill.max_conversations"] = self.get(
"bridge.backfill.max_conversations", initial_chat_sync
)
else:
copy("bridge.backfill.max_conversations")
copy("bridge.backfill.min_sync_thread_delay")
copy("bridge.backfill.unread_hours_threshold")
copy("bridge.backfill.backoff.thread_list")
copy("bridge.backfill.backoff.message_history")
copy("bridge.backfill.incremental.max_pages")
copy("bridge.backfill.incremental.max_total_pages")
copy("bridge.backfill.incremental.page_delay")
copy("bridge.backfill.incremental.post_batch_delay")
if "bridge.periodic_reconnect_interval" in self:
base["bridge.periodic_reconnect.interval"] = self["bridge.periodic_reconnect_interval"]
base["bridge.periodic_reconnect.mode"] = self["bridge.periodic_reconnect_mode"]
else:
copy("bridge.periodic_reconnect.interval")
copy("bridge.periodic_reconnect.mode")
copy("bridge.periodic_reconnect.always")
copy("bridge.periodic_reconnect.min_connected_time")
copy("bridge.resync_max_disconnected_time")
copy("bridge.max_startup_thread_sync_count")
copy("bridge.temporary_disconnect_notices")
copy("bridge.disable_bridge_notices")
copy("bridge.bridge_matrix_notices")
if "bridge.refresh_on_reconnection_fail" in self:
base["bridge.on_reconnection_fail.action"] = (
"refresh" if self["bridge.refresh_on_reconnection_fail"] else None
)
base["bridge.on_reconnection_fail.wait_for"] = 0
elif "bridge.on_reconnection_fail.refresh" in self:
base["bridge.on_reconnection_fail.action"] = (
"refresh" if self["bridge.on_reconnection_fail.refresh"] else None
)
copy("bridge.on_reconnection_fail.wait_for")
else:
copy("bridge.on_reconnection_fail.action")
copy("bridge.on_reconnection_fail.wait_for")
copy("bridge.resend_bridge_info")
copy("bridge.mute_bridging")
copy("bridge.tag_only_on_create")
copy("bridge.sandbox_media_download")
copy_dict("bridge.permissions")
copy("bridge.get_proxy_api_url")
copy("bridge.private_chat_portal_meta")
if base["bridge.private_chat_portal_meta"] not in ("default", "always", "never"):
base["bridge.private_chat_portal_meta"] = "default"
copy("bridge.disable_reply_fallbacks")
for key in (
"bridge.periodic_reconnect.interval",
"bridge.on_reconnection_fail.wait_for",
):
value = base.get(key, None)
if isinstance(value, list) and len(value) != 2:
raise ValueError(f"{key} must only be a list of two items")
copy("facebook.device_seed")
if base["facebook.device_seed"] == "generate":
base["facebook.device_seed"] = self._new_token()
copy("facebook.default_region_hint")
copy("facebook.connection_type")
copy("facebook.carrier")
copy("facebook.hni")
copy("facebook.mqtt_keepalive")
def _get_permissions(self, key: str) -> tuple[bool, bool, bool, str]:
level = self["bridge.permissions"].get(key, "")
admin = level == "admin"
user = level == "user" or admin
relay = level == "relay" or user
return relay, user, admin, level
def get_permissions(self, mxid: UserID) -> tuple[bool, bool, bool, str]:
permissions = self["bridge.permissions"] or {}
if mxid in permissions:
return self._get_permissions(mxid)
homeserver = mxid[mxid.index(":") + 1 :]
if homeserver in permissions:
return self._get_permissions(homeserver)
return self._get_permissions("*")