util

package
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AppConfigDir = ".config/stegodon"
)
View Source
const ConfigFileName = "config.yaml"
View Source
const Name = "stegodon"

Variables

This section is empty.

Functions

func ConvertPrivateKeyToPKCS8 added in v1.2.2

func ConvertPrivateKeyToPKCS8(pkcs1PEM string) (string, error)

ConvertPrivateKeyToPKCS8 converts a PKCS#1 private key PEM to PKCS#8 format The cryptographic key material remains unchanged, only the encoding format changes

func ConvertPublicKeyToPKIX added in v1.2.2

func ConvertPublicKeyToPKIX(pkcs1PEM string) (string, error)

ConvertPublicKeyToPKIX converts a PKCS#1 public key PEM to PKIX (PKCS#8 public) format The cryptographic key material remains unchanged, only the encoding format changes

func CountVisibleChars added in v1.2.1

func CountVisibleChars(text string) int

CountVisibleChars counts only the visible characters (runes) in text, ignoring: - Markdown links [text](url) - only the 'text' portion is counted - ANSI escape sequences (SGR codes like \033[38;5;75m) - OSC 8 hyperlinks (\033]8;;url\033\\text\033]8;;\033\\) This function counts Unicode runes, not bytes, so multi-byte characters like "·" (middle dot) are counted as 1 visible character.

func DateTimeFormat

func DateTimeFormat() string
func ExtractMarkdownLinks(text string) []string

ExtractMarkdownLinks returns a list of URLs from Markdown links in text

func FormatClickableURL added in v1.5.0

func FormatClickableURL(url string, maxWidth int, prefix string) string

FormatClickableURL creates an OSC 8 hyperlink for a URL with optional prefix The URL is truncated to maxWidth (visible characters only) and rendered as a clickable terminal link Returns the formatted link string ready for display

func GetConfigDir added in v1.1.0

func GetConfigDir() (string, error)

GetConfigDir returns the stegodon config directory path (~/.config/stegodon/) and creates it if it doesn't exist

func GetLogWriter added in v1.2.3

func GetLogWriter() io.Writer

GetLogWriter returns the current log writer (for use by other packages)

func GetMarkdownLinkCount added in v1.4.2

func GetMarkdownLinkCount(text string) int

GetMarkdownLinkCount returns the number of valid markdown links in the text

func GetNameAndVersion

func GetNameAndVersion() string

func GetVersion

func GetVersion() string

func HashtagsToActivityPubHTML added in v1.4.0

func HashtagsToActivityPubHTML(text string, baseURL string) string

HashtagsToActivityPubHTML converts hashtags in text to ActivityPub-compliant HTML links. Uses the format: <a href="https://hostname/tags/tag" class="hashtag" rel="tag">#<span>tag</span></a> The baseURL should be the full https:// URL of the server (e.g., "https://example.com")

func HighlightHashtagsHTML added in v1.4.0

func HighlightHashtagsHTML(text string) string

HighlightHashtagsHTML converts hashtags in text to clickable HTML links. Each hashtag becomes a link to /tags/{tag} page.

func HighlightHashtagsTerminal added in v1.4.0

func HighlightHashtagsTerminal(text string) string

HighlightHashtagsTerminal colors hashtags in text for terminal display. Uses secondary color for hashtags to make them visually distinct.

func HighlightMentionsHTML added in v1.4.0

func HighlightMentionsHTML(text string, localDomain string) string

HighlightMentionsHTML converts mentions in text to clickable HTML links. For local users (same domain), displays as @username and links to /users/{username}. For remote users, displays as @username@domain and links to their profile URL.

func HighlightMentionsTerminal added in v1.4.0

func HighlightMentionsTerminal(text string, localDomain string) string

HighlightMentionsTerminal colors mentions in text for terminal display and makes them clickable. Uses mention color with OSC 8 hyperlinks to the user's profile. Local users are displayed as @username, remote users as @username@domain.

func IsURL added in v1.2.1

func IsURL(text string) bool

IsURL checks if a given string is a valid HTTP or HTTPS URL

func IsValidWebFingerUsername added in v1.3.1

func IsValidWebFingerUsername(username string) (bool, string)

IsValidWebFingerUsername validates that a username meets WebFinger/ActivityPub requirements.

WebFinger allows these characters without percent-encoding: A-Z a-z 0-9 - . _ ~ ! $ & ' ( ) * + , ; =

