Esempio n. 1
0
func (cmd *AliasCommand) Parse(msg msgsystem.Message) bool {
	channel := msg.To
	m := strings.Split(msg.Msg, " ")
	command := m[0]
	params := strings.Join(m[1:], " ")

	switch command {
	case "!alias":
		if !msg.Authed {
			r := msgsystem.Message{
				To:  channel,
				Msg: "Security breach. Talk to ircflu admin!",
			}
			cmd.messagesOut <- r
			return true
		}

		a := strings.Split(params, "=")
		if len(a) == 2 {
			a[0] = strings.TrimSpace(a[0])
			a[1] = strings.TrimSpace(a[1])
			cmd.aliases[a[0]] = a[1]
			r := msgsystem.Message{
				To:  channel,
				Msg: "Added new alias '" + a[0] + "' for command '" + a[1] + "'",
			}
			cmd.messagesOut <- r
		} else {
			r := msgsystem.Message{
				To:  channel,
				Msg: "Usage: !alias [new command] = [actual command]",
			}
			cmd.messagesOut <- r

			for k, v := range cmd.aliases {
				r.Msg = "Alias: " + irctools.Colored(k, "red") + " = " + irctools.Colored(v, "teal")
				cmd.messagesOut <- r
			}
		}

		return true

	default:
		v, ok := cmd.aliases[strings.TrimSpace(msg.Msg)[1:]]
		if ok {
			fmt.Println("Alias:", v, strings.TrimSpace(msg.Msg), ok)
			r := msgsystem.Message{
				To:     channel,
				Source: msg.Source,
				Authed: msg.Authed,
				Msg:    "!" + v,
			}
			cmd.messagesIn <- r

			return true
		}
	}

	return false
}
Esempio n. 2
0
func (hook *GitLabHook) Request(ctx *web.Context) {
	b, err := ioutil.ReadAll(ctx.Request.Body)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println("Params:", string(b))

	var payload interface{}
	err = json.Unmarshal(b, &payload)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	data := payload.(map[string]interface{})

	before := data["before"].(string)
	after := data["after"].(string)
	ref := data["ref"].(string)
	ref = ref[strings.LastIndex(ref, "/")+1:]
	user := data["user_name"].(string)
	commitData := data["commits"].([]interface{})
	commitCount := int(data["total_commits_count"].(float64))

	repoData := data["repository"].(map[string]interface{})
	repo := repoData["name"].(string)
	url := repoData["homepage"].(string) + "/compare/" + before[:8] + "..." + after[:8]

	commitToken := "commits"
	if commitCount == 1 {
		commitToken = "commit"
	}
	ircmsg := msgsystem.Message{
		Msg: fmt.Sprintf("[%s] %s pushed %s new %s to %s: %s", irctools.Colored(repo, "lightblue"), irctools.Colored(user, "teal"), irctools.Bold(strconv.Itoa(commitCount)), commitToken, irctools.Colored(ref, "purple"), url),
	}
	hook.messages <- ircmsg

	for _, c := range commitData {
		commit := c.(map[string]interface{})
		commitId := commit["id"].(string)
		if commitId == before {
			continue
		}

		author := commit["author"]
		authorName := ""
		if author != nil {
			authorMap := author.(map[string]interface{})
			authorName = authorMap["name"].(string)
		}

		message := commit["message"].(string)
		ircmsg := msgsystem.Message{
			Msg: fmt.Sprintf("%s/%s %s %s: %s", irctools.Colored(repo, "lightblue"), irctools.Colored(ref, "purple"), irctools.Colored(commitId[:8], "grey"), irctools.Colored(authorName, "teal"), message),
		}
		hook.messages <- ircmsg
	}
}
Esempio n. 3
0
func (cmd *ExecCommand) Parse(msg msgsystem.Message) bool {
	channel := msg.To
	m := strings.Split(msg.Msg, " ")
	command := m[0]
	params := strings.TrimSpace(strings.Join(m[1:], " "))

	switch command {
	case "!exec":
		if !msg.Authed || strings.Index(params, "rm ") >= 0 || strings.Index(params, "mv ") >= 0 {
			r := msgsystem.Message{
				To:  channel,
				Msg: "Security breach. Talk to ircflu admin!",
			}
			cmd.messagesOut <- r
			return true
		}

		if len(params) > 0 {
			r := msgsystem.Message{
				To:  channel,
				Msg: irctools.Colored("Executing command!", "red"),
			}
			cmd.messagesOut <- r

			c := strings.Split(params, " ")
			e := exec.Command(c[0], c[1:]...)
			out, err := e.CombinedOutput()
			fmt.Println("Output:", string(out))
			fmt.Println("Error:", err)

			r = msgsystem.Message{
				To: []string{msg.Source},
			}
			if err != nil {
				r.Msg = "Command '" + params + "' failed: " + err.Error()
			} else {
				r.Msg = "Command '" + params + "' succeeded!"
			}

			cmd.messagesOut <- r
		} else {
			r := msgsystem.Message{
				To:  channel,
				Msg: "Usage: !exec [command]",
			}
			cmd.messagesOut <- r
		}

		return true
	}

	return false
}
Esempio n. 4
0
func (hook *JiraHook) Request(ctx *web.Context) {
	payload, err := ioutil.ReadAll(ctx.Request.Body)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	var data JiraWebhook
	err = json.Unmarshal(payload, &data)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	reg, err := regexp.Compile("rest/api.*")
	if err != nil {
		fmt.Println(err)
	}
	url := reg.ReplaceAllLiteralString(data.Issue.Self, "browse/"+data.Issue.Key)
	action := ""
	event := data.WebhookEvent
	switch {
	case data.Comment != nil:
		action = "Commented"
	case event == "jira:issue_created":
		action = "Created issue"
	case event == "jira:issue_updated":
		action = "Updated issue"
	default:
		action = event
	}

	msg := msgsystem.Message{
		Msg: fmt.Sprintf("[%s] %s %s %s %s", irctools.Colored(data.Issue.Key, "lightblue"), data.Issue.Fields.Summary, irctools.Colored(data.User.DisplayName, "teal"), action, url),
	}
	hook.messages <- msg

	if data.Changelog != nil {
		for _, change := range data.Changelog.Items {
			field := change.Field
			var msg msgsystem.Message
			if change.ToString == "" {
				msg = msgsystem.Message{
					Msg: fmt.Sprintf("Deleted %s %s", irctools.Colored(field, "lightblue"), irctools.Colored(change.FromString, "teal")),
				}
			} else {
				switch field {
				case "Attachment":
					msg = msgsystem.Message{
						Msg: fmt.Sprintf("Added %s %s", irctools.Colored(strings.Title(field), "lightblue"), irctools.Colored(change.ToString, "teal")),
					}
				default:
					msg = msgsystem.Message{
						Msg: fmt.Sprintf("Changed %s from %s to %s", irctools.Colored(strings.Title(field), "lightblue"), irctools.Colored(change.FromString, "purple"), irctools.Colored(change.ToString, "teal")),
					}
				}

			}
			hook.messages <- msg
		}
	}
}
Esempio n. 5
0
func (hook *GitHubHook) Request(ctx *web.Context) {
	payloadString, ok := ctx.Params["payload"]
	if !ok {
		fmt.Println("Couldn't find GitHub payload!")
		return
	}

	b := []byte(payloadString)

	var payload interface{}
	err := json.Unmarshal(b, &payload)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	data := payload.(map[string]interface{})

	before := data["before"].(string)
	after := data["after"].(string)
	ref := data["ref"].(string)
	ref = ref[strings.LastIndex(ref, "/")+1:]
	user := ""
	commitData := data["commits"].([]interface{})
	commitCount := 0

	repoData := data["repository"].(map[string]interface{})
	repo := repoData["name"].(string)
	url := repoData["url"].(string) + "/compare/" + before[:8] + "..." + after[:8]

	var ircmsgs []msgsystem.Message
	for _, c := range commitData {
		commit := c.(map[string]interface{})
		commitId := commit["id"].(string)
		if commitId == before {
			continue
		}

		if len(user) == 0 {
			author := commit["author"].(map[string]interface{})
			user = author["name"].(string)
		}

		commitCount++
		message := commit["message"].(string)

		msg := msgsystem.Message{
			Msg: fmt.Sprintf("%s/%s %s %s: %s", irctools.Colored(repo, "lightblue"), irctools.Colored(ref, "purple"), irctools.Colored(commitId[:8], "grey"), irctools.Colored(user, "teal"), message),
		}
		ircmsgs = append(ircmsgs, msg)
	}

	commitToken := "commits"
	if commitCount == 1 {
		commitToken = "commit"
	}
	msg := msgsystem.Message{
		Msg: fmt.Sprintf("[%s] %s pushed %s new %s to %s: %s", irctools.Colored(repo, "lightblue"), irctools.Colored(user, "teal"), irctools.Bold(strconv.Itoa(commitCount)), commitToken, irctools.Colored(ref, "purple"), url),
	}
	hook.messages <- msg

	for _, m := range ircmsgs {
		hook.messages <- m
	}
}