2019-05-23 12:20:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.fromouter.space/hamcha/tg"
|
|
|
|
"github.com/mattn/go-mastodon"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mastodonConfig struct {
|
|
|
|
Server string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
Email string
|
|
|
|
Password string
|
|
|
|
Chats []int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var mastodonClient *mastodon.Client
|
|
|
|
var mastodonChats map[int64]bool
|
|
|
|
|
|
|
|
func mastodon_init() {
|
|
|
|
file, err := os.Open(*mastodonconf)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
var cfg mastodonConfig
|
|
|
|
err = json.NewDecoder(file).Decode(&cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("[mastodon] ERR: Could not load mastodon config file (malformed or unreadable file): " + err.Error())
|
|
|
|
}
|
|
|
|
mastodonClient = mastodon.NewClient(&mastodon.Config{
|
|
|
|
Server: cfg.Server,
|
|
|
|
ClientID: cfg.ClientID,
|
|
|
|
ClientSecret: cfg.ClientSecret,
|
|
|
|
})
|
|
|
|
err = mastodonClient.Authenticate(context.Background(), cfg.Email, cfg.Password)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("[mastodon] ERR: Error authenticating: " + err.Error())
|
|
|
|
}
|
|
|
|
mastodonChats = make(map[int64]bool)
|
|
|
|
for _, chatid := range cfg.Chats {
|
|
|
|
mastodonChats[chatid] = true
|
|
|
|
}
|
|
|
|
log.Println("[mastodon] Loaded and ready!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func mastodon_message(broker *tg.Broker, update tg.APIMessage) {
|
|
|
|
if isCommand(update, "post") {
|
|
|
|
// Make sure only whitelisted chats can post
|
|
|
|
if _, ok := mastodonChats[update.Chat.ChatID]; !ok {
|
|
|
|
broker.SendTextMessage(update.Chat, "Mi spiace, non puoi postare stati da qui!", &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make replies work
|
|
|
|
if update.ReplyTo != nil {
|
|
|
|
update.User = update.ReplyTo.User
|
2019-05-23 12:31:28 +00:00
|
|
|
update.Text = update.ReplyTo.Text
|
2019-05-23 12:20:09 +00:00
|
|
|
if update.Text != nil && update.ReplyTo.Photo != nil {
|
|
|
|
update.Photo = update.ReplyTo.Photo
|
|
|
|
update.Caption = update.Text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for media
|
|
|
|
if update.Photo != nil && update.Caption != nil {
|
|
|
|
maxsz := 0
|
|
|
|
photo := tg.APIPhotoSize{}
|
|
|
|
for _, curphoto := range update.Photo {
|
|
|
|
if curphoto.Width > maxsz {
|
|
|
|
maxsz = curphoto.Width
|
|
|
|
photo = curphoto
|
|
|
|
}
|
|
|
|
}
|
|
|
|
go broker.GetFile(photo.FileID, func(broker *tg.Broker, data tg.BrokerUpdate) {
|
|
|
|
if data.Type == tg.BError {
|
|
|
|
log.Printf("[mastodon] Received error from broker: %s\n", *data.Error)
|
|
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pbytes, err := base64.StdEncoding.DecodeString(*data.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[mastodon] Base64 decode error: %s\n", err.Error())
|
|
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
imgreader := bytes.NewReader(pbytes)
|
|
|
|
broker.SendChatAction(update.Chat, tg.ActionUploadingPhoto)
|
|
|
|
attach, err := mastodonClient.UploadMediaFromReader(context.Background(), imgreader)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[mastodon] Upload failed: %s\n", err.Error())
|
|
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-23 14:31:15 +00:00
|
|
|
text := ""
|
|
|
|
if update.Caption != nil {
|
|
|
|
text = *(update.Caption)
|
|
|
|
}
|
|
|
|
status, err := sendToot(text, attach.ID)
|
2019-05-23 12:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[mastodon] Send toot failed: %s\n", err.Error())
|
|
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
broker.SendTextMessage(update.Chat, fmt.Sprintf("<a href=\"%s\">Fatto!</a>", status.URL), &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
2019-05-23 12:20:09 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-23 14:31:15 +00:00
|
|
|
status, err := sendToot(*(update.Text), "")
|
2019-05-23 12:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("[mastodon] Send toot failed: %s\n", err.Error())
|
|
|
|
broker.SendTextMessage(update.Chat, "<b>ERRORE!</b> @hamcha controlla la console!", &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
broker.SendTextMessage(update.Chat, fmt.Sprintf("<a href=\"%s\">Fatto!</a>", status.URL), &tg.MessageOptions{
|
|
|
|
ReplyID: &update.MessageID,
|
|
|
|
})
|
2019-05-23 12:20:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:31:28 +00:00
|
|
|
func sendToot(status string, media mastodon.ID) (*mastodon.Status, error) {
|
2019-05-23 12:20:09 +00:00
|
|
|
var medialist []mastodon.ID
|
|
|
|
if media != "" {
|
|
|
|
medialist = []mastodon.ID{media}
|
|
|
|
}
|
2019-05-23 12:31:28 +00:00
|
|
|
return mastodonClient.PostStatus(context.Background(), &mastodon.Toot{
|
2019-05-23 12:20:09 +00:00
|
|
|
Status: status,
|
|
|
|
MediaIDs: medialist,
|
|
|
|
Sensitive: false,
|
|
|
|
Visibility: "public",
|
|
|
|
})
|
|
|
|
}
|