Any other Unicode character (like ä, 字, 🔥) must be percent-encoded and is rejected here. Non-printable/control characters are also rejected.

Returns (true, "") if valid, or (false, "error message") if invalid.

func LinkifyRawURLsHTML added in v1.6.0

func LinkifyRawURLsHTML(text string) string

LinkifyRawURLsHTML converts raw URLs in text to HTML anchor tags. This should be called AFTER MarkdownLinksToHTML to avoid double-linkifying. URLs that are already inside <a> tags (from markdown conversion) are skipped.

func LinkifyRawURLsTerminal added in v1.6.0

func LinkifyRawURLsTerminal(text string) string

LinkifyRawURLsTerminal converts raw URLs in text to OSC 8 terminal hyperlinks. This should be called AFTER MarkdownLinksToTerminal to avoid double-linkifying.

func LoadAvatarImage added in v1.6.2

func LoadAvatarImage(avatarURL string) image.Image

LoadAvatarImage loads an avatar image from disk given a relative URL like /avatars/{uuid}.png. Returns nil if the URL is empty, the file doesn't exist, or decoding fails.

func LoadDefaultAvatarImage added in v1.6.3

func LoadDefaultAvatarImage() image.Image

LoadDefaultAvatarImage returns the embedded stegodon logo as a fallback avatar.

func LoadRemoteAvatarImage added in v1.7.0

func LoadRemoteAvatarImage(url string) image.Image

LoadRemoteAvatarImage fetches an avatar image from a remote URL. Returns nil if the URL is empty, the request fails, or decoding fails. Uses a 5-second timeout to avoid blocking on slow/unresponsive servers.

func LogPublicKey

func LogPublicKey(s ssh.Session)

func MarkdownLinksToHTML added in v1.1.0

func MarkdownLinksToHTML(text string) string

MarkdownLinksToHTML converts Markdown links [text](url) to HTML <a> tags

func MarkdownLinksToTerminal added in v1.1.0

func MarkdownLinksToTerminal(text string) string

MarkdownLinksToTerminal converts Markdown links [text](url) to OSC 8 hyperlinks Format: OSC 8 wrapped link text only (no URL shown) For terminals that support OSC 8, this creates clickable links with link color

func MentionsToActivityPubHTML added in v1.4.0

func MentionsToActivityPubHTML(text string, mentionURIs map[string]string) string

MentionsToActivityPubHTML converts mentions in text to ActivityPub-compliant HTML links. Uses the format: <span class="h-card"><a href="actorURI" class="u-url mention">@<span>username</span></a></span> This requires pre-resolved actor URIs, so it takes a map of mention -> actorURI.

func NormalizeEmojis added in v1.6.2

func NormalizeEmojis(text string) string

NormalizeEmojis is an alias for SanitizeRemoteContent for backwards compatibility

func NormalizeInput

func NormalizeInput(text string) string

func ParseActivityPubURL added in v1.5.0

func ParseActivityPubURL(urlStr string) (username string, domain string, ok bool)

ParseActivityPubURL attempts to extract username and domain from an ActivityPub profile URL. Supports common URL patterns: - https://example.com/u/username (stegodon format) - https://example.com/@username (mastodon format) - https://example.com/users/username (common activitypub format) - example.com/u/username (will automatically add https://) Returns username, domain, and true if successfully parsed; empty strings and false otherwise.

func ParseHashtags added in v1.4.0

func ParseHashtags(text string) []string

ParseHashtags extracts hashtags from text and returns them as lowercase, deduplicated strings. Hashtags must start with a letter and can contain letters, numbers, and underscores. Examples: #hello, #Go123, #my_tag are valid; #123, #_ are not.

func PkToHash

func PkToHash(pk string) string

func PrettyPrint

func PrettyPrint(i any) string

func PublicKeyToString

func PublicKeyToString(s ssh.PublicKey) string

func RandomString

func RandomString(length int) string

func RenderImageToHalfBlocks added in v1.6.2

func RenderImageToHalfBlocks(img image.Image, cols, rows int) string

RenderImageToHalfBlocks renders an image as a string using Unicode half-block characters with ANSI256 foreground/background colors. Each character cell encodes 2 vertical pixels: foreground color for the top pixel, background for the bottom.

func ReplacePlaceholders added in v1.5.0

func ReplacePlaceholders(text string, sshPort int) string

