Skip to content

Commit

Permalink
Make agent name configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
dullgiulio committed Apr 9, 2015
1 parent b47ddb7 commit 5f22179
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type config struct {
keys indexKey
listen string
root string
agent string
interval duration
}

Expand All @@ -65,6 +66,7 @@ func (c *config) parseFlags() {
flag.Var(&parts, "P", "Header that can be matched by a substring")
flag.Var(&c.interval, "i", "Interval between runs of the crawler")
flag.StringVar(&c.listen, "s", "0.0.0.0:8888", "Where to listen from (default: 0.0.0.0:8888)")
flag.StringVar(&c.agent, "a", "MAILER-DAEMON-PERSO", "What to write after 'From ' in mbox format")
flag.Parse()

for i := range headers {
Expand Down
10 changes: 4 additions & 6 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ func (m mailFile) filename() string {
return m.mailbox + m.file
}

func (m mailFile) writeTo(w io.Writer) error {
recipient := "MAILER-DAEMON"

func (m mailFile) writeTo(w io.Writer, c *config) error {
r, err := os.Open(m.filename())
if err != nil {
return err
}
defer r.Close()

if _, err := fmt.Fprintf(w, "From %s %s\n", recipient, m.date.Format(time.UnixDate)); err != nil {
if _, err := fmt.Fprintf(w, "From %s %s\n", c.agent, m.date.Format(time.UnixDate)); err != nil {
return err
}
_, err = io.Copy(w, r)
Expand All @@ -67,9 +65,9 @@ func newMailFiles() mailFiles {
return make([]mailFile, 0)
}

func (ms mailFiles) writeTo(w io.Writer) {
func (ms mailFiles) writeTo(w io.Writer, c *config) {
for _, m := range ms {
if err := m.writeTo(w); err != nil {
if err := m.writeTo(w, c); err != nil {
log.Print("could not write ", m, ": ", err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
type httpHandler struct {
help *help
cache *caches
config *config
indexer *mailIndexer
}

func newHttpHandler(help *help, cache *caches, indexer *mailIndexer) *httpHandler {
func newHttpHandler(help *help, cache *caches, config *config, indexer *mailIndexer) *httpHandler {
return &httpHandler{
help: help,
cache: cache,
config: config,
indexer: indexer,
}
}
Expand Down Expand Up @@ -57,6 +59,6 @@ func (h *httpHandler) writeFromURL(url string, w io.Writer) error {
return err
}

data.writeTo(w)
data.writeTo(w, h.config)
return nil
}
2 changes: 1 addition & 1 deletion perso.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ func main() {
}

// Handle all HTTP requests here
handler := newHttpHandler(help, caches, indexer)
handler := newHttpHandler(help, caches, conf, indexer)
log.Fatal(http.ListenAndServe(conf.listen, handler))
}

0 comments on commit 5f22179

Please sign in to comment.