2019-01-19 00:48:12 +00:00
|
|
|
// © 2019 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
|
|
|
|
|
|
|
package nerdepedia
|
|
|
|
|
|
|
|
import (
|
2019-01-19 00:58:31 +00:00
|
|
|
"fmt"
|
2019-01-19 00:48:12 +00:00
|
|
|
"strings"
|
2019-01-19 00:58:31 +00:00
|
|
|
"html"
|
2019-01-19 00:48:12 +00:00
|
|
|
"net/http"
|
|
|
|
"bufio"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-01-19 00:58:31 +00:00
|
|
|
descriptionPrefix = "<meta name=\"description\" content=\""
|
|
|
|
linkPrefix = "<link rel=\"canonical\" href=\""
|
|
|
|
|
|
|
|
closingTagSuffix = "\" />"
|
2019-01-19 00:48:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type NerdepediaPlugin struct {
|
|
|
|
bot bot.Bot
|
|
|
|
config *config.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNerdepediaPlugin creates a new NerdepediaPlugin with the Plugin interface
|
|
|
|
func New(bot bot.Bot) *NerdepediaPlugin {
|
|
|
|
return &NerdepediaPlugin{
|
|
|
|
bot: bot,
|
|
|
|
config: bot.Config(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message responds to the bot hook on recieving messages.
|
|
|
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
|
|
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
|
|
|
func (p *NerdepediaPlugin) Message(message msg.Message) bool {
|
|
|
|
lowerCase := strings.ToLower(message.Body)
|
|
|
|
query := ""
|
|
|
|
if lowerCase == "may the force be with you" || lowerCase == "help me obi-wan" {
|
|
|
|
query = "http://starwars.wikia.com/wiki/Special:Random"
|
2019-01-19 00:58:31 +00:00
|
|
|
} else if lowerCase == "beam me up scotty" || lowerCase == "live long and prosper" {
|
2019-01-19 00:48:12 +00:00
|
|
|
query = "http://memory-alpha.wikia.com/wiki/Special:Random"
|
|
|
|
}
|
|
|
|
|
|
|
|
if query != "" {
|
|
|
|
resp, err := http.Get(query)
|
|
|
|
if err != nil {
|
2019-01-19 00:58:31 +00:00
|
|
|
return false
|
2019-01-19 00:48:12 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
2019-01-19 00:58:31 +00:00
|
|
|
link := ""
|
|
|
|
description := ""
|
2019-01-19 00:48:12 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2019-01-19 00:58:31 +00:00
|
|
|
if description == "" {
|
|
|
|
index := strings.Index(line, linkPrefix)
|
|
|
|
if index >= 0 {
|
|
|
|
description = html.UnescapeString(strings.TrimSuffix(strings.TrimPrefix(line, linkPrefix), closingTagSuffix))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if link == "" {
|
|
|
|
index := strings.Index(line, descriptionPrefix)
|
|
|
|
if index >= 0 {
|
|
|
|
link = strings.TrimSuffix(strings.TrimPrefix(line, descriptionPrefix), closingTagSuffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if description != "" && link != "" {
|
|
|
|
p.bot.SendMessage(message.Channel, fmt.Sprintf("%s (%s)", description, link))
|
2019-01-19 00:48:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
|
|
|
func (p *NerdepediaPlugin) Help(channel string, parts []string) {
|
|
|
|
p.bot.SendMessage(channel, "star wars/trek stuff")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty event handler because this plugin does not do anything on event recv
|
|
|
|
func (p *NerdepediaPlugin) Event(kind string, message msg.Message) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler for bot's own messages
|
|
|
|
func (p *NerdepediaPlugin) BotMessage(message msg.Message) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register any web URLs desired
|
|
|
|
func (p *NerdepediaPlugin) RegisterWeb() *string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NerdepediaPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }
|