askrobots / dbbasic-email
Public
Email for dbbasic apps: Unix-style local mailboxes, and SMTP delivery through dbbasic-queue.
cli.py · 3.7 KB · 152 lines
| 1 | """ |
| 2 | CLI commands for dbbasic-email |
| 3 | |
| 4 | Commands are automatically discovered by dbbasic-cli |
| 5 | """ |
| 6 | |
| 7 | import sys |
| 8 | from datetime import datetime |
| 9 | |
| 10 | |
| 11 | def queue_command(args): |
| 12 | """Show email queue status""" |
| 13 | print("📧 Email Queue\n") |
| 14 | |
| 15 | try: |
| 16 | from dbbasic_queue import _read_tsv |
| 17 | |
| 18 | rows = _read_tsv() |
| 19 | email_jobs = [row for row in rows if len(row) >= 2 and row[1] == 'send_email'] |
| 20 | |
| 21 | if not email_jobs: |
| 22 | print("No emails in queue") |
| 23 | return |
| 24 | |
| 25 | stats = {'pending': 0, 'processing': 0, 'completed': 0, 'failed': 0} |
| 26 | |
| 27 | for row in email_jobs: |
| 28 | if len(row) >= 4: |
| 29 | status = row[3] |
| 30 | if status in stats: |
| 31 | stats[status] += 1 |
| 32 | |
| 33 | print(f"Total: {len(email_jobs)}") |
| 34 | print(f"Pending: {stats['pending']}") |
| 35 | print(f"Completed: {stats['completed']}") |
| 36 | print(f"Failed: {stats['failed']}") |
| 37 | |
| 38 | except ImportError: |
| 39 | print("Error: dbbasic-queue not installed") |
| 40 | except Exception as e: |
| 41 | print(f"Error: {e}") |
| 42 | |
| 43 | |
| 44 | def inbox_command(args): |
| 45 | """View user's local mailbox""" |
| 46 | if not args: |
| 47 | print("Usage: dbbasic email:inbox <username>") |
| 48 | return |
| 49 | |
| 50 | username = args[0] |
| 51 | limit = 20 |
| 52 | |
| 53 | if '-n' in args: |
| 54 | idx = args.index('-n') |
| 55 | if idx + 1 < len(args): |
| 56 | limit = int(args[idx + 1]) |
| 57 | |
| 58 | from . import read_mail |
| 59 | |
| 60 | print(f"📬 Inbox for {username}\n") |
| 61 | |
| 62 | try: |
| 63 | messages = read_mail(username, limit=limit) |
| 64 | |
| 65 | if not messages: |
| 66 | print("No messages") |
| 67 | return |
| 68 | |
| 69 | for i, msg in enumerate(messages, 1): |
| 70 | print(f"{i}. {msg['subject']}") |
| 71 | print(f" Date: {msg['date']}") |
| 72 | print(f" {msg['body'][:100]}...") |
| 73 | print() |
| 74 | |
| 75 | except Exception as e: |
| 76 | print(f"Error: {e}") |
| 77 | |
| 78 | |
| 79 | def send_command(args): |
| 80 | """Send a test email""" |
| 81 | if len(args) < 3: |
| 82 | print("Usage: dbbasic email:send <to> <subject> <body>") |
| 83 | print("\nExample:") |
| 84 | print(' dbbasic email:send user@example.com "Hello" "Test message"') |
| 85 | return |
| 86 | |
| 87 | to = args[0] |
| 88 | subject = args[1] |
| 89 | body = args[2] |
| 90 | |
| 91 | from . import send_email |
| 92 | |
| 93 | try: |
| 94 | job_id = send_email(to, subject, body) |
| 95 | print(f"✓ Email queued: {job_id}") |
| 96 | print("Worker will send it via SMTP") |
| 97 | except Exception as e: |
| 98 | print(f"Error: {e}") |
| 99 | |
| 100 | |
| 101 | def local_command(args): |
| 102 | """Send local mail to user""" |
| 103 | if len(args) < 3: |
| 104 | print("Usage: dbbasic email:local <username> <subject> <body>") |
| 105 | print("\nExample:") |
| 106 | print(' dbbasic email:local john "Test" "Hello from CLI"') |
| 107 | return |
| 108 | |
| 109 | username = args[0] |
| 110 | subject = args[1] |
| 111 | body = args[2] |
| 112 | |
| 113 | from . import mail |
| 114 | |
| 115 | try: |
| 116 | message_id = mail(username, subject, body) |
| 117 | print(f"✓ Mail delivered to {username}") |
| 118 | print(f" Message ID: {message_id}") |
| 119 | except Exception as e: |
| 120 | print(f"Error: {e}") |
| 121 | |
| 122 | |
| 123 | def worker_command(args): |
| 124 | """Run email worker (process SMTP queue)""" |
| 125 | print("📧 Starting email worker...") |
| 126 | print("Press Ctrl+C to stop\n") |
| 127 | |
| 128 | try: |
| 129 | from dbbasic_queue import process_jobs |
| 130 | from . import QUEUE_HANDLERS |
| 131 | import time |
| 132 | |
| 133 | while True: |
| 134 | process_jobs(QUEUE_HANDLERS, max_attempts=3) |
| 135 | time.sleep(1) |
| 136 | |
| 137 | except KeyboardInterrupt: |
| 138 | print("\n\n✓ Worker stopped") |
| 139 | except ImportError: |
| 140 | print("Error: dbbasic-queue not installed") |
| 141 | except Exception as e: |
| 142 | print(f"Error: {e}") |
| 143 | |
| 144 | |
| 145 | # Register commands with dbbasic CLI |
| 146 | COMMANDS = { |
| 147 | 'email:queue': queue_command, |
| 148 | 'email:inbox': inbox_command, |
| 149 | 'email:send': send_command, |
| 150 | 'email:local': local_command, |
| 151 | 'email:worker': worker_command, |
| 152 | } |