Skip to content

Commit

Permalink
Add opt/in/out commands
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefmansy1 committed Mar 2, 2023
1 parent 24f6747 commit 5353b32
Show file tree
Hide file tree
Showing 63 changed files with 8,998 additions and 0 deletions.
64 changes: 64 additions & 0 deletions gateway/command.go
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions gateway/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 61 additions & 0 deletions gateway/persistent.go
Original file line number Diff line number Diff line change
@@ -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, newStaus OptOutStatus) error {
userdata := new(UserData)
r.UserStore.Get(UserID, userdata)

userdata.OptOut = newStaus

err := r.UserStore.Set(UserID, userdata)
if err != nil {
r.logger.Errorf(err.Error())
}
return err
}
11 changes: 11 additions & 0 deletions gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ require (
modernc.org/sqlite v1.20.3
)

require (
github.com/philippgille/gokv/bbolt v0.6.0 // indirect
github.com/philippgille/gokv/encoding v0.0.0-20191011213304-eb77f15b9c61 // indirect
github.com/philippgille/gokv/util v0.0.0-20191011213304-eb77f15b9c61 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
)

require (
filippo.io/edwards25519 v1.0.0 // indirect
github.com/Benau/go_rlottie v0.0.0-20210807002906-98c1b2421989 // indirect
Expand Down Expand Up @@ -107,6 +114,7 @@ require (
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/philhofer/fwd v1.1.1 // indirect
github.com/philippgille/gokv v0.6.0
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,17 @@ github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/philhofer/fwd v1.1.1 h1:GdGcTjf5RNAxwS4QLsiMzJYj5KEvPJD3Abr261yRQXQ=
github.com/philhofer/fwd v1.1.1/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
github.com/philippgille/gokv v0.0.0-20191001201555-5ac9a20de634/go.mod h1:OCoWPt+mbYuTO1FUVrQ2SxQU0oaaHBsn6lRhFX3JHOc=
github.com/philippgille/gokv v0.5.1-0.20191011213304-eb77f15b9c61/go.mod h1:OCoWPt+mbYuTO1FUVrQ2SxQU0oaaHBsn6lRhFX3JHOc=
github.com/philippgille/gokv v0.6.0 h1:fNEx/tSwV73nzlYd3iRYB8F+SEVJNNFzH1gsaT8SK2c=
github.com/philippgille/gokv v0.6.0/go.mod h1:tjXRFw9xDHgxLS8WJdfYotKGWp8TWqu4RdXjMDG/XBo=
github.com/philippgille/gokv/bbolt v0.6.0 h1:1Dz1vfth4CmQlgiU2SNXr0guQfncm0suLQD3V9N2/+g=
github.com/philippgille/gokv/bbolt v0.6.0/go.mod h1:usoSAx4i7w+e9MdyfO/cRVDJPaakISTk+oHyn4IkznQ=
github.com/philippgille/gokv/encoding v0.0.0-20191011213304-eb77f15b9c61 h1:IgQDuUPuEFVf22mBskeCLAtvd5c9XiiJG2UYud6eGHI=
github.com/philippgille/gokv/encoding v0.0.0-20191011213304-eb77f15b9c61/go.mod h1:SjxSrCoeYrYn85oTtroyG1ePY8aE72nvLQlw8IYwAN8=
github.com/philippgille/gokv/test v0.0.0-20191011213304-eb77f15b9c61/go.mod h1:EUc+s9ONc1+VOr9NUEd8S0YbGRrQd/gz/p+2tvwt12s=
github.com/philippgille/gokv/util v0.0.0-20191011213304-eb77f15b9c61 h1:ril/jI0JgXNjPWwDkvcRxlZ09kgHXV2349xChjbsQ4o=
github.com/philippgille/gokv/util v0.0.0-20191011213304-eb77f15b9c61/go.mod h1:2dBhsJgY/yVIkjY5V3AnDUxUbEPzT6uQ3LvoVT8TR20=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
Expand Down Expand Up @@ -1731,6 +1742,7 @@ gitlab.com/nyarla/go-crypt v0.0.0-20160106005555-d9a5dc2b789b/go.mod h1:T3BPAOm2
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
go.etcd.io/etcd v0.5.0-alpha.5.0.20200910180754-dd1b699fc489/go.mod h1:yVHk9ub3CSBatqGNg7GRmsnfLWtoW60w4eDYfh7vHDg=
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
Expand Down Expand Up @@ -2051,6 +2063,7 @@ golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191022100944-742c48ecaeb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191112214154-59a1497f0cea/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/philippgille/gokv/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions vendor/github.com/philippgille/gokv/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions vendor/github.com/philippgille/gokv/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5353b32

Please sign in to comment.