Ejemplo n.º 1
0
func RunCommand(commandName string, extraArgs string, context *CommandContext) {
	log.Info("Processing command", commandName, "with args", extraArgs)
	commandName = strings.ToLower(commandName)
	splitArgs := strings.Fields(extraArgs)

	CommandMap[commandName](extraArgs, splitArgs, context)
}
Ejemplo n.º 2
0
func RunCommandBatch(commandLines [][]string, post discourse.S_Post, bot *discourse.DiscourseSite) {
	log.Info("Processing commands in post", post.Topic_id, post.Post_number, commandLines)

	var context = CommandContext{
		User:        CredentialsFromPost(post),
		Post:        post,
		Bot:         bot,
		replyBuffer: make([]string, 0),
	}

	for _, command := range commandLines {
		log.Debug(command[1], "X", command[2])
		if HasCommand(command[1]) {
			RunCommand(command[1], command[2], &context)
		} else {
			log.Warn("No such command", command[1])
		}
	}

	if context.redis != nil {
		context.redis.Close()
	}
	if len(context.replyBuffer) > 0 {
		bot.Reply(post.Topic_id, post.Post_number, strings.Join(context.replyBuffer, "\n\n"))
	}
}
Ejemplo n.º 3
0
func likethat(extraArgs string, splitArgs []string, c *CommandContext) {
	repliedPost, err := c.Bot.GetPostByNumber(c.Post.Topic_id, c.Post.Reply_to_post_number)
	if err == nil {
		c.Bot.LikePost(repliedPost.Id)
		log.Info("liked post", repliedPost.Id, "by likethat command")
	}
}
Ejemplo n.º 4
0
func (bot *DiscourseSite) Login(config Config) (err error) {
	var responseCurrent struct {
		Current_user struct {
			Id       int
			Username string
		}
	}

	// expected: ErrorBadJsonType and 404
	err = bot.DGetJsonTyped("/session/current.json", &responseCurrent)
	if err == nil {
		if responseCurrent.Current_user.Username == config.Username {

			log.Info("Already logged in as", config.Username)
			bot.userId = responseCurrent.Current_user.Id

			return
		}
	}

	loginData := url.Values{}
	loginData.Set("login", config.Username)
	loginData.Set("password", config.Password)
	var response ResponseUserSerializer

	err = bot.DPostJsonTyped("/session", loginData, &response)
	if response.User.Username == config.Username {

		log.Info("Logged in as", config.Username)
		bot.userId = response.User.Id
		bot.saveCookies()

		return nil
	}
	if err != nil {
		return err
	} else {
		return ResponseGenericError{[]string{"Login failed"}, "login_failed"}
	}
}
Ejemplo n.º 5
0
func likepost(extraArgs string, splitArgs []string, c *CommandContext) {
	if len(splitArgs) < 2 {
		c.AddReply("Not enough arguments.")
		return
	}
	topicId, err := strconv.Atoi(splitArgs[0])
	if err != nil {
		c.AddReply(fmt.Sprintf(
			"Cannot parse %s as number: %s", splitArgs[0], err))
		return
	}
	postNum, err := strconv.Atoi(splitArgs[1])
	if err != nil {
		c.AddReply(fmt.Sprintf(
			"Cannot parse %s as number: %s", splitArgs[1], err))
		return
	}
	postToLike, err := c.Bot.GetPostByNumber(topicId, postNum)
	if err != nil {
		return
	}
	c.Bot.LikePost(postToLike.Id)
	log.Info("liked post", postToLike.Id, "by likepost command")
}
Ejemplo n.º 6
0
func likeme(extraArgs string, splitArgs []string, c *CommandContext) {
	c.Bot.LikePost(c.Post.Id)
	log.Info("liked post", c.Post.Id, "by likeme command")
}