?
This commit is contained in:
85
imap.go
85
imap.go
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"container/list"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
_ "golang.org/x/net/html"
|
_ "golang.org/x/net/html"
|
||||||
@@ -32,6 +34,30 @@ type MailBox struct {
|
|||||||
Client *imapclient.Client
|
Client *imapclient.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Index struct {
|
||||||
|
index map[string][]string
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIndex() *Index {
|
||||||
|
return &Index{
|
||||||
|
index: make(map[string][]string),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (idx *Index) Add(word, id string) {
|
||||||
|
idx.mu.Lock()
|
||||||
|
defer idx.mu.Unlock()
|
||||||
|
ids, ok := idx.index[word]
|
||||||
|
if !ok {
|
||||||
|
idx.index[word] = []string{id}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, existingID := range ids {
|
||||||
|
if existingID == id {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
func (mb *MailBox) Configure(conf *MailBoxConfiguration) error {
|
func (mb *MailBox) Configure(conf *MailBoxConfiguration) error {
|
||||||
|
|
||||||
mb.Server = conf.Server
|
mb.Server = conf.Server
|
||||||
@@ -88,20 +114,68 @@ func (mb *MailBox) ListFolders() ([]string, error) {
|
|||||||
}
|
}
|
||||||
return folders, nil
|
return folders, nil
|
||||||
}
|
}
|
||||||
func (mb *MailBox) GetMessages() ([]Post, error) {
|
|
||||||
|
|
||||||
|
func (mb *MailBox) Search(req string, posts []Post) ([]Post, error) {
|
||||||
|
index := NewIndex()
|
||||||
|
for _, post := range posts {
|
||||||
|
text := string(post.HTML) + post.Text
|
||||||
|
words := strings.Split(text, " ")
|
||||||
|
for _, word := range words {
|
||||||
|
word = strings.ToLower(strings.Trim(word, " .,:;(){}[]?!\"'`"))
|
||||||
|
if len(word) > 2 {
|
||||||
|
index.Add(word, post.Id)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
stems := strings.Split(req, " ")
|
||||||
|
var results []Post
|
||||||
|
|
||||||
|
added := make(map[string]bool)
|
||||||
|
for _, stem := range stems {
|
||||||
|
stem = strings.ToLower(strings.Trim(stem, " .,:;(){}[]?!\"'`"))
|
||||||
|
if len(stem) > 2 {
|
||||||
|
ids, ok := index.index[stem]
|
||||||
|
if ok {
|
||||||
|
for _, id := range ids {
|
||||||
|
if added[id] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, post := range posts {
|
||||||
|
if post.Id == id {
|
||||||
|
results = append(results, post)
|
||||||
|
added[id] = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
func (mb *MailBox) GetMessages() ([]Post, error) {
|
||||||
inbox, err := mb.Client.Select(mb.InBox, nil).Wait()
|
inbox, err := mb.Client.Select(mb.InBox, nil).Wait()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error selecting mailbox:", mb.InBox, err)
|
log.Fatal("Error selecting mailbox:", mb.InBox, err)
|
||||||
}
|
}
|
||||||
log.Println("Inbox has", inbox.NumMessages, "messages")
|
log.Println("Inbox has", inbox.NumMessages, "messages")
|
||||||
|
|
||||||
|
inbox, err = mb.Client.Select(mb.InBox, nil).Wait()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error selecting mailbox:", mb.InBox, err)
|
||||||
|
}
|
||||||
|
log.Println("Inbox has", inbox.NumMessages, "messages")
|
||||||
|
|
||||||
if inbox.NumMessages <= 0 {
|
if inbox.NumMessages <= 0 {
|
||||||
return nil, fmt.Errorf("nomessages")
|
return nil, fmt.Errorf("nomessages")
|
||||||
}
|
}
|
||||||
posts := make([]Post, 0)
|
posts := make([]Post, 0)
|
||||||
for i := uint32(1); i <= inbox.NumMessages; i++ {
|
for i := uint32(1); i <= inbox.NumMessages; i++ {
|
||||||
var post Post
|
|
||||||
seqSet := imap.SeqSetNum(i)
|
seqSet := imap.SeqSetNum(i)
|
||||||
bodySection := &imap.FetchItemBodySection{}
|
bodySection := &imap.FetchItemBodySection{}
|
||||||
fetchOptions := &imap.FetchOptions{
|
fetchOptions := &imap.FetchOptions{
|
||||||
@@ -112,6 +186,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error fetching mails:", err)
|
log.Fatal("Error fetching mails:", err)
|
||||||
}
|
}
|
||||||
|
var post Post
|
||||||
post.Title = strings.Trim(messages[0].Envelope.Subject, "\t ")
|
post.Title = strings.Trim(messages[0].Envelope.Subject, "\t ")
|
||||||
if !strings.HasPrefix(strings.ToLower(post.Title), "blog ") {
|
if !strings.HasPrefix(strings.ToLower(post.Title), "blog ") {
|
||||||
continue
|
continue
|
||||||
@@ -131,6 +206,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
part, err := mailReader.NextPart()
|
part, err := mailReader.NextPart()
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
@@ -197,6 +273,7 @@ func (mb *MailBox) GetMessages() ([]Post, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
posts = append(posts, post)
|
posts = append(posts, post)
|
||||||
|
|
||||||
}
|
}
|
||||||
sort.Slice(posts, func(i, j int) bool {
|
sort.Slice(posts, func(i, j int) bool {
|
||||||
return posts[i].DateTime.After(posts[j].DateTime)
|
return posts[i].DateTime.After(posts[j].DateTime)
|
||||||
@@ -221,3 +298,7 @@ func findHTMLNode(node *html.Node) (*html.Node, error) {
|
|||||||
}
|
}
|
||||||
return nil, fmt.Errorf("notfound")
|
return nil, fmt.Errorf("notfound")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Queue struct {
|
||||||
|
list *list.List
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user