From 0ecc456536f504ad9a4d23dcb438f8d6418b59a3 Mon Sep 17 00:00:00 2001 From: Gregory Rudolph Date: Tue, 3 Mar 2020 10:11:07 -0500 Subject: [PATCH] Cleaning up --- mailHelper.go | 10 ++++++---- main.go | 43 ++++++++++++++++++++++++++----------------- pgpHelper.go | 11 +++++------ 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/mailHelper.go b/mailHelper.go index 4742fc9..36d623b 100644 --- a/mailHelper.go +++ b/mailHelper.go @@ -3,12 +3,11 @@ package main import "fmt" import "io/ioutil" import "net/smtp" +import "samhofi.us/x/keybase" -func send(e Email) { +func send(e Email, api keybase.ChatAPI) { e.Body = appendSignature(e.Body) - //e.Body = signMessage(e.Body) - message := fmt.Sprintf("From: %s\n", conf.MyEmail) for _, recipient := range e.Recipients { message += fmt.Sprintf("To: %s\n", recipient) @@ -24,12 +23,15 @@ func send(e Email) { log.LogInfo("Message created") log.LogDebug(message) log.LogInfo("Sending message") + chat.React(api.Msg.ID, ":mailbox_with_no_mail:") err := smtp.SendMail(conf.SmtpServer, smtp.PlainAuth("", conf.MyEmail, conf.EmailPass, conf.AuthServer), conf.MyEmail, e.Recipients, []byte(message)) if err != nil { log.LogErrorType(err) } + chat.React(api.Msg.ID, ":mailbox_with_no_mail:") + chat.React(api.Msg.ID, ":mailbox_with_mail:") log.LogInfo("Email Sent") } @@ -38,6 +40,6 @@ func appendSignature(body string) string { if err != nil { log.LogErrorType(err) } - return signMessage(fmt.Sprintf("%s\n\n%s", body, string(bytes))) + return signMessage(fmt.Sprintf("%s\n\n%s\n", body, string(bytes))) } diff --git a/main.go b/main.go index 35958ba..78cf18e 100644 --- a/main.go +++ b/main.go @@ -16,14 +16,20 @@ var ( k = keybase.NewKeybase() logOpts = loggy.LogOpts{ - //OutFile: "Keybase-Email.log", - //KBTeam: "nightmarehaus.logs", - //KBChann: "general", - //ProgName: "KB-Email", - Level: 5, + OutFile: "Keybase-Email.log", + KBTeam: "nightmarehaus.logs", + KBChann: "general", + ProgName: "KB-Email", + Level: 4, UseStdout: true, } + chann = keybase.Channel{ + Name: "rudi9719", + MembersType: keybase.USER, + } + chat keybase.Chat + log = loggy.NewLogger(logOpts) conf = Config{} ) @@ -36,6 +42,7 @@ func main() { conf = loadConfig() setupCredentials() log.LogInfo("Starting keybase") + chat = k.NewChat(chann) k.Run(func(api keybase.ChatAPI) { handleMessage(api) }) @@ -43,27 +50,29 @@ func main() { } func handleMessage(api keybase.ChatAPI) { - if api.Msg.Channel.Name != k.Username { - log.LogInfo("Wrong channel detected.") - return - } - if api.Msg.Sender.Username != k.Username { - log.LogInfo("Wrong username detected.") - return - } if api.Msg.Content.Type != "text" { - log.LogInfo("Wrong message type detected.") + log.LogDebug("Wrong message type detected.") return } parts := strings.Split(api.Msg.Content.Text.Body, " ") if parts[0] != "!email" { - log.LogInfo("Wrong command detected") + log.LogDebug("Wrong command detected") return } + if api.Msg.Sender.Username != k.Username { + log.LogDebug("Wrong username detected.") + chat = k.NewChat(api.Msg.Channel) + chat.Reply(api.Msg.ID, "[EMBot] No thanks!") + return + } + if len(parts) < 4 { - log.LogInfo("Wrong length of parts detected.") + log.LogDebug("Wrong length of parts detected.") + chat.Send("[KB-Email] Not enough components to send email.") return } + chann = api.Msg.Channel + chat = k.NewChat(chann) var e Email partCounter := 1 for _, subj := range parts[1:] { @@ -101,7 +110,7 @@ func handleMessage(api keybase.ChatAPI) { e.Body += fmt.Sprintf("%s ", word) } log.LogDebug(fmt.Sprintf("%+v", e)) - go send(e) + go send(e, api) } func loadConfig() Config { diff --git a/pgpHelper.go b/pgpHelper.go index 92a5b32..2f579d4 100644 --- a/pgpHelper.go +++ b/pgpHelper.go @@ -12,18 +12,18 @@ import ( func getPrivateKey() *openpgp.Entity { pp := conf.KeyPass ppb := []byte(pp) - log.LogInfo("Getting entityList") + log.LogDebug("Getting entityList") entitylist, err := openpgp.ReadArmoredKeyRing(strings.NewReader(conf.PrivateKey)) if err != nil { log.LogErrorType(err) } - log.LogInfo(fmt.Sprintf("Getting entity 0 ```%+v```", entitylist)) + log.LogDebug(fmt.Sprintf("Getting entity 0 ```%+v```", entitylist)) entity := entitylist[0] - log.LogInfo("if PrivateKey != nil") + log.LogDebug("if PrivateKey != nil") if entity.PrivateKey != nil && entity.PrivateKey.Encrypted { err := entity.PrivateKey.Decrypt(ppb) if err != nil { - fmt.Println("Failed to decrypt key") + log.LogErrorType(err) } } @@ -31,7 +31,7 @@ func getPrivateKey() *openpgp.Entity { if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted { err := subkey.PrivateKey.Decrypt(ppb) if err != nil { - fmt.Println("Failed to decrypt subkey") + log.LogErrorType(err) } } } @@ -42,7 +42,6 @@ func signMessage(m string) string { pk := getPrivateKey() out := new(bytes.Buffer) in, err := clearsign.Encode(out, pk.PrivateKey, nil) - //in, err := openpgp.Sign(out, pk, nil, nil) if err != nil { log.LogErrorType(err) }