aboutsummaryrefslogtreecommitdiff
path: root/util.go
blob: c811a1e9e67664a03fef2802b137b49d1502ee16 (plain) (blame)
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
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
}