aboutsummaryrefslogtreecommitdiff
path: root/util.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2020-02-26 16:07:33 +0100
committerAlex Auvolat <alex@adnab.me>2020-02-26 16:07:33 +0100
commit67c7f7361d63a282788f159494a6f43172c8806a (patch)
treefe80e56bf8cba13ff4ffbd126dc0d6fec146d657 /util.go
parenta8e87e378ea0a232cb06ef65d77fd39009270676 (diff)
downloadeasybridge-67c7f7361d63a282788f159494a6f43172c8806a.tar.gz
easybridge-67c7f7361d63a282788f159494a6f43172c8806a.zip
Move appservice/ to /
Diffstat (limited to 'util.go')
-rw-r--r--util.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/util.go b/util.go
new file mode 100644
index 0000000..c811a1e
--- /dev/null
+++ b/util.go
@@ -0,0 +1,58 @@
+package main
+
+import (
+ "fmt"
+ "unicode"
+
+ 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 := ""
+ for _, c := range in {
+ if c == '@' {
+ id2 += "__"
+ } else if c == ':' {
+ id2 += "_"
+ } else if unicode.IsDigit(c) || unicode.IsLetter(c) {
+ id2 += string(c)
+ }
+ }
+ return id2
+}