-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24f6747
commit f544a14
Showing
63 changed files
with
8,997 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
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 | ||
!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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.