-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimsg-tui.py
More file actions
209 lines (182 loc) · 8.6 KB
/
imsg-tui.py
File metadata and controls
209 lines (182 loc) · 8.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
"""iMessage TUI client using imsg CLI (https://github.com/steipete/imsg).
Usage: python imsg_client.py [--imsg-path /path/to/imsg]
"""
from __future__ import annotations
import argparse, curses, json, subprocess, threading, time
from datetime import datetime
SIDEBAR_W = 28
POLL_SEC = 1
LOAD_HISTORY_LIMIT= "2"
POLL_LIMIT= "1"
CHAT_ROSTER_LIMIT= "2"
DEBUG = False
load_history_dbg = ""
poll_loop_dbg = ""
if DEBUG:
load_history_dbg = " <<< LOAD"
poll_loop_dbg = " <<< POLL"
def imsg(bin, *args):
try:
r = subprocess.run([bin]+list(args), capture_output=True, text=True, timeout=15)
return [json.loads(l) for l in r.stdout.strip().splitlines() if l.strip()] if r.returncode == 0 else []
except Exception:
return []
def main(stdscr, bin):
curses.curs_set(1); curses.use_default_colors()
for i, c in enumerate([curses.COLOR_CYAN, curses.COLOR_GREEN, curses.COLOR_YELLOW, curses.COLOR_RED], 1):
curses.init_pair(i, c, -1)
chats = [] # [{id, name, identifier, service, msgs:[], rowid:0, unread:0}]
sel, active, buf = 0, -1, ""
lock = threading.Lock()
running = [True]
def load_chats():
nonlocal chats
raw = imsg(bin, "chats", "--limit", CHAT_ROSTER_LIMIT, "--json")
with lock:
chats = [{"id": c.get("id"), "name": c.get("name") or c.get("identifier","?"),
"identifier": c.get("identifier",""), "service": c.get("service",""),
"msgs": [], "rowid": 0, "unread": 0} for c in raw]
def load_history(chat):
raw = imsg(bin, "history", "--chat-id", str(chat["id"]), "--limit", LOAD_HISTORY_LIMIT, "--json")
msgs, mr = [], chat["rowid"]
for m in raw:
rid = m.get("id", 0)
if rid > mr: mr = rid
txt = m.get("text", "")
if not txt: continue
try: ts = datetime.fromisoformat(m["created_at"].replace("Z","+00:00")).strftime("%H:%M")
except Exception: ts = "??:??"
who = "me" if m.get("is_from_me") else (m.get("sender") or chat["name"])
msgs.append((ts, who, txt + load_history_dbg))
msgs.reverse()
with lock:
chat["msgs"] = msgs; chat["rowid"] = mr
def poll_loop():
while running[0]:
time.sleep(POLL_SEC)
with lock:
snapshot = [(i, c) for i, c in enumerate(chats)]
for i, c in snapshot:
if not running[0]: break
raw = imsg(bin, "history", "--chat-id", str(c["id"]), "--limit", POLL_LIMIT, "--json")
new, mr = [], c["rowid"]
for m in raw:
rid = m.get("id", 0)
if rid <= c["rowid"]: continue
if rid > mr: mr = rid
txt = m.get("text", "")
if not txt: continue
try: ts = datetime.fromisoformat(m["created_at"].replace("Z","+00:00")).strftime("%H:%M")
except Exception: ts = "??:??"
who = "me" if m.get("is_from_me") else (m.get("sender") or c["name"])
new.append((ts, who, txt + poll_loop_dbg))
if new:
with lock:
c["msgs"].extend(new); c["rowid"] = mr
if i != active: c["unread"] += len(new)
draw()
def mkwins():
h, w = stdscr.getmaxyx(); sw = min(SIDEBAR_W, w//3)
return {"st": curses.newwin(1,w,0,0), "ro": curses.newwin(h-3,sw,1,0),
"dv": curses.newwin(h-3,1,1,sw), "ch": curses.newwin(h-3,max(1,w-sw-1),1,sw+1),
"sp": curses.newwin(1,w,h-2,0), "in": curses.newwin(1,w,h-1,0)}, h, w, sw
wins, H, W, SW = mkwins()
def draw():
try:
with lock:
h, w = H, W
ac = chats[active] if 0 <= active < len(chats) else None
# Status bar
wn = wins["st"]; wn.erase(); wn.bkgd(' ', curses.A_REVERSE)
st = f" {ac['service']} | {ac['name']}" if ac else f" iMessage | {len(chats)} chats | Up/Down Tab Esc Ctrl+C"
wn.addnstr(0,0,st,w-1,curses.A_REVERSE|curses.A_BOLD); wn.refresh()
# Roster
wn = wins["ro"]; wn.erase(); rh, rw = wn.getmaxyx()
try:
wn.addnstr(0,0," CHATS",rw-1,curses.A_BOLD|curses.color_pair(3))
wn.addnstr(1,0," "+"-"*(rw-2),rw-1,curses.color_pair(3))
except curses.error: pass
for i, c in enumerate(chats):
if i+2 >= rh-1: break
badge = f" ({c['unread']})" if c["unread"] else ""
arrow = " <" if i == active else ""
ln = f" {c['name'][:rw-5-len(badge)-len(arrow)]}{badge}{arrow}".ljust(rw-1)[:rw-1]
attr = curses.A_REVERSE if i == sel else (curses.color_pair(2)|curses.A_BOLD if c["unread"] else curses.color_pair(1))
try: wn.addnstr(i+2,0,ln,rw-1,attr)
except curses.error: pass
wn.refresh()
# Divider
wn = wins["dv"]; wn.erase()
for i in range(wn.getmaxyx()[0]):
try: wn.addch(i,0,"|",curses.color_pair(3))
except curses.error: pass
wn.refresh()
# Chat area
wn = wins["ch"]; wn.erase(); ch, cw = wn.getmaxyx()
if not ac:
try: wn.addnstr(ch//2,max(0,(cw-18)//2),"Tab to open a chat",cw-1,curses.color_pair(3))
except curses.error: pass
else:
for i, (ts, who, txt) in enumerate(ac["msgs"][-(ch):]):
if i >= ch-1: break
try:
col = curses.color_pair(1) if who == "me" else curses.color_pair(2)
wn.addnstr(i,1,f"{ts} ",6,curses.color_pair(3))
wn.addnstr(f"{who}: ",len(who)+2,col|curses.A_BOLD)
rem = cw-8-len(who)-2
if rem > 0: wn.addnstr(txt,rem)
except curses.error: pass
wn.refresh()
# Separator + input
wn = wins["sp"]; wn.erase()
try: wn.addnstr(0,0,"-"*(w-1),w-1,curses.color_pair(3))
except curses.error: pass
wn.refresh()
wn = wins["in"]; wn.erase(); pr = " iMessage> " if ac else " > "
try: wn.addnstr(0,0,pr,len(pr),curses.color_pair(1)|curses.A_BOLD); wn.addstr(buf[:w-len(pr)-1])
except curses.error: pass
wn.refresh()
except Exception: pass
# Init
load_chats(); draw()
threading.Thread(target=poll_loop, daemon=True).start()
while running[0]:
try: ch = stdscr.getch()
except KeyboardInterrupt: break
with lock:
if ch == curses.KEY_RESIZE:
wins, H, W, SW = mkwins()
elif ch == curses.KEY_UP and chats:
sel = max(0, sel-1)
elif ch == curses.KEY_DOWN and chats:
sel = min(len(chats)-1, sel+1)
elif ch == 9 and chats: # Tab
active = sel
if 0 <= active < len(chats):
chats[active]["unread"] = 0
c = chats[active]
threading.Thread(target=lambda: (load_history(c), draw()), daemon=True).start()
elif ch == 27: # Esc
active = -1
elif ch in (10, 13) and buf.strip():
txt = buf.strip(); buf = ""
if txt == "/quit": break
elif txt == "/refresh":
threading.Thread(target=lambda: (load_chats(), draw()), daemon=True).start()
elif 0 <= active < len(chats):
c = chats[active]; handle = c["identifier"]
if handle:
# c["msgs"].append((datetime.now().strftime("%H:%M"), "me", txt + "foo"))
threading.Thread(target=lambda t=txt,h=handle: imsg(bin,"send","--to",h,"--text",t), daemon=True).start()
elif ch in (curses.KEY_BACKSPACE, 127, 8):
buf = buf[:-1]
elif ch == 21: buf = "" # Ctrl+U
elif 32 <= ch <= 126: buf += chr(ch)
else: continue
draw()
running[0] = False
if __name__ == "__main__":
p = argparse.ArgumentParser(description="iMessage TUI")
p.add_argument("--imsg-path", default="imsg", help="Path to imsg binary")
curses.wrapper(main, p.parse_args().imsg_path)