aboutsummaryrefslogtreecommitdiff
path: root/appservice/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'appservice/util.go')
-rw-r--r--appservice/util.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/appservice/util.go b/appservice/util.go
new file mode 100644
index 0000000..4175fb0
--- /dev/null
+++ b/appservice/util.go
@@ -0,0 +1,51 @@
+package appservice
+
+import (
+ "fmt"
+ "strings"
+
+ log "github.com/sirupsen/logrus"
+
+ . "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
+)
+
+const EASYBRIDGE_SYSTEM_PROTOCOL string = "✯◡✯"
+
+func ezbrMxId() string {
+ return fmt.Sprintf("@%s:%s", registration.SenderLocalpart, config.MatrixDomain)
+}
+
+func ezbrSystemRoom(user_mx_id string) (string, error) {
+ return dbGetMxPmRoom(EASYBRIDGE_SYSTEM_PROTOCOL, UserID("Easybridge"), ezbrMxId(), user_mx_id, "easybridge")
+}
+
+func ezbrSystemSend(user_mx_id string, msg string) {
+ mx_room_id, err := ezbrSystemRoom(user_mx_id)
+ if err == nil {
+ err = mx.SendMessageAs(mx_room_id, "m.text", msg, ezbrMxId())
+ }
+ if err != nil {
+ log.Warnf("(%s) %s", user_mx_id, msg)
+ }
+}
+
+func ezbrSystemSendf(user_mx_id string, format string, args ...interface{}) {
+ ezbrSystemSend(user_mx_id, fmt.Sprintf(format, args...))
+}
+
+// ----
+
+func roomAlias(protocol string, id RoomID) string {
+ return fmt.Sprintf("_ezbr__%s__%s", safeStringForId(string(id)), protocol)
+}
+
+func userMxId(protocol string, id UserID) string {
+ return fmt.Sprintf("_ezbr__%s__%s", safeStringForId(string(id)), protocol)
+}
+
+func safeStringForId(in string) string {
+ id2 := strings.ReplaceAll(in, "#", "")
+ id2 = strings.ReplaceAll(id2, "@", "__")
+ id2 = strings.ReplaceAll(id2, ":", "_")
+ return id2
+}