2020-11-25 19:44:45 +00:00
|
|
|
// Copyright (c) 2020, the Drone Plugins project authors.
|
2021-09-13 21:00:00 +00:00
|
|
|
// Copyright (c) 2021, Robert Kaussow <mail@thegeeklab.de>
|
|
|
|
|
2020-11-25 19:44:45 +00:00
|
|
|
// Use of this source code is governed by an Apache 2.0 license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2023-02-08 09:14:07 +00:00
|
|
|
"errors"
|
2020-11-25 19:44:45 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
|
|
|
"github.com/russross/blackfriday/v2"
|
2022-05-28 14:56:24 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-02-08 15:30:46 +00:00
|
|
|
"github.com/thegeeklab/drone-plugin-lib/v2/template"
|
2022-05-28 14:56:24 +00:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/event"
|
|
|
|
"maunium.net/go/mautrix/id"
|
2020-11-25 19:44:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Settings for the plugin.
|
|
|
|
type Settings struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
UserID string
|
|
|
|
AccessToken string
|
|
|
|
Homeserver string
|
|
|
|
RoomID string
|
|
|
|
Template string
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:14:07 +00:00
|
|
|
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
|
|
|
|
|
2020-11-25 19:44:45 +00:00
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
2023-02-08 09:14:07 +00:00
|
|
|
if (p.settings.Username == "" || p.settings.Password == "") &&
|
|
|
|
(p.settings.UserID == "" || p.settings.AccessToken == "") {
|
|
|
|
return ErrAuthSourceNotSet
|
2022-05-28 14:56:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 19:44:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute provides the implementation of the plugin.
|
|
|
|
func (p *Plugin) Execute() error {
|
2022-05-28 14:56:24 +00:00
|
|
|
muid := id.NewUserID(prepend("@", p.settings.UserID), p.settings.Homeserver)
|
2023-02-08 09:14:07 +00:00
|
|
|
|
2022-05-28 14:56:24 +00:00
|
|
|
client, err := mautrix.NewClient(p.settings.Homeserver, muid, p.settings.AccessToken)
|
2020-11-25 19:44:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to initialize client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.settings.UserID == "" || p.settings.AccessToken == "" {
|
2022-05-28 14:56:24 +00:00
|
|
|
_, err := client.Login(&mautrix.ReqLogin{
|
2020-11-25 19:44:45 +00:00
|
|
|
Type: "m.login.password",
|
2022-05-28 14:56:24 +00:00
|
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: p.settings.Username},
|
2020-11-25 19:44:45 +00:00
|
|
|
Password: p.settings.Password,
|
|
|
|
InitialDeviceDisplayName: "Drone",
|
2022-05-28 14:56:24 +00:00
|
|
|
StoreCredentials: true,
|
2020-11-25 19:44:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to authenticate user: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:14:07 +00:00
|
|
|
|
|
|
|
logrus.Info("logged in successfully")
|
2020-11-25 19:44:45 +00:00
|
|
|
|
2022-05-28 14:56:24 +00:00
|
|
|
joined, err := client.JoinRoom(prepend("!", p.settings.RoomID), "", nil)
|
2020-11-25 19:44:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to join room: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-08 15:30:46 +00:00
|
|
|
message, err := template.RenderTrim(p.network.Context, *p.network.Client, p.settings.Template, p.pipeline)
|
2020-11-25 19:44:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to render template: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted := bluemonday.UGCPolicy().SanitizeBytes(
|
|
|
|
blackfriday.Run([]byte(message)),
|
|
|
|
)
|
|
|
|
|
|
|
|
content := gomatrix.HTMLMessage{
|
|
|
|
Body: message,
|
|
|
|
MsgType: "m.notice",
|
|
|
|
Format: "org.matrix.custom.html",
|
|
|
|
FormattedBody: string(formatted),
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:56:24 +00:00
|
|
|
if _, err := client.SendMessageEvent(joined.RoomID, event.EventMessage, content); err != nil {
|
2020-11-25 19:44:45 +00:00
|
|
|
return fmt.Errorf("failed to submit message: %w", err)
|
|
|
|
}
|
2023-02-08 09:14:07 +00:00
|
|
|
|
2022-05-28 14:56:24 +00:00
|
|
|
logrus.Info("message sent successfully")
|
2020-11-25 19:44:45 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-08 09:14:07 +00:00
|
|
|
func prepend(prefix, input string) string {
|
|
|
|
if strings.TrimSpace(input) == "" {
|
|
|
|
return input
|
2020-11-25 19:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:14:07 +00:00
|
|
|
if strings.HasPrefix(input, prefix) {
|
|
|
|
return input
|
2020-11-25 19:44:45 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:14:07 +00:00
|
|
|
return prefix + input
|
2020-11-25 19:44:45 +00:00
|
|
|
}
|