summaryrefslogtreecommitdiff
path: root/pim/cmd/icalfs
diff options
context:
space:
mode:
Diffstat (limited to 'pim/cmd/icalfs')
-rw-r--r--pim/cmd/icalfs/config.go137
-rw-r--r--pim/cmd/icalfs/main.go222
2 files changed, 359 insertions, 0 deletions
diff --git a/pim/cmd/icalfs/config.go b/pim/cmd/icalfs/config.go
new file mode 100644
index 0000000..b081743
--- /dev/null
+++ b/pim/cmd/icalfs/config.go
@@ -0,0 +1,137 @@
+package main
+
+import (
+ "pim/lib/cal"
+
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "time"
+)
+
+// A Cal is one calendar the server keeps current.
+//
+// The config is per-user, in ndb's attribute-pair syntax, one calendar
+// per line:
+//
+// cal=work me=you@work.example
+// cal=home me=you@home.example,alias@home.example
+//
+// In iTIP your identity is the mailto: in your own ATTENDEE line -- there
+// is no separate field for it -- so a calendar has to be told whose it
+// is before anything can reply on its behalf. Aliases are listed because
+// you may be invited at one address and send from another.
+//
+// Fetching is not this server's business. A subscribed calendar is a
+// file somebody else wrote; see pim/fetch(1).
+//
+// It lives in $home/lib/pim by default. The urls of private calendars
+// are secrets, which is the other reason they belong in a file rather
+// than in argv where ps(1) would show them.
+type Cal struct {
+ Name string
+ Refresh time.Duration // how often to re-stat, if the config says
+ File string
+ Me []string // the addresses that count as you on this calendar
+
+ mu sync.Mutex
+ last time.Time
+ err string
+}
+
+func (c *Cal) status() (time.Time, string) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.last, c.err
+}
+
+func (c *Cal) note(t time.Time, err error) {
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ c.last = t
+ if err != nil {
+ c.err = err.Error()
+ } else {
+ c.err = ""
+ }
+}
+
+// readConfig parses the calendar list. Blank lines and lines beginning
+// with # are ignored; everything else is a tuple of attr=value pairs.
+func readConfig(path, dir string) ([]*Cal, error) {
+ b, err := os.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+ var cals []*Cal
+ for n, line := range strings.Split(string(b), "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+ c := &Cal{Refresh: 15 * time.Minute}
+ for _, f := range strings.Fields(line) {
+ k, v, ok := strings.Cut(f, "=")
+ if !ok {
+ return nil, fmt.Errorf("%s:%d: %q is not attr=value", path, n+1, f)
+ }
+ v = strings.Trim(v, `"'`)
+ switch k {
+ case "cal":
+ c.Name = v
+ case "url":
+ // fetching moved out; the url belongs to the script
+ // that writes the file
+ cal.Warnf("%s:%d: url= is ignored, see pim/fetch", path, n+1)
+ case "me":
+ for _, a := range strings.Split(v, ",") {
+ if a = strings.TrimSpace(a); a != "" {
+ c.Me = append(c.Me, a)
+ }
+ }
+ case "refresh":
+ d, err := time.ParseDuration(v)
+ if err != nil || d <= 0 {
+ return nil, fmt.Errorf("%s:%d: bad refresh %q", path, n+1, v)
+ }
+ c.Refresh = d
+ default:
+ return nil, fmt.Errorf("%s:%d: unknown attribute %q", path, n+1, k)
+ }
+ }
+ if c.Name == "" {
+ return nil, fmt.Errorf("%s:%d: no cal= name", path, n+1)
+ }
+ c.File = filepath.Join(dir, c.Name+".ics")
+ cals = append(cals, c)
+ }
+ if len(cals) == 0 {
+ return nil, fmt.Errorf("%s: no calendars", path)
+ }
+ return cals, nil
+}
+
+// pick selects one calendar by name. A server serves exactly one; the
+// config lists them all so that whatever starts them has a single place
+// to read.
+func pick(cals []*Cal, name string) (*Cal, error) {
+ if name == "" {
+ if len(cals) == 1 {
+ return cals[0], nil
+ }
+ var names []string
+ for _, c := range cals {
+ names = append(names, c.Name)
+ }
+ return nil, fmt.Errorf("which calendar? -N one of: %s",
+ strings.Join(names, " "))
+ }
+ for _, c := range cals {
+ if c.Name == name {
+ return c, nil
+ }
+ }
+ return nil, fmt.Errorf("no calendar named %q", name)
+}
diff --git a/pim/cmd/icalfs/main.go b/pim/cmd/icalfs/main.go
new file mode 100644
index 0000000..c155252
--- /dev/null
+++ b/pim/cmd/icalfs/main.go
@@ -0,0 +1,222 @@
+// ical/fs serves a directory of .ics files as a 9p file system.
+//
+// It does not fetch anything. A subscribed calendar is a file somebody
+// else wrote -- pim/fetch(1), a svc entry, an editor -- which is why a
+// local calendar and a subscribed one are the same thing here. Writing
+// a file and poking ctl is the whole interface for keeping it current.
+//
+// It is one backend behind pim/lib/cal, which owns the tree. Files on
+// disk are read-only as far as scheduling goes: there is nowhere to PUT
+// and no METHOD:REQUEST to reply to, so this backend reports "read" and
+// nothing above it has to guess.
+package main
+
+import (
+ "flag"
+ "fmt"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+ "sync"
+ "time"
+
+ "pim/lib/cal"
+)
+
+func fatal(format string, a ...interface{}) {
+ cal.Warnf(format, a...)
+ os.Exit(1)
+}
+
+// backend reads a directory of .ics files.
+type backend struct {
+ c *Cal // nil when serving -d alone
+ dir string
+ poll time.Duration
+
+ mu sync.Mutex
+ evs []*cal.Event
+ sig string // what the directory looked like when last loaded
+ last time.Time
+ err string
+}
+
+func (b *backend) Name() string {
+ if b.c != nil {
+ return b.c.Name
+ }
+ return "calendar"
+}
+
+// A published feed can only be read. See pim/doc/design.md.
+func (b *backend) Caps() string { return "read" }
+
+// Refresh is how often to re-stat the directory. Whoever writes the
+// files should poke ctl instead; this only catches a hand edit.
+func (b *backend) Refresh() time.Duration { return b.poll }
+
+func (b *backend) Status() (time.Time, string) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ return b.last, b.err
+}
+
+func (b *backend) Describe() string {
+ s := fmt.Sprintf("dir %s\n", b.dir)
+ if b.c != nil && len(b.c.Me) > 0 {
+ s += "me " + joinComma(b.c.Me) + "\n"
+ }
+ return s
+}
+
+// Sync reloads if the directory has changed. Name, size and mtime are
+// enough to notice: a rewrite that keeps all three identical is a
+// rewrite of identical content.
+func (b *backend) Sync() (bool, error) {
+ sig, err := dirsig(b.dir)
+ b.note(time.Now(), err)
+ if err != nil {
+ return false, err
+ }
+ b.mu.Lock()
+ same := sig == b.sig && b.evs != nil
+ b.mu.Unlock()
+ if same {
+ return false, nil
+ }
+ evs, err := cal.LoadDir(b.dir)
+ if err != nil {
+ return false, err
+ }
+ b.mu.Lock()
+ b.evs, b.sig = evs, sig
+ b.mu.Unlock()
+ return true, nil
+}
+
+// dirsig summarises every .ics in dir.
+func dirsig(dir string) (string, error) {
+ names, err := filepath.Glob(filepath.Join(dir, "*.ics"))
+ if err != nil {
+ return "", err
+ }
+ sort.Strings(names)
+ var b strings.Builder
+ for _, n := range names {
+ fi, err := os.Stat(n)
+ if err != nil {
+ continue
+ }
+ fmt.Fprintf(&b, "%s:%d:%d\n", n, fi.Size(), fi.ModTime().UnixNano())
+ }
+ return b.String(), nil
+}
+
+func (b *backend) Events() ([]*cal.Event, error) {
+ b.mu.Lock()
+ evs := b.evs
+ b.mu.Unlock()
+ if evs != nil {
+ return evs, nil
+ }
+ evs, err := cal.LoadDir(b.dir)
+ if err != nil {
+ return nil, err
+ }
+ b.mu.Lock()
+ b.evs = evs
+ b.mu.Unlock()
+ return evs, nil
+}
+
+func (b *backend) note(t time.Time, err error) {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ b.last = t
+ if err != nil {
+ b.err = err.Error()
+ } else {
+ b.err = ""
+ }
+}
+
+func joinComma(a []string) string {
+ s := ""
+ for i, x := range a {
+ if i > 0 {
+ s += ","
+ }
+ s += x
+ }
+ return s
+}
+
+func main() {
+ cal.Argv0 = "ical/fs"
+ var (
+ dir = flag.String("d", ".", "directory of .ics files")
+ name = flag.String("s", "", "service name to post in /srv (default ical.$user.$pid)")
+ days = flag.Int("w", 400, "expansion window in days")
+ dry = flag.Bool("n", false, "load and report, do not serve")
+ conf = flag.String("c", "", "calendar config (default $home/lib/pim)")
+ which = flag.String("N", "", "which calendar in the config to serve")
+ poll = flag.Duration("r", 0, "re-stat the directory this often (0: only on ctl refresh)")
+ )
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr,
+ "usage: ical/fs [-n] [-c config] [-N name] [-d dir] [-s srv] [-w days] [-r poll]\n")
+ os.Exit(2)
+ }
+ flag.Parse()
+
+ user := os.Getenv("user")
+ if user == "" {
+ user = "glenda"
+ }
+ if *name == "" {
+ // as rio(1) and plumb(1) name theirs, so several may run at once
+ *name = fmt.Sprintf("ical.%s.%d", user, os.Getpid())
+ }
+
+ path := *conf
+ if path == "" {
+ path = filepath.Join(os.Getenv("home"), "lib", "pim")
+ if _, err := os.Stat(path); err != nil {
+ path = "" // no config is fine: -d alone works
+ }
+ }
+
+ // One server serves one calendar; several calendars means several
+ // servers, each mounted at its own name under /mnt/pim/calendars.
+ be := &backend{dir: *dir, poll: *poll}
+ if *which != "" {
+ // a name even without a config: -o still wants to be called
+ // something other than "calendar" under calendars/
+ be.c = &Cal{Name: *which}
+ }
+ if path != "" {
+ cals, err := readConfig(path, *dir)
+ if err != nil {
+ fatal("%v", err)
+ }
+ if be.c, err = pick(cals, *which); err != nil {
+ fatal("%v", err)
+ }
+ }
+
+ s := cal.New(be, cal.Config{
+ User: user, Srv: *name, Conf: path,
+ Window: time.Duration(*days) * 24 * time.Hour,
+ })
+
+ if *dry {
+ if err := s.Report(); err != nil {
+ fatal("%v", err)
+ }
+ return
+ }
+ if err := s.Serve(); err != nil {
+ fatal("%v", err)
+ }
+}