2023-02-02 05:45:23 +00:00
// notification.go
// Copyright (C) Andrew Woodlee 2023
// License: Apache-2.0
package backy
import (
"fmt"
2023-09-09 04:42:13 +00:00
"strings"
2023-02-02 05:45:23 +00:00
2023-09-09 04:42:13 +00:00
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
2023-02-02 05:45:23 +00:00
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/mail"
"github.com/nikoksr/notify/service/matrix"
"maunium.net/go/mautrix/id"
)
2023-09-09 04:42:13 +00:00
type MatrixStruct struct {
Homeserver string ` yaml:"homeserver" `
Roomid id . RoomID ` yaml:"room-id" `
AccessToken string ` yaml:"access-token" `
UserId id . UserID ` yaml:"user-id" `
2023-02-02 05:45:23 +00:00
}
2023-09-09 04:42:13 +00:00
type MailConfig struct {
Host string ` yaml:"host" `
Port string ` yaml:"port" `
Username string ` yaml:"username" `
SenderAddress string ` yaml:"senderaddress" `
To [ ] string ` yaml:"to" `
Password string ` yaml:"password" `
2023-02-02 05:45:23 +00:00
}
// SetupNotify sets up notify instances for each command list.
2023-09-09 04:42:13 +00:00
func ( opts * ConfigOpts ) SetupNotify ( ) {
2023-02-02 05:45:23 +00:00
2023-09-09 04:42:13 +00:00
// check if we have individual commands instead of lists to execute
if len ( opts . executeCmds ) != 0 {
return
}
2023-02-02 05:45:23 +00:00
2023-09-09 04:42:13 +00:00
for confName , cmdConfig := range opts . CmdConfigLists {
2024-08-28 20:06:25 +00:00
2023-02-02 05:45:23 +00:00
var services [ ] notify . Notifier
2023-09-09 04:42:13 +00:00
for _ , id := range cmdConfig . Notifications {
if ! strings . Contains ( id , "." ) {
opts . Logger . Info ( ) . Str ( "id" , id ) . Str ( "list" , cmdConfig . Name ) . Msg ( "key does not contain a \".\" Make sure to follow the docs: https://backy.cybershell.xyz/config/notifications/" )
logging . ExitWithMSG ( fmt . Sprintf ( "notification id %s in cmd list %s does not contain a \".\" \nMake sure to follow the docs: https://backy.cybershell.xyz/config/notifications/" , id , cmdConfig . Name ) , 1 , & opts . Logger )
}
2023-02-02 05:45:23 +00:00
2023-09-09 04:42:13 +00:00
confSplit := strings . Split ( id , "." )
confType := confSplit [ 0 ]
confId := confSplit [ 1 ]
switch confType {
case "mail" :
conf , ok := opts . NotificationConf . MailConfig [ confId ]
if ! ok {
opts . Logger . Info ( ) . Err ( fmt . Errorf ( "error: ID %s not found in mail object" , confId ) ) . Str ( "list" , confName ) . Send ( )
continue
}
mailConf := setupMail ( conf )
services = append ( services , mailConf )
case "matrix" :
conf , ok := opts . NotificationConf . MatrixConfig [ confId ]
if ! ok {
opts . Logger . Info ( ) . Err ( fmt . Errorf ( "error: ID %s not found in matrix object" , confId ) ) . Str ( "list" , confName ) . Send ( )
continue
2023-02-02 05:45:23 +00:00
}
2023-09-09 04:42:13 +00:00
mtrxConf , mtrxErr := setupMatrix ( conf )
if mtrxErr != nil {
opts . Logger . Info ( ) . Str ( "list" , confName ) . Err ( fmt . Errorf ( "error: configuring matrix id %s failed during setup: %w" , id , mtrxErr ) )
continue
}
// append the services
services = append ( services , mtrxConf )
// service is not recognized
default :
opts . Logger . Info ( ) . Err ( fmt . Errorf ( "id %s not found" , id ) ) . Str ( "list" , confName ) . Send ( )
2023-02-02 05:45:23 +00:00
}
}
cmdConfig . NotifyConfig = notify . NewWithServices ( services ... )
}
// logging.ExitWithMSG("This was a test of notifications", 0, nil)
}
2023-09-09 04:42:13 +00:00
func setupMatrix ( config MatrixStruct ) ( * matrix . Matrix , error ) {
matrixClient , matrixErr := matrix . New ( config . UserId , config . Roomid , config . Homeserver , config . AccessToken )
2023-02-02 05:45:23 +00:00
if matrixErr != nil {
2023-09-09 04:42:13 +00:00
return nil , matrixErr
2023-02-02 05:45:23 +00:00
}
return matrixClient , nil
}
2023-09-09 04:42:13 +00:00
func setupMail ( config MailConfig ) * mail . Mail {
mailClient := mail . New ( config . SenderAddress , config . Host + ":" + config . Port )
mailClient . AuthenticateSMTP ( "" , config . Username , config . Password , config . Host )
mailClient . AddReceivers ( config . To ... )
2023-02-12 05:50:19 +00:00
mailClient . BodyFormat ( mail . PlainText )
2023-02-02 05:45:23 +00:00
return mailClient
}