aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: ec2e91f22df24c2a264f9c2629ad3b62d1a7d750 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main

import (
	"flag"
	"fmt"
	"net/url"
	"os"

	"github.com/Necoro/feed2imap-go/internal/feed"
	"github.com/Necoro/feed2imap-go/internal/imap"
	"github.com/Necoro/feed2imap-go/pkg/config"
	"github.com/Necoro/feed2imap-go/pkg/log"
)

var cfgFile = flag.String("f", "config.yml", "configuration file")
var cacheFile = flag.String("c", "feed.cache", "cache file")
var verbose = flag.Bool("v", false, "enable verbose output")
var debug = flag.Bool("d", false, "enable debug output")
var dryRun = flag.Bool("dry-run", false, "do everything short of uploading and writing the cache")
var buildCache = flag.Bool("build-cache", false, "only (re)build the cache; useful after migration or when the cache is lost or corrupted")

func processFeed(feed *feed.Feed, client *imap.Client, dryRun bool) {
	msgs, err := feed.Messages()
	if err != nil {
		log.Errorf("Processing items of feed %s: %s", feed.Name, err)
		return
	}

	if dryRun || len(msgs) == 0 {
		feed.MarkSuccess()
		return
	}

	folder := client.NewFolder(feed.Target)
	if err = client.EnsureFolder(folder); err != nil {
		log.Errorf("Creating folder of feed %s: %s", feed.Name, err)
		return
	}

	if err = msgs.Upload(client, folder, feed.Reupload); err != nil {
		log.Errorf("Uploading messages of feed %s: %s", feed.Name, err)
		return
	}

	log.Printf("Uploaded %d messages to '%s' @ %s", len(msgs), feed.Name, folder)

	feed.MarkSuccess()
}

func run() error {
	flag.Parse()
	if *debug {
		log.SetDebug()
	} else if *verbose {
		log.SetVerbose()
	}

	log.Print("Starting up...")

	cfg, err := config.Load(*cfgFile)
	if err != nil {
		return err
	}

	if err = cfg.Validate(); err != nil {
		return fmt.Errorf("Configuration invalid: %w", err)
	}

	state := feed.NewState(cfg)

	err = state.LoadCache(*cacheFile, *buildCache)
	if err != nil {
		return err
	}

	state.RemoveUndue()

	if state.NumFeeds() == 0 {
		log.Print("Nothing to do, exiting.")
		// nothing to do
		return nil
	}

	if success := state.Fetch(); success == 0 {
		return fmt.Errorf("No successful feed fetch.")
	}

	state.Filter()

	imapUrl, err := url.Parse(cfg.Target)
	if err != nil {
		return fmt.Errorf("parsing 'target': %w", err)
	}

	var c *imap.Client
	if !*dryRun && !*buildCache {
		if c, err = imap.Connect(imapUrl); err != nil {
			return err
		}

		defer c.Disconnect()
	}

	if !*buildCache {
		state.ForeachGo(func(f *feed.Feed) {
			processFeed(f, c, *dryRun)
		})
	}

	if !*dryRun {
		if err = state.StoreCache(*cacheFile); err != nil {
			return err
		}
	}

	return nil
}

func main() {
	if err := run(); err != nil {
		log.Error(err)
		os.Exit(1)
	}
}