Example #1
0
// HandleIncoming checks incoming SendEvents for URLs and posts the link title
// for the first URL returning a valid title.
func (l *LinkTitleHandler) HandleIncoming(r *gobot.Room, p *proto.Packet) (*proto.Packet, error) {
	if p.Type != proto.SendEventType {
		return nil, nil
	}
	r.Logger.Debugf("Handler received SendEvent")
	payload, err := p.Payload()
	if err != nil {
		return nil, err
	}
	msg, ok := payload.(*proto.SendEvent)
	if !ok {
		return nil, fmt.Errorf("Could not assert SendEvent as such.")
	}
	if msg.Sender.Name == "euphoriabot" {
		return nil, nil
	}
	r.Logger.Debugf("Received message with content: %s", msg.Content)
	urls := linkMatcher.FindAllString(msg.Content, -1)
	for _, url := range urls {
		r.Logger.Debugf("Trying URL %s", url)
		if !strings.HasPrefix(url, "http") {
			url = "http://" + url
		}
		title, err := getLinkTitle(url)
		if err == nil && title != "" {
			r.SendText(&msg.ID, "Link title: "+title)
			break
		}
	}
	return nil, nil
}
Example #2
0
// HandleIncoming checks incoming commands for !uptime or !uptime @[BotName] and
// responds with the duration the bot has been up.
func (u *UptimeHandler) HandleIncoming(r *gobot.Room, p *proto.Packet) (*proto.Packet, error) {
	if p.Type != proto.SendEventType {
		return nil, nil
	}
	raw, err := p.Payload()
	if err != nil {
		return nil, err
	}
	payload, ok := raw.(*proto.SendEvent)
	if !ok {
		r.Logger.Warningln("Unable to assert packet as SendEvent.")
		return nil, err
	}
	if !strings.HasPrefix(payload.Content, "!uptime") {
		return nil, nil
	}
	if payload.Content != "!uptime" && payload.Content != "!uptime @"+r.BotName {
		return nil, nil
	}
	uptime := time.Since(u.t0)
	days := int(uptime.Hours()) / 24
	hours := int(uptime.Hours()) % 24
	minutes := int(uptime.Minutes()) % 60
	seconds := int(uptime.Seconds()) % 60
	if _, err := r.SendText(&payload.ID, fmt.Sprintf(
		"This bot has been up for %dd %dh %dm %.3fs.",
		days, hours, minutes, seconds)); err != nil {
		return nil, err
	}
	return nil, nil
}
Example #3
0
// HandleIncoming satisfies the Handler interface.
func (ph *PongHandler) HandleIncoming(r *gobot.Room, p *proto.Packet) (*proto.Packet, error) {
	r.Logger.Debugln("Checking for ping command...")
	if p.Type != proto.SendEventType {
		return nil, nil
	}
	raw, err := p.Payload()
	if err != nil {
		return nil, err
	}
	payload, ok := raw.(*proto.SendEvent)
	if !ok {
		r.Logger.Warningln("Unable to assert packet as SendEvent.")
		return nil, err
	}
	if !strings.HasPrefix(payload.Content, "!ping") {
		return nil, nil
	}
	if strings.Contains(payload.Content, "@") && !strings.HasPrefix(payload.Content, "!ping @"+r.BotName) {
		return nil, nil
	}
	r.Logger.Debugln("Sending !ping reply...")
	if _, err := r.SendText(&payload.ID, "pong!"); err != nil {
		return nil, err
	}
	return nil, nil
}
Example #4
0
// HandleIncoming checks incoming SendEvents for help commands and responds
// appropriately.
func (h *HelpHandler) HandleIncoming(r *gobot.Room, p *proto.Packet) (*proto.Packet, error) {
	if p.Type != proto.SendEventType {
		return nil, nil
	}
	raw, err := p.Payload()
	if err != nil {
		return nil, err
	}
	payload, ok := raw.(*proto.SendEvent)
	if !ok {
		r.Logger.Warningln("Unable to assert packet as SendEvent.")
		return nil, err
	}
	if !strings.HasPrefix(payload.Content, "!help") {
		return nil, nil
	}
	if payload.Content != "!help @"+r.BotName && payload.Content != "!help" {
		return nil, nil
	}
	if payload.Content == "!help" {
		if _, err := r.SendText(&payload.ID, h.ShortDesc); err != nil {
			return nil, err
		}
		return nil, nil
	}
	if _, err := r.SendText(&payload.ID, h.LongDesc); err != nil {
		return nil, err
	}
	return nil, nil
}
Example #5
0
func (c *CIListener) Run(r *gobot.Room) {
	ghReturn := make(chan string)
	go c.GithubListener(r, c.port, c.secret, ghReturn)
	for {
		select {
		case msg <- ghReturn:
			_, err := r.SendText("", msg)
			if err != nil {
				r.Logger.Warning(err)
			}
		}
	}
}
Example #6
0
func (c *CIListener) GithubListener(r *gobot.Room, port int, secret string, returnChan chan string) {
	gServer := gohook.NewServer(port, secret, "/postreceive")
	gServer.GoListenAndServe()
	for {
		et := <-gServer.EventAndTypes
		var msg string
		switch et.Type {
		case gohook.PingEventType:
			continue
		case gohook.CommitCommentEventType:
			payload, ok := et.Event.(*gohook.CommitCommentEvent)
			if !ok {
				panic("Malformed *CommitCommentEvent.")
			}
			msg = fmt.Sprintf("[ %s ] Comment on commit: %s (%s)",
				payload.Repository.Name,
				payload.Comment.Body,
				payload.Comment.HTMLURL,
			)
		case gohook.CreateEventType:
			payload, ok := et.Event.(*gohook.CreateEvent)
			if !ok {
				panic("Malformed *CreateEvent.")
			}
			msg = fmt.Sprintf("[ %s | Branch/Tag: %s] Created.",
				payload.Repository.Name,
				payload.RefType,
			)
		case gohook.DeleteEventType:
			payload, ok := et.Event.(*gohook.DeleteEvent)
			if !ok {
				panic("Malformed *DeleteEvent.")
			}
			msg = fmt.Sprintf("[ %s | Branch/Tag: %s] Deleted.",
				payload.Repository,
				payload.RefType,
			)
		case gohook.IssueCommentEventType:
			payload, ok := et.Event.(*gohook.IssueCommentEvent)
			if !ok {
				panic("Malformed *CommitCommentEvent.")
			}
			msg = fmt.Sprintf("[ %s | Issue: %s ] Comment: %s (%s)",
				payload.Repository.Name,
				payload.Issue.Title,
				payload.Comment.Body,
				payload.Comment.HTMLURL,
			)
		case gohook.IssuesEventType:
			payload, ok := et.Event.(*gohook.IssuesEvent)
			if !ok {
				panic("Malformed *IssuesEvent.")
			}
			msg = fmt.Sprintf("[ %s | Issue: %s ] Action: %s. (%s)",
				payload.Repository.Name,
				payload.Issue.Title,
				payload.Action,
				payload.Issue.HTMLURL,
			)
		case gohook.PullRequestEventType:
			payload, ok := et.Event.(*gohook.PullRequestEvent)
			if !ok {
				panic("Malformed *PullRequestEvent.")
			}
			action := payload.Action
			if action == "synced" {
				action = "New commits made to synced branch."
			}
			msg = fmt.Sprintf(":pencil: [ %s | PR: %s ] %s (%s)",
				payload.Repository.Name,
				payload.PullRequest.Title,
				action,
				payload.PullRequest.HTMLURL,
			)
		case gohook.PullRequestReviewCommentEventType:
			payload, ok := et.Event.(*gohook.PullRequestReviewCommentEvent)
			if !ok {
				panic("Malformed *PullRequestReviewCommentEvent.")
			}
			msg = fmt.Sprintf(":speech_balloon: [ %s | PR: %s ] Comment: %s: %s (%s)",
				payload.Repository.Name,
				payload.PullRequest.Title,
				payload.Sender.Login,
				payload.Comment.Body,
				payload.PullRequest.HTMLURL,
			)
		case gohook.RepositoryEventType:
			payload, ok := et.Event.(*gohook.RepositoryEvent)
			if !ok {
				panic("Malformed *RepositoryEvent.")
			}
			msg = fmt.Sprintf("[ Repository: %s ] Action: created. (%s) ",
				payload.Repository.Name,
				payload.Repository.HTMLURL,
			)
		case gohook.PushEventType:
			payload, ok := et.Event.(*gohook.PushEvent)
			if !ok {
				panic("Malformed *PushEvent.")
			}
			if payload.HeadCommit.Message == "" {
				return
			}
			if len(payload.Commits) > 1 {
				msg = fmt.Sprintf(":repeat: [ %s | Branch: %s ] %v Commits: %s (%s)",
					payload.Repository.Name,
					payload.Ref[11:], // this discards "refs/heads/"
					len(payload.Commits),
					payload.HeadCommit.Message,
					payload.Compare,
				)
			} else {
				msg = fmt.Sprintf(":repeat: [ %s | Branch: %s ] Commit: %s (%s)",
					payload.Repository.Name,
					payload.Ref[11:], // this discards "refs/heads/"
					payload.HeadCommit.Message,
					payload.HeadCommit.URL,
				)
			}
			msgID, err := r.SendText("", msg)
			if err != nil {
				continue
			}
			c.commitToMsgID[payload.HeadCommit.ID] = msgID
			continue
		}
		returnChan <- msg
	}
}