Example #1
0
func (self *address_matcher) run(name string) {
	queries := [3]*notmuch.Query{}

	// open the database
	if db, status := notmuch.OpenDatabase(self.user_db_path,
		notmuch.DATABASE_MODE_READ_ONLY); status == notmuch.STATUS_SUCCESS {
		self.db = db
	} else {
		log.Fatalf("Failed to open the database: %v\n", status)
	}

	// pass 1: look at all from: addresses with the address book tag
	query := "tag:" + self.user_addrbook_tag
	if name != "" {
		query = query + " and from:" + name + "*"
	}
	queries[0] = self.db.CreateQuery(query)

	// pass 2: look at all to: addresses sent from our primary mail
	query = ""
	if name != "" {
		query = "to:" + name + "*"
	}
	if self.user_primary_email != "" {
		query = query + " from:" + self.user_primary_email
	}
	queries[1] = self.db.CreateQuery(query)

	// if that leads only to a few hits, we check every from too
	if queries[0].CountMessages()+queries[1].CountMessages() < 10 {
		query = ""
		if name != "" {
			query = "from:" + name + "*"
		}
		queries[2] = self.db.CreateQuery(query)
	}

	// actually retrieve and sort addresses
	results := search_address_passes(queries, name)
	for _, v := range results {
		if v != "" && v != "\n" {
			fmt.Println(v)
		}
	}
	return
}
Example #2
0
func main() {
	var query *notmuch.Query
	var nmdb *notmuch.Database

	if db, status := notmuch.OpenDatabase(getMaildirLoc(),
		notmuch.DATABASE_MODE_READ_ONLY); status == notmuch.STATUS_SUCCESS {
		nmdb = db
	} else {
		log.Fatalf("Failed to open the database: %v\n", status)
	}

	quit := make(chan bool)
	resultOut := make(chan Result)

	query = nmdb.CreateQuery("tag:new")

	println(">", query.CountMessages(), "<")
	msgs := query.SearchMessages()

	var filenames []string
	if query.CountMessages() > 0 {
		for ; msgs.Valid(); msgs.MoveToNext() {
			msg := msgs.Get()

			filenames = append(filenames, msg.GetFileName())
		}
	}

	go studyMsgs(resultOut, quit, filenames)

	//    var query *notmuch.Query
	var msgIDRegexp = regexp.MustCompile("^<(.*?)>")
	var tagRegexp = regexp.MustCompile("([\\+-])(\\S+)")

	nmdb.Close()
	// Reopen the database
	if db, status := notmuch.OpenDatabase(getMaildirLoc(),
		1); status == notmuch.STATUS_SUCCESS {
		nmdb = db
	} else {
		log.Fatalf("Failed to open the database: %v\n", status)
	}

	var running int = NCPU + 1
	for {
		result := <-resultOut

		if result.Die {

			running--

			if running > 0 {
				continue
			} else {
				break
			}
		}

		// Message-ID without the <>
		fmt.Printf("MessageID: %s\n", result.MessageID)
		reResult := msgIDRegexp.FindStringSubmatch(result.MessageID)
		if reResult == nil {
			fmt.Printf("Can't parse MessageID for mail %s\n", result.Filename)
			continue
		}

		msgID := reResult[1]
		filter := "id:"
		filter += msgID
		query := nmdb.CreateQuery(filter)
		msgs := query.SearchMessages()
		msg := msgs.Get()
		if msg == nil {
			fmt.Printf("Can't find MessageID %s for mail %s\n", msgID, result.Filename)
			continue
		}

		fmt.Printf("%s, tags: %s\n", msgID, result.Tags)
		for _, v := range tagRegexp.FindAllStringSubmatch(result.Tags, -1) {
			if v[1] == "+" {
				msg.AddTag(v[2])
			} else if v[1] == "-" {
				msg.RemoveTag(v[2])
			}
		}

	}
	fmt.Printf("exit\n")
	nmdb.Close()
	os.Exit(0)

}