2023-03-23 13:03:16 -05:00
package config
import (
"github.com/BurntSushi/toml"
2023-05-03 21:06:35 -05:00
vocab "github.com/go-ap/activitypub"
2023-03-23 13:03:16 -05:00
"github.com/kirsle/configdir"
2023-03-23 21:20:44 -05:00
"golang.org/x/crypto/bcrypt"
2023-03-23 13:03:16 -05:00
"path/filepath"
)
2023-03-23 20:43:49 -05:00
type ProfileMetadata struct {
Key string ` toml:"key" `
Value string ` toml:"value" `
}
type BlockedServer struct {
Hostname string ` toml:"hostname" `
Reason string ` toml:"reason" `
}
type PrivacyReplace struct {
Domain string ` toml:"domain" `
ReplaceBy string ` toml:"replace_by" `
}
2023-03-23 13:03:16 -05:00
type Config struct {
2023-03-23 20:43:49 -05:00
Domain string ` toml:"domain" `
Username string ` toml:"username" `
Password string ` toml:"admin_password" `
Name string ` toml:"name" `
Summary string ` toml:"summary" `
Https bool ` toml:"https" `
IconUrl string ` toml:"icon_url"" `
ImageUrl string ` toml:"image_url" `
Metadata [ ] ProfileMetadata ` toml:"metadata" `
Secret string ` toml:"secret" `
TrustedHosts [ ] string ` toml:"trusted_hosts" `
ManuallyApprove bool ` toml:"manually_approves_followers" `
BlockedServers [ ] BlockedServer ` toml:"blocked_servers" `
CustomFooter string ` toml:"custom_footer" `
Emoji string ` toml:"emoji" `
DisabledNotificaions [ ] string ` toml:"disabled_notifications" `
CodeTheme string ` toml:"code_highlighting_theme" `
AlsoKnownAs string ` toml:"also_known_as" `
HideFollowers bool ` toml:"hides_followers" `
HideFollowing bool ` toml:"hides_following" `
InboxRetentionDays int ` toml:"inbox_retention_days" `
CustomCSP string ` toml:"custom_content_security_policy" `
WebfingerDomain string ` toml:"webfinger_domain" `
Debug bool ` toml:"debug" `
PrivacyReplace [ ] PrivacyReplace ` toml:"privacy_replace" `
KeyPath string ` toml:"key_path" `
SessionTimeout int ` toml:"session_timeout" `
Id string ` toml:"id" `
2023-03-23 13:03:16 -05:00
}
2023-06-30 14:56:52 -05:00
func ( c Config ) GetIcon ( ) * vocab . Object {
var link = vocab . ObjectNew ( vocab . ImageType )
link . URL = vocab . IRI ( c . IconUrl )
// @todo figure out actual mime type
link . MediaType = vocab . MimeType ( "image/jpeg" )
2023-05-03 21:06:35 -05:00
2023-06-30 14:56:52 -05:00
return link
2023-05-03 21:06:35 -05:00
}
2023-06-30 14:56:52 -05:00
func ( c Config ) GetActor ( ) * vocab . Actor {
var me = vocab . PersonNew ( vocab . IRI ( c . Id ) )
me . Name = vocab . DefaultNaturalLanguageValue ( c . Name )
me . Icon = c . GetIcon ( )
// @todo parse markdown into html
me . Summary = vocab . DefaultNaturalLanguageValue ( c . Summary )
return me
2023-05-03 21:06:35 -05:00
}
2023-03-23 13:03:16 -05:00
var config Config
2023-03-23 20:43:49 -05:00
func newConfig ( ) Config {
return Config {
TrustedHosts : [ ] string { "127.0.0.1" } ,
CodeTheme : "friendly_grayscale" ,
InboxRetentionDays : 15 ,
SessionTimeout : 3600 * 24 * 3 ,
}
}
2023-03-23 13:03:16 -05:00
func init ( ) {
2023-03-23 20:43:49 -05:00
config = newConfig ( )
2023-03-25 20:42:54 -05:00
configPath := configdir . LocalConfig ( "lenore" )
2023-03-23 13:03:16 -05:00
if err := configdir . MakePath ( configPath ) ; err != nil {
panic ( err )
}
configFile := filepath . Join ( configPath , "profile.toml" )
if _ , err := toml . DecodeFile ( configFile , & config ) ; err != nil {
panic ( err )
}
}
func GetConfig ( ) Config {
return config
}
2023-03-23 21:20:44 -05:00
func CheckPassword ( pass string ) bool {
err := bcrypt . CompareHashAndPassword ( [ ] byte ( config . Password ) , [ ] byte ( pass ) )
return err == nil
}