Compare commits

..

10 Commits

Author SHA1 Message Date
Bel LaPointe
13d2acdf16 Merge branch 'master' of https://gogs.inhome.blapointe.com/local-sandbox/contact 2023-10-16 18:45:46 -06:00
Bel LaPointe
cae30cc5d1 oops pop3 is ordered asc 2023-10-16 18:45:38 -06:00
bel
bb4b7b4da8 down to go 1.20 2023-10-16 09:20:17 -06:00
Bel LaPointe
deb6c2d196 oops 2023-10-16 09:00:04 -06:00
Bel LaPointe
5191add42b pop3 log printf 2023-10-16 08:57:27 -06:00
Bel LaPointe
e3d5a7e221 recv now has -json for pretty print 2023-10-16 08:50:59 -06:00
Bel LaPointe
7d372adb78 fix pop3 fetching, limit recv to N bytes 2023-10-16 08:47:16 -06:00
Bel LaPointe
6b04a324f3 recv uses pop3 if provided 2023-10-16 08:23:42 -06:00
Bel LaPointe
48cb9bf32b accept username/password 2023-10-16 08:18:44 -06:00
Bel LaPointe
2313cc3ac5 mv /cmd to /cmd/* 2023-10-16 08:16:43 -06:00
10 changed files with 147 additions and 61 deletions

View File

@@ -1 +0,0 @@
wcBMA1Z2i1z86bGuAQgAZ1hb8CH2dcUuKWfstpILJqaonxEsinE2KgrzqHjEtogawL8pqNCWXPYgFxPh2sj4z9zI4/7+jcAcd3wz/mXj/FdaBUpFyMaC1cV9OmD8jSTvamBpG5KI2HHxuHRv0Y19rgjcXUjtZSJcu1ZYj6yMn2+KdDTHScRBXsiapOHc5S9nKrXJ69cFF7jvQRIgnn8KNVWmcau5BXKYrXa9XnwqZtNTFYBaPgyOWb8TMnBK93T4NegMTRj/XeGQ4NAT/5sfHNRvC84mYxZQFojuUTnT3zVgdET7o6AKrtsbHstL8mzaPjM9nMPZfTotSCzsXBE07Dt1zLmPXeo4eT0VyOUtpdLgAeMsLYZM1RdSXeECzuBk4EThv3zgP+IDH09o4OzkfKlNi862TzMNh6rMpCvBGuA14x4vOzvLsDLZ4MDiagQM2eCA4LzgQeQO24U6HvCz7cdAy22h7BqG4pOs1Hfhj8oA

View File

@@ -1,48 +0,0 @@
package main
import (
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"gogs.inhome.blapointe.com/local-sandbox/contact/contact"
"gogs.inhome.blapointe.com/local/args"
)
func main() {
emailer := contact.NewEmailer()
as := args.NewArgSet()
as.Append(args.STRING, "imap", "imap server:port", "imap.gmail.com:993")
as.Append(args.INT, "n", "limit (<1 for inf)", 10)
if err := as.Parse(); err != nil {
panic(err)
}
emailer.IMAP = as.Get("imap").GetString()
emailer.Limit = as.Get("n").GetInt()
msgs, err := emailer.Read()
if err != nil {
panic(err)
}
for msg := range msgs {
b, _ := ioutil.ReadAll(io.LimitReader(msg.Body, 1024))
s := strings.ReplaceAll(string(b), "\r\n", " ")
s = strings.ReplaceAll(string(s), "\n", " ")
s = strings.ReplaceAll(string(s), "\r", " ")
if !strings.Contains(s, " ") {
s = "..."
}
d, _ := msg.Header.Date()
d = d.In(time.Local)
fmt.Printf(
"@%+v @%+v: %+v: %s\n",
d.Format("06-01-02T15:04Z07"),
msg.Header.Get("From"),
msg.Header.Get("Subject"),
s,
)
}
}

View File

@@ -1,11 +1,11 @@
module gogs.inhome.blapointe.com/local-sandbox/contact
module gogs.inhome.blapointe.com/contact/cmd/recv
go 1.20
replace gogs.inhome.blapointe.com/local-sandbox/contact/contact => ./contact
replace gogs.inhome.blapointe.com/local-sandbox/contact => ../../
require (
gogs.inhome.blapointe.com/local-sandbox/contact/contact v0.0.0-00010101000000-000000000000
gogs.inhome.blapointe.com/local-sandbox/contact v0.0.0-00010101000000-000000000000
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34
)

3
cmd/recv/go.sh Executable file
View File

@@ -0,0 +1,3 @@
#! /bin/sh
GOPRIVATE= GONOSUMDB=\* GONOPROXY=\* GOPROXY= go "$@"

81
cmd/recv/main.go Executable file
View File

@@ -0,0 +1,81 @@
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/mail"
"os"
"strings"
"time"
"gogs.inhome.blapointe.com/local-sandbox/contact"
"gogs.inhome.blapointe.com/local/args"
)
func main() {
emailer := contact.NewEmailer()
as := args.NewArgSet()
as.Append(args.STRING, "imap", "imap server:port", "imap.gmail.com:993")
as.Append(args.STRING, "pop3", "pop3 server:port", "")
as.Append(args.INT, "n", "limit (<1 for inf)", 10)
as.Append(args.STRING, "u", "username", emailer.From)
as.Append(args.STRING, "p", "password", emailer.Password)
as.Append(args.INT, "b", "dont read more than this many characters", 4096)
as.Append(args.BOOL, "json", "output as json", false)
if err := as.Parse(); err != nil {
panic(err)
}
emailer.IMAP = as.Get("imap").GetString()
emailer.POP3 = as.Get("pop3").GetString()
emailer.Limit = as.Get("n").GetInt()
emailer.From = as.GetString("u")
emailer.Password = as.GetString("p")
var msgs <-chan *mail.Message
var err error
if emailer.POP3 != "" {
msgs, err = emailer.ReadPOP3()
} else {
msgs, err = emailer.Read()
}
if err != nil {
panic(err)
}
emails := make([]map[string]any, 0, emailer.Limit)
for msg := range msgs {
b, _ := ioutil.ReadAll(io.LimitReader(msg.Body, int64(as.GetInt("b"))))
s := strings.ReplaceAll(string(b), "\r\n", "\n")
s = strings.ReplaceAll(string(s), "\n", "\n")
s = strings.ReplaceAll(string(s), "\r", "\n")
if !strings.Contains(s, " ") {
s = "..."
}
d, _ := msg.Header.Date()
d = d.In(time.Local)
if as.GetBool("json") {
emails = append(emails, map[string]any{
"t": d.Format("06-01-02T15:04Z07"),
"from": msg.Header.Get("From"),
"subject": msg.Header.Get("Subject"),
"body": s,
})
} else {
fmt.Printf(
"@%+v @%+v: \n\t%+v: \n\t%s\n",
d.Format("06-01-02T15:04Z07"),
msg.Header.Get("From"),
msg.Header.Get("Subject"),
s,
)
}
}
if as.GetBool("json") {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(emails)
}
}

18
cmd/send/go.mod Normal file
View File

@@ -0,0 +1,18 @@
module gogs.inhome.blapointe.com/contact/cmd/send
go 1.21.0
replace gogs.inhome.blapointe.com/local-sandbox/contact => ../../
require (
gogs.inhome.blapointe.com/local-sandbox/contact v0.0.1
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34
)
require (
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e // indirect
github.com/emersion/go-imap v1.2.0 // indirect
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

18
cmd/send/go.sum Normal file
View File

@@ -0,0 +1,18 @@
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e h1:mQTN05gz0rDZSABqKMzAPMb5ATWcvvdMljRzEh0LjBo=
github.com/bytbox/go-pop3 v0.0.0-20120201222208-3046caf0763e/go.mod h1:alXX+s7a4cKaIprgjeEboqi4Tm7XR/HXEwUTxUV/ywU=
github.com/emersion/go-imap v1.2.0 h1:lyUQ3+EVM21/qbWE/4Ya5UG9r5+usDxlg4yfp3TgHFA=
github.com/emersion/go-imap v1.2.0/go.mod h1:Qlx1FSx2FTxjnjWpIlVNEuX+ylerZQNFE5NsmKFSejY=
github.com/emersion/go-message v0.15.0/go.mod h1:wQUEfE+38+7EW8p8aZ96ptg6bAb1iwdgej19uXASlE4=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-textwrapper v0.0.0-20200911093747-65d896831594/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34 h1:0tuX5dfOksiOQD1vbJjVNVTVxTTIng7UrUdSLF5T+Ao=
gogs.inhome.blapointe.com/local/args v0.0.0-20230410154220-44370f257b34/go.mod h1:YG9n3Clg7683ohkVnJK2hdX8bBS9EojIsd1qPZumX0Y=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

View File

@@ -1,7 +1,7 @@
package main
import (
"gogs.inhome.blapointe.com/local-sandbox/contact/contact"
"gogs.inhome.blapointe.com/local-sandbox/contact"
"gogs.inhome.blapointe.com/local/args"
)

View File

@@ -86,23 +86,35 @@ func (e *Emailer) ReadIMAP() (chan *mail.Message, error) {
}
func (e *Emailer) ReadPOP3() (chan *mail.Message, error) {
emails := []*mail.Message{}
limit := e.Limit
if limit < 1 {
limit = 1000
}
emails := make(chan *mail.Message, limit)
defer close(emails)
log.Printf("pop3.DialTLS(%s)", e.POP3)
c, err := pop3.DialTLS(e.POP3)
if err != nil {
return nil, err
}
defer c.Rset()
log.Printf("c.Auth(%s, xyz)", e.From)
if err := c.Auth(e.From, e.Password); err != nil {
return nil, err
}
log.Printf("c.ListAll()")
ids, _, err := c.ListAll()
if err != nil {
return nil, err
}
for j, id := range ids {
if e.Limit > 0 && len(ids)-1-j >= e.Limit {
break
}
log.Printf("/c.ListAll() = %v", ids)
for i := len(ids) - 1; i >= 0; i-- {
id := ids[i]
log.Printf("c.Retr(%v)", id)
raw, err := c.Retr(id)
if err != nil {
return nil, err
@@ -111,10 +123,13 @@ func (e *Emailer) ReadPOP3() (chan *mail.Message, error) {
if err != nil {
return nil, err
}
emails = append(emails, msg)
select {
case emails <- msg:
default:
return emails, nil
}
}
return nil, nil
//return emails, nil //c.Quit()
return emails, nil
}
func (e *Emailer) Send(to, subj, msg string) error {