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: [,] isTemplate = true inputForm, displayForm, _ := strings.Cut(value, ",") template.InputFormPath = resolveFileReference("input from", inputForm) template.DisplayFormPath = resolveFileReference("display form", displayForm) case "Replytemplate": // ReplyTemplate: