diff options
| author | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-11-02 15:27:18 +0800 |
| commit | e9c24f4af7ed56760f6db7941827d09f6db9020b (patch) | |
| tree | 62128c43b883ce5e3148113350978755779bb5de /teleirc/matterbridge/vendor/gomod.garykim.dev | |
| parent | 58d5e7cfda4781d8a57ec52aefd02983835c301a (diff) | |
add matterbridge
Diffstat (limited to 'teleirc/matterbridge/vendor/gomod.garykim.dev')
8 files changed, 1101 insertions, 0 deletions
diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/LICENSE b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/LICENSE new file mode 100644 index 0000000..8f71f43 --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/constants/constants.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/constants/constants.go new file mode 100644 index 0000000..4447354 --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/constants/constants.go @@ -0,0 +1,25 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package constants + +const ( + // BaseEndpoint is the api endpoint for Nextcloud Talk + BaseEndpoint = "/ocs/v2.php/apps/spreed/api/v1/" +) + +// RemoteDavEndpoint returns the endpoint for the Dav API for Nextcloud +func RemoteDavEndpoint(username string, davType string) string { + return "/remote.php/dav/" + davType + "/" + username + "/" +} diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go new file mode 100644 index 0000000..8b2919f --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/capabilities.go @@ -0,0 +1,46 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ocs + +// Capabilities describes the response from the capabilities request +type Capabilities struct { + ocs + Data struct { + Capabilities struct { + SpreedCapabilities SpreedCapabilities `json:"spreed"` + } `json:"capabilities"` + } `json:"data"` +} + +// SpreedCapabilities describes the Nextcloud Talk capabilities response +type SpreedCapabilities struct { + Features []string `json:"features"` + Config struct { + Attachments struct { + Allowed bool `json:"allowed"` + Folder string `json:"folder"` + } `json:"attachments"` + Chat struct { + MaxLength int `json:"max-length"` + ReadPrivacy int `json:"read-privacy"` + } `json:"chat"` + Conversations struct { + CanCreate bool `json:"can-create"` + } `json:"conversations"` + Previews struct { + MaxGifSize int `json:"max-gif-size"` + } `json:"previews"` + } `json:"config"` +} diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/message.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/message.go new file mode 100644 index 0000000..1a8f95d --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/message.go @@ -0,0 +1,177 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ocs + +import ( + "encoding/json" + "strings" +) + +// MessageType describes what kind of message a returned Nextcloud Talk message is +type MessageType string + +// ActorType describes what kind of actor a returned Nextcloud Talk message is from +type ActorType string + +const ( + // MessageComment is a Nextcloud Talk message that is a comment + MessageComment MessageType = "comment" + + // MessageSystem is a Nextcloud Talk message that is a system + MessageSystem MessageType = "system" + + // MessageCommand is a Nextcloud Talk message that is a command + MessageCommand MessageType = "command" + + // MessageDelete is a Nextcloud Talk message indicating a message that was deleted + // + // If a message has been deleted, a message of MessageType MessageSystem is + // sent through the channel for which the parent message's MessageType is MessageDelete. + // So, in order to check if a new message is a message deletion request, a check + // like this can be used: + // msg.MessageType == ocs.MessageSystem && msg.Parent != nil && msg.Parent.MessageType == ocs.MessageDelete + MessageDelete MessageType = "comment_deleted" + + // ActorUser is a Nextcloud Talk message sent by a user + ActorUser ActorType = "users" + + // ActorGuest is a Nextcloud Talk message sent by a guest + ActorGuest ActorType = "guests" +) + +// TalkRoomMessageData describes the data part of a ocs response for a Talk room message +// +// Error will be set if a message request ran into an error. +type TalkRoomMessageData struct { + Error error `json:"-"` + Message string `json:"message"` + ID int `json:"id"` + ActorType ActorType `json:"actorType"` + ActorID string `json:"actorId"` + ActorDisplayName string `json:"actorDisplayName"` + SystemMessage string `json:"systemMessage"` + Timestamp int `json:"timestamp"` + MessageType MessageType `json:"messageType"` + Deleted bool `json:"deleted"` + Parent *TalkRoomMessageData `json:"parent"` + MessageParameters map[string]RichObjectString `json:"-"` +} + +// talkRoomMessageParameters is used to unmarshal only MessageParameters +type talkRoomMessageParameters struct { + MessageParameters map[string]RichObjectString `json:"messageParameters"` +} + +// PlainMessage returns the message string with placeholders replaced +// +// * User and group placeholders will be replaced with the name of the user or group respectively. +// +// * File placeholders will be replaced with the name of the file. +func (m *TalkRoomMessageData) PlainMessage() string { + tr := m.Message + for key, value := range m.MessageParameters { + tr = strings.ReplaceAll(tr, "{"+key+"}", value.Name) + } + return tr +} + +// DisplayName returns the display name for the sender of the message (" (Guest)" is appended if sent by a guest user) +func (m *TalkRoomMessageData) DisplayName() string { + if m.ActorType == ActorGuest { + if m.ActorDisplayName == "" { + return "Guest" + } + return m.ActorDisplayName + " (Guest)" + } + return m.ActorDisplayName +} + +// TalkRoomMessage describes an ocs response for a Talk room message +type TalkRoomMessage struct { + OCS talkRoomMessage `json:"ocs"` +} + +type talkRoomMessage struct { + ocs + TalkRoomMessage []TalkRoomMessageData `json:"data"` +} + +// TalkRoomMessageDataUnmarshal unmarshals given ocs request data and returns a TalkRoomMessageData +func TalkRoomMessageDataUnmarshal(data *[]byte) (*TalkRoomMessage, error) { + message := &TalkRoomMessage{} + err := json.Unmarshal(*data, message) + if err != nil { + return nil, err + } + + // Get RCS + var rcs struct { + OCS struct { + ocs + TalkRoomMessage []talkRoomMessageParameters `json:"data"` + } `json:"ocs"` + } + err = json.Unmarshal(*data, &rcs) + // There is no RCS data + if err != nil { + for i := range message.OCS.TalkRoomMessage { + message.OCS.TalkRoomMessage[i].MessageParameters = map[string]RichObjectString{} + } + return message, nil + } + + // There is RCS data + for i := range message.OCS.TalkRoomMessage { + message.OCS.TalkRoomMessage[i].MessageParameters = rcs.OCS.TalkRoomMessage[i].MessageParameters + } + return message, nil +} + +// TalkRoomSentResponse describes an ocs response for what is returned when a message is sent +type TalkRoomSentResponse struct { + OCS talkRoomSentResponse `json:"ocs"` +} + +type talkRoomSentResponse struct { + ocs + TalkRoomMessage TalkRoomMessageData `json:"data"` +} + +// TalkRoomSentResponseUnmarshal unmarshals given ocs request data and returns a TalkRoomMessageData +func TalkRoomSentResponseUnmarshal(data *[]byte) (*TalkRoomSentResponse, error) { + message := &TalkRoomSentResponse{} + err := json.Unmarshal(*data, message) + if err != nil { + return nil, err + } + + // Get RCS + var rcs struct { + OCS struct { + ocs + TalkRoomMessage talkRoomMessageParameters `json:"data"` + } `json:"ocs"` + } + err = json.Unmarshal(*data, &rcs) + // There is no RCS data + if err != nil { + message.OCS.TalkRoomMessage.MessageParameters = map[string]RichObjectString{} + return message, nil + } + + // There is RCS data + message.OCS.TalkRoomMessage.MessageParameters = rcs.OCS.TalkRoomMessage.MessageParameters + return message, nil +} diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/ocs.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/ocs.go new file mode 100644 index 0000000..28607b6 --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/ocs.go @@ -0,0 +1,25 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ocs + +type ocs struct { + OCSMeta ocsMeta `json:"meta"` +} + +type ocsMeta struct { + Status string `json:"status"` + StatusCode int `json:"statuscode"` + Message string `json:"message"` +} diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/ros.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/ros.go new file mode 100644 index 0000000..82f72bb --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/ocs/ros.go @@ -0,0 +1,38 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// File describes Nextcloud's Rich Object Strings (https://github.com/nextcloud/server/issues/1706) + +package ocs + +// RichObjectString describes the content of placeholders in TalkRoomMessageData +type RichObjectString struct { + Type RichObjectStringType `json:"type"` + ID string `json:"id"` + Name string `json:"name"` + Path string `json:"path"` + Link string `json:"link"` +} + +// RichObjectStringType describes what a rich object string is describing +type RichObjectStringType string + +const ( + // ROSTypeUser describes a rich object string that is a user + ROSTypeUser RichObjectStringType = "user" + // ROSTypeGroup describes a rich object string that is a group + ROSTypeGroup RichObjectStringType = "group" + // ROSTypeFile describes a rich object string that is a file + ROSTypeFile RichObjectStringType = "file" +) diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/room/room.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/room/room.go new file mode 100644 index 0000000..9e94f9d --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/room/room.go @@ -0,0 +1,267 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package room + +import ( + "context" + "errors" + "io/ioutil" + "net/http" + "strconv" + "time" + + "github.com/monaco-io/request" + + "gomod.garykim.dev/nc-talk/constants" + "gomod.garykim.dev/nc-talk/ocs" + "gomod.garykim.dev/nc-talk/user" +) + +var ( + // ErrEmptyToken is returned when the room token is empty + ErrEmptyToken = errors.New("given an empty token") + // ErrRoomNotFound is returned when a room with the given token could not be found + ErrRoomNotFound = errors.New("room could not be found") + // ErrUnauthorized is returned when the room could not be accessed due to being unauthorized + ErrUnauthorized = errors.New("unauthorized error when accessing room") + // ErrNotModeratorInLobby is returned when the room is in lobby mode but the user is not a moderator + ErrNotModeratorInLobby = errors.New("room is in lobby mode but user is not a moderator") + // ErrUnexpectedReturnCode is returned when the server did not respond with an expected return code + ErrUnexpectedReturnCode = errors.New("unexpected return code") + // ErrTooManyRequests is returned if the server returns a 429 + ErrTooManyRequests = errors.New("too many requests") + // ErrLackingCapabilities is returned if the server lacks the required capability for the given function + ErrLackingCapabilities = errors.New("lacking required capabilities") + // ErrForbidden is returned if the user is forbidden from accessing the requested resource + ErrForbidden = errors.New("request forbidden") + // ErrUnexpectedResponse is returned if the response from the Nextcloud Talk server is not formatted as expected + ErrUnexpectedResponse = errors.New("unexpected response") +) + +// TalkRoom represents a room in Nextcloud Talk +type TalkRoom struct { + User *user.TalkUser + Token string +} + +// Message represents a message to be sent +type Message struct { + Message string + ActorDisplayName string + ReplyTo int +} + +func (t *Message) toParameters() map[string]string { + return map[string]string{ + "message": t.Message, + "actorDisplayName": t.ActorDisplayName, + "replyTo": strconv.Itoa(t.ReplyTo), + } +} + +// NewTalkRoom returns a new TalkRoom instance +// Token should be the Nextcloud Room Token (e.g. "d6zoa2zs" if the room URL is https://cloud.mydomain.me/call/d6zoa2zs) +func NewTalkRoom(tuser *user.TalkUser, token string) (*TalkRoom, error) { + if token == "" { + return nil, ErrEmptyToken + } + if tuser == nil { + return nil, user.ErrUserIsNil + } + return &TalkRoom{ + User: tuser, + Token: token, + }, nil +} + +// SendMessage sends a string message in the Talk room +func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) { + return t.SendComplexMessage(&Message{Message: msg}) +} + +// SendComplexMessage sends a Message type message in the talk room +func (t *TalkRoom) SendComplexMessage(msg *Message) (*ocs.TalkRoomMessageData, error) { + url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token + + client := t.User.RequestClient(request.Client{ + URL: url, + Method: "POST", + Params: msg.toParameters(), + }) + res, err := client.Do() + if err != nil { + return nil, err + } + if res.StatusCode() != 201 { + return nil, ErrUnexpectedReturnCode + } + msgInfo, err := ocs.TalkRoomSentResponseUnmarshal(&res.Data) + if err != nil { + return nil, err + } + return &msgInfo.OCS.TalkRoomMessage, err +} + +// DeleteMessage deletes the message with the given messageID on the server. +// +// Requires "delete-messages" capability on the Nextcloud Talk server +func (t *TalkRoom) DeleteMessage(messageID int) (*ocs.TalkRoomMessageData, error) { + // Check for required capability + capable, err := t.User.Capabilities() + if err != nil { + return nil, err + } + if !capable.DeleteMessages { + return nil, ErrLackingCapabilities + } + + url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token + "/" + strconv.Itoa(messageID) + + client := t.User.RequestClient(request.Client{ + URL: url, + Method: "DELETE", + }) + res, err := client.Do() + if err != nil { + return nil, err + } + if res.StatusCode() != http.StatusOK && res.StatusCode() != http.StatusAccepted { + return nil, ErrUnexpectedReturnCode + } + msgInfo, err := ocs.TalkRoomSentResponseUnmarshal(&res.Data) + if err != nil { + return nil, err + } + return &msgInfo.OCS.TalkRoomMessage, nil +} + +// ReceiveMessages starts watching for new messages +func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessageData, error) { + c := make(chan ocs.TalkRoomMessageData) + err := t.TestConnection() + if err != nil { + return nil, err + } + url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token + requestParam := map[string]string{ + "lookIntoFuture": "1", + "includeLastKnown": "0", + } + lastKnown := "" + res, err := t.User.GetRooms() + if err != nil { + return nil, err + } + for _, r := range *res { + if r.Token == t.Token { + lastKnown = strconv.Itoa(r.LastReadMessage) + break + } + } + go func() { + for { + if ctx.Err() != nil { + return + } + if lastKnown != "" { + requestParam["lastKnownMessageId"] = lastKnown + } + client := t.User.RequestClient(request.Client{ + URL: url, + Params: requestParam, + Timeout: time.Second * 60, + }) + + res, err := client.Resp() + if err != nil { + continue + } + + // If it seems that we no longer have access to the chat for one reason or another, stop the goroutine and set error in the next return. + if res.StatusCode == http.StatusNotFound { + _ = res.Body.Close() + c <- ocs.TalkRoomMessageData{Error: ErrRoomNotFound} + return + } + if res.StatusCode == http.StatusUnauthorized { + _ = res.Body.Close() + c <- ocs.TalkRoomMessageData{Error: ErrUnauthorized} + return + } + if res.StatusCode == http.StatusTooManyRequests { + _ = res.Body.Close() + c <- ocs.TalkRoomMessageData{Error: ErrTooManyRequests} + return + } + if res.StatusCode == http.StatusForbidden { + _ = res.Body.Close() + c <- ocs.TalkRoomMessageData{Error: ErrForbidden} + return + } + + if res.StatusCode == http.StatusOK { + lastKnown = res.Header.Get("X-Chat-Last-Given") + data, err := ioutil.ReadAll(res.Body) + _ = res.Body.Close() + if err != nil { + continue + } + message, err := ocs.TalkRoomMessageDataUnmarshal(&data) + if err != nil { + continue + } + for _, msg := range message.OCS.TalkRoomMessage { + c <- msg + } + continue + } + _ = res.Body.Close() + } + }() + return c, nil +} + +// TestConnection tests the connection with the Nextcloud Talk instance and returns an error if it could not connect +func (t *TalkRoom) TestConnection() error { + if t.Token == "" { + return ErrEmptyToken + } + url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token + requestParam := map[string]string{ + "lookIntoFuture": "0", + "includeLastKnown": "0", + } + client := t.User.RequestClient(request.Client{ + URL: url, + Params: requestParam, + Timeout: time.Second * 30, + }) + + res, err := client.Do() + if err != nil { + return err + } + switch res.StatusCode() { + case http.StatusOK: + return nil + case http.StatusNotModified: + return nil + case http.StatusNotFound: + return ErrRoomNotFound + case http.StatusPreconditionFailed: + return ErrNotModeratorInLobby + } + return ErrUnexpectedReturnCode +} diff --git a/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/user/user.go b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/user/user.go new file mode 100644 index 0000000..e557cff --- /dev/null +++ b/teleirc/matterbridge/vendor/gomod.garykim.dev/nc-talk/user/user.go @@ -0,0 +1,321 @@ +// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package user + +import ( + "crypto/tls" + "encoding/json" + "errors" + "strings" + + "github.com/monaco-io/request" + + "gomod.garykim.dev/nc-talk/constants" + "gomod.garykim.dev/nc-talk/ocs" +) + +const ( + ocsCapabilitiesEndpoint = "/ocs/v2.php/cloud/capabilities" + ocsRoomsv2Endpoint = "/ocs/v2.php/apps/spreed/api/v2/room" + ocsRoomsv4Endpoint = "/ocs/v2.php/apps/spreed/api/v4/room" +) + +var ( + // ErrUserIsNil is returned when a function is called with an nil user. + ErrUserIsNil = errors.New("user is nil") + + // ErrCannotDownloadFile is returned when a function cannot download the requested file + ErrCannotDownloadFile = errors.New("cannot download file") +) + +// TalkUser represents a user of Nextcloud Talk +type TalkUser struct { + User string + Pass string + NextcloudURL string + Config *TalkUserConfig + capabilities *Capabilities +} + +// TalkUserConfig is configuration options for TalkUsers +type TalkUserConfig struct { + TLSConfig *tls.Config +} + +// Capabilities describes the capabilities that the Nextcloud Talk instance is capable of. Visit https://nextcloud-talk.readthedocs.io/en/latest/capabilities/ for more info. +type Capabilities struct { + AttachmentsFolder string `ocscapability:"config => attachments => folder"` + Audio bool `ocscapability:"audio"` + Video bool `ocscapability:"video"` + Chat bool `ocscapability:"chat"` + GuestSignaling bool `ocscapability:"guest-signaling"` + EmptyGroupRoom bool `ocscapability:"empty-group-room"` + GuestDisplayNames bool `ocscapability:"guest-display-names"` + MultiRoomUsers bool `ocscapability:"multi-room-users"` + ChatV2 bool `ocscapability:"chat-v2"` + Favorites bool `ocscapability:"favorites"` + LastRoomActivity bool `ocscapability:"last-room-activity"` + NoPing bool `ocscapability:"no-ping"` + SystemMessages bool `ocscapability:"system-messages"` + MentionFlag bool `ocscapability:"mention-flag"` + InCallFlags bool `ocscapability:"in-call-flags"` + InviteByMail bool `ocscapability:"invite-by-mail"` + NotificationLevels bool `ocscapability:"notification-levels"` + InviteGroupsAndMails bool `ocscapability:"invite-groups-and-mails"` + LockedOneToOneRooms bool `ocscapability:"locked-one-to-one-rooms"` + ReadOnlyRooms bool `ocscapability:"read-only-rooms"` + ChatReadMarker bool `ocscapability:"chat-read-marker"` + WebinaryLobby bool `ocscapability:"webinary-lobby"` + StartCallFlag bool `ocscapability:"start-call-flag"` + ChatReplies bool `ocscapability:"chat-replies"` + CirclesSupport bool `ocscapability:"circles-support"` + AttachmentsAllowed bool `ocscapability:"config => attachments => allowed"` + ConversationsCanCreate bool `ocscapability:"config => conversations => can-create"` + ForceMute bool `ocscapability:"force-mute"` + ConversationV2 bool `ocscapability:"conversation-v2"` + ChatReferenceID bool `ocscapability:"chat-reference-id"` + ConversationV3 bool `ocscapability:"conversation-v3"` + ConversationV4 bool `ocscapability:"conversation-v4"` + SIPSupport bool `ocscapability:"sip-support"` + ChatReadStatus bool `ocscapability:"chat-read-status"` + ListableRooms bool `ocscapability:"listable-rooms"` + PhonebookSearch bool `ocscapability:"phonebook-search"` + RaiseHand bool `ocscapability:"raise-hand"` + RoomDescription bool `ocscapability:"room-description"` + DeleteMessages bool `ocscapability:"delete-messages"` + RichObjectSharing bool `ocscapability:"rich-object-sharing"` + ConversationCallFlags bool `ocscapability:"conversation-call-flags"` + GeoLocationSharing bool `ocscapability:"geo-location-sharing"` + ReadPrivacyConfig bool `ocscapability:"config => chat => read-privacy"` + SignalingV3 bool `ocscapability:"signaling-v3"` + TempUserAvatarAPI bool `ocscapability:"temp-user-avatar-api"` + MaxGifSizeConfig int `ocscapability:"config => previews => max-gif-size"` + ChatMaxLength int `ocscapability:"config => chat => max-length"` +} + +// RoomInfo contains information about a room +type RoomInfo struct { + Token string `json:"token"` + Name string `json:"name"` + DisplayName string `json:"displayName"` + SessionID string `json:"sessionId"` + ObjectType string `json:"objectType"` + ObjectID string `json:"objectId"` + Type int `json:"type"` + ParticipantType int `json:"participantType"` + ParticipantFlags int `json:"participantFlags"` + ReadOnly int `json:"readOnly"` + LastPing int `json:"lastPing"` + LastActivity int `json:"lastActivity"` + NotificationLevel int `json:"notificationLevel"` + LobbyState int `json:"lobbyState"` + LobbyTimer int `json:"lobbyTimer"` + UnreadMessages int `json:"unreadMessages"` + LastReadMessage int `json:"lastReadMessage"` + HasPassword bool `json:"hasPassword"` + HasCall bool `json:"hasCall"` + CanStartCall bool `json:"canStartCall"` + CanDeleteConversation bool `json:"canDeleteConversation"` + CanLeaveConversation bool `json:"canLeaveConversation"` + IsFavorite bool `json:"isFavorite"` + UnreadMention bool `json:"unreadMention"` + LastMessage *ocs.TalkRoomMessageData `json:"lastMessage"` +} + +// NewUser returns a TalkUser instance +// The url should be the full URL of the Nextcloud instance (e.g. https://cloud.mydomain.me) +func NewUser(url string, username string, password string, config *TalkUserConfig) (*TalkUser, error) { + return &TalkUser{ + NextcloudURL: strings.TrimSuffix(url, "/"), + User: username, + Pass: password, + Config: config, + }, nil +} + +// RequestClient returns a monaco-io that is preconfigured to make OCS API calls +func (t *TalkUser) RequestClient(client request.Client) *request.Client { + if client.Header == nil { + client.Header = make(map[string]string) + } + if client.Header["OCS-APIRequest"] == "" { + client.Header["OCS-APIRequest"] = "true" + } + if client.Header["Accept"] == "" { + client.Header["Accept"] = "application/json" + } + client.BasicAuth = request.BasicAuth{ + Username: t.User, + Password: t.Pass, + } + + // Set Nextcloud URL if there is no host + if !strings.HasPrefix(client.URL, t.NextcloudURL) { + if strings.HasPrefix(client.URL, "/") { + client.URL = t.NextcloudURL + client.URL + } else { + client.URL = t.NextcloudURL + "/" + client.URL + } + } + + // Set TLS Config + if t.Config != nil { + client.TLSConfig = t.Config.TLSConfig + } + + return &client +} + +// GetRooms returns a list of all rooms the user is in +func (t *TalkUser) GetRooms() (*[]RoomInfo, error) { + endpoint := ocsRoomsv2Endpoint + capabilities, err := t.Capabilities() + if err != nil { + return nil, err + } + if capabilities.ConversationV4 { + endpoint = ocsRoomsv4Endpoint + } + + client := t.RequestClient(request.Client{ + URL: endpoint, + }) + res, err := client.Do() + if err != nil { + return nil, err + } + + var roomsRequest struct { + OCS struct { + Data []RoomInfo `json:"data"` + } `json:"ocs"` + } + + err = json.Unmarshal(res.Data, &roomsRequest) + if err != nil { + return nil, err + } + + return &roomsRequest.OCS.Data, nil +} + +// Capabilities returns an instance of Capabilities that describes what the Nextcloud Talk instance supports +func (t *TalkUser) Capabilities() (*Capabilities, error) { + if t.capabilities != nil { + return t.capabilities, nil + } + + client := t.RequestClient(request.Client{ + URL: ocsCapabilitiesEndpoint, + }) + res, err := client.Do() + if err != nil { + return nil, err + } + + capabilitiesRequest := &struct { + Ocs ocs.Capabilities `json:"ocs"` + }{} + + err = json.Unmarshal(res.Data, capabilitiesRequest) + if err != nil { + return nil, err + } + + sc := capabilitiesRequest.Ocs.Data.Capabilities.SpreedCapabilities + + tr := &Capabilities{ + Audio: sliceContains(sc.Features, "audio"), + Video: sliceContains(sc.Features, "video"), + Chat: sliceContains(sc.Features, "chat"), + GuestSignaling: sliceContains(sc.Features, "guest-signaling"), + EmptyGroupRoom: sliceContains(sc.Features, "empty-group-room"), + GuestDisplayNames: sliceContains(sc.Features, "guest-display-names"), + MultiRoomUsers: sliceContains(sc.Features, "multi-room-users"), + ChatV2: sliceContains(sc.Features, "chat-v2"), + Favorites: sliceContains(sc.Features, "favorites"), + LastRoomActivity: sliceContains(sc.Features, "last-room-activity"), + NoPing: sliceContains(sc.Features, "no-ping"), + SystemMessages: sliceContains(sc.Features, "system-messages"), + MentionFlag: sliceContains(sc.Features, "mention-flag"), + InCallFlags: sliceContains(sc.Features, "in-call-flags"), + InviteByMail: sliceContains(sc.Features, "invite-by-mail"), + NotificationLevels: sliceContains(sc.Features, "notification-levels"), + InviteGroupsAndMails: sliceContains(sc.Features, "invite-groups-and-mails"), + LockedOneToOneRooms: sliceContains(sc.Features, "locked-one-to-one-rooms"), + ReadOnlyRooms: sliceContains(sc.Features, "read-only-rooms"), + ChatReadMarker: sliceContains(sc.Features, "chat-read-marker"), + WebinaryLobby: sliceContains(sc.Features, "webinary-lobby"), + StartCallFlag: sliceContains(sc.Features, "start-call-flag"), + ChatReplies: sliceContains(sc.Features, "chat-replies"), + CirclesSupport: sliceContains(sc.Features, "circles-support"), + AttachmentsAllowed: sc.Config.Attachments.Allowed, + AttachmentsFolder: sc.Config.Attachments.Folder, + ConversationsCanCreate: sc.Config.Conversations.CanCreate, + ForceMute: sliceContains(sc.Features, "force-mute"), + ConversationV2: sliceContains(sc.Features, "conversation-v2"), + ChatReferenceID: sliceContains(sc.Features, "chat-reference-id"), + ChatMaxLength: sc.Config.Chat.MaxLength, + ConversationV3: sliceContains(sc.Features, "conversation-v3"), + ConversationV4: sliceContains(sc.Features, "conversation-v4"), + SIPSupport: sliceContains(sc.Features, "sip-support"), + ChatReadStatus: sliceContains(sc.Features, "chat-read-status"), + ListableRooms: sliceContains(sc.Features, "listable-rooms"), + PhonebookSearch: sliceContains(sc.Features, "phonebook-search"), + RaiseHand: sliceContains(sc.Features, "raise-hand"), + RoomDescription: sliceContains(sc.Features, "room-description"), + ReadPrivacyConfig: sc.Config.Chat.ReadPrivacy != 0, + MaxGifSizeConfig: sc.Config.Previews.MaxGifSize, + DeleteMessages: sliceContains(sc.Features, "delete-messages"), + RichObjectSharing: sliceContains(sc.Features, "rich-object-sharing"), + ConversationCallFlags: sliceContains(sc.Features, "conversation-call-flags"), + GeoLocationSharing: sliceContains(sc.Features, "geo-location-sharing"), + SignalingV3: sliceContains(sc.Features, "signaling-v3"), + TempUserAvatarAPI: sliceContains(sc.Features, "temp-user-avatar-api"), + } + + t.capabilities = tr + return tr, nil +} + +// sliceContains does the slice contain the string +func sliceContains(s []string, search string) bool { + for _, n := range s { + if n == search { + return true + } + } + return false +} + +// DownloadFile downloads the file at the given path +// +// Meant to be used with rich object string's path. +func (t *TalkUser) DownloadFile(path string) (data *[]byte, err error) { + url := t.NextcloudURL + constants.RemoteDavEndpoint(t.User, "files") + path + c := t.RequestClient(request.Client{ + URL: url, + }) + res, err := c.Do() + if err != nil { + return + } + if res.StatusCode() != 200 { + err = ErrCannotDownloadFile + return + } + data = &res.Data + return +} |
