summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-09-19 12:53:23 -0400
committerrsiddharth <s@ricketyspace.net>2020-09-19 12:55:15 -0400
commit2ea847a95d10768384197da6c01dbe117fcc9a1a (patch)
treebbd7667cd7e711adcb4af2f92ee5ca678b0d8d66
parent7929e121961b3c14522aad58a60f0e6880267737 (diff)
cedar.go: flesh out Entry.email
- Add constant `sendmail`. - Flesh out Entry.email. Also add argument `section`. - Add Entry.makeEmail function. - Update Entry.email call in `processNews` function.
-rw-r--r--cedar.go48
1 files changed, 42 insertions, 6 deletions
diff --git a/cedar.go b/cedar.go
index 8187373..7255f4a 100644
--- a/cedar.go
+++ b/cedar.go
@@ -4,10 +4,13 @@ import (
"encoding/json"
"encoding/xml"
"flag"
+ "fmt"
"io"
"net/http"
"os"
+ "os/exec"
"path"
+ "strings"
)
type Link struct {
@@ -29,6 +32,8 @@ type Feed struct {
type Ids []string
+const sendmail string = "/usr/sbin/sendmail"
+
func init() {
var emailTo string
flag.StringVar(&emailTo, "t", "", "Email address for sending emails to")
@@ -167,9 +172,39 @@ func (entry Entry) in(cache Ids) bool {
return false
}
-func (entry Entry) email() error {
- // Dummy for now.
- fmt.Printf("Mailing %v...\n", entry.Id)
+func (entry Entry) makeEmail(section string) string {
+ return fmt.Sprintf(`To: %s
+Subject: FSF India - %s - %s
+
+FSF India published "%s":
+
+ %s
+`,
+ emailTo,
+ strings.Title(section),
+ entry.Title,
+ entry.Title,
+ entry.Link.Href)
+}
+
+func (entry Entry) email(section string) error {
+ cmd := exec.Command(sendmail, "-t")
+
+ stdin, err := cmd.StdinPipe()
+ if err != nil {
+ return err
+ }
+
+ io.WriteString(stdin, entry.makeEmail(section))
+ stdin.Close()
+
+ _, err = cmd.CombinedOutput()
+ if err != nil {
+ return err
+ }
+ fmt.Printf("Successfully sent %s to %s\n",
+ entry.Id, emailTo)
+
return nil
}
@@ -194,10 +229,11 @@ func processNews() error {
continue
}
- err := news.Entry[i].email()
- if err == nil {
- cache.add(news.Entry[i])
+ err := news.Entry[i].email("news")
+ if err != nil {
+ return err
}
+ cache.add(news.Entry[i])
}
err = cache.save("news")
if err != nil {