ref: 80962463c194e3d5e85b4db184ba090c7bc68f6c
dir: /main.go/
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/mattn/go-mastodon"
)
func main() {
account, err := loadConfig()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
client := initClient(account)
hc, err := NewHellclient()
rl := hc.rl
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
return
}
//Horrible io pipe hack
//Replaces system stdout with the readline one
r, w, _ := os.Pipe()
os.Stdout = w
go func() {
io.Copy(rl.Stdout(), r)
}()
homeMap := hc.homeMap
debugMap := hc.debugMap
postref := "a"
var recentpost *mastodon.Status
currentUser, err := client.GetAccountCurrentUser(context.Background())
if err != nil {
fmt.Println("Couldn't get our own profile: %v\n", err)
return
}
go StreamHomeTimeline(client, homeMap, hc)
for {
func() {
line, err := rl.Readline()
if err != nil {
if err == EOF || err == ErrInterrupt {
if(len(line) == 0) {
os.Exit(0)
} else {
return
}
}
if err != nil {
fmt.Println("Error:", err)
return
}
}
command, arguments := processInput(line)
//empty line
if command == "" && arguments == "" {
if hc.isPaused {
hc.pause(false)
}
return
}
//if we didn't get a slash command then the user is just posting
if command == "" && arguments != "" {
recentpost, err = postStatus(fmt.Sprintf(line), *client, "public")
if err != nil {
fmt.Println(err)
}
return
}
hc.lock()
defer hc.unlock()
index, content, _ := strings.Cut(arguments, " ")
postItem, postOK := homeMap[index]
debugItem, debugOK := debugMap[index]
//Contextual commands that need to handle their own requirements
switch command {
case "pause":
hc.togglepause()
return
case "resume":
hc.pause(false)
return
case "rm":
if !postOK && recentpost != nil {
err = client.DeleteStatus(context.Background(), recentpost.ID)
if err != nil {
fmt.Println(err)
}
recentpost = nil
return
}
if !postOK {
fmt.Println("No recent status to delete or post index not valid")
return
}
err = client.DeleteStatus(context.Background(), postItem.ID)
if err != nil {
fmt.Println(err)
}
return
}
if arguments == "" {
fmt.Printf("%v requires an argument\n", command)
return
}
//Commands that don't take an index
switch command {
case "dm":
recentpost, err = postStatus(arguments, *client, "direct")
if err != nil {
fmt.Println(err)
}
return
if !postOK && !debugOK {
fmt.Printf("\"%v\" not a valid index\n", index)
return
}
}
//Commands that accept debug indexes
switch command {
case "examine":
if postOK {
PrintObjectProperties(postItem, debugMap)
return
}
PrintObjectProperties(debugItem, debugMap)
return
}
//if a user passes a debug index to a status command
if !postOK {
fmt.Printf("\"%v\" not a valid post index\n", index)
return
}
//Commands require status indexes
switch command {
case "like":
_, err := client.Favourite(context.Background(), postItem.ID)
if err != nil {
printMastodonErr(err)
} else {
fmt.Printf(hc.formatFavorite(postItem, arguments) + "\n")
}
case "mark":
_, err := client.Bookmark(context.Background(), postItem.ID)
if err != nil {
printMastodonErr(err)
} else {
fmt.Printf(hc.formatBookmark(postItem, arguments) + "\n")
}
case "unmark":
postCopy, err := client.GetStatus(context.Background(), postItem.ID)
if postCopy.Bookmarked.(bool) && err == nil {
fmt.Printf("Post not bookmarked.\n")
return
}
_, err = client.Unbookmark(context.Background(), postItem.ID)
if err != nil {
printMastodonErr(err)
} else {
fmt.Printf(hc.formatUnbookmark(postItem, arguments) + "\n")
}
case "open":
url := fmt.Sprintf("%v/statuses/%v", client.Config.Server, postItem.ID)
cmd := exec.Command("open", url, "-a", "Eldritch Café")
cmd.Run()
case "url":
_, indexstr, _ := strings.Cut(arguments, " ")
urlindex, err := strconv.Atoi(indexstr)
if err != nil {
urlindex = 1
}
if urlindex > len(hc.urlMap[index]) {
fmt.Printf("Bad url index\n")
return
}
cmd := exec.Command("open", hc.urlMap[index][urlindex-1])
cmd.Run()
case "preview":
err := hc.previewPostImages(postItem, "open -W -a \"Quick Look\"")
if err != nil {
fmt.Printf("Image preview failed: %v\n", err)
}
case "import":
err := hc.previewPostImages(postItem, "shortcuts run \"media collector\" --input-path")
if err != nil {
fmt.Printf("Image preview failed: %v\n", err)
}
case "download":
savePostImages(postItem, "/Users/penny/Downloads/")
case "reply":
recentpost, err = postReply(content, *client, postItem.ID, currentUser.ID, postItem)
if err != nil {
fmt.Println(err)
}
return
case "rt":
rtStatus, err := client.Reblog(context.Background(), postItem.ID)
if err != nil {
fmt.Println(err)
return
}
recentpost = rtStatus
saveWorkRef(homeMap, rtStatus, postref)
hc.printPost("?"+postref, rtStatus)
postref = IncrementString(postref)
case "parent":
if postItem.InReplyToID == nil {
fmt.Printf("%v doesn't have a parent\n", index)
return
}
parentStatus, _ := client.GetStatus(context.Background(), mastodon.ID(postItem.InReplyToID.(string)))
saveWorkRef(homeMap, parentStatus, postref)
hc.printPost("?"+postref, parentStatus)
postref = IncrementString(postref)
case "children":
context, err := client.GetStatusContext(context.Background(), postItem.ID)
if err != nil {
fmt.Println(err)
return
}
if len(context.Descendants) == 0 {
fmt.Printf("\"%v\" has no children\n")
}
for post := range context.Descendants {
saveWorkRef(homeMap, context.Descendants[post], postref)
hc.printPost("?"+postref, context.Descendants[post])
postref = IncrementString(postref)
}
case "thread":
context, err := client.GetStatusContext(context.Background(), postItem.ID)
if err != nil {
fmt.Println(err)
return
}
hc.pause(true) // pause so user can read the thread
for post := range context.Ancestors {
saveWorkRef(homeMap, context.Ancestors[post], postref)
hc.printPost("?"+postref, context.Ancestors[post])
postref = IncrementString(postref)
}
hc.printPost(index, postItem)
for post := range context.Descendants {
saveWorkRef(homeMap, context.Descendants[post], postref)
hc.printPost("?"+postref, context.Descendants[post])
postref = IncrementString(postref)
}
case "account":
account := postItem.Account
fmt.Printf(formatAccount(&account))
default:
fmt.Printf("Unimplemented command \"%v\"?\"\n", command)
}
}()
}
}