Add opt/in/out commands

This commit is contained in:
Yousef Mansy
2023-02-28 22:50:17 -08:00
parent 839f384e45
commit 7f9c3620e3
47 changed files with 7170 additions and 0 deletions

64
gateway/command.go Normal file
View File

@@ -0,0 +1,64 @@
package gateway
import (
"github.com/42wim/matterbridge/bridge/config"
)
// returns true if a command was registered (therefore a should not be relayed
func (r *Router) handleCommand(msg *config.Message) bool {
switch text := msg.Text; text {
case "!chatId":
r.logger.Infof("!chatId: %s", msg.Channel)
case "!optin":
r.logger.Debugf("!optin: %s", msg.UserID)
r.handleOptOutCmd(msg, OptIn)
case "!optout":
r.logger.Debugf("!optout: %s", msg.UserID)
r.handleOptOutCmd(msg, OptOut)
case "!optoutmedia":
r.logger.Debugf("!optoutmedia: %s", msg.UserID)
r.handleOptOutCmd(msg, OptOutMediaOnly)
case "!help":
r.logger.Debug("!help")
help := `!optout - opt out from all message relaying
!optoutmedia - only opt out from relaying attachments
!optin - opt back into chat relaying
!help - display this message`
r.replyCmd(msg, help)
case "!ping":
r.logger.Debug("!pong:")
r.replyCmd(msg, "pong!")
default:
return false
}
return true
}
func (r *Router) replyCmd(msg *config.Message, str string) {
srcBridge := r.getBridge(msg.Account)
reply := config.Message{
Text: str,
Channel: msg.Channel,
Account: msg.Account,
Username: "",
UserID: "",
Protocol: msg.Protocol,
Gateway: msg.Gateway,
ParentID: msg.ID,
}
srcBridge.Send(reply)
}
func (r *Router) handleOptOutCmd(msg *config.Message, newStaus OptOutStatus) {
err := r.setOptOutStatus(msg.UserID, newStaus)
reply := "Successfully set message relay preferences."
if err != nil {
reply = "Error setting message relay preferences, try again later or contact the moderators."
}
r.replyCmd(msg, reply)
}

View File

@@ -250,6 +250,43 @@ func (gw *Gateway) handleExtractNicks(msg *config.Message) {
}
}
func (r *Router) handleOptOutUser(msg *config.Message) {
if msg.UserID == "" {
return
}
status := r.getOptOutStatus(msg.UserID)
if status == OptOut {
msg.Avatar = ""
msg.Username = "[Opt-out User]"
if msg.Text != "" {
msg.Text = "Redacted Text\n"
}
files, exists := msg.Extra["file"]
if exists {
if files[0].(config.FileInfo).Comment != "" {
msg.Text = "Redacted Text\n"
}
msg.Text += fmt.Sprintf("Redacted %d Attachment(s)", len(files))
delete(msg.Extra, "file")
}
} else if status == OptOutMediaOnly {
files, exists := msg.Extra["file"]
if exists {
for _, f := range files {
file := f.(config.FileInfo)
if file.Comment != "" {
msg.Text += file.Comment + "\n"
}
}
msg.Text += fmt.Sprintf("Redacted %d Attachment(s)", len(files))
delete(msg.Extra, "file")
}
}
}
// extractNick searches for a username (based on "search" a regular expression).
// if this matches it extracts a nick (based on "extract" another regular expression) from text
// and replaces username with this result.

View File

@@ -8,6 +8,7 @@ import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/gateway/samechannel"
"github.com/philippgille/gokv"
"github.com/sirupsen/logrus"
)
@@ -19,6 +20,7 @@ type Router struct {
Gateways map[string]*Gateway
Message chan config.Message
MattermostPlugin chan config.Message
UserStore gokv.Store
logger *logrus.Entry
}
@@ -99,6 +101,11 @@ func (r *Router) Start() error {
}
}
}
userStorePath, exists := r.Config.GetString("UserStorePath")
if exists {
r.UserStore = r.getUserStore(userStorePath)
}
go r.handleReceive()
//go r.updateChannelMembers()
return nil
@@ -130,9 +137,13 @@ func (r *Router) getBridge(account string) *bridge.Bridge {
func (r *Router) handleReceive() {
for msg := range r.Message {
msg := msg // scopelint
if r.handleCommand(&msg) {
continue
}
r.handleEventGetChannelMembers(&msg)
r.handleEventFailure(&msg)
r.handleEventRejoinChannels(&msg)
r.handleOptOutUser(&msg)
// Set message protocol based on the account it came from
msg.Protocol = r.getBridge(msg.Account).Protocol

61
gateway/userstore.go Normal file
View File

@@ -0,0 +1,61 @@
package gateway
import (
"github.com/philippgille/gokv"
"github.com/philippgille/gokv/bbolt"
"github.com/philippgille/gokv/encoding"
)
type OptOutStatus int64
const (
OptIn OptOutStatus = 0
OptOut OptOutStatus = 1
OptOutMediaOnly OptOutStatus = 2
)
type UserData struct {
OptOut OptOutStatus
}
func (r *Router) getUserStore(path string) gokv.Store {
options := bbolt.Options{
BucketName: "UserData",
Path: path,
Codec: encoding.Gob,
}
store, err := bbolt.NewStore(options)
if err != nil {
r.logger.Errorf("Could not connect to db: %s", path)
}
return store
}
func (r *Router) getOptOutStatus(UserID string) OptOutStatus {
userdata := new(UserData)
found, err := r.UserStore.Get(UserID, userdata)
if err != nil {
r.logger.Error(err)
}
if found {
return userdata.OptOut
}
return OptIn
}
func (r *Router) setOptOutStatus(UserID string, newStatus OptOutStatus) error {
userdata := new(UserData)
r.UserStore.Get(UserID, userdata)
userdata.OptOut = newStatus
err := r.UserStore.Set(UserID, userdata)
if err != nil {
r.logger.Errorf(err.Error())
}
return err
}