osmike-mail: read the owner mailbox (read only)

This commit is contained in:
OsMike-Agent 2026-11-13 20:29:20 +00:00
parent ae2df0e112
commit f9f427fc81

111
osmike-mail Executable file
View File

@ -0,0 +1,111 @@
#!/usr/bin/env python3
"""osmike-mail: READ the mailbox of the person you work for (<username>@osmike.com on mail.osmike.com). Read only.
osmike-mail address the address to give websites when you register for your person
osmike-mail list [--q WORDS] [--unread] [--minutes N] [--limit N]
osmike-mail read ID full text + links + codes of one message
osmike-mail links ID | code ID only the links / only the verification codes
osmike-mail wait [--from X] [--subject Y] [--q WORDS] [--timeout 180]
wait for a NEW message (run it right after submitting a sign-up form),
then print it with its links and codes
Typical sign-up: `osmike-mail address` → fill in the form (osmike-surf) → `osmike-mail wait --from site.com` →
open the verification link (osmike-surf open <link>) or type the code. Nothing is marked read, moved or sent.
The mail belongs to your owner: only use it for their tasks and never pass its contents to anyone else.
"""
import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
def die(msg, code=1):
print(msg, file=sys.stderr)
sys.exit(code)
def get(path, params=None, timeout=60):
api, key = os.environ.get('A2A_API', '').rstrip('/'), os.environ.get('A2A_AGENT_KEY', '')
if not api or not key:
die('A2A_API and A2A_AGENT_KEY must be set (they are in every agent).')
q = ('?' + urllib.parse.urlencode({k: v for k, v in (params or {}).items() if v not in (None, False, '')})) if params else ''
req = urllib.request.Request(api + path + q, headers={'X-API-KEY': key})
try:
with urllib.request.urlopen(req, timeout=timeout) as r:
return json.load(r)
except urllib.error.HTTPError as e:
try:
detail = json.load(e).get('detail')
except Exception:
detail = e.reason
die('%s (HTTP %s)' % (detail, e.code))
except urllib.error.URLError as e:
die('Could not reach OsMike: %s' % e.reason)
def opts(args, flags):
out, i = {}, 0
while i < len(args):
a = args[i]
if a in flags and flags[a] and i + 1 < len(args):
out[a[2:]] = args[i + 1]
i += 2
elif a in flags:
out[a[2:]] = True
i += 1
else:
die('unknown option %s\n\n%s' % (a, __doc__))
return out
def show_message(m, what='all'):
if what == 'links':
print('\n'.join(m.get('links') or []) or '(no links)')
return
if what == 'code':
print('\n'.join(m.get('codes') or []) or '(no code found; read the message)')
return
print('From: %s <%s>' % (m.get('from_name') or '', m.get('from')))
print('Subject: %s' % m.get('subject'))
print('Date: %s id %s' % (m.get('received_at'), m.get('id')))
if m.get('codes'):
print('Codes: %s' % ', '.join(m['codes']))
if m.get('links'):
print('Links:\n ' + '\n '.join(m['links'][:15]))
print('\n' + (m.get('text') or ''))
def main(argv):
if not argv or argv[0] in ('-h', '--help', 'help'):
print(__doc__)
return
cmd, rest = argv[0], argv[1:]
if cmd == 'address':
d = get('/v1/runs/mail')
print(d['address'])
print('(%s unread of %s; read only)' % (d['unread'], d['total']), file=sys.stderr)
elif cmd == 'list':
o = opts(rest, {'--q': 1, '--unread': 0, '--minutes': 1, '--limit': 1})
d = get('/v1/runs/mail/messages', {'q': o.get('q'), 'unread': 'true' if o.get('unread') else None,
'since_minutes': o.get('minutes'), 'limit': o.get('limit', 20)})
if not d['messages']:
print('(no messages)')
for m in d['messages']:
print('%s %s %-28s %s' % (m['id'], (m['received_at'] or '')[:16].replace('T', ' '), (m['from'] or '')[:28], m['subject'] or ''))
elif cmd in ('read', 'links', 'code') and rest:
show_message(get('/v1/runs/mail/messages/' + urllib.parse.quote(rest[0])), 'all' if cmd == 'read' else cmd)
elif cmd == 'wait':
o = opts(rest, {'--from': 1, '--subject': 1, '--q': 1, '--timeout': 1})
t = int(o.get('timeout', 180))
d = get('/v1/runs/mail/wait', {'sender': o.get('from'), 'subject': o.get('subject'), 'q': o.get('q'), 'timeout': t}, timeout=t + 30)
if not d.get('found'):
die('No new message arrived within %ss at %s.' % (t, d.get('address')), 2)
show_message(d)
else:
die(__doc__)
if __name__ == '__main__':
main(sys.argv[1:])