-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
191 lines (162 loc) · 5.08 KB
/
app.go
File metadata and controls
191 lines (162 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"context"
"encoding/hex"
"fmt"
"log"
"net/smtp"
"os"
"github.com/BurntSushi/toml"
"github.com/wailsapp/wails/v2/pkg/menu"
rt "github.com/wailsapp/wails/v2/pkg/runtime"
)
var Settings struct {
EncryptionKey string `valid:"-"`
SmtpUser string `valid:"-"`
SmtpPassword string `valid:"-"`
SmtpServer string `valid:"-"`
SmtpPort string `valid:"int,required"`
FromEmail string `valid:"-"`
Recipients []string
Name string `valid:"-"`
Button1Label string `valid:"-"`
Button2Label string `valid:"-"`
}
type Credentials struct {
SmtpUser string `json:"smtpUser"`
SmtpPassword string `json:"smtpPassword"`
}
var configFile = "config.toml"
var Button1Label = "I'm OK"
var Button2Label = "Call Me"
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
func (a *App) Menu() *menu.Menu {
AppMenu := menu.NewMenu()
ConfigureMenu := AppMenu.AddSubmenu("Configuration")
ConfigureMenu.AddText("&Configure", nil, menuCallbackEmit(a, "configure"))
return AppMenu
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.Init()
}
func (a *App) domReady(ctx context.Context) {
rt.EventsEmit(ctx, "setButtonText", Button1Label, Button2Label)
}
func menuCallbackEmit(a *App, eventName string, data ...interface{}) func(cd *menu.CallbackData) {
return func(cd *menu.CallbackData) {
rt.EventsEmit(a.ctx, eventName, data...)
}
}
func (a *App) Init() {
if _, err := toml.DecodeFile(configFile, &Settings); err != nil {
log.Println(err)
}
// log.Println(Settings)
if len(Settings.Button1Label) > 0 {
Button1Label = Settings.Button1Label
}
if len(Settings.Button2Label) > 0 {
Button2Label = Settings.Button2Label
}
}
// Button1Action sends the message with the Button 2 labels.
func (a *App) Button1Action() string {
smtpStatus := SendMessage("Status Updater - "+Settings.Button1Label, Settings.Name+" sent the message \""+Settings.Button1Label+"\".")
return fmt.Sprintf(smtpStatus)
}
// Button2Action sends the message with the Button 2 labels.
func (a *App) Button2Action() string {
smtpStatus := SendMessage("Status Updater - "+Settings.Button2Label, Settings.Name+" sent the message \""+Settings.Button2Label+"\".")
return fmt.Sprintf(smtpStatus)
}
// SaveAction saves the configuration
func (a *App) SaveAction(smtpUserName string, smtpPassword string) string {
err := EncryptCredentials(smtpUserName, smtpPassword)
saveStatus := "Saved!"
if err != nil {
saveStatus = fmt.Sprint("Problem Saving Configuration: " + err.Error())
}
return fmt.Sprintf(saveStatus)
}
func SendMessage(subject string, message string) string {
smtpStatus := "Email Sent"
err := SendEmail(subject, message)
if err != nil {
smtpStatus = fmt.Sprint("Problem Sending Email: " + err.Error())
}
return fmt.Sprintf(smtpStatus)
}
// SendEmail provides the implementation of the EmailSender type for runtime usage.
func SendEmail(subject string, message string) error {
// Set up authentication information.
smtpUser, smtpPassword, err := DecryptCredentials()
if err != nil {
return err
}
smtpServer := Settings.SmtpServer
smtpPort := Settings.SmtpPort
auth := smtp.PlainAuth("", smtpUser, smtpPassword, smtpServer)
server := smtpServer + ":" + smtpPort
fromEmail := Settings.FromEmail
msg := []byte("Subject: " + subject + "\r\n\r\n" +
message + "\r\n")
err = smtp.SendMail(server, auth, fromEmail, Settings.Recipients, msg)
if err != nil {
return err
}
return nil
}
func (a *App) GetCredentials() (credentials Credentials) {
smtpUser, smtpPassword, err := DecryptCredentials()
if err != nil {
credentials.SmtpUser = ""
credentials.SmtpPassword = ""
return credentials
}
credentials.SmtpUser = smtpUser
credentials.SmtpPassword = smtpPassword
return credentials
}
func DecryptCredentials() (smtpUser string, smtpPassword string, err error) {
smtpUserEncrypted, err := hex.DecodeString(Settings.SmtpUser)
smtpPasswordEncrypted, err := hex.DecodeString(Settings.SmtpPassword)
if err != nil {
return "", "", err
}
smtpUserDecrypted, err := decrypt([]byte(Settings.EncryptionKey), smtpUserEncrypted)
smtpPasswordDecrypted, err := decrypt([]byte(Settings.EncryptionKey), smtpPasswordEncrypted)
if err != nil {
return "", "", err
}
return fmt.Sprintf("%s", smtpUserDecrypted), fmt.Sprintf("%s", smtpPasswordDecrypted), nil
}
func EncryptCredentials(smtpUserName string, smtpPassword string) (err error) {
smtpUserNameByte, err := encrypt([]byte(Settings.EncryptionKey), []byte(smtpUserName))
smtpPasswordByte, err := encrypt([]byte(Settings.EncryptionKey), []byte(smtpPassword))
if err != nil {
return err
}
Settings.SmtpUser = hex.EncodeToString(smtpUserNameByte)
Settings.SmtpPassword = hex.EncodeToString(smtpPasswordByte)
configWriter, err := os.Create(configFile)
if err != nil {
return err
}
if err := toml.NewEncoder(configWriter).Encode(&Settings); err != nil {
return err
}
if err := configWriter.Close(); err != nil {
return err
}
return nil
}