Compare commits

...

5 Commits
1.1 ... main

5 changed files with 321 additions and 33 deletions

View File

@ -1,4 +1,4 @@
VERSION=1.1
VERSION=1.2
SOURCES=$(shell find . -name "*.go" -type f)
all: amd64 arm7 dockerimage

18
main.go
View File

@ -23,12 +23,18 @@ import (
)
type Blog struct {
Id string `json:"id"`
Content string `json:"content"`
Date string `json:"created_at"`
Author Account `json:"account"`
Tags []Tag `json:"tags"`
Mentions []Mention `json:"mentions"`
Id string `json:"id"`
Content string `json:"content"`
Date string `json:"created_at"`
Author Account `json:"account"`
Tags []Tag `json:"tags"`
Mentions []Mention `json:"mentions"`
Reblog *Blog `json:"reblog"`
Medias []Media `json:"media_attachments"`
}
type Media struct {
Url string `json:"url"`
}
type Config struct {

View File

@ -21,10 +21,45 @@ import (
"net/http"
"log"
"encoding/json"
"io/ioutil"
"io"
"fmt"
)
func getBlogAndReblog(baseURL, account string) ([]Blog, error) {
if baseURL == "" || account == "" {
log.Println("baseURL or account is empty")
return nil, fmt.Errorf("BaseURL or account is empty")
}
resp, err := http.Get(baseURL + "/api/v1/accounts/" + account + "/statuses?exclude_reblogs=false&exclude_replies=true")
if err != nil {
log.Println("Mastodon API request: %s", err)
return nil, fmt.Errorf("Failed to request Mastodon API")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Println("Mastodon API response: %s", resp.Status)
return nil, fmt.Errorf("Mastodon instance failed")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return nil, fmt.Errorf("Failed to read Mastodon response body")
}
var blogs []Blog
err = json.Unmarshal(body, &blogs)
if err != nil {
log.Println("Mastodon response %s", err)
return nil, fmt.Errorf("Failed to parse Mastodon response")
}
return blogs, nil
}
func getBlog(baseURL, account string) ([]Blog, error) {
if baseURL == "" || account == "" {
log.Println("baseURL or account is empty")
@ -44,7 +79,7 @@ func getBlog(baseURL, account string) ([]Blog, error) {
return nil, fmt.Errorf("Mastodon instance failed")
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return nil, fmt.Errorf("Failed to read Mastodon response body")
@ -60,6 +95,42 @@ func getBlog(baseURL, account string) ([]Blog, error) {
return blogs, nil
}
func getTimeline(baseURL string) ([]Blog, error) {
var toots []Blog
if baseURL == "" {
log.Println("baseURL is empty")
return toots, fmt.Errorf("baseURL is empty")
}
resp, err := http.Get(baseURL + "/api/v1/timelines/public")
if err != nil {
log.Println("Mastodon API request: %s", err)
return toots, fmt.Errorf("API request failed")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Println("Mastodon API response: %s", resp.Status)
return toots, fmt.Errorf("API response is not 200")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return toots, fmt.Errorf("Failed to read response")
}
err = json.Unmarshal(body, &toots)
if err != nil {
log.Println("Mastodon response %s", err)
return toots, fmt.Errorf("Failed to parse response")
}
return toots, nil
}
func getAccount(baseURL, accountId string) (Account, error) {
if baseURL == "" || accountId == "" {
log.Println("baseURL or account is empty")
@ -79,7 +150,7 @@ func getAccount(baseURL, accountId string) (Account, error) {
return Account{}, fmt.Errorf("API response is not 200")
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return Account{}, fmt.Errorf("Failed to read response")
@ -114,7 +185,7 @@ func getToot(baseURL, tootId string) (Blog, error) {
return Blog{}, fmt.Errorf("API response is not 200")
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return Blog{}, fmt.Errorf("Failed to read response")
@ -130,6 +201,34 @@ func getToot(baseURL, tootId string) (Blog, error) {
return toot, nil
}
func getMedia(mediaURL string) (string, []byte, error) {
if mediaURL == "" {
log.Println("mediaURL is empty")
return "", nil, fmt.Errorf("mediaURL is empty")
}
resp, err := http.Get(mediaURL)
if err != nil {
log.Println("Mastodon API request: %s", err)
return "", nil, fmt.Errorf("API request failed")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Println("Mastodon API response: %s", resp.Status)
return "", nil, fmt.Errorf("API response is not 200")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return "", nil, fmt.Errorf("Failed to read response")
}
return resp.Header["Content-Type"][0], body , nil
}
func getThread(baseURL, tootId string) (Thread, error) {
if baseURL == "" || tootId == "" {
log.Println("baseURL or tootID is empty")
@ -149,7 +248,7 @@ func getThread(baseURL, tootId string) (Thread, error) {
return Thread{}, fmt.Errorf("API response is not 200")
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return Thread{}, fmt.Errorf("Failed to read response")
@ -184,7 +283,7 @@ func getTag(baseURL, tag string) ([]Blog, error) {
return nil, fmt.Errorf("API response is not 200")
}
body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Mastodon response body: %s", err)
return nil, fmt.Errorf("Failed to read response")

191
server.go
View File

@ -115,7 +115,7 @@ func handleConn(conn *tls.Conn, baseURL, title, home_message string, rateLimit i
if path == "" || path == "/" {
log.Println("Received request for home page")
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# %s\n\n%s\n\n=> /tag Search for a tag\n=> /about About MastoGem", title, home_message)
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# %s\n\n%s\n\n=> /timeline View public timeline\n=> /tag Search for a tag\n=> /about About MastoGem", title, home_message)
if err != nil {
log.Println("send error: %s", err)
return
@ -125,22 +125,39 @@ func handleConn(conn *tls.Conn, baseURL, title, home_message string, rateLimit i
// profile
if strings.HasPrefix(path, "/profile/") {
// skip prefix
path = path[9:]
_, err = strconv.ParseUint(path, 10, 64)
if err != nil {
log.Println("invalid request: %s", err)
_, err = fmt.Fprintf(conn, "59 Can't parse request\r\n")
if strings.HasSuffix(path, "/reblog") {
// skip prefix and suffix
path = path[9:len(path)-len("/reblog")]
_, err = strconv.ParseUint(path, 10, 64)
if err != nil {
log.Println("send error: %s", err)
log.Println("invalid request: %s", err)
_, err = fmt.Fprintf(conn, "59 Can't parse request\r\n")
if err != nil {
log.Println("send error: %s", err)
return
}
return
}
return
log.Println("Received request for account with reblog " + path)
printProfileWithReblog(conn, baseURL, path)
} else {
// skip prefix
path = path[9:]
_, err = strconv.ParseUint(path, 10, 64)
if err != nil {
log.Println("invalid request: %s", err)
_, err = fmt.Fprintf(conn, "59 Can't parse request\r\n")
if err != nil {
log.Println("send error: %s", err)
return
}
return
}
log.Println("Received request for account " + path)
printProfile(conn, baseURL, path)
}
log.Println("Received request for account " + path)
printProfile(conn, baseURL, path)
} /* thread */ else if strings.HasPrefix(path, "/thread/") {
// skip prefix
path = path[8:]
@ -213,6 +230,23 @@ This capsule use %s Mastodon instance.
log.Println("Received request for toot " + path)
printToot(conn, baseURL, path)
} /* timeline */ else if strings.HasPrefix(path, "/timeline") {
log.Println("Received request for timeline")
printTimeline(conn, baseURL)
} /* media */ else if strings.HasPrefix(path, "/media") {
if query == "" {
_, err = fmt.Fprintf(conn, "59 Invalid request\r\n")
if err != nil {
log.Println("send: %s", err)
return
}
return
}
log.Println("Received request for media " + query)
proxyMedia(conn, baseURL, query)
} else {
_, err = fmt.Fprintf(conn, "59 Invalid request\r\n")
if err != nil {
@ -222,6 +256,49 @@ This capsule use %s Mastodon instance.
}
}
func proxyMedia(conn *tls.Conn, baseURL, query string) {
mediaURL, err := url.QueryUnescape(query)
if err != nil {
_, err = fmt.Fprintf(conn, "59 Invalid url encoded media\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
if !strings.HasPrefix(mediaURL, baseURL) {
_, err = fmt.Fprintf(conn, "59 Invalid media url\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
mime, media, err := getMedia(mediaURL)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
_, err = fmt.Fprintf(conn, "20 %s\r\n", mime)
if err != nil {
log.Println("handleConn: %s", err)
return
}
_, err = conn.Write(media)
if err != nil {
log.Println("handleConn: %s", err)
return
}
}
func printProfile(conn *tls.Conn, baseURL, profileID string) {
account, err := getAccount(baseURL, profileID)
if err != nil {
@ -257,13 +334,83 @@ func printProfile(conn *tls.Conn, baseURL, profileID string) {
}
}
_, err = fmt.Fprintf(conn, "\n=> %s Go to %s account", account.Url, account.Name)
_, err = fmt.Fprintf(conn, "\n=> /profile/%s/reblog This profile with reblog\n=> %s Go to %s account", account.Id, account.Url, account.Name)
if err != nil {
log.Println("add link: %s", err)
return
}
}
func printProfileWithReblog(conn *tls.Conn, baseURL, profileID string) {
account, err := getAccount(baseURL, profileID)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
blogs, err := getBlogAndReblog(baseURL, profileID)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n# Toots for %s account\n", account.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
}
for _, blog := range blogs {
_, err = fmt.Fprintf(conn, "\n%s\n=> /thread/%s View the thread\n", formatBlog(blog), blog.Id)
if err != nil {
log.Println("read blogs: %s", err)
return
}
}
_, err = fmt.Fprintf(conn, "\n=> /profile/%s This profile without reblog\n=> %s Go to %s account", account.Id, account.Url, account.Name)
if err != nil {
log.Println("add link: %s", err)
return
}
}
func printTimeline(conn *tls.Conn, baseURL string) {
toots, err := getTimeline(baseURL)
if err != nil {
_, err = fmt.Fprintf(conn, "40 Remote mastodon instance failed\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
return
}
// Print header
_, err = fmt.Fprintf(conn, "20 text/gemini\r\n")
if err != nil {
log.Println("handleConn: %s", err)
return
}
// Print toots
for _, toot := range toots {
_, err = fmt.Fprintf(conn, "\n%s\n=> /thread/%s View the thread\n", formatBlog(toot), toot.Id)
if err != nil {
log.Println("read timeline: %s", err)
return
}
}
}
func printToot(conn *tls.Conn, baseURL, tootID string) {
toot, err := getToot(baseURL, tootID)
if err != nil {
@ -283,10 +430,18 @@ func printToot(conn *tls.Conn, baseURL, tootID string) {
}
// Print toot
_, err = fmt.Fprintf(conn, "# Toot\n\n%s\n=> /thread/%s View the thread\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Id, toot.Author.Id, toot.Author.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
if toot.Reblog == nil {
_, err = fmt.Fprintf(conn, "# Toot\n\n%s\n=> /thread/%s View the thread\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Id, toot.Author.Id, toot.Author.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
}
} else {
_, err = fmt.Fprintf(conn, "# Toot\n\n%s\n=> /toot/%s Original toot\n=> /thread/%s View the thread\n=> /profile/%s More toots from %s\n=> /profile/%s More toots from %s\n", formatBlog(toot), toot.Reblog.Id, toot.Id, toot.Author.Id, toot.Author.Name, toot.Reblog.Author.Id, toot.Reblog.Author.Name)
if err != nil {
log.Println("handleConn: %s", err)
return
}
}
// print mentions

32
util.go
View File

@ -26,6 +26,7 @@ import (
"encoding/json"
"log"
"time"
"net/url"
)
func getConfig() Config {
@ -88,7 +89,12 @@ func removeHTMLTags(content string) string {
}
func formatBlog(toot Blog) string {
content := toot.Content
var content string
if toot.Reblog == nil {
content = toot.Content
} else {
content = toot.Reblog.Content
}
content = removeHTMLTags(content)
content = strings.Trim(content, " \n\r")
content = strings.ReplaceAll(content, "\n#", "\n[#]")
@ -104,7 +110,29 @@ func formatBlog(toot Blog) string {
author = toot.Author.DisplayName
}
return "### Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + "\n" + content + "\n=> /toot/" + toot.Id + " More informations about this toot"
var header string
if toot.Reblog == nil {
header = "### Written by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16]
} else {
var originalAuthor string
if toot.Reblog.Author.DisplayName == "" {
originalAuthor = toot.Reblog.Author.Name
} else {
originalAuthor = toot.Reblog.Author.DisplayName
}
header = "### Shared by " + author + " on " + toot.Date[0:10] + " at " + toot.Date[11:16] + " (original by " + originalAuthor + ")"
}
footer := "\n"
for _, media := range toot.Medias {
mediaURL := url.QueryEscape(media.Url)
footer += "=> /media?" + mediaURL + " View attached media\n"
}
footer += "\n=> /toot/" + toot.Id + " More informations about this toot"
return header + "\n" + content + "\n" + footer
}
func rateIsOk(tab map[string]Rate, remoteIP string, limit int) bool {