Ejemplo n.º 1
0
// Get actually sends the data
func (m *Middleware) Get(ib *irc.Connection, from string, to string, message string) {
	if !m.Started {
		return
	}
	for _, bit := range strings.Fields(message) {
		rs := re.FindAllStringSubmatch(bit, -1)
		if len(rs) > 0 {
			var extraStr string
			if len(rs[0][3]) > 0 {
				if strings.HasPrefix(rs[0][4], "issues/") {
					// If issue, get its info
					endpoint := fmt.Sprintf(apiURL, rs[0][1], rs[0][2], rs[0][3])
					ri := IssueInfo{}
					if err := utils.FetchURL(endpoint, &ri); err == nil {
						status := ri.FormatStatus()
						extraStr = fmt.Sprintf("| #%4d %s - Status : %s", ri.Number, ri.Title, status)
					}
				} else if strings.HasPrefix(rs[0][4], "commit/") {
					// If commit, get its info
					endpoint := fmt.Sprintf(apiURL, rs[0][1], rs[0][2], strings.Replace(rs[0][3], "/commit/", "/commits/", 1))
					ri := CommitInfo{}
					if err := utils.FetchURL(endpoint, &ri); err == nil {
						extraStr = fmt.Sprintf("| %s <%s> committed “%v”", ri.Commit.Author.Name, ri.Commit.Author.Email, strings.Replace(ri.Commit.Message, "\n", " ", -1))
					}
				} else if strings.HasPrefix(rs[0][4], "pull/") {
					// If pull request, get its info
					endpoint := fmt.Sprintf(apiURL, rs[0][1], rs[0][2], strings.Replace(rs[0][3], "/pull/", "/pulls/", 1))
					ri := PullRequestInfo{}
					if err := utils.FetchURL(endpoint, &ri); err == nil && len(ri.State) > 0 {
						status := ri.FormatStatus()
						extraStr = fmt.Sprintf("| %s PR %s by %s : \x0303+%d\x03 \x0304-%d\x0F\x03 in %d commits across %d files", status, ri.Title, ri.User.Login, ri.Additions, ri.Deletions, ri.Commits, ri.ChangedFiles)
					}
				}
			}
			endpoint := fmt.Sprintf(apiURL, rs[0][1], rs[0][2], "")
			ri := RepoInfo{}
			if err := utils.FetchURL(endpoint, &ri); err == nil {
				var finalStr string
				if len(extraStr) > 0 {
					finalStr = fmt.Sprintf("%s %s", ri.FullName, extraStr)
				} else {
					finalStr = fmt.Sprintf("%s : %v", ri.FullName, ri.Description)
				}
				ib.Privmsgf(configuration.Config.Channel, finalStr)
			}
		}
	}
}
Ejemplo n.º 2
0
func (c *Command) fetch(query string) (string, error) {
	var t message
	url := utils.EncodeURL(apiURL, query)
	if err := utils.FetchURL(url, &t); err != nil {
		return "", err
	}
	return t.Abstract, nil
}
Ejemplo n.º 3
0
// fetch queries the apiURL with the given query and populates the p Command.
func (c *Command) fetch(query string) error {
	var t message
	url := utils.EncodeURL(apiURL, query)
	err := utils.FetchURL(url, &t)
	if err != nil {
		return err
	}
	if len(t.List) == 0 {
		return errors.New("No result")
	}
	c.Last = t
	c.Current = 0
	c.CurrentResult = t.List[c.Current]
	c.sanitize()
	return nil
}
Ejemplo n.º 4
0
// Get is the actual call to your plugin.
func (c *Command) Get(ib *irc.Connection, from string, to string, args []string) {
	cnf := configuration.Config
	if !c.Started {
		return
	}
	if to == cnf.BotName {
		to = from
	}
	if len(args) > 2 && args[len(args)-2] == ">" {
		lang := args[len(args)-1]
		q := url.QueryEscape(strings.Join(args[:len(args)-2], " "))
		endpoint := fmt.Sprintf(translateEndpoint, cnf.YandexTrnslKey, lang, q)
		yr := Yandex{}
		if err := utils.FetchURL(endpoint, &yr); err != nil {
			log.Println(err)
			return
		}
		if len(yr.Text) > 0 {
			ib.Privmsgf(to, "%v: \x0314[%v]\x0F\x03 %v", from, yr.Lang, yr.Text[0])
		} else {
			ib.Privmsgf(to, "%v: \x0314Unrecognised language.\x0F\x03", from)
		}
	}
}