-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
120 lines (105 loc) · 2.74 KB
/
bridge.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
115
116
117
118
119
120
package main
import (
"fmt"
"log"
"strings"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
func truncateText(s string, max int) string {
if max >= len(s) {
return s
}
return s[:max] + "..."
// return s[:strings.LastIndexAny(s[:max], " .,:;-")] + "..."
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func get_notification(text string) Notification {
return Notification{
Priority: "info",
IconType: "info",
Model: Model{
Frames: []Frames{
Frames{
Text: text,
},
},
Sound: Sound{
Category: "notifications",
ID: "notification2",
},
Cycles: 1,
},
}
}
func bridge_messages(l LaMetric, client *mautrix.Client, c Config) {
active := false
syncer := client.Syncer.(*mautrix.DefaultSyncer)
go func() {
time.Sleep(time.Second * 3)
active = true
log.Println("Starting to listen for messages")
}()
syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
if !active {
return
}
if evt.Sender == client.UserID {
log.Println("Send message")
return
}
if c.Blacklist.Active {
if contains(c.Blacklist.Rooms, fmt.Sprintf("%v", evt.RoomID)) {
// log.Println("Message in blacklisted room", evt.RoomID)
return
}
}
if c.Whitelist.Active {
if !contains(c.Whitelist.Rooms, fmt.Sprintf("%v", evt.RoomID)) {
// log.Println("Message not in whitelisted room", evt.RoomID)
return
}
}
log.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
msg := strings.TrimSpace(evt.Content.AsMessage().Body)
text := truncateText(msg, 10)
parts := strings.Split(fmt.Sprintf("%v", evt.Sender), ":")
fmt.Println(parts)
sender := fmt.Sprintf("%v", evt.Sender)
// fmt.Println("0")
if len(parts) >= 1 {
sender = parts[0]
sender = strings.ReplaceAll(sender, "@", "")
}
// fmt.Println("1")
toSend := fmt.Sprint(sender+": ", text)
n := get_notification(toSend)
// fmt.Println("2", toSend, n)
// fmt.Println(n)
_, err := l.SendNotification(n)
if err != nil {
log.Println("Error sending notification:", err)
}
// fmt.Println("3", res, err)
log.Println("Sent notification:", toSend)
})
err := client.Sync()
if err != nil {
log.Println(err)
}
// syncer := client.Syncer.(*mautrix.DefaultSyncer)
// syncer.OnEventType(event.EventMessage, func(source mautrix.EventSource, evt *event.Event) {
// // fmt.Printf("<%[1]s> %[4]s (%[2]s/%[3]s)\n", evt.Sender, evt.Type.String(), evt.ID, evt.Content.AsMessage().Body)
// // l.SendNotification(get_notification("Message from " + evt.Sender))
// fmt.Println("Sending notification")
// // + ": " + evt.Content.AsMessage().Body
// })
}