Browse Source

Cleaning up

master
Gregory Rudolph 5 years ago
parent
commit
0ecc456536
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 10
      mailHelper.go
  2. 43
      main.go
  3. 11
      pgpHelper.go

10
mailHelper.go

@ -3,12 +3,11 @@ package main @@ -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) { @@ -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 { @@ -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)))
}

43
main.go

@ -16,14 +16,20 @@ var ( @@ -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() { @@ -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() { @@ -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) { @@ -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 {

11
pgpHelper.go

@ -12,18 +12,18 @@ import ( @@ -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 { @@ -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 { @@ -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)
}

Loading…
Cancel
Save