2021-03-03 10:02:35 +01:00
/ *
MastoGem , A Mastodon proxy for Gemini
Copyright ( C ) 2021 Romain de Laage
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Affero General Public License for more details .
You should have received a copy of the GNU Affero General Public License
along with this program . If not , see < https : //www.gnu.org/licenses/>.
* /
2021-03-02 20:31:38 +01:00
package main
import (
"html"
"regexp"
"strings"
"os"
"io/ioutil"
"encoding/json"
"log"
2021-04-17 12:10:27 +02:00
"time"
2021-03-02 20:31:38 +01:00
)
func getConfig ( ) Config {
configPath := os . Getenv ( "MASTOGEM_CONFIG_PATH" )
if configPath == "" {
log . Println ( "MASTOGEM_CONFIG_PATH was not set, using default settings" )
config := Config {
Listen : "127.0.0.1:1965" ,
CertPath : "certs/cert.pem" ,
KeyPath : "certs/key.rsa" ,
BaseURL : "https://mamot.fr" ,
Title : "MastoGem" ,
HomeMessage : "Welcome on MastoGem, this is a Mastodon proxy for Gemini. You can view the last 20 toots of a Mastodon account by providing its ID." ,
2021-04-18 10:43:12 +02:00
RateLimit : 45 ,
2021-03-02 20:31:38 +01:00
}
return config
}
configFile , err := ioutil . ReadFile ( configPath )
if err != nil {
log . Fatalln ( "config file: %s" , err )
}
var config Config
2021-03-05 10:32:55 +01:00
err = json . Unmarshal ( configFile , & config )
if err != nil {
log . Fatalln ( "config file: %s" , err )
}
2021-03-02 20:31:38 +01:00
return config
}
func removeHTMLTags ( content string ) string {
text := strings . ReplaceAll ( content , "<p>" , "" )
text = strings . ReplaceAll ( text , "</p>" , "\n\n" )
text = strings . ReplaceAll ( text , "<br />" , "\n" )
text = strings . ReplaceAll ( text , "<br>" , "\n" )
text = strings . ReplaceAll ( text , "</a>" , "" )
text = strings . ReplaceAll ( text , "</span>" , "" )
regexString := "<a( [^>]*)?>"
regex , err := regexp . Compile ( regexString )
if err != nil {
log . Println ( "regex: %s" , err )
return ""
}
text = regex . ReplaceAllLiteralString ( text , "" )
regexString = "<span( [^>]*)?>"
regex , err = regexp . Compile ( regexString )
if err != nil {
log . Println ( "regex: %s" , err )
return ""
}
text = regex . ReplaceAllLiteralString ( text , "" )
text = html . UnescapeString ( text )
return text
}
2021-03-03 16:30:26 +01:00
func formatBlog ( toot Blog ) string {
content := toot . Content
content = removeHTMLTags ( content )
content = strings . Trim ( content , " \n\r" )
content = strings . ReplaceAll ( content , "\n#" , "\n[#]" )
if strings . HasPrefix ( content , "#" ) {
content = "[#]" + content [ 1 : ]
}
var author string
if toot . Author . DisplayName == "" {
author = toot . Author . Name
} else {
author = toot . Author . DisplayName
}
2021-05-02 19:30:47 +02:00
if toot . Reblog == nil {
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"
} else {
var originalAuthor string
if toot . Reblog . Author . DisplayName == "" {
originalAuthor = toot . Reblog . Author . Name
} else {
originalAuthor = toot . Reblog . Author . DisplayName
}
return "### Shared by " + author + " on " + toot . Date [ 0 : 10 ] + " at " + toot . Date [ 11 : 16 ] + " (original by " + originalAuthor + ")\n" + content + "\n=> /toot/" + toot . Id + " More informations about this toot"
}
2021-03-03 16:30:26 +01:00
}
2021-04-17 12:10:27 +02:00
func rateIsOk ( tab map [ string ] Rate , remoteIP string , limit int ) bool {
elmt , ok := tab [ remoteIP ]
if ok == false {
2021-04-18 10:43:12 +02:00
tab [ remoteIP ] = Rate { time . Now ( ) , 1 }
2021-04-17 12:10:27 +02:00
return true
} else {
if time . Since ( elmt . Date ) . Minutes ( ) >= 1 {
2021-04-18 10:43:12 +02:00
tab [ remoteIP ] = Rate { time . Now ( ) , 1 }
2021-04-17 12:10:27 +02:00
return true
} else {
if elmt . Count < limit {
tab [ remoteIP ] = Rate { elmt . Date , elmt . Count + 1 }
return true
} else {
return false
}
}
}
}