ReplacePlaceholders replaces template placeholders in text with actual values Currently supports: {{SSH_PORT}}

func ResolveFilePath added in v1.1.0

func ResolveFilePath(filename string) string

ResolveFilePath resolves a file path with the following priority: 1. Local working directory (e.g., ./database.db) 2. User config directory (e.g., ~/.config/stegodon/database.db) 3. Returns the user config directory path if neither exists (for creation)

func ResolveFilePathWithSubdir added in v1.1.0

func ResolveFilePathWithSubdir(subdir, filename string) string

ResolveFilePathWithSubdir resolves a file path in a subdirectory Priority: 1. Local working directory (e.g., ./.ssh/stegodonhostkey) 2. User config directory (e.g., ~/.config/stegodon/.ssh/stegodonhostkey) 3. Returns the user config directory path if neither exists (for creation)

func SanitizeRemoteContent added in v1.6.2

func SanitizeRemoteContent(text string) string

SanitizeRemoteContent removes characters that cause terminal rendering issues. This includes: - Control characters (except newline, tab) - ANSI escape sequences - Carriage returns (cause line overwrites) - Skin tone modifiers (U+1F3FB to U+1F3FF): 🏻🏼🏽🏾🏿 - Variation selectors (U+FE0E text, U+FE0F emoji) - Zero Width Joiner (U+200D) - splits compound emojis into individual ones - Zero-width characters (U+200B, U+FEFF) - Unicode BiDi control characters (U+200E-200F, U+202A-202E, U+2066-2069) - Ambiguous-width characters are replaced with ASCII equivalents:

  • Circled numbers (①②③...) → (1)(2)(3)...
  • CJK punctuation (「」『』【】〈〉《》etc.) → ASCII quotes/brackets

This helps fix rendering issues in terminals.

func SetupLogging added in v1.2.3

func SetupLogging(withJournald bool)

SetupLogging configures the logging system based on the journald flag

func StripHTMLTags added in v1.4.2

func StripHTMLTags(html string) string

StripHTMLTags removes HTML tags from a string and converts common HTML entities

func TruncateContent added in v1.6.2

func TruncateContent(s string, maxLen int) string

TruncateContent truncates content to maxLen characters, adding "[more]" indicator. Users can press 'o' to view full content via original link.

func TruncateVisibleLength added in v1.2.0

func TruncateVisibleLength(s string, maxLen int) string

TruncateVisibleLength truncates a string based on visible character count, ignoring ANSI escape sequences and OSC 8 hyperlinks. This ensures proper truncation for strings containing terminal formatting.

func UnescapeHTML added in v1.5.1

func UnescapeHTML(text string) string

UnescapeHTML converts common HTML entities back to their characters Used to display user-entered text that was escaped during storage

func ValidateNoteLength added in v1.2.1

func ValidateNoteLength(text string) error

ValidateNoteLength checks if the full note text (including markdown syntax) exceeds the database limit. Returns an error if the text is too long.

Types

type AppConfig

type AppConfig struct {
	Conf struct {
		Host            string
		SshPort         int    `yaml:"sshPort"`
		HttpPort        int    `yaml:"httpPort"`
		SslDomain       string `yaml:"sslDomain"`
		WithAp          bool   `yaml:"withAp"`
		Single          bool   `yaml:"single"`
		Closed          bool   `yaml:"closed"`
		NodeDescription string `yaml:"nodeDescription"`
		WithJournald    bool   `yaml:"withJournald"`
		WithPprof       bool   `yaml:"withPprof"`
		MaxChars        int    `yaml:"maxChars"`
		ShowGlobal      bool   `yaml:"showGlobal"`
		SshOnly         bool   `yaml:"sshOnly"`
		ShowTos         bool   `yaml:"showTos"`
	}
}

func ReadConf

func ReadConf() (*AppConfig, error)

type ErrMsg

type ErrMsg error

type Mention added in v1.4.0

type Mention struct {
	Username string
	Domain   string
}

Mention represents a parsed @username@domain mention

func ParseMentions added in v1.4.0

func ParseMentions(text string) []Mention

ParseMentions extracts @username@domain mentions from text. Returns deduplicated mentions preserving order of first occurrence.

type RsaKeyPair

type RsaKeyPair struct {
	Private string
	Public  string
}

func GeneratePemKeypair

func GeneratePemKeypair() *RsaKeyPair

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL