Talking to Easybridge is now possible for some things

This commit is contained in:
Alex 2020-02-17 21:04:21 +01:00
parent 5ab2608ee8
commit 31bba8d946
6 changed files with 137 additions and 24 deletions

View File

@ -25,6 +25,7 @@ func AddAccount(a *Account) {
registeredAccounts[a.MatrixUser] = make(map[string]*Account)
}
registeredAccounts[a.MatrixUser][a.AccountName] = a
ezbrSystemSendf(a.MatrixUser, "Connecting to account %s (%s)", a.AccountName, a.Protocol)
}
func FindAccount(mxUser string, name string) *Account {
@ -104,9 +105,13 @@ func (a *Account) leftInternal(roomId RoomID) error {
return err
}
log.Printf("Joined %s (%s)\n", roomId, a.MatrixUser)
log.Printf("Left %s (%s)\n", roomId, a.MatrixUser)
return mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
err = mxRoomKick(mx_room_id, a.MatrixUser, fmt.Sprintf("got leave room event on %s", a.Protocol))
if err != nil && strings.Contains(err.Error(), "not in the room") {
err = nil
}
return err
}
// ----

View File

@ -14,12 +14,14 @@ import (
"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("system", connector.UserID("Easybridge"), ezbrMxId(), user_mx_id, "easybridge")
return dbGetMxPmRoom(EASYBRIDGE_SYSTEM_PROTOCOL, connector.UserID("Easybridge"), ezbrMxId(), user_mx_id, "easybridge")
}
func ezbrSystemSend(user_mx_id string, msg string) {
@ -32,6 +34,10 @@ func ezbrSystemSend(user_mx_id string, msg string) {
}
}
func ezbrSystemSendf(user_mx_id string, format string, args ...interface{}) {
ezbrSystemSend(user_mx_id, fmt.Sprintf(format, args...))
}
// ----
var httpClient *http.Client

View File

