aboutsummaryrefslogtreecommitdiff
path: root/appservice/account.go
blob: 431636283144a496e8fa365f11cfcbbb32ab3281 (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package appservice

import (
	"fmt"
	"log"

	"git.deuxfleurs.fr/Deuxfleurs/easybridge/mxlib"
	. "git.deuxfleurs.fr/Deuxfleurs/easybridge/connector"
)

type Account struct {
	MatrixUser string
	AccountName string
	Protocol string
	Conn Connector
}

func (a *Account) Joined(roomId RoomID) {
	mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
	if err != nil {
		return
	}

	log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)

	err = mxRoomInvite(mx_room_id, a.MatrixUser)
	if err != nil {
		log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
	}
}

func (a *Account) Left(roomId RoomID) {
	mx_room_id, err := dbGetMxRoom(a.Protocol, roomId)
	if err != nil {
		return
	}

	log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)

	err = mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
	if err != nil {
		log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
	}
}

func (a *Account) UserInfoUpdated(user UserID, info *UserInfo) {
	// TODO
}

func (a *Account) RoomInfoUpdated(roomId RoomID, info *RoomInfo) {
	// TODO
}

func (a *Account) Event(event *Event) {
	mx_user_id, err := dbGetMxUser(a.Protocol, event.Author)
	if err != nil {
		return
	}

	if event.Type == EVENT_JOIN {
		log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
		mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
		if err != nil {
			return
		}

		err = mxRoomInvite(mx_room_id, mx_user_id)
		if err != nil {
			log.Printf("Could not invite %s to %s", a.MatrixUser, mx_room_id)
		}

		err = mxRoomJoinAs(mx_room_id, mx_user_id)
		if err != nil {
			log.Printf("Could not join %s as %s", a.MatrixUser, mx_room_id)
		}
	} else if event.Type == EVENT_LEAVE {
		log.Printf("%s join %s %s", a.Protocol, event.Author, event.Room)
		mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
		if err != nil {
			return
		}

		err = mxRoomLeaveAs(mx_room_id, mx_user_id)
		if err != nil {
			log.Printf("Could not leave %s as %s", a.MatrixUser, mx_room_id)
		}
	} else if event.Type == EVENT_MESSAGE {
		if len(event.Room) > 0 {
			log.Printf("%s msg %s %s", a.Protocol, event.Author, event.Room)
			mx_room_id, err := dbGetMxRoom(a.Protocol, event.Room)
			if err != nil {
				return
			}

			err = mxSendMessageAs(mx_room_id, event.Text, mx_user_id)
			if err != nil {
				log.Printf("Could not send %s as %s", event.Text, mx_user_id)
			}
		} else {
			// TODO
		}
	}
	// TODO
}

// ----

func dbGetMxRoom(protocol string, roomId RoomID) (string, error) {
	var room DbRoomMap

	// Check if room exists in our mapping,
	// If not create it
	must_create := db.First(&room, DbRoomMap{
		Protocol: protocol,
		RoomID: roomId,
	}).RecordNotFound()
	if must_create {
		alias := roomAlias(protocol, roomId)
		// Lookup alias
		mx_room_id, err := mxDirectoryRoom(fmt.Sprintf("#%s:%s", alias, config.MatrixDomain))

		// If no alias found, create room
		if err != nil {
			name := fmt.Sprintf("%s (%s)", roomId, protocol)

			mx_room_id, err = mxCreateRoom(name, alias, []string{})
			if err != nil {
				log.Printf("Could not create room for %s: %s", name, err)
				return "", err
			}
		}

		room = DbRoomMap{
			Protocol: protocol,
			RoomID: roomId,
			MxRoomID: mx_room_id,
		}
		db.Create(&room)
	}
	log.Printf("Got room id: %s", room.MxRoomID)

	return room.MxRoomID, nil
}

func dbGetMxUser(protocol string, userId UserID) (string, error) {
	var user DbUserMap

	must_create := db.First(&user, DbUserMap{
		Protocol: protocol,
		UserID: userId,
	}).RecordNotFound()
	if must_create {
		username := userMxId(protocol, userId)

		err := mxRegisterUser(username)
		if err != nil {
			if mxE, ok := err.(*mxlib.MxError); !ok || mxE.ErrCode != "M_USER_IN_USE" {
				log.Printf("Could not register %s: %s", username, err)
				return "", err
			}
		}

		mxid := fmt.Sprintf("@%s:%s", username, config.MatrixDomain)
		mxProfileDisplayname(mxid, fmt.Sprintf("%s (%s)", userId, protocol))

		user = DbUserMap{
			Protocol: protocol,
			UserID: userId,
			MxUserID: mxid,
		}
		db.Create(&user)
	}

	return user.MxUserID, nil
}