Ejemplo n.º 1
0
func (bot *Bot) RunBuiltinCommands(event *irc.Event) {
	args := strings.Split(strings.TrimSpace(event.Message()), " ")
	command := args[0]

	// Bin the command from the arg list
	if len(args) > 1 {
		args = args[1:]
	} else {
		args = []string{}
	}

	// Get the current channel privileges for the nick sending this command
	privs, ok := bot.state.GetPrivs(event.Arguments[0], event.Nick)

	// Commands must be run by known users
	switch {
	case command == "!rejoin" && event.Arguments[0] == bot.conn.GetNick():
		bot.conn.Join(bot.cfg.Irc.NormalChannel)
		bot.conn.Join(bot.cfg.Irc.StaffChannel)
	case ok && command == "!reload":
		if privs.Owner || privs.Admin || privs.Op {
			bot.pm.InitJS()
			utils.IRCAction(bot.conn, event.Arguments[0], "has reloaded its plugins")
		} else {
			utils.IRCAction(bot.conn, event.Arguments[0], fmt.Sprintf("slaps %s's hands away from the op only controls", event.Nick))
		}
	case command == "!ping":
		bot.conn.Privmsg(event.Arguments[0], fmt.Sprintf("%s: PONG!", event.Nick))
	case ok && command == "!quit":
		if privs.Owner || privs.Admin || privs.Op {
			bot.Quit()
		} else {
			utils.IRCAction(bot.conn, event.Arguments[0], fmt.Sprintf("slaps %s's hands away from the op only controls", event.Nick))
		}
	case ok && command == "!voice":
		if privs.Owner || privs.Admin || privs.Op {
			bot.VoiceAll(event)
		} else {
			utils.IRCAction(bot.conn, event.Arguments[0], fmt.Sprintf("slaps %s's hands away from the op only controls", event.Nick))
		}
	case command == "!help":
		if len(args) == 0 {
			bot.ShowCommandList(event.Arguments[0], event.Nick)
		} else {
			bot.ShowCommandHelp(event.Arguments[0], event.Nick, args[0])
		}
	case ok && command == "!import" && len(args) >= 2 && bot.cfg.Irc.StaffChannel == event.Arguments[0]:
		if privs.Owner {
			overwrite := false
			if len(args) == 3 {
				overwrite = args[2] == "overwrite"
			}
			if err := bot.pm.ImportPlugin(event.Nick, args[0], args[1], overwrite); err != nil {
				bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("ALERT: %s tried to use !import with %s and got this error: %s", event.Nick, args[0], err.Error()))
			} else {
				bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("ALERT: %s successfully used !import with %s", event.Nick, args[0]))
			}
		} else {
			utils.IRCAction(bot.conn, event.Arguments[0], fmt.Sprintf("slaps %s's hands away from the op only controls", event.Nick))
		}
	case ok && command == "!debug" && bot.cfg.Irc.StaffChannel == event.Arguments[0]:
		if privs.Owner || privs.Admin || privs.Op {
			switch {
			case len(args) > 0 && args[0] == "on":
				port, alreadyRunning := debug.StartDebugServer()
				if alreadyRunning {
					bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("%s: Debug server running on port %s", event.Nick, port))
				} else {
					bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("%s: Debug server started on port %s", event.Nick, port))
				}
				bot.cfg.Debug = true
			case len(args) > 0 && args[0] == "off":
				debug.StopDebugServer()
				bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("%s: Debug server stopped", event.Nick))
				bot.cfg.Debug = false
			case len(args) > 0 && args[0] == "status":
				status := debug.DebugServerStatus()
				bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("%s: Debug server is %s", event.Nick, status))
			default:
				bot.conn.Privmsg(bot.cfg.Irc.StaffChannel, fmt.Sprintf("%s: usage - !debug [on|off]", event.Nick))
			}
			bot.conn.VerboseCallbackHandler = bot.cfg.Debug
		} else {
			utils.IRCAction(bot.conn, event.Arguments[0], fmt.Sprintf("slaps %s's hands away from the op only controls", event.Nick))
		}
	}
}
Ejemplo n.º 2
0
func (pm *PluginManager) InitIRCJSBridge() {
	bridge := &pmIRCJSBridge{
		Nick: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.Nick(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		GetNick: func(call otto.FunctionCall) otto.Value {
			val, err := otto.ToValue(pm.conn.GetNick())
			if err != nil {
				return otto.NullValue()
			}
			return val
		},
		SendRaw: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.SendRaw(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Privmsg: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
				pm.conn.Privmsg(call.Argument(0).String(), call.Argument(1).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Notice: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
				pm.conn.Notice(call.Argument(0).String(), call.Argument(1).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Part: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.Part(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Join: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.Join(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Who: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.Who(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Whois: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.Whois(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Mode: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				pm.conn.Mode(call.Argument(0).String())
				return otto.TrueValue()
			} else {
				if len(call.ArgumentList) > 1 && call.ArgumentList[0].IsString() {
					var args []string
					for _, arg := range call.ArgumentList[1:] {
						if !arg.IsString() {
							return otto.FalseValue()
						}
						args = append(args, arg.String())
					}
					pm.conn.Mode(call.Argument(0).String(), args...)
				}
				return otto.FalseValue()
			}
		},
		Nicks: func(call otto.FunctionCall) otto.Value {
			return utils.SliceToJavascriptArray(pm.js, pm.state.Nicks())
		},
		Channels: func(call otto.FunctionCall) otto.Value {
			return utils.SliceToJavascriptArray(pm.js, pm.state.Channels())
		},
		Action: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
				utils.IRCAction(pm.conn, call.Argument(0).String(), call.Argument(1).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Topic: func(call otto.FunctionCall) otto.Value {
			switch {
			case len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString():
				utils.IRCTopic(pm.conn, call.Argument(0).String())
				return otto.TrueValue()
			case len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString():
				utils.IRCTopic(pm.conn, call.Argument(0).String(), call.Argument(1).String())
				return otto.TrueValue()
			default:
				return otto.FalseValue()
			}
		},
		Away: func(call otto.FunctionCall) otto.Value {
			switch {
			case len(call.ArgumentList) == 0:
				utils.IRCAway(pm.conn)
				return otto.TrueValue()
			case len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString():
				utils.IRCAway(pm.conn, call.Argument(0).String())
				return otto.TrueValue()
			default:
				return otto.FalseValue()
			}
		},
		Oper: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
				utils.IRCOper(pm.conn, call.Argument(0).String(), call.Argument(1).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		Invite: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
				utils.IRCInvite(pm.conn, call.Argument(0).String(), call.Argument(1).String())
				return otto.TrueValue()
			} else {
				return otto.FalseValue()
			}
		},
		GetPrivs: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) == 2 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() {
				if privs, ok := pm.state.GetPrivs(call.Argument(0).String(), call.Argument(1).String()); ok {
					if val, err := pm.js.ToValue(privs); err == nil {
						return val
					}
				}
			}
			return otto.FalseValue()
		},
		Redispatch: func(call otto.FunctionCall) otto.Value {
			if len(call.ArgumentList) >= 7 {
				arguments := make([]string, 0)
				for _, arg := range call.ArgumentList {
					if !arg.IsString() {
						return otto.FalseValue()
					}
					arguments = append(arguments, arg.String())
				}
				utils.IRCRedispatch(
					pm.conn,
					arguments[0],
					arguments[1],
					arguments[2],
					arguments[3],
					arguments[4],
					arguments[5],
					arguments[6:]...,
				)
				return otto.TrueValue()
			}
			return otto.FalseValue()
		},
	}
	pm.js.Set("IRC", bridge)
}