@ -77,14 +77,22 @@ func handleTxn(w http.ResponseWriter, r *http.Request) {
err := json.NewDecoder(r.Body).Decode(&txn)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
log.Printf("JSON decode error: %s\n", err)
log.Warnf("JSON decode error: %s\n", err)
return
}
log.Debugf("Got transaction %#v\n", txn)
for i := range txn.Events {
handleTxnEvent(&txn.Events[i])
ev := &txn.Events[i]
if ev.Sender == ezbrMxId() {
// Don't do anything with events originating from the system
continue
}
err = handleTxnEvent(ev)
if err != nil {
ezbrSystemSend(ev.Sender, fmt.Sprintf("Could not process %s (%s): %s", ev.Type, ev.Sender, err))
}
}
fmt.Fprintf(w, "{}\n")
@ -93,7 +101,7 @@ func handleTxn(w http.ResponseWriter, r *http.Request) {
}
}
func handleTxnEvent(e *mxlib.Event) {
func handleTxnEvent(e *mxlib.Event) error {
if e.Type == "m.room.message" {
ev := &connector.Event{
Type: connector.EVENT_MESSAGE,
@ -104,56 +112,140 @@ func handleTxnEvent(e *mxlib.Event) {
ev.Type = connector.EVENT_MESSAGE
}
// Look up if this is a private message room
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
if pm_room.Protocol == EASYBRIDGE_SYSTEM_PROTOCOL {
handleSystemMessage(e.Sender, e.Content["body"].(string))
return nil
}
// If this is a private message room
acct := FindAccount(pm_room.MxUserID, pm_room.AccountName)
if acct != nil && e.Sender == pm_room.MxUserID {
if acct == nil {
return fmt.Errorf("Not connected to %s", pm_room.AccountName)
} else if e.Sender == pm_room.MxUserID {
ev.Author = acct.Conn.User()
ev.Recipient = pm_room.UserID
acct.Conn.Send(ev)
return acct.Conn.Send(ev)
}
}
// Look up if this is a regular room
if room := dbIsPublicRoom(e.RoomId); room != nil {
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
// If this is a regular room
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil {
ev.Author = acct.Conn.User()
ev.Room = room.RoomID
acct.Conn.Send(ev)
return acct.Conn.Send(ev)
} else {
log.Debugf("Could not find room account for %s %s %s", e.Sender, room.Protocol, room.RoomID)
mxRoomKick(e.RoomId, e.Sender, fmt.Sprintf("Not present in %s on %s, please talk with Easybridge to rejoin", room.RoomID, room.Protocol))
return fmt.Errorf("not joined %s on %s", room.RoomID, room.Protocol)
}
} else {
return fmt.Errorf("Room not bridged")
}
} else if e.Type == "m.room.member" {
ms := e.Content["membership"].(string)
if ms == "leave" {
// If leaving a PM room, we must delete it
if pm_room := dbIsPmRoom(e.RoomId); pm_room != nil {
// If leaving a PM room, we must delete it
them_mx := userMxId(pm_room.Protocol, pm_room.UserID)
mxRoomLeaveAs(e.RoomId, them_mx)
db.Delete(pm_room)
}
// If leaving a public room, leave from server as well
if room := dbIsPublicRoom(e.RoomId); room != nil {
return nil
} else if room := dbIsPublicRoom(e.RoomId); room != nil {
// If leaving a public room, leave from server as well
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil {
acct.Conn.Leave(room.RoomID)
return nil
// TODO: manage autojoin list, remove this room
} else {
log.Debugf("Could not find room account for %s %s %s", e.Sender, room.Protocol, room.RoomID)
mxRoomKick(e.RoomId, e.Sender, fmt.Sprintf("Not present in %s on %s, please talk with Easybridge to rejoin", room.RoomID, room.Protocol))
return fmt.Errorf("not joined %s on %s", room.RoomID, room.Protocol)
}
} else {
return fmt.Errorf("Room not bridged")
}
}
} else if e.Type == "m.room.topic" {
if room := dbIsPublicRoom(e.RoomId); room != nil {
acct := FindJoinedAccount(e.Sender, room.Protocol, room.RoomID)
if acct != nil {
acct.Conn.SetRoomInfo(room.RoomID, &connector.RoomInfo{
return acct.Conn.SetRoomInfo(room.RoomID, &connector.RoomInfo{
Topic: e.Content["topic"].(string),
})
} else {
return fmt.Errorf("Could not find room account for %s %s %s", e.Sender, room.Protocol, room.RoomID)
}
}
}
return nil
}
func handleSystemMessage(mxid string, msg string) {
cmd := strings.Fields(msg)
switch cmd[0] {
case "help":
ezbrSystemSend(mxid, "Welcome to Easybridge! Here is a list of available commands:")
ezbrSystemSend(mxid, "- help: request help")
ezbrSystemSend(mxid, "- list: list accounts")
ezbrSystemSend(mxid, "- accounts: list accounts")
ezbrSystemSend(mxid, "- join <protocol or account> <room id>: join public chat room")
ezbrSystemSend(mxid, "- query <protocol or account> <user id>: open private buffer to contact")
case "list":
case "account":
case "accounts":
one := false
if accts, ok := registeredAccounts[mxid]; ok {
for name, acct := range accts {
one = true
ezbrSystemSendf(mxid, "- %s (%s)", name, acct.Protocol)
}
}
if !one {
ezbrSystemSendf(mxid, "No account currently configured")
}
case "join":
var account *Account
if accts, ok := registeredAccounts[mxid]; ok {
for name, acct := range accts {
if name == cmd[1] || acct.Protocol == cmd[1] {
account = acct
break
}
}
}
if account != nil {
err := account.Conn.Join(connector.RoomID(cmd[2]))
if err != nil {
ezbrSystemSendf(mxid, "%s", err)
}
} else {
ezbrSystemSendf(mxid, "No account with name or using protocol %s", cmd[1])
}
case "query":
var account *Account
if accts, ok := registeredAccounts[mxid]; ok {
for name, acct := range accts {
if name == cmd[1] || acct.Protocol == cmd[1] {
account = acct
break
}
}
}
if account != nil {
quser := connector.UserID(cmd[2])
account.Conn.Invite(quser, connector.RoomID(""))
quser_mxid, err := dbGetMxUser(account.Protocol, quser)
if err != nil {
ezbrSystemSendf(mxid, "%s", err)
return
}
_, err = dbGetMxPmRoom(account.Protocol, quser, quser_mxid, mxid, account.AccountName)
if err != nil {
ezbrSystemSendf(mxid, "%s", err)
}
} else {
ezbrSystemSendf(mxid, "No account with name or using protocol %s", cmd[1])
}
default:
ezbrSystemSend(mxid, "Unrecognized command. Type `help` if you need some help!")
}
}

View File

@ -49,6 +49,7 @@ type Connector interface {
Join(roomId RoomID) error
// Try to invite someone to a channel
// Or if roomId == "", just try adding them as friends
Invite(user UserID, roomId RoomID) error
// Leave a channel

View File

@ -102,7 +102,7 @@ func (irc *IRC) User() UserID {
func (irc *IRC) checkRoomId(id RoomID) (string, error) {
x := strings.Split(string(id), "@")
if len(x) != 2 || x[1] != irc.server || x[0][0] != '#' {
if x[0][0] != '#' || (len(x) == 2 && x[1] != irc.server) {
return "", fmt.Errorf("Invalid room ID: %s", id)
}
return x[0], nil
@ -110,7 +110,7 @@ func (irc *IRC) checkRoomId(id RoomID) (string, error) {
func (irc *IRC) checkUserId(id UserID) (string, error) {
x := strings.Split(string(id), "@")
if len(x) != 2 || x[1] != irc.server || x[0][0] == '#' {
if x[0][0] == '#' || (len(x) == 2 && x[1] != irc.server) {
return "", fmt.Errorf("Invalid user ID: %s", id)
}
return x[0], nil
@ -149,6 +149,10 @@ func (irc *IRC) Join(roomId RoomID) error {
}
func (irc *IRC) Invite(userId UserID, roomId RoomID) error {
if roomId == "" {
return nil
}
ch, err := irc.checkRoomId(roomId)
if err != nil {
return err

View File

@ -294,6 +294,11 @@ func (xm *XMPP) Join(roomId RoomID) error {
}
func (xm *XMPP) Invite(userId UserID, roomId RoomID) error {
if roomId == "" {
xm.conn.RequestSubscription(string(userId))
xm.conn.ApproveSubscription(string(userId))
return nil
}
// TODO
return fmt.Errorf("Not implemented")
}