Initial commit

This commit is contained in:
ChaoticByte 2023-10-11 22:14:01 +02:00
parent f6c2eafc54
commit a6377b805d
10 changed files with 799 additions and 2 deletions

71
template.go Normal file
View file

@ -0,0 +1,71 @@
package main
import (
"bytes"
"fmt"
"text/template"
)
const DEFAULT_SUBJECT_TEMPLATE = "{{ if .Status }}{{.Status}} {{ end }}[{{.Classification}}] {{.Title}}"
const DEFAULT_BODY_TEMPLATE = `{{.Name}} - "{{.Title}}" with classification '{{.Classification}}'
Link: {{.PortalUrl}}
Published: {{.Published}}
{{ if .Status }}Status: {{.Status}}
{{ end -}}
{{ if gt .Basescore -1 }}Basescore: {{.Basescore}}
{{ end -}}
{{ if eq .NoPatch "true" }}There is no patch available at the moment!
{{ end }}
Affected Products:
{{ range $product := .ProductNames }} - {{ $product }}
{{ else }} unknown
{{ end }}
Assigned CVEs:
{{ range $cve := .Cves }} - {{ $cve }}
{{ else }} unknown
{{ end }}`
type MailTemplateConfig struct {
SubjectTemplate string `json:"subject"`
BodyTemplate string `json:"body"`
}
type MailTemplate struct {
SubjectTemplate template.Template
BodyTemplate template.Template
}
func (t MailTemplate) generate(notice WidNotice) (MailContent, error) {
c := MailContent{}
buffer := &bytes.Buffer{}
err := t.SubjectTemplate.Execute(buffer, notice)
if err != nil {
return c, err
}
c.Subject = buffer.String()
buffer.Truncate(0) // we can recycle our buffer
err = t.BodyTemplate.Execute(buffer, notice)
if err != nil {
return c, err
}
c.Body = buffer.String()
return c, nil
}
func NewTemplateFromTemplateConfig(tc MailTemplateConfig) MailTemplate {
subjectTemplate, err := template.New("subject").Parse(tc.SubjectTemplate)
if err != nil {
fmt.Println("ERROR\tCould not parse template.")
panic(err)
}
bodyTemplate, err := template.New("body").Parse(tc.BodyTemplate)
if err != nil {
fmt.Println("ERROR\tCould not parse template.")
panic(err)
}
return MailTemplate{
SubjectTemplate: *subjectTemplate,
BodyTemplate: *bodyTemplate,
}
}