Incrementally add contacts from messages.

By default imports from messages that have no values in the contact table.
Added -all option use all rows in original table.
This commit is contained in:
Bill Thiede 2014-03-30 19:57:15 -07:00
parent 646ec2864f
commit 4eaa3b8710

View File

@ -14,6 +14,9 @@ import (
_ "github.com/lib/pq"
)
var all = flag.Bool("all", false,
"extract addresses from all messages. Default is to only extract from unprocessed messages.")
type contact struct {
hash string
name string
@ -91,13 +94,29 @@ VALUES (
errc <- nil
}
func fetchMessages(db *sql.DB) error {
rows, err := db.Query(`
func fetchMessages(db *sql.DB, all bool) error {
var query string
if all {
query = `
SELECT
hash, blob
FROM
original
`)
`
} else {
query = `
SELECT
original.hash AS hash,
original.blob AS blob
FROM
original
LEFT
JOIN contact ON original.hash = contact.hash
WHERE
contact.hash IS NULL;
`
}
rows, err := db.Query(query)
if err != nil {
return err
}
@ -140,7 +159,8 @@ FROM
for _, hdr := range []string{"to", "cc", "from"} {
addrs, err := msg.Header.AddressList(hdr)
if err != nil && err != mail.ErrHeaderNotPresent {
glog.Errorf("%s %q header: %v", h, hdr, err)
glog.Errorf("%s %q header: %v: %q", h, hdr, err,
msg.Header.Get(hdr))
continue
}
for _, addr := range addrs {
@ -169,7 +189,7 @@ func main() {
if err != nil {
glog.Fatal(err)
}
if err := fetchMessages(db); err != nil {
if err := fetchMessages(db, *all); err != nil {
glog.Fatal(err)
}
}