go-mastodon-matrix-webhooks/main.go

217 lines
4.2 KiB
Go
Raw Permalink Normal View History

2022-11-20 04:36:31 +00:00
package main
2022-11-20 04:44:58 +00:00
import (
2022-11-20 05:34:03 +00:00
"bytes"
2022-11-20 04:44:58 +00:00
"encoding/json"
2022-11-20 05:11:08 +00:00
"fmt"
2022-11-20 04:44:58 +00:00
"log"
"net/http"
"os"
2022-11-20 05:11:08 +00:00
"github.com/ip2location/ip2location-go/v9"
2022-11-20 04:44:58 +00:00
"github.com/joho/godotenv"
)
var MATRIX_WEBHOOK_URL string
var MATRIX_WEBHOOK_API_KEY string
2022-11-21 02:31:18 +00:00
var MATRIX_ACCOUNT_CHANNEL string
var MATRIX_REPORT_CHANNEL string
2022-11-20 04:44:58 +00:00
var PORT string
2022-11-20 05:23:37 +00:00
var IP2LOCATION_FILE string
2023-01-29 07:26:02 +00:00
var MASTODON_INSTANCE string
2022-11-20 04:44:58 +00:00
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
MATRIX_WEBHOOK_URL = os.Getenv("MATRIX_WEBHOOK_URL")
if MATRIX_WEBHOOK_URL == "" {
log.Fatal("MATRIX_WEBHOOK_URL empty or invalid")
}
MATRIX_WEBHOOK_API_KEY = os.Getenv("MATRIX_WEBHOOK_API_KEY")
if MATRIX_WEBHOOK_API_KEY == "" {
log.Fatal("MATRIX_WEBHOOK_API_KEY empty or invalid")
}
2022-11-21 02:31:18 +00:00
MATRIX_ACCOUNT_CHANNEL = os.Getenv("MATRIX_ACCOUNT_CHANNEL")
if MATRIX_ACCOUNT_CHANNEL == "" {
log.Fatal("MATRIX_ACCOUNT_CHANNEL empty or invalid")
}
MATRIX_REPORT_CHANNEL = os.Getenv("MATRIX_REPORT_CHANNEL")
if MATRIX_REPORT_CHANNEL == "" {
log.Fatal("MATRIX_REPORT_CHANNEL empty or invalid")
2022-11-20 04:44:58 +00:00
}
2023-01-29 07:26:02 +00:00
MASTODON_INSTANCE = os.Getenv("MASTODON_INSTANCE")
if MASTODON_INSTANCE == "" {
log.Fatal("MASTODON_INSTANCE is empty or invalid")
}
2022-11-20 04:44:58 +00:00
PORT = os.Getenv("PORT")
if PORT == "" {
log.Fatal("PORT empty or invalid")
}
2022-11-20 05:11:08 +00:00
2022-11-20 05:25:06 +00:00
IP2LOCATION_FILE = os.Getenv("IP2LOCATION_FILE")
2022-11-20 04:44:58 +00:00
}
2022-11-20 04:36:31 +00:00
func main() {
2022-11-20 04:44:58 +00:00
http.HandleFunc("/", handler)
if err := http.ListenAndServe(":"+PORT, nil); err != nil {
log.Fatal(err)
}
}
2022-11-20 05:11:08 +00:00
// Handle requests
2022-11-20 04:44:58 +00:00
func handler(w http.ResponseWriter, r *http.Request) {
if r.Body != nil {
2022-11-20 05:18:15 +00:00
var i MastodonEvent
2022-11-20 04:55:05 +00:00
err := json.NewDecoder(r.Body).Decode(&i)
2022-11-20 04:52:34 +00:00
if err != nil {
2022-11-20 04:56:08 +00:00
log.Println(err.Error())
2022-11-20 04:52:34 +00:00
return
}
if i.Event == "report.created" {
2023-01-29 07:14:02 +00:00
// Build message
msg := fmt.Sprintf(
"[New Report](%s): **%s** has reported **%s**: %s",
2022-11-20 06:09:08 +00:00
fmt.Sprintf(
2023-01-29 07:26:02 +00:00
"https://%s/admin/reports/%s",
MASTODON_INSTANCE,
2023-01-29 07:14:02 +00:00
i.Object.ID,
2022-11-20 06:09:08 +00:00
),
2023-01-29 07:14:02 +00:00
i.Object.Account.Username,
i.Object.TargetAccount.Username,
i.Object.Comment,
)
// Log to stdout
log.Println(msg)
// Send to matrix
err = sendWebhook(
msg,
2022-11-21 02:31:18 +00:00
MATRIX_REPORT_CHANNEL,
2022-11-20 06:09:08 +00:00
)
2022-11-20 05:12:08 +00:00
if err != nil {
log.Println(err.Error())
return
}
2022-11-20 04:52:34 +00:00
} else if i.Event == "account.created" {
2023-01-29 07:14:02 +00:00
// Build message
2022-11-20 05:18:15 +00:00
country := ipLookup(i.Object.IP)
2023-01-29 07:14:02 +00:00
msg := fmt.Sprintf(
"[New Signup](%s) %s: **%s** (%s). %s",
2022-11-20 05:33:06 +00:00
fmt.Sprintf(
2023-01-29 07:26:02 +00:00
"https://%s/admin/accounts/%s",
MASTODON_INSTANCE,
2023-01-29 07:14:02 +00:00
i.Object.ID,
2022-11-20 05:33:06 +00:00
),
2023-01-29 07:14:02 +00:00
country,
i.Object.Username,
i.Object.Email,
fmt.Sprintf(
"Notes: %s",
i.Object.Notes,
),
)
// Log to stdout
log.Println(msg)
// Send to matrix
err = sendWebhook(
msg,
2022-11-21 02:31:18 +00:00
MATRIX_ACCOUNT_CHANNEL,
2022-11-20 05:33:06 +00:00
)
2022-11-20 05:12:08 +00:00
if err != nil {
log.Println(err.Error())
return
}
2023-01-29 07:03:50 +00:00
} else if i.Event == "account.approved" {
2023-01-29 07:14:02 +00:00
msg := fmt.Sprintf(
2023-01-29 07:21:09 +00:00
"[Signup Approved](%s): %s",
2023-01-29 07:14:02 +00:00
fmt.Sprintf(
2023-01-29 07:26:02 +00:00
"https://%s/admin/accounts/%s",
MASTODON_INSTANCE,
2023-01-29 07:14:02 +00:00
i.Object.ID,
),
2023-01-29 07:17:09 +00:00
i.Object.Username,
2023-01-29 07:14:02 +00:00
)
// Log to stdout
log.Println(msg)
// Send to Matrix
err = sendWebhook(
msg,
MATRIX_ACCOUNT_CHANNEL,
)
if err != nil {
log.Println(err.Error())
return
}
} else {
log.Printf("Unknown event %s", i.Event)
2022-11-20 04:52:34 +00:00
}
2022-11-20 04:44:58 +00:00
}
2022-11-20 04:36:31 +00:00
}
2022-11-20 05:11:08 +00:00
// sendWebhook - takes msg, sends to matrix
2022-11-21 02:31:18 +00:00
func sendWebhook(msgText string, channel string) error {
2022-11-20 05:51:49 +00:00
// log.Println(msgText)
2022-11-20 05:34:03 +00:00
data := MatrixWebhook{
Key: MATRIX_WEBHOOK_API_KEY,
}
data.Body = msgText
b, err := json.Marshal(data)
if err != nil {
return err
}
2022-11-20 05:40:30 +00:00
2022-11-21 02:31:18 +00:00
// log.Printf("Sending %s to %s", b, MATRIX_WEBHOOK_URL+"/"+channel)
req, err := http.NewRequest("POST", MATRIX_WEBHOOK_URL+"/"+channel, bytes.NewBuffer(b))
2022-11-20 05:34:03 +00:00
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
2022-11-20 05:59:26 +00:00
log.Print(err)
return nil
2022-11-20 05:34:03 +00:00
}
2022-11-20 05:59:26 +00:00
2022-11-20 05:34:03 +00:00
defer resp.Body.Close()
2022-11-20 05:11:08 +00:00
return nil
}
// Lookup to country!
func ipLookup(ip string) string {
2022-11-20 05:29:46 +00:00
if IP2LOCATION_FILE == "" {
return ""
}
2022-11-20 05:26:24 +00:00
2022-11-20 05:23:37 +00:00
db, err := ip2location.OpenDB(IP2LOCATION_FILE)
2022-11-20 05:11:08 +00:00
if err != nil {
2022-11-20 05:26:24 +00:00
log.Print(err)
2022-11-20 05:11:08 +00:00
return ""
}
2022-11-20 05:23:37 +00:00
results, err := db.Get_all(ip)
2022-11-20 05:11:08 +00:00
if err != nil {
2022-11-20 05:26:24 +00:00
log.Print(err)
2022-11-20 05:11:08 +00:00
return ""
}
2022-11-20 05:51:49 +00:00
return " from " + results.Country_long
2022-11-20 05:11:08 +00:00
}