File: template.go

package info (click to toggle)
pat 0.19.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,296 kB
  • sloc: javascript: 3,891; sh: 124; makefile: 11
file content (169 lines) | stat: -rw-r--r-- 4,895 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package forms

import (
	"bufio"
	"fmt"
	"io/fs"
	"log"
	"net/textproto"
	"os"
	"path/filepath"
	"strings"

	"github.com/la5nta/pat/internal/debug"
	"github.com/la5nta/pat/internal/directories"
)

// Template holds information about a Winlink template.
type Template struct {
	// The name of this template.
	Name string `json:"name"`

	// Absolute path to the template file represented by this struct.
	//
	// Note: The web gui uses relative paths, and for these instances the
	// value is set accordingly.
	Path string `json:"template_path"`

	// Absolute path to the optional HTML Form composer (aka "input form").
	InputFormPath string `json:"-"`

	// Absolute path to the optional HTML Form viewer (aka "display form").
	DisplayFormPath string `json:"-"`

	// Absolute path to the optional reply template.
	ReplyTemplatePath string `json:"-"`
}

func readTemplate(path string, filesMap formFilesMap) (Template, error) {
	f, err := os.Open(path)
	if err != nil {
		return Template{}, err
	}
	defer f.Close()

	template := Template{
		Name: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
		Path: path,
	}

	resolveFileReference := func(kind string, ref string) string {
		if ref == "" {
			return ""
		}
		resolved := resolveFileReference(filesMap, filepath.Dir(template.Path), strings.TrimSpace(ref))
		if resolved == "" {
			debugName, _ := filepath.Rel(filepath.Join(template.Path, "..", ".."), template.Path)
			debug.Printf("%s: failed to resolve referenced %s %q", debugName, kind, ref)
		}
		return resolved
	}
	scanner := bufio.NewScanner(newTrimBomReader(f))
	var isTemplate bool
	for scanner.Scan() {
		switch key, value, _ := strings.Cut(scanner.Text(), ":"); textproto.CanonicalMIMEHeaderKey(key) {
		case "Msg":
			isTemplate = true
		case "Form": // Form: <input form>[,<display form>]
			isTemplate = true
			inputForm, displayForm, _ := strings.Cut(value, ",")
			template.InputFormPath = resolveFileReference("input from", inputForm)
			template.DisplayFormPath = resolveFileReference("display form", displayForm)
		case "Replytemplate": // ReplyTemplate: <template>
			template.ReplyTemplatePath = resolveFileReference("reply template", value)
		}
	}
	if !isTemplate {
		return template, fmt.Errorf("not a template")
	}
	return template, err
}

// resolveFileReference searches for files referenced in .txt files.
//
// If found the returned path is the absolute path, otherwise an empty string is returned.
func resolveFileReference(filesMap formFilesMap, basePath string, referencePath string) string {
	path := filepath.Join(basePath, referencePath)
	if !directories.IsInPath(basePath, path) {
		debug.Printf("%q escapes template's base path (%q)", referencePath, basePath)
		return ""
	}
	if _, err := os.Stat(path); err == nil {
		return path
	}
	// Attempt by guessing the file extension.
	debugName := filepath.Join(filepath.Base(basePath), referencePath)
	for _, ext := range []string{htmlFileExt, replyFileExt, txtFileExt} {
		if _, err := os.Stat(path + ext); err == nil {
			debug.Printf("found %q by guessing file extension (%s)", debugName, ext)
			return path + ext
		}
	}
	// Fallback to map based lookup.
	if path := filesMap.get(referencePath); path != "" {
		debug.Printf("found %q by map based lookup", debugName)
		return path
	}
	return ""
}

type formFilesMap map[string]string

func (m formFilesMap) get(name string) string {
	if path, ok := m[strings.ToLower(name)]; ok {
		return path
	}
	if filepath.Ext(name) != "" {
		return ""
	}
	// Attempt by guessing the file extension
	for _, ext := range []string{htmlFileExt, replyFileExt, txtFileExt} {
		if path := m.get(name + ext); path != "" {
			debug.Printf("found %q (in map) by guessing file extension (%s)", name, ext)
			return path
		}
	}
	return ""
}

// formFilesFromPath returns a map from filenames to absolute paths of
// identified HTML Forms and reply templates within the given base path.
func formFilesFromPath(basePath string) formFilesMap {
	m := make(map[string]string)
	isFormFile := func(name string) bool {
		return strings.EqualFold(filepath.Ext(name), htmlFileExt)
	}
	isReplyTemplate := func(name string) bool {
		return strings.EqualFold(filepath.Ext(name), replyFileExt)
	}
	add := func(name, path string) {
		name = strings.ToLower(name)
		if !(isFormFile(name) || isReplyTemplate(name)) {
			return
		}
		if dup, ok := m[name]; ok {
			debug.Printf("duplicate filenames: %q, %q", path, dup)
		}
		m[name] = path
	}
	err := fs.WalkDir(os.DirFS(basePath), ".", func(path string, d fs.DirEntry, err error) error {
		switch {
		case path == ".":
			return nil
		case err != nil:
			return err
		case d.IsDir():
			for k, v := range formFilesFromPath(path) {
				add(k, v)
			}
			return nil
		default:
			add(d.Name(), filepath.Join(basePath, path))
			return nil
		}
	})
	if err != nil {
		log.Printf("failed to walk path %q: %v", basePath, err)
	}
	return m
}