-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
348 lines (289 loc) · 6.87 KB
/
app.go
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/watzon/postpilot/internal/notify"
"github.com/watzon/postpilot/internal/smtp"
)
type Email struct {
ID string `json:"id"`
From string `json:"from"`
To []string `json:"to"`
Subject string `json:"subject"`
Body string `json:"body"`
HTML string `json:"html"`
Timestamp time.Time `json:"timestamp"`
}
type UISettings struct {
Theme string `json:"theme"`
ShowPreview bool `json:"showPreview"`
TimeFormat string `json:"timeFormat" enum:"12,24"`
Notification bool `json:"notification"`
Persistence bool `json:"persistence"`
}
type SMTPSettings struct {
Host string `json:"host"`
Port int `json:"port"`
Auth string `json:"auth"`
Username string `json:"username"`
Password string `json:"password"`
TLS string `json:"tls"`
}
type Settings struct {
UI UISettings `json:"ui"`
SMTP SMTPSettings `json:"smtp"`
}
type App struct {
ctx context.Context
smtp *smtp.Server
emails []*Email
mu sync.RWMutex
}
func NewApp() *App {
return &App{
emails: make([]*Email, 0),
}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// Load settings first
settings, err := a.GetSettings()
if err != nil {
log.Printf("Failed to load settings: %v", err)
}
// Load emails from disk if persistence is enabled
if settings.UI.Persistence {
if err := a.loadEmails(); err != nil {
log.Printf("Failed to load emails: %v", err)
}
} else {
// Clear any existing emails if persistence is disabled
a.emails = make([]*Email, 0)
_ = os.Remove(a.getEmailsPath())
}
// Start SMTP server
if err := a.startSMTPServer(); err != nil {
log.Printf("Failed to start SMTP server: %v", err)
return
}
// Start handling incoming emails
go a.handleIncomingEmails()
}
func (a *App) shutdown(ctx context.Context) {
if a.smtp != nil {
if err := a.smtp.Stop(); err != nil {
log.Printf("Error stopping SMTP server: %v", err)
}
}
}
func (a *App) startSMTPServer() error {
settings, err := a.GetSettings()
if err != nil {
return fmt.Errorf("failed to get settings: %w", err)
}
// Create SMTP server
s := smtp.NewServer(
settings.SMTP.Host,
settings.SMTP.Port,
settings.SMTP.Auth,
settings.SMTP.Username,
settings.SMTP.Password,
settings.SMTP.TLS,
)
// Start server
if err := s.Start(); err != nil {
return fmt.Errorf("failed to start SMTP server: %w", err)
}
a.smtp = s
// Start goroutine to handle incoming emails
go a.handleIncomingEmails()
return nil
}
func (a *App) handleIncomingEmails() {
emailChan := a.smtp.EmailsChan()
for email := range emailChan {
// Convert SMTP email to our Email type
newEmail := &Email{
ID: email.ID,
From: email.From,
To: email.To,
Subject: email.Subject,
Body: email.Body,
HTML: email.HTML,
Timestamp: email.Timestamp,
}
// Store email
a.mu.Lock()
a.emails = append(a.emails, newEmail)
a.mu.Unlock()
// Get current settings
settings, err := a.GetSettings()
if err != nil {
log.Printf("Failed to get settings: %v", err)
return
}
// Send notification
if settings.UI.Notification {
go func(e *Email) {
notify.SendNotification(
"New Email",
fmt.Sprintf("From: %s\nSubject: %s", e.From, e.Subject),
)
}(newEmail)
}
// Save emails to disk
if err := a.saveEmails(); err != nil {
log.Printf("Failed to save emails: %v", err)
}
// Emit event to frontend
runtime.EventsEmit(a.ctx, "new:email", email)
}
}
// GetEmails returns all stored emails
func (a *App) GetEmails() []*Email {
a.mu.RLock()
defer a.mu.RUnlock()
return a.emails
}
// RestartSMTPServer restarts the SMTP server with new settings
func (a *App) RestartSMTPServer() error {
if a.smtp != nil {
if err := a.smtp.Stop(); err != nil {
return fmt.Errorf("failed to stop SMTP server: %w", err)
}
}
return a.startSMTPServer()
}
func (a *App) GetSettings() (Settings, error) {
configPath := a.getConfigPath()
// Default settings
settings := Settings{
UI: UISettings{
Theme: "system",
ShowPreview: false,
TimeFormat: "12",
Notification: true,
Persistence: false,
},
SMTP: SMTPSettings{
Host: "localhost",
Port: 1025,
Auth: "none",
TLS: "none",
},
}
// Check if config file exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
// Create default config file
return settings, a.SaveSettings(settings)
}
// Read config file
data, err := os.ReadFile(configPath)
if err != nil {
return settings, err
}
// Parse JSON
err = json.Unmarshal(data, &settings)
return settings, err
}
func (a *App) SaveSettings(settings Settings) error {
configPath := a.getConfigPath()
// Create config directory if it doesn't exist
configDir := filepath.Dir(configPath)
if err := os.MkdirAll(configDir, 0755); err != nil {
return err
}
// Marshal settings to JSON
data, err := json.MarshalIndent(settings, "", " ")
if err != nil {
return err
}
// Write to file
return os.WriteFile(configPath, data, 0644)
}
func (a *App) getConfigPath() string {
// Get user config directory
configDir, err := os.UserConfigDir()
if err != nil {
configDir = "."
}
return filepath.Join(configDir, "postpilot", "settings.json")
}
func (a *App) getEmailsPath() string {
configDir, err := os.UserConfigDir()
if err != nil {
configDir = "."
}
return filepath.Join(configDir, "postpilot", "emails.json")
}
func (a *App) saveEmails() error {
settings, err := a.GetSettings()
if err != nil {
return err
}
if !settings.UI.Persistence {
return nil
}
emailsPath := a.getEmailsPath()
configDir := filepath.Dir(emailsPath)
if err := os.MkdirAll(configDir, 0755); err != nil {
return err
}
a.mu.RLock()
data, err := json.MarshalIndent(a.emails, "", " ")
a.mu.RUnlock()
if err != nil {
return err
}
return os.WriteFile(emailsPath, data, 0644)
}
func (a *App) loadEmails() error {
settings, err := a.GetSettings()
if err != nil {
return err
}
if !settings.UI.Persistence {
// Clear any existing emails file
emailsPath := a.getEmailsPath()
_ = os.Remove(emailsPath)
return nil
}
emailsPath := a.getEmailsPath()
if _, err := os.Stat(emailsPath); os.IsNotExist(err) {
return nil
}
data, err := os.ReadFile(emailsPath)
if err != nil {
return err
}
var emails []*Email
if err := json.Unmarshal(data, &emails); err != nil {
return err
}
a.mu.Lock()
a.emails = emails
a.mu.Unlock()
return nil
}
// ClearEmails clears all stored emails
func (a *App) ClearEmails() error {
a.mu.Lock()
a.emails = make([]*Email, 0)
a.mu.Unlock()
// Remove the emails file if it exists
emailsPath := a.getEmailsPath()
_ = os.Remove(emailsPath)
// Emit event to frontend
runtime.EventsEmit(a.ctx, "emails:cleared")
return nil
}
func (a *App) GetVersion() string {
return version
}