Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit bd369ce2390321a7939156b55a8b248af94e5be2 Author: Gary Kim <gary@garykim.dev> Date: Sat Jul 18 22:08:25 2020 +0000 Add Nextcloud Talk support (#1167) Signed-off-by: Gary Kim <gary@garykim.dev> diff --git a/bridge/nctalk/nctalk.go b/bridge/nctalk/nctalk.go new file mode 100644 index 0000000..7f7d8ab --- /dev/null +++ b/bridge/nctalk/nctalk.go @@ -00 +1114 @@ +package nctalk + +import ( + "context" + "strconv" + + "github.com/42wim/matterbridge/bridge" + "github.com/42wim/matterbridge/bridge/config" + + talk "gomod.garykim.dev/nc-talk" + "gomod.garykim.dev/nc-talk/ocs" + "gomod.garykim.dev/nc-talk/room" + "gomod.garykim.dev/nc-talk/user" +) + +type Btalk struct { + user *user.TalkUser + rooms []Broom + *bridge.Config +} + +func New(cfg *bridge.Config) bridge.Bridger { + return &Btalk{Config: cfg} +} + +type Broom struct { + room *room.TalkRoom + ctx context.Context + ctxCancel context.CancelFunc +} + +func (b *Btalk) Connect() error { + b.Log.Info("Connecting") + b.user = talk.NewUser(b.GetString("Server"), b.GetString("Login"), b.GetString("Password")) + _, err := b.user.Capabilities() + if err != nil { + b.Log.Error("Cannot Connect") + return err + } + b.Log.Info("Connected") + return nil +} + +func (b *Btalk) Disconnect() error { + for _, r := range b.rooms { + r.ctxCancel() + } + return nil +} + +func (b *Btalk) JoinChannel(channel config.ChannelInfo) error { + newRoom := Broom{ + room: talk.NewRoom(b.user, channel.Name), + } + newRoom.ctx, newRoom.ctxCancel = context.WithCancel(context.Background()) + c, err := newRoom.room.ReceiveMessages(newRoom.ctx) + if err != nil { + return err + } + b.rooms = append(b.rooms, newRoom) + go func() { + for msg := range c { + // ignore messages that are one of the following + // * not a message from a user + // * from ourselves + if msg.MessageType != ocs.MessageComment || msg.ActorID == b.user.User { + continue + } + remoteMessage := config.Message{ + Text: msg.Message, + Channel: newRoom.room.Token, + Username: msg.ActorDisplayName, + UserID: msg.ActorID, + Account: b.Account, + } + // It is possible for the ID to not be set on older versions of Talk so we only set it if + // the ID is not blank + if msg.ID != 0 { + remoteMessage.ID = strconv.Itoa(msg.ID) + } + b.Log.Debugf("<= Message is %#v", remoteMessage) + b.Remote <- remoteMessage + } + }() + return nil +} + +func (b *Btalk) Send(msg config.Message) (string, error) { + r := b.getRoom(msg.Channel) + if r == nil { + b.Log.Errorf("Could not find room for %v", msg.Channel) + return "", nil + } + + // Talk currently only supports sending normal messages + if msg.Event != "" { + return "", nil + } + sentMessage, err := r.room.SendMessage(msg.Username + msg.Text) + if err != nil { + b.Log.Errorf("Could not send message to room %v from %v: %v", msg.Channel, msg.Username, err) + return "", nil + } + return strconv.Itoa(sentMessage.ID), nil +} + +func (b *Btalk) getRoom(token string) *Broom { + for _, r := range b.rooms { + if r.room.Token == token { + return &r + } + } + return nil +} diff --git a/gateway/bridgemap/bnctalk.go b/gateway/bridgemap/bnctalk.go new file mode 100644 index 0000000..c194392 --- /dev/null +++ b/gateway/bridgemap/bnctalk.go @@ -00 +111 @@ +// +build !nonctalk + +package bridgemap + +import ( + btalk "github.com/42wim/matterbridge/bridge/nctalk" +) + +func init() { + FullMap["nctalk"] = btalk.New +} diff --git a/go.mod b/go.mod index 174e49e..af174c7 100644 --- a/go.mod +++ b/go.mod @@ -556 +557 @@ require (   github.com/zfjagann/golang-ring v0.0.0-20190304061218-d34796e0a6c2   golang.org/x/image v0.0.0-20200430140353-33d19683fad8   golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d + gomod.garykim.dev/nc-talk v0.0.1   gopkg.in/fsnotify.v1 v1.4.7 // indirect   gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect   gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect diff --git a/go.sum b/go.sum index c1e79f8..b125d87 100644 --- a/go.sum +++ b/go.sum @@ -2246 +2248 @@ github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQz  github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=  github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=  github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/monaco-io/request v1.0.3 h1:FsiIwXCCbHEyWx9A7lgg6JBTMHhHlEEsADsgAOvZ9HA= +github.com/monaco-io/request v1.0.3/go.mod h1:EmggwHktBsbJmCgwZXqy7o0H1NNsAstQBWZrFVd3xtQ=  github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474 h1:oKIteTqeSpenyTrOVj5zkiyCaflLa8B+CD0324otT+o=  github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8=  github.com/mrexodia/wray v0.0.0-20160318003008-78a2c1f284ff h1:HLGD5/9UxxfEuO9DtP8gnTmNtMxbPyhYltfxsITel8g= @@ -4486 +4508 @@ golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtn  golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=  golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=  golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gomod.garykim.dev/nc-talk v0.0.1 h1:6mgjcAf5/HMkV0CFGeXVfYHG7FAUCQcGR8eg9oM6fCc= +gomod.garykim.dev/nc-talk v0.0.1/go.mod h1:0/Ksg0osAYmnWKs1OcCG+gBQ4HU1xiF1699g9B6jWZw=  google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=  google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=  google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= diff --git a/matterbridge.toml.sample b/matterbridge.toml.sample index 156e1f2..5f60930 100644 --- a/matterbridge.toml.sample +++ b/matterbridge.toml.sample @@ -13837 +138322 @@ StripNick=false  #OPTIONAL (default false)  ShowTopicChange=false   +################################################################### +# +# NCTalk (Nextcloud Talk) +# +###################################################################   +[nctalk.bridge] + +# Url of your Nextcloud server +Server = "https://cloud.youdomain.me" + +# Username of the bot +Login = "talkuser" + +# Password of the bot +Password = "talkuserpass"    ###################################################################  # @@ -17027 +17177 @@ enable=true # REQUIRED account="irc.freenode"   - # The channel key in each gateway is mapped to a similar group chat ID on the chat platform + # The channel key in each gateway is mapped to a similar group chat ID on the chat platform # To find the group chat ID for different platforms, refer to the table below # # Platform | Identifier name | Example | Description @@ -17306 +17458 @@ enable=true # ------------------------------------------------------------------------------------------------------------------------------------- # steam | chatid | example needed | The number in the URL when you click "enter chat room" in the browser # ------------------------------------------------------------------------------------------------------------------------------------- + # nctalk | token | xs25tz5y | The token in the URL when you are in a chat. It will be the last part of the URL. + # ------------------------------------------------------------------------------------------------------------------------------------- # telegram | chatid | -123456789 | A large negative number. see https://www.linkedin.com/pulse/telegram-bots-beginners-marco-frau # ------------------------------------------------------------------------------------------------------------------------------------- # whatsapp | group JID | 48111222333-123455678999@g.us | A unique group JID. If you specify an empty string, bridge will list all the possibilities