asgard.git

commit afdac2f3f85df7b90d69b5e93ee11c956698ec8b

Author: Adam <git@apiote.xyz>

converting in eostre

 eostre.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 eostre_2.go | 45 ++++++++++++++++++++++++++++-------
 go.mod | 2 
 go.sum | 4 +-
 main.go | 11 +++++++
 tyr.go | 2 


diff --git a/eostre.sh b/eostre.sh
new file mode 100755
index 0000000000000000000000000000000000000000..96b7791a2e906b58b1f38c7e8d38280c1a8ad359
--- /dev/null
+++ b/eostre.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+# NEEDS sed,grep,zip,unzip
+
+set -e
+
+# shellcheck disable=SC2010
+years=$(ls Current/ | grep -E '^[0-9]{4}' -o | sort -u)
+
+if [ -n "$years" ]
+then
+	mkdir -p tmp
+	unzip diary.epub -d tmp
+fi
+
+spine=""
+manifest=""
+
+for year in $years
+do
+	echo year "$year"
+	# shellcheck disable=SC2010
+	files=$(ls | grep -E "^$year")
+
+	# shellcheck disable=SC2086
+	cat $files > "../tmp/$year.html"
+
+	# todo hyphenate
+	# https://github.com/hunspell/hyphen
+	# https://github.com/hunspell/hyphen/blob/master/example.c
+	# ./newscripts/hyph_en_GB.dic
+
+	sed -i 's|<hr>|<hr/>|g' "tmp/$year.html"
+
+	footer="</body></html>"
+	if [ ! -e "tmp/OEBPS/Text/$year.xhtml" ]
+	then
+		header='<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title>'"$year"'</title><link href="../Styles/Style.css" rel="stylesheet" type="text/css"/></head><body><h1><a id="navPoint-'"$year"'">'"$year"'</a></h1>'
+		chapter=$(printf "%s\n%s\n%s" "$header" "$(cat "tmp/$year.html")" "$footer")
+	else
+		sed -i -E 's$</(body|html)>$$' "tmp/OEBPS/Text/$year.xhtml"
+		chapter=$(printf "%s\n%s\n%s" "$(cat "tmp/OEBPS/Text/$year.xhtml")" "$(cat "tmp/$year.html")" "$footer")
+	fi
+	echo "$chapter" > "tmp/OEBPS/Text/$year.xhtml"
+done
+
+# shellcheck disable=SC2010
+years=$(ls tmp/OEBPS/Text/ | grep -E '^[0-9]{4}' -o | sort -u)
+for year in $years
+do
+	manifest="$manifest\n\t\t<item href=\"Text/$year.xhtml\" id=\"$year.xhtml\" media-type=\"application/xhtml+xml\"/>"
+	spine="$spine\n\t\t<itemref idref=\"$year.xhtml\"/>"
+	cp templates/content.opf.template tmp/OEBPS/content.opf
+	sed -i "s|{{manifest}}|$manifest|" tmp/OEBPS/content.opf
+	sed -i "s|{{spine}}|$spine|" tmp/OEBPS/content.opf
+
+	toc="$toc\n\t\t<li><a href=\"$year.xhtml\">$year</a></li>"
+	cp templates/nav.xhtml.template tmp/OEBPS/Text/nav.xhtml
+	sed -i "s|{{toc}}|$toc|" tmp/OEBPS/Text/nav.xhtml
+done
+
+rm diary.epub
+
+cd tmp
+zip -X ../diary.epub mimetype
+zip -rg ../diary.epub META-INF
+zip -rg ../diary.epub OEBPS
+cd ..
+
+rm -rf tmp
+rm ./*.html




diff --git a/eostre_2.go b/eostre_2.go
index 46be8c047dd15c23a566ebbc7f4ffc21880aba6a..74dac2ce2c07f393f5d0ef6136aaf9d318a74b73 100644
--- a/eostre_2.go
+++ b/eostre_2.go
@@ -3,9 +3,12 @@
 import (
 	"bytes"
 	"encoding/base64"
+	"fmt"
 	"io"
 	"log"
 	"os"
+	"os/exec"
+	"path"
 	"strings"
 	"time"
 
@@ -18,11 +21,12 @@ 	"github.com/emersion/go-sasl"
 	"github.com/emersion/go-smtp"
 )
 
-func eostre2(config Config) error {
+func downloadDiary(config Config) error {
 	c, err := client.DialTLS(config.Eostre.DiaryImapAddress, nil)
 	if err != nil {
 		log.Fatalln(err)
 	}
+	defer c.Close()
 	log.Println("Connected")
 	defer c.Logout()
 	if err := c.Login(config.Eostre.DiaryImapUsername, config.Eostre.DiaryImapPassword); err != nil {
@@ -43,6 +47,7 @@ 	section := &imap.BodySectionName{}
 	go func() {
 		done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope, section.FetchItem(), imap.FetchFlags, imap.FetchUid}, messages)
 	}()
+	// TODO find latest
 	for msg := range messages {
 		subject := msg.Envelope.Subject
 		if subject != config.Eostre.DiarySubject {
@@ -83,13 +88,25 @@ 			if _, err := io.Copy(out, r); err != nil {
 				log.Fatalf("Failed to read encrypted file: %v", err)
 			}
 			out.Close()
-			break
+			return nil
 		} else {
 			log.Printf("%d is not binary or age\n", msg.Uid)
 			continue
 		}
 	}
-	// TODO run diary script
+	return nil
+}
+
+func updateDiary(config Config) error {
+	home, err := os.UserHomeDir()
+	if err != nil {
+		return err
+	}
+	_, err = exec.Command(path.Join(home, ".local/share/asgard/eostre.sh")).Output()
+	return err
+}
+
+func sendDiary(config Config) error {
 	recipient, err := age.ParseX25519Recipient(config.Eostre.DiaryPublicKey)
 	if err != nil {
 		log.Fatalf("Failed to parse public key: %v", err)
@@ -108,26 +125,36 @@ 	if _, err := io.Copy(w, in); err != nil {
 		log.Fatalf("Failed to read decrypted file: %v", err)
 	}
 	in.Close()
+	w.Close()
 	os.Remove("diary.epub")
 
-	now := time.Now().Format(time.RFC3339)
+	a := b.Bytes()
+
+	now := time.Now().Format("20060102T150405Z0700")
 	msg := strings.NewReader("To: " + config.Eostre.DiaryRecipient + "\r\n" +
 		"From: " + config.Eostre.DiarySender + "\r\n" +
 		"Date: " + now + "\r\n" +
 		"Message-ID: " + now + "_eostre@apiote.xyz\r\n" +
 		"MIME-Version: 1.0\r\n" +
 		"Subject: Diary\r\n" +
-		"Content-Type: application/age\r\n" +
+		"Content-Type: application/age; name=diary.epub.age\r\n" +
 		"Content-Transfer-Encoding: base64\r\n" +
 		"\r\n" +
-		string(b.Bytes()),
+		string(a),
 	)
+	fmt.Println(string(a))
+	c, err := smtp.DialTLS(config.Eostre.DiarySmtpAddress, nil)
+	if err != nil {
+		log.Fatalf("Failed smtp dial: %v", err)
+	}
 	auth := sasl.NewPlainClient("", config.Eostre.DiarySmtpUsername, config.Eostre.DiarySmtpPassword)
-	err = smtp.SendMail(config.Eostre.DiarySmtpAddress, auth, config.Eostre.DiarySender, []string{config.Eostre.DiaryRecipient}, msg)
-
+	err = c.Auth(auth)
+	if err != nil {
+		log.Fatalf("Failed smtp auth: %v", err)
+	}
+	err = c.SendMail(config.Eostre.DiarySender, []string{config.Eostre.DiaryRecipient}, msg)
 	if err != nil {
 		return err
 	}
-	defer c.Close()
 	return nil
 }




diff --git a/go.mod b/go.mod
index 79b82064ee8787a5fe4f4f40ec20a143bcc4282b..0804615a3122e1a04fd5a254812bf625bff93825 100644
--- a/go.mod
+++ b/go.mod
@@ -14,7 +14,7 @@ 	github.com/emersion/go-imap-move v0.0.0-20210907172020-fe4558f9c872
 	github.com/emersion/go-message v0.16.0
 	github.com/emersion/go-msgauth v0.6.6
 	github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac
-	github.com/emersion/go-smtp v0.15.0
+	github.com/emersion/go-smtp v0.17.0
 	github.com/mattn/go-sqlite3 v1.14.14
 	notabug.org/apiote/gott v1.1.2
 )




diff --git a/go.sum b/go.sum
index f8a54aed9840694f2bea913ec41528520d1b1482..0181bd6655a5b36e4138ab5ba48f3f433b105338 100644
--- a/go.sum
+++ b/go.sum
@@ -89,8 +89,8 @@ github.com/emersion/go-msgauth v0.6.6/go.mod h1:A+/zaz9bzukLM6tRWRgJ3BdrBi+TFKTvQ3fGMFOI9SM=
 github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
 github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac h1:tn/OQ2PmwQ0XFVgAHfjlLyqMewry25Rz7jWnVoh4Ggs=
 github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
-github.com/emersion/go-smtp v0.15.0 h1:3+hMGMGrqP/lqd7qoxZc1hTU8LY8gHV9RFGWlqSDmP8=
-github.com/emersion/go-smtp v0.15.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
+github.com/emersion/go-smtp v0.17.0 h1:tq90evlrcyqRfE6DSXaWVH54oX6OuZOQECEmhWBMEtI=
+github.com/emersion/go-smtp v0.17.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
 github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
 github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594 h1:IbFBtwoTQyw0fIM5xv1HF+Y+3ZijDR839WMulgxCcUY=
 github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=




diff --git a/main.go b/main.go
index 8bed52bc97351b671430588f42d2b6a030635161..375dd430c5f95333a7245fc8e1c05ad857438197 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,8 @@
 package main
 
 import (
+	"errors"
+	"fmt"
 	"log"
 	"net/http"
 	"os"
@@ -74,6 +76,7 @@ 	Firefly           string
 	ImapAddress       string
 	ImapUsername      string
 	ImapPassword      string
+	ImapInbox         string
 	MessageMime       string
 	DoneFolder        string
 	DefaultSource     string
@@ -124,7 +127,7 @@ 	defer db.Close()
 	log.Println("Migrated")
 
 	args := getopt.Args()
-	switch args[1] {
+	switch args[0] {
 	case "hermodr":
 		fallthrough
 	case "hermóðr":
@@ -167,6 +170,12 @@ 	case "ēostre":
 		fallthrough
 	case "eostre":
 		log.Println(eostre(config))
+		_, err = os.Stat("diary.epub")
+		if err != nil && errors.Is(err, os.ErrNotExist) {
+			log.Println(downloadDiary(config))
+		}
+		fmt.Println(updateDiary(config))
+		fmt.Println(sendDiary(config))
 
 	case "gersemi":
 		log.Println(gersemi(config))




diff --git a/tyr.go b/tyr.go
index 0a988c0fabb447063bbb0f4331c2bafdeb6373f0..40150cc316956d3922cf2f57706c5cd6da7e2d35 100644
--- a/tyr.go
+++ b/tyr.go
@@ -288,7 +288,7 @@ 	return nil
 }
 
 func tyr_serve(w http.ResponseWriter, r *http.Request) {
-	config, err := readConfig() // fixme shouldn’t read config every time
+	config, err := readConfig("") // fixme shouldn’t read config every time
 	if err != nil {
 		log.Fatalln(err)
 	}