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-08-15 22:15:24 +00:00
|
|
|
"context"
|
2023-02-08 09:14:07 +00:00
|
|
|
"errors"
|
2020-11-25 19:44:45 +00:00
|
|
|
"fmt"
|
2023-12-06 20:53:01 +00:00
|
|
|
"net/http"
|
2020-11-25 19:44:45 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2023-08-15 22:15:24 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/thegeeklab/wp-plugin-go/template"
|
2022-05-28 14:56:24 +00:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
"maunium.net/go/mautrix/event"
|
2023-12-06 08:09:37 +00:00
|
|
|
"maunium.net/go/mautrix/format"
|
2022-05-28 14:56:24 +00:00
|
|
|
"maunium.net/go/mautrix/id"
|
2020-11-25 19:44:45 +00:00
|
|
|
)
|
|
|
|
|
2023-02-08 09:14:07 +00:00
|
|
|
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
|
|
|
|
|
2023-08-15 22:15:24 +00:00
|
|
|
//nolint:revive
|
2023-08-21 09:36:07 +00:00
|
|
|
func (p *Plugin) run(ctx context.Context) error {
|
2023-08-15 22:15:24 +00:00
|
|
|
if err := p.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("validation failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.Execute(); err != nil {
|
|
|
|
return fmt.Errorf("execution failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 19:44:45 +00:00
|
|
|
// Validate handles the settings validation of the plugin.
|
|
|
|
func (p *Plugin) Validate() error {
|
2023-08-15 22:15:24 +00:00
|
|
|
if (p.Settings.Username == "" || p.Settings.Password == "") &&
|
|
|
|
(p.Settings.UserID == "" || p.Settings.AccessToken == "") {
|
2023-02-08 09:14:07 +00:00
|
|
|
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 {
|
2023-08-15 22:15:24 +00:00
|
|
|
muid := id.NewUserID(prepend("@", p.Settings.UserID), p.Settings.Homeserver)
|
2023-02-08 09:14:07 +00:00
|
|
|
|
2023-08-15 22:15: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)
|
|
|
|
}
|
|
|
|
|
2023-08-15 22:15:24 +00:00
|
|
|
if p.Settings.UserID == "" || p.Settings.AccessToken == "" {
|
2024-01-16 19:05:45 +00:00
|
|
|
_, err := client.Login(
|
|
|
|
p.Network.Context,
|
|
|
|
&mautrix.ReqLogin{
|
|
|
|
Type: "m.login.password",
|
|
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: p.Settings.Username},
|
|
|
|
Password: p.Settings.Password,
|
|
|
|
InitialDeviceDisplayName: "Woodpecker CI",
|
|
|
|
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
|
|
|
|
2023-08-15 22:15:24 +00:00
|
|
|
log.Info().Msg("logged in successfully")
|
2020-11-25 19:44:45 +00:00
|
|
|
|
2024-01-16 19:05:45 +00:00
|
|
|
joined, err := client.JoinRoom(p.Network.Context, 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-12-06 20:53:01 +00:00
|
|
|
content, err := p.messageContent(p.Network.Context, *p.Network.Client)
|
2020-11-25 19:44:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to render template: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:05:45 +00:00
|
|
|
if _, err := client.SendMessageEvent(p.Network.Context, 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
|
|
|
|
2023-08-15 22:15:24 +00:00
|
|
|
log.Info().Msg("message sent successfully")
|
2020-11-25 19:44:45 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 20:53:01 +00:00
|
|
|
func (p *Plugin) messageContent(ctx context.Context, client http.Client) (event.MessageEventContent, error) {
|
|
|
|
message, err := template.RenderTrim(ctx, client, p.Settings.Template, p.Metadata)
|
|
|
|
if err != nil {
|
|
|
|
return event.MessageEventContent{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
content := format.RenderMarkdown(message, true, p.Settings.TemplateUnsafe)
|
|
|
|
|
|
|
|
safeBody := format.HTMLToMarkdown(bluemonday.UGCPolicy().Sanitize(content.FormattedBody))
|
|
|
|
if content.Body != safeBody {
|
|
|
|
content.Body = safeBody
|
|
|
|
}
|
|
|
|
|
|
|
|
if content.FormattedBody != "" {
|
|
|
|
content.FormattedBody = bluemonday.UGCPolicy().Sanitize(content.FormattedBody)
|
|
|
|
}
|
|
|
|
|
|
|
|
return content, 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
|
|
|
}
|