|
@@ -29,6 +29,7 @@ type CommandHandler struct {
|
|
log maulogger.Logger
|
|
log maulogger.Logger
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// NewCommandHandler creates a CommandHandler
|
|
func NewCommandHandler(bridge *Bridge) *CommandHandler {
|
|
func NewCommandHandler(bridge *Bridge) *CommandHandler {
|
|
return &CommandHandler{
|
|
return &CommandHandler{
|
|
bridge: bridge,
|
|
bridge: bridge,
|
|
@@ -36,6 +37,7 @@ func NewCommandHandler(bridge *Bridge) *CommandHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// CommandEvent stores all data which might be used to handle commands
|
|
type CommandEvent struct {
|
|
type CommandEvent struct {
|
|
Bot *appservice.IntentAPI
|
|
Bot *appservice.IntentAPI
|
|
Bridge *Bridge
|
|
Bridge *Bridge
|
|
@@ -45,6 +47,7 @@ type CommandEvent struct {
|
|
Args []string
|
|
Args []string
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Reply sends a reply to command as notice
|
|
func (ce *CommandEvent) Reply(msg string) {
|
|
func (ce *CommandEvent) Reply(msg string) {
|
|
_, err := ce.Bot.SendNotice(string(ce.RoomID), msg)
|
|
_, err := ce.Bot.SendNotice(string(ce.RoomID), msg)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -52,6 +55,7 @@ func (ce *CommandEvent) Reply(msg string) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Handle handles messages to the bridge
|
|
func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, message string) {
|
|
func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, message string) {
|
|
args := strings.Split(message, " ")
|
|
args := strings.Split(message, " ")
|
|
cmd := strings.ToLower(args[0])
|
|
cmd := strings.ToLower(args[0])
|
|
@@ -70,9 +74,14 @@ func (handler *CommandHandler) Handle(roomID types.MatrixRoomID, user *User, mes
|
|
handler.CommandLogout(ce)
|
|
handler.CommandLogout(ce)
|
|
case "help":
|
|
case "help":
|
|
handler.CommandHelp(ce)
|
|
handler.CommandHelp(ce)
|
|
|
|
+ default:
|
|
|
|
+ ce.Reply("Unknown Command")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const cmdLoginHelp = `login - Authenticate this Bridge as WhatsApp Web Client`
|
|
|
|
+
|
|
|
|
+// CommandLogin handles login command
|
|
func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
|
|
func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
|
|
if ce.User.Session != nil {
|
|
if ce.User.Session != nil {
|
|
ce.Reply("You're already logged in.")
|
|
ce.Reply("You're already logged in.")
|
|
@@ -83,6 +92,9 @@ func (handler *CommandHandler) CommandLogin(ce *CommandEvent) {
|
|
ce.User.Login(ce.RoomID)
|
|
ce.User.Login(ce.RoomID)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const cmdLogoutHelp = `logout - Logout from WhatsApp`
|
|
|
|
+
|
|
|
|
+// CommandLogout handles !logout command
|
|
func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
|
func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
|
if ce.User.Session == nil {
|
|
if ce.User.Session == nil {
|
|
ce.Reply("You're not logged in.")
|
|
ce.Reply("You're not logged in.")
|
|
@@ -100,6 +112,14 @@ func (handler *CommandHandler) CommandLogout(ce *CommandEvent) {
|
|
ce.Reply("Logged out successfully.")
|
|
ce.Reply("Logged out successfully.")
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+const cmdHelpHelp = `help - Prints this help`
|
|
|
|
+
|
|
|
|
+// CommandHelp handles help command
|
|
func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
|
func (handler *CommandHandler) CommandHelp(ce *CommandEvent) {
|
|
- ce.Reply("Help is not yet implemented 3:")
|
|
|
|
|
|
+ cmdPrefix := handler.bridge.Config.Bridge.CommandPrefix + " "
|
|
|
|
+ ce.Reply(strings.Join([]string{
|
|
|
|
+ cmdPrefix + cmdHelpHelp,
|
|
|
|
+ cmdPrefix + cmdLoginHelp,
|
|
|
|
+ cmdPrefix + cmdLogoutHelp,
|
|
|
|
+ }, "\n"))
|
|
}
|
|
}
|