Exemple #1
0
func (m *Module) Load(client *irc.Client) error {
	if len(m.URL) <= 0 {
		return nil
	}

	duration, err := time.ParseDuration(m.Interval)
	if err != nil {
		return err
	}

	if err := m.updateHandler(client); err != nil {
		return err
	}

	if m.api.API != apiVersion {
		return errors.New("unsupported spaceapi version")
	}

	go func(c *irc.Client) {
		for {
			time.Sleep(duration)
			m.updateHandler(c)
		}
	}(client)

	client.CmdHook("privmsg", m.statusCmd)
	return nil
}
Exemple #2
0
func (m *ModuleSet) helpCmd(client *irc.Client, msg irc.Message) error {
	if msg.Data != "!help" {
		return nil
	}

	return client.Write("NOTICE %s :%s", msg.Receiver,
		"Use !help MODULE to see the help message for the given module, use !modules to list all modules.")
}
Exemple #3
0
func (m *Module) notify(client *irc.Client, post post) {
	ftitle := html.UnescapeString(post.Feed.Title)
	for _, ch := range client.Channels {
		ititle := html.UnescapeString(post.Item.Title)
		client.Write("NOTICE %s :%s -- %s new entry %s: %s",
			ch, strings.ToUpper(m.Name()), ftitle, ititle, post.Item.Link)
	}
}
Exemple #4
0
func (m *Module) tweetCmd(client *irc.Client, msg irc.Message) error {
	splited := strings.Fields(msg.Data)
	if len(splited) < 2 || splited[0] != "!tweet" || !client.Connected(msg.Receiver) {
		return nil
	}

	status := strings.Join(splited[1:], " ")
	return m.tweet(status, url.Values{}, client, msg)
}
Exemple #5
0
func (m *Module) timeCmd(client *irc.Client, msg irc.Message) error {
	if msg.Data != "!time" {
		return nil
	}

	now := time.Now().UTC()
	return client.Write("NOTICE %s :%s",
		msg.Receiver, now.Format(m.Format))
}
Exemple #6
0
func (m *Module) Load(client *irc.Client) error {
	regex, err := regexp.Compile(m.RegexStr)
	if err != nil {
		return err
	}

	m.regex = regex
	client.CmdHook("privmsg", m.urlCmd)

	return nil
}
Exemple #7
0
func (m *Module) tweet(t string, v url.Values, c *irc.Client, p irc.Message) error {
	_, err := m.api.PostTweet(t, v)
	if err != nil && len(t) > maxChars {
		return c.Write("NOTICE %s :ERROR: Tweet is too long, remove %d characters",
			p.Receiver, len(t)-maxChars)
	} else if err != nil {
		return c.Write("NOTICE %s :ERROR: %s", p.Receiver, err.Error())
	} else {
		return nil
	}
}
Exemple #8
0
func (m *Module) notify(client *irc.Client, open bool) {
	var oldState, newState string
	if open {
		oldState = "closed"
		newState = "open"
	} else {
		oldState = "open"
		newState = "closed"
	}

	for _, ch := range client.Channels {
		client.Write("NOTICE %s :%s changed door status from %s to %s",
			ch, m.api.Space, oldState, newState)
	}
}
Exemple #9
0
func (m *ModuleSet) modulesCmd(client *irc.Client, msg irc.Message) error {
	if msg.Data != "!modules" || len(m.modules) <= 0 {
		return nil
	}

	var names []string
	for _, module := range m.modules {
		names = append(names, module.Name())
	}

	help := fmt.Sprintf("The following modules are available: %s",
		strings.Join(names, ", "))

	return client.Write("NOTICE %s :%s", msg.Receiver, help)
}
Exemple #10
0
func (m *Module) directMsgCmd(client *irc.Client, msg irc.Message) error {
	splited := strings.Fields(msg.Data)
	if len(splited) < 3 || splited[0] != "!directmsg" || !client.Connected(msg.Receiver) {
		return nil
	}

	scname := splited[1]
	status := strings.Join(splited[2:], " ")

	if _, err := m.api.PostDMToScreenName(status, scname); err != nil {
		return client.Write("NOTICE %s :ERROR: %s",
			msg.Receiver, err.Error())
	}

	return nil
}
Exemple #11
0
func (m *ModuleSet) moduleCmd(client *irc.Client, msg irc.Message) error {
	splited := strings.Fields(msg.Data)
	if len(splited) < 2 || splited[0] != "!help" {
		return nil
	}

	name := strings.ToLower(splited[1])
	module := m.findModule(name)
	if module == nil {
		return client.Write("NOTICE %s :Module %q isn't installed",
			msg.Receiver, name)
	}

	return client.Write("NOTICE %s :%s: %s",
		msg.Receiver, module.Name(), module.Help())
}
Exemple #12
0
func (m *Module) Load(client *irc.Client) error {
	if len(m.Password) <= 0 {
		return nil
	}

	client.CmdHook("notice", func(c *irc.Client, msg irc.Message) error {
		if msg.Sender.Name != m.NickServ || !strings.Contains(msg.Data, m.Keyword) {
			return nil
		}

		return c.Write("PRIVMSG %s :identify %s",
			m.NickServ, m.Password)
	})

	return nil
}
Exemple #13
0
func (m *Module) replyCmd(client *irc.Client, msg irc.Message) error {
	splited := strings.Fields(msg.Data)
	if len(splited) < 3 || splited[0] != "!reply" || !client.Connected(msg.Receiver) {
		return nil
	}

	status := strings.Join(splited[2:], " ")
	if !strings.Contains(status, "@") {
		return client.Write("NOTICE %s :ERROR: %s",
			msg.Receiver, "A reply must contain an @mention")
	}

	values := url.Values{}
	values.Add("in_reply_to_status_id", splited[1])

	return m.tweet(status, values, client, msg)
}
Exemple #14
0
func (m *Module) Load(client *irc.Client) error {
	duration, err := time.ParseDuration(m.Timeout)
	if err != nil {
		return err
	}

	client.CmdHook("kick", func(c *irc.Client, msg irc.Message) error {
		if msg.Data != client.Nickname {
			return nil
		}

		time.Sleep(duration)
		return c.Write("JOIN %s", strings.Fields(msg.Receiver)[0])
	})

	return nil
}
Exemple #15
0
func (m *Module) favoriteCmd(client *irc.Client, msg irc.Message) error {
	splited := strings.Fields(msg.Data)
	if len(splited) < 2 || splited[0] != "!favorite" || !client.Connected(msg.Receiver) {
		return nil
	}

	id, err := strconv.Atoi(splited[1])
	if err != nil {
		return err
	}

	if _, err := m.api.Favorite(int64(id)); err != nil {
		return client.Write("NOTICE %s :ERROR: %s",
			msg.Receiver, err.Error())
	}

	return nil
}
Exemple #16
0
func (m *Module) statusCmd(client *irc.Client, msg irc.Message) error {
	if msg.Data != "!spacestatus" {
		return nil
	} else if m.api == nil {
		return client.Write("NOTICE %s :Status currently unknown.",
			msg.Receiver)
	}

	var state string
	if m.api.State.Open {
		state = "open"
	} else {
		state = "closed"
	}

	return client.Write("NOTICE %s :%s is currently %s",
		msg.Receiver, m.api.Space, state)
}
Exemple #17
0
func (m *Module) urlCmd(client *irc.Client, msg irc.Message) error {
	url := m.regex.FindString(msg.Data)
	if len(url) <= 0 {
		return nil
	}

	resp, err := http.Head(url)
	if err != nil {
		return err
	}
	resp.Body.Close() // HEAD response doesn't have a body

	info := m.infoString(resp)
	if len(info) <= 0 {
		return nil
	}

	return client.Write("NOTICE %s :%s", msg.Receiver, info)
}
Exemple #18
0
func (m *Module) statCmd(client *irc.Client, msg irc.Message) error {
	splited := strings.Fields(msg.Data)
	if len(splited) < 2 || splited[0] != "!stat" || !client.Connected(msg.Receiver) {
		return nil
	}

	id, err := strconv.Atoi(splited[1])
	if err != nil {
		return err
	}

	tweet, err := m.api.GetTweet(int64(id), url.Values{})
	if err != nil {
		return err
	}

	return client.Write("NOTICE %s :Stats for tweet %d by %s: ↻ %d ★ %d",
		msg.Receiver, tweet.Id, tweet.User.ScreenName, tweet.RetweetCount, tweet.FavoriteCount)
}
Exemple #19
0
func (m *Module) Load(client *irc.Client) error {
	users := make(map[string]int)
	client.CmdHook("privmsg", func(c *irc.Client, msg irc.Message) error {
		splited := strings.Fields(msg.Data)
		if len(splited) < 3 || splited[0] != "!remind" {
			return nil
		}

		duration, err := time.ParseDuration(splited[1])
		if err != nil {
			return err
		}

		limit := time.Duration(m.TimeLimit) * time.Hour
		if duration > limit {
			return c.Write("NOTICE %s :%v hours exceeds the limit of %v hours",
				msg.Receiver, duration.Hours(), limit.Hours())
		}

		if users[msg.Sender.Host] >= m.UserLimit {
			return c.Write("NOTICE %s :You can only run %d reminders at a time",
				msg.Receiver, m.UserLimit)
		}

		users[msg.Sender.Host]++
		reminder := strings.Join(splited[2:], " ")
		time.AfterFunc(duration, func() {
			users[msg.Sender.Host]--
			c.Write("PRIVMSG %s :Reminder: %s",
				msg.Sender.Name, reminder)
		})

		return c.Write("NOTICE %s :Reminder setup for %s",
			msg.Receiver, duration.String())
	})

	return nil
}
Exemple #20
0
func (m *Module) Load(client *irc.Client) error {
	anaconda.SetConsumerKey(m.ConsumerKey)
	anaconda.SetConsumerSecret(m.ConsumerSecret)

	m.api = anaconda.NewTwitterApi(m.AccessToken, m.AccessTokenSecret)
	client.CmdHook("privmsg", m.statCmd)

	if !m.ReadOnly {
		client.CmdHook("privmsg", m.tweetCmd)
		client.CmdHook("privmsg", m.replyCmd)
		client.CmdHook("privmsg", m.retweetCmd)
		client.CmdHook("privmsg", m.favoriteCmd)
		client.CmdHook("privmsg", m.directMsgCmd)
	}

	values := url.Values{}
	values.Add("skip_status", "true")

	user, err := m.api.GetSelf(values)
	if err != nil {
		return err
	} else {
		m.user = user
	}

	values = url.Values{}
	values.Add("replies", "all")
	values.Add("with", "user")

	go func(c *irc.Client, v url.Values) {
		for {
			m.streamHandler(c, v)
		}
	}(client, values)

	return nil
}
Exemple #21
0
func (m *Module) Load(client *irc.Client) error {
	client.CmdHook("privmsg", m.timeCmd)
	return nil
}
Exemple #22
0
func (m *Module) notify(client *irc.Client, text string) {
	for _, ch := range client.Channels {
		client.Write("NOTICE %s :%s", ch, text)
	}
}