0
0
mirror of https://github.com/thegeeklab/wp-matrix.git synced 2024-11-09 18:10:39 +00:00
wp-matrix/plugin/impl.go

118 lines
3.1 KiB
Go
Raw Normal View History

2020-11-25 19:44:45 +00:00
// Copyright (c) 2020, the Drone Plugins project authors.
// 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 (
"context"
"errors"
2020-11-25 19:44:45 +00:00
"fmt"
"strings"
"github.com/matrix-org/gomatrix"
"github.com/microcosm-cc/bluemonday"
"github.com/rs/zerolog/log"
2020-11-25 19:44:45 +00:00
"github.com/russross/blackfriday/v2"
"github.com/thegeeklab/wp-plugin-go/template"
"github.com/urfave/cli/v2"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
2020-11-25 19:44:45 +00:00
)
var ErrAuthSourceNotSet = errors.New("either username and password or userid and accesstoken are required")
// Execute provides the implementation of the plugin.
//
//nolint:revive
func (p *Plugin) run(ctx context.Context, cCtx *cli.Context) error {
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 {
if (p.Settings.Username == "" || p.Settings.Password == "") &&
(p.Settings.UserID == "" || p.Settings.AccessToken == "") {
return ErrAuthSourceNotSet
}
2020-11-25 19:44:45 +00:00
return nil
}
// Execute provides the implementation of the plugin.
func (p *Plugin) Execute() error {
muid := id.NewUserID(prepend("@", p.Settings.UserID), p.Settings.Homeserver)
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 == "" {
_, err := client.Login(&mautrix.ReqLogin{
2020-11-25 19:44:45 +00:00
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)
}
}
log.Info().Msg("logged in successfully")
2020-11-25 19:44:45 +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)
}
message, err := template.RenderTrim(p.Network.Context, *p.Network.Client, p.Settings.Template, p.Metadata)
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),
}
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)
}
log.Info().Msg("message sent successfully")
2020-11-25 19:44:45 +00:00
return nil
}
func prepend(prefix, input string) string {
if strings.TrimSpace(input) == "" {
return input
2020-11-25 19:44:45 +00:00
}
if strings.HasPrefix(input, prefix) {
return input
2020-11-25 19:44:45 +00:00
}
return prefix + input
2020-11-25 19:44:45 +00:00
}