-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (77 loc) · 1.64 KB
/
main.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
package main
import (
"bufio"
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"regexp"
"time"
)
const (
matrixToken = ""
matrixEndpoint = ""
kchatAddr = "localhost:1337"
kchatUser = "matrix_bridge"
kchatPass = ""
captchaText = "kchat-connect"
)
type message struct {
MsgType string `json:"msgtype"`
Body string `json:"body"`
}
var R *regexp.Regexp
func authenticate(conn net.Conn, user, pass string) {
fmt.Fprintln(conn, captchaText)
fmt.Fprintln(conn, "/login "+user+" "+pass)
}
func handleMessage(conn net.Conn) {
for {
msg, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Println("Receive error")
break
}
log.Println("Received:", msg)
if R.MatchString(msg) {
res := R.FindStringSubmatch(msg)
fmt.Println(res[1] + ": " + res[2])
text := fmt.Sprintf("%s: %s", res[1], res[2])
sendMessage(text)
}
}
}
func sendMessage(msg string) {
m := message{
MsgType: "m.text",
Body: msg,
}
j, _ := json.Marshal(m)
p, _ := rand.Prime(rand.Reader, 64)
req, _ := http.NewRequest(http.MethodPut,
matrixEndpoint+p.String(), bytes.NewBuffer(j))
req.Header.Set("Authorization", "Bearer "+matrixToken)
client := &http.Client{}
_, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
}
func main() {
R = regexp.MustCompile(`^\r\x1b\[1;\d{2}m(?P<Nick>.+)\x1b\[0m: (?P<Message>.+)`)
for {
conn, err := net.Dial("tcp", kchatAddr)
if err != nil {
log.Println("Retrying in 3 seconds")
time.Sleep(3 * time.Second)
continue
}
log.Println("Succesfully connected")
defer conn.Close()
authenticate(conn, kchatUser, kchatPass)
handleMessage(conn)
}
}