email-cli 0.6.6

Agent-friendly email CLI for Resend
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use anyhow::Result;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, OnceLock};
use std::thread;
use std::time::Duration;

use objc2::MainThreadMarker;
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject, NSObjectProtocol};
use objc2::{MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{
    NSApplication, NSApplicationActivationPolicy, NSImage, NSMenu, NSMenuItem, NSStatusBar,
    NSStatusItem,
};
use objc2_foundation::{NSData, NSString, NSTimer};

use crate::app::App;
use crate::cli::DaemonArgs;
use crate::helpers::{normalize_email, send_desktop_notification};
use crate::output::Format;

const ICON_PNG: &[u8] = include_bytes!("../../assets/menubar_icon.png");

// ── Global state shared between ObjC callbacks and the background thread ────

static MENU_SYNC_FLAG: OnceLock<Arc<AtomicBool>> = OnceLock::new();
static MENU_MARK_READ_FLAG: OnceLock<Arc<AtomicBool>> = OnceLock::new();
static UNREAD_COUNT: OnceLock<Arc<AtomicUsize>> = OnceLock::new();
static IS_SYNCING: OnceLock<Arc<AtomicBool>> = OnceLock::new();

// UI elements accessed by the timer and action handlers (main thread only)
struct UiState {
    status_item: Retained<NSStatusItem>,
    icon: Retained<NSImage>,
    status_label: Retained<NSMenuItem>,
    account_label: String,
}
// SAFETY: only accessed from the main thread (ObjC callbacks + NSTimer)
unsafe impl Send for UiState {}
unsafe impl Sync for UiState {}

static UI: OnceLock<UiState> = OnceLock::new();

// ── ObjC classes ────────────────────────────────────────────────────────────

// Menu item action handler
define_class!(
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    struct MenuHandler;

    unsafe impl NSObjectProtocol for MenuHandler {}

    impl MenuHandler {
        #[unsafe(method(syncAction:))]
        fn sync_action(&self, _sender: &AnyObject) {
            eprintln!("daemon: sync requested via menu");
            if let Some(f) = MENU_SYNC_FLAG.get() {
                f.store(true, Ordering::Relaxed);
            }
            // Optimistic UI: show syncing indicator immediately
            refresh_display(true);
        }

        #[unsafe(method(markReadAction:))]
        fn mark_read_action(&self, _sender: &AnyObject) {
            eprintln!("daemon: mark-all-read requested via menu");
            if let Some(f) = MENU_MARK_READ_FLAG.get() {
                f.store(true, Ordering::Relaxed);
            }
            // Optimistic UI: show 0 unread immediately
            if let Some(c) = UNREAD_COUNT.get() {
                c.store(0, Ordering::Relaxed);
            }
            refresh_display(false);
        }

        #[unsafe(method(quitAction:))]
        fn quit_action(&self, _sender: &AnyObject) {
            eprintln!("daemon: quit requested via menu");
            std::process::exit(0);
        }
    }
);

// Timer target for periodic display refresh
define_class!(
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    struct TimerTarget;

    unsafe impl NSObjectProtocol for TimerTarget {}

    impl TimerTarget {
        #[unsafe(method(tick:))]
        fn tick(&self, _timer: &NSTimer) {
            let syncing = IS_SYNCING
                .get()
                .map(|f| f.load(Ordering::Relaxed))
                .unwrap_or(false);
            refresh_display(syncing);
        }
    }
);

/// Update the status item display from global state (must be called on main thread)
fn refresh_display(syncing: bool) {
    let count = UNREAD_COUNT
        .get()
        .map(|c| c.load(Ordering::Relaxed))
        .unwrap_or(0);

    if let Some(ui) = UI.get() {
        let mtm = unsafe { MainThreadMarker::new_unchecked() };
        let (done, total) = crate::commands::sync::sync_progress();
        update_status_display(&ui.status_item, &ui.icon, count, syncing, done, total, mtm);

        let text = if syncing {
            if total > 0 {
                format!(
                    "Syncing {}/{} \u{00b7} {}",
                    done, total, ui.account_label
                )
            } else {
                format!("Syncing\u{2026} \u{00b7} {}", ui.account_label)
            }
        } else {
            format!("{} unread \u{00b7} {}", count, ui.account_label)
        };
        ui.status_label.setTitle(&NSString::from_str(&text));
    }
}

// ── Daemon entry point ──────────────────────────────────────────────────────

impl App {
    pub fn daemon(&self, args: DaemonArgs) -> Result<()> {
        let interval = args.interval;
        let account_filter = args.account.clone();
        let db_path = self.db_path.clone();

        // Ensure the notification bundle is extracted so UNUserNotificationCenter
        // (not osascript) fires for incoming-mail alerts.
        if let Err(e) = crate::bundle::ensure_installed() {
            eprintln!("daemon: warning — notification bundle install failed: {e}");
        }

        let initial_unread = self.count_unread(account_filter.as_deref()).unwrap_or(0);
        let account_label = account_filter
            .as_deref()
            .unwrap_or("All accounts")
            .to_string();

        // Shared state
        let unread_count = Arc::new(AtomicUsize::new(initial_unread));
        let sync_requested = Arc::new(AtomicBool::new(false));
        let mark_read_requested = Arc::new(AtomicBool::new(false));
        let is_syncing = Arc::new(AtomicBool::new(false));

        // Publish to globals for ObjC callbacks
        MENU_SYNC_FLAG.set(sync_requested.clone()).ok();
        MENU_MARK_READ_FLAG.set(mark_read_requested.clone()).ok();
        UNREAD_COUNT.set(unread_count.clone()).ok();
        IS_SYNCING.set(is_syncing.clone()).ok();

        // Background sync thread
        let unread_bg = unread_count.clone();
        let sync_req_bg = sync_requested.clone();
        let mark_read_bg = mark_read_requested.clone();
        let syncing_bg = is_syncing.clone();
        let account_filter_bg = account_filter.clone();

        thread::spawn(move || {
            let Ok(app) = App::new(db_path, Format::Json) else {
                eprintln!("daemon: failed to open database");
                return;
            };

            loop {
                if mark_read_bg.swap(false, Ordering::Relaxed) {
                    let _ = app.mark_all_read(account_filter_bg.as_deref());
                    let c = app.count_unread(account_filter_bg.as_deref()).unwrap_or(0);
                    unread_bg.store(c, Ordering::Relaxed);
                }

                syncing_bg.store(true, Ordering::Relaxed);
                if let Err(e) = daemon_sync(&app, account_filter_bg.as_deref()) {
                    eprintln!("sync error: {}", e);
                }
                let count = app.count_unread(account_filter_bg.as_deref()).unwrap_or(0);
                unread_bg.store(count, Ordering::Relaxed);
                syncing_bg.store(false, Ordering::Relaxed);

                for _ in 0..(interval * 4) {
                    if sync_req_bg.swap(false, Ordering::Relaxed) {
                        break;
                    }
                    if mark_read_bg.swap(false, Ordering::Relaxed) {
                        let _ = app.mark_all_read(account_filter_bg.as_deref());
                        let c = app.count_unread(account_filter_bg.as_deref()).unwrap_or(0);
                        unread_bg.store(c, Ordering::Relaxed);
                    }
                    thread::sleep(Duration::from_millis(250));
                }
            }
        });

        // ── Main thread: AppKit setup ──────────────────────────────────────
        let mtm = unsafe { MainThreadMarker::new_unchecked() };

        let app = NSApplication::sharedApplication(mtm);
        app.setActivationPolicy(NSApplicationActivationPolicy::Accessory);

        let status_bar = NSStatusBar::systemStatusBar();
        let status_item = status_bar.statusItemWithLength(-1.0);

        let icon = load_icon(ICON_PNG, mtm);
        update_status_display(&status_item, &icon, initial_unread, false, 0, 0, mtm);

        // ── Menu ───────────────────────────────────────────────────────────
        let menu = NSMenu::new(mtm);
        menu.setAutoenablesItems(false);

        let status_label = new_menu_item(
            &format!("{} unread \u{00b7} {}", initial_unread, account_label),
            mtm,
        );
        status_label.setEnabled(false);
        menu.addItem(&status_label);

        menu.addItem(&NSMenuItem::separatorItem(mtm));

        let handler: Retained<MenuHandler> = unsafe { msg_send![mtm.alloc::<MenuHandler>(), init] };

        let sync_item = make_action_item("Sync Now", sel!(syncAction:), &handler, mtm);
        menu.addItem(&sync_item);

        let mark_read_item =
            make_action_item("Mark All Read", sel!(markReadAction:), &handler, mtm);
        menu.addItem(&mark_read_item);

        menu.addItem(&NSMenuItem::separatorItem(mtm));

        let quit_item = make_action_item("Quit", sel!(quitAction:), &handler, mtm);
        menu.addItem(&quit_item);

        status_item.setMenu(Some(&menu));
        std::mem::forget(handler);

        // Publish UI elements to globals for refresh_display()
        UI.set(UiState {
            status_item,
            icon,
            status_label,
            account_label,
        })
        .ok();

        // ── NSTimer: refresh display every 1s ──────────────────────────────
        let timer_target: Retained<TimerTarget> =
            unsafe { msg_send![mtm.alloc::<TimerTarget>(), init] };
        unsafe {
            NSTimer::scheduledTimerWithTimeInterval_target_selector_userInfo_repeats(
                1.0,
                &timer_target,
                sel!(tick:),
                None,
                true,
            )
        };
        std::mem::forget(timer_target);

        app.finishLaunching();

        eprintln!("daemon: menu bar active — {} unread", initial_unread);

        app.run();
        Ok(())
    }

    fn count_unread(&self, account_filter: Option<&str>) -> Result<usize> {
        let (sql, params): (&str, Vec<Box<dyn rusqlite::types::ToSql>>) = match account_filter {
            Some(acct) => (
                "SELECT COUNT(*) FROM messages WHERE is_read = 0 AND direction = 'received' AND account_email = ?1",
                vec![Box::new(acct.to_string())],
            ),
            None => (
                "SELECT COUNT(*) FROM messages WHERE is_read = 0 AND direction = 'received'",
                vec![],
            ),
        };
        let refs: Vec<&dyn rusqlite::types::ToSql> = params.iter().map(|p| p.as_ref()).collect();
        let count: i64 = self
            .conn
            .query_row(sql, refs.as_slice(), |row| row.get(0))?;
        Ok(count as usize)
    }

    fn mark_all_read(&self, account_filter: Option<&str>) -> Result<()> {
        match account_filter {
            Some(acct) => {
                self.conn.execute(
                    "UPDATE messages SET is_read = 1 WHERE is_read = 0 AND direction = 'received' AND account_email = ?1",
                    [acct],
                )?;
            }
            None => {
                self.conn.execute(
                    "UPDATE messages SET is_read = 1 WHERE is_read = 0 AND direction = 'received'",
                    [],
                )?;
            }
        }
        Ok(())
    }
}

// ── AppKit helpers ──────────────────────────────────────────────────────────

fn load_icon(data: &[u8], mtm: MainThreadMarker) -> Retained<NSImage> {
    let ns_data = NSData::with_bytes(data);
    let image = NSImage::initWithData(mtm.alloc(), &ns_data).expect("failed to load icon");
    image.setTemplate(true);
    let size = objc2_foundation::NSSize::new(18.0, 18.0);
    image.setSize(size);
    image
}

fn update_status_display(
    item: &NSStatusItem,
    icon: &NSImage,
    unread: usize,
    syncing: bool,
    sync_done: usize,
    sync_total: usize,
    mtm: MainThreadMarker,
) {
    if let Some(button) = item.button(mtm) {
        button.setImage(Some(icon));
        // Fixed-width badge: pad single digits with a leading space for consistent width
        let title = if syncing {
            if sync_total > 0 {
                format!(" {}/{} ", sync_done, sync_total)
            } else {
                " \u{21BB} ".to_string() // ↻ with padding
            }
        } else if unread > 99 {
            "99+".to_string()
        } else if unread > 0 {
            if unread < 10 {
                format!(" {} ", unread) // pad single digit
            } else {
                format!("{}", unread)
            }
        } else {
            String::new()
        };
        button.setTitle(&NSString::from_str(&title));
    }
}

fn new_menu_item(title: &str, mtm: MainThreadMarker) -> Retained<NSMenuItem> {
    let ns_title = NSString::from_str(title);
    let ns_key = NSString::from_str("");
    unsafe { NSMenuItem::initWithTitle_action_keyEquivalent(mtm.alloc(), &ns_title, None, &ns_key) }
}

fn make_action_item(
    title: &str,
    action: objc2::runtime::Sel,
    handler: &MenuHandler,
    mtm: MainThreadMarker,
) -> Retained<NSMenuItem> {
    let ns_title = NSString::from_str(title);
    let ns_key = NSString::from_str("");
    let item = unsafe {
        NSMenuItem::initWithTitle_action_keyEquivalent(
            mtm.alloc(),
            &ns_title,
            Some(action),
            &ns_key,
        )
    };
    unsafe { item.setTarget(Some(handler)) };
    item
}

fn daemon_sync(app: &App, account_filter: Option<&str>) -> Result<()> {
    let accounts = if let Some(account) = account_filter {
        vec![app.get_account(&normalize_email(account))?]
    } else {
        app.list_accounts()?
    };

    for account in accounts {
        let client = app.client_for_profile(&account.profile_name)?;
        let _ = app.sync_sent_account(&client, &account, 25);
        let (_received, new_messages) =
            app.sync_received_account_with_details(&client, &account, 25)?;
        for (from, subject) in new_messages {
            send_desktop_notification(
                &format!("New email to {}", account.email),
                &format!("From: {}\n{}", from, subject),
            );
        }
    }

    Ok(())
}