parent
567a37868d
commit
3940cb5953
@ -0,0 +1,99 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"github.com/PuerkitoBio/goquery" |
||||
"github.com/ddliu/go-httpclient" |
||||
"io" |
||||
"net/http" |
||||
"os" |
||||
"strconv" |
||||
"strings" |
||||
) |
||||
|
||||
type FAPlugin struct { |
||||
} |
||||
|
||||
func (plugin *FAPlugin) GetName() string { |
||||
return "FA Mirrorer" |
||||
} |
||||
|
||||
func (plugin *FAPlugin) GetCommands() []string { |
||||
return []string{"/fa"} |
||||
} |
||||
|
||||
func (plugin *FAPlugin) GetHelpText() []string { |
||||
return []string{"/fa [link] - mirrors an image from FurAffinity"} |
||||
} |
||||
|
||||
func (plugin *FAPlugin) Setup() { |
||||
a, ok := config.Plugins["fa_a"] |
||||
if !ok { |
||||
fmt.Print("FurAffinity Cookie a: ") |
||||
fmt.Scanln(&a) |
||||
|
||||
config.Plugins["fa_a"] = a |
||||
} |
||||
|
||||
b, ok := config.Plugins["fa_b"] |
||||
if !ok { |
||||
fmt.Print("FurAffinity Cookie b: ") |
||||
fmt.Scanln(&b) |
||||
|
||||
config.Plugins["fa_b"] = b |
||||
} |
||||
} |
||||
|
||||
func (plugin *FAPlugin) GotCommand(command string, message Message, args []string) { |
||||
if len(args) == 0 { |
||||
bot.sendMessage(NewMessage(message.Chat.Id, "You need to include a link!")) |
||||
|
||||
return |
||||
} |
||||
|
||||
bot.sendChatAction(NewChatAction(message.Chat.Id, CHAT_UPLOAD_PHOTO)) |
||||
|
||||
_, err := strconv.Atoi(args[0]) |
||||
if err == nil { |
||||
args[0] = "http://www.furaffinity.net/view/" + args[0] |
||||
} |
||||
|
||||
resp, err := httpclient.WithCookie(&http.Cookie{ |
||||
Name: "b", |
||||
Value: config.Plugins["fa_b"], |
||||
}).WithCookie(&http.Cookie{ |
||||
Name: "a", |
||||
Value: config.Plugins["fa_a"], |
||||
}).Get(args[0], nil) |
||||
if err != nil { |
||||
bot.sendMessage(NewMessage(message.Chat.Id, "ERR : "+err.Error())) |
||||
} |
||||
|
||||
defer resp.Body.Close() |
||||
|
||||
doc, err := goquery.NewDocumentFromReader(resp.Body) |
||||
if err != nil { |
||||
bot.sendMessage(NewMessage(message.Chat.Id, "ERR : "+err.Error())) |
||||
} |
||||
|
||||
sel := doc.Find("#submissionImg") |
||||
for i := range sel.Nodes { |
||||
single := sel.Eq(i) |
||||
|
||||
val, _ := single.Attr("src") |
||||
|
||||
tokens := strings.Split(val, "/") |
||||
fileName := tokens[len(tokens)-1] |
||||
|
||||
output, _ := os.Create(fileName) |
||||
defer output.Close() |
||||
defer os.Remove(output.Name()) |
||||
|
||||
resp, _ := http.Get("http:" + val) |
||||
defer resp.Body.Close() |
||||
|
||||
io.Copy(output, resp.Body) |
||||
|
||||
bot.sendPhoto(NewPhotoUpload(message.Chat.Id, output.Name())) |
||||
} |
||||
} |
@ -0,0 +1,153 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"log" |
||||
"strings" |
||||
) |
||||
|
||||
type ManagePlugin struct { |
||||
} |
||||
|
||||
func (plugin *ManagePlugin) GetName() string { |
||||
return "Plugin manager" |
||||
} |
||||
|
||||
func (plugin *ManagePlugin) GetCommands() []string { |
||||
return []string{ |
||||
"/enable", |
||||
"Enable", |
||||
"/disable", |
||||
"Disable", |
||||
"/reload", |
||||
} |
||||
} |
||||
|
||||
func (plugin *ManagePlugin) GetHelpText() []string { |
||||
return []string{ |
||||
"/enable [name] - enables a plugin", |
||||
"/disable [name] - disables a plugin", |
||||
"/reload - reloads bot configuration", |
||||
} |
||||
} |
||||
|
||||
func (plugin *ManagePlugin) Setup() { |
||||
} |
||||
|
||||
func (plugin *ManagePlugin) GotCommand(command string, message Message, args []string) { |
||||
log.Println(command) |
||||
|
||||
if command == "/enable" { |
||||
keyboard := [][]string{} |
||||
|
||||
hasDisabled := false |
||||
for _, plug := range plugins { |
||||
enabled, _ := config.EnabledPlugins[plug.GetName()] |
||||
if enabled { |
||||
continue |
||||
} |
||||
|
||||
hasDisabled = true |
||||
keyboard = append(keyboard, []string{"Enable " + plug.GetName()}) |
||||
} |
||||
|
||||
if !hasDisabled { |
||||
msg := NewMessage(message.Chat.Id, "All plugins are enabled!") |
||||
msg.ReplyToMessageId = message.MessageId |
||||
|
||||
bot.sendMessage(msg) |
||||
|
||||
return |
||||
} |
||||
|
||||
msg := NewMessage(message.Chat.Id, "Please specify which plugin to enable") |
||||
msg.ReplyToMessageId = message.MessageId |
||||
msg.ReplyMarkup = ReplyKeyboardMarkup{ |
||||
Keyboard: keyboard, |
||||
OneTimeKeyboard: true, |
||||
Selective: true, |
||||
ResizeKeyboard: true, |
||||
} |
||||
|
||||
bot.sendMessage(msg) |
||||
} else if command == "Enable" { |
||||
pluginName := strings.SplitN(message.Text, " ", 2) |
||||
|
||||
msg := NewMessage(message.Chat.Id, "") |
||||
msg.ReplyToMessageId = message.MessageId |
||||
msg.ReplyMarkup = ReplyKeyboardHide{ |
||||
HideKeyboard: true, |
||||
Selective: true, |
||||
} |
||||
|
||||
_, ok := config.EnabledPlugins[pluginName[1]] |
||||
if !ok { |
||||
msg.Text = "Unknown plugin!" |
||||
msg.ReplyToMessageId = message.MessageId |
||||
bot.sendMessage(msg) |
||||
|
||||
return |
||||
} |
||||
|
||||
config.EnabledPlugins[pluginName[1]] = true |
||||
msg.Text = fmt.Sprintf("Enabled '%s'!", pluginName[1]) |
||||
bot.sendMessage(msg) |
||||
} else if command == "/disable" { |
||||
keyboard := [][]string{} |
||||
|
||||
hasEnabled := false |
||||
for _, plug := range plugins { |
||||
enabled, _ := config.EnabledPlugins[plug.GetName()] |
||||
if !enabled { |
||||
continue |
||||
} |
||||
|
||||
hasEnabled = true |
||||
keyboard = append(keyboard, []string{"Disable " + plug.GetName()}) |
||||
} |
||||
|
||||
if !hasEnabled { |
||||
msg := NewMessage(message.Chat.Id, "All plugins are disabled!") |
||||
msg.ReplyToMessageId = message.MessageId |
||||
|
||||
bot.sendMessage(msg) |
||||
|
||||
return |
||||
} |
||||
|
||||
msg := NewMessage(message.Chat.Id, "Please specify which plugin to disable") |
||||
msg.ReplyToMessageId = message.MessageId |
||||
msg.ReplyMarkup = ReplyKeyboardMarkup{ |
||||
Keyboard: keyboard, |
||||
OneTimeKeyboard: true, |
||||
Selective: true, |
||||
ResizeKeyboard: true, |
||||
} |
||||
|
||||
bot.sendMessage(msg) |
||||
} else if command == "Disable" { |
||||
pluginName := strings.SplitN(message.Text, " ", 2) |
||||
|
||||
msg := NewMessage(message.Chat.Id, "") |
||||
msg.ReplyToMessageId = message.MessageId |
||||
msg.ReplyMarkup = ReplyKeyboardHide{ |
||||
HideKeyboard: true, |
||||
Selective: true, |
||||
} |
||||
|
||||
_, ok := config.EnabledPlugins[pluginName[1]] |
||||
if !ok { |
||||
msg.Text = "Unknown plugin!" |
||||
msg.ReplyToMessageId = message.MessageId |
||||
bot.sendMessage(msg) |
||||
|
||||
return |
||||
} |
||||
|
||||
config.EnabledPlugins[pluginName[1]] = false |
||||
msg.Text = fmt.Sprintf("Disabled '%s'!", pluginName[1]) |
||||
bot.sendMessage(msg) |
||||
} |
||||
|
||||
saveConfig() |
||||
} |
Reference in new issue