-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcf
More file actions
executable file
·303 lines (264 loc) · 9.72 KB
/
Copy pathcf
File metadata and controls
executable file
·303 lines (264 loc) · 9.72 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
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
#!/bin/bash
# Chatfile tool for Claude Code
# Usage:
# cf create-room [name] [-g] - Create room (local or global with -g)
# cf list-rooms - List local and global rooms
# cf delete-room <file> - Delete room (prompts sudo for +a files)
# cf register <name> - Register with chatfile (searches local & global)
# cf join - Join the room (announces entry)
# cf leave - Leave the room (announces exit)
# cf send "message" - Send message
# cf await - Wait for new message
# cf read - Show recent messages
#
# State stored in .cf_session (no env vars needed)
set -e
GLOBAL_DIR="$HOME/.chatfiles"
CMD="${1:-help}"
shift 2>/dev/null || true
find_session() {
if [ -f ".cf_session" ]; then
echo ".cf_session"
elif [ -f "$HOME/.cf_session" ]; then
echo "$HOME/.cf_session"
else
return 1
fi
}
load_session() {
local sf
sf=$(find_session) || { echo "No session. Run: cf register <chatfile>" >&2; exit 1; }
read -r CHATFILE < "$sf"
read -r MYNAME < <(sed -n '2p' "$sf")
JOINED=$(sed -n '3p' "$sf")
}
save_session() {
local sf
sf=$(find_session) || sf=".cf_session"
printf '%s\n%s\n%s\n' "$CHATFILE" "$MYNAME" "$JOINED" > "$sf"
}
case "$CMD" in
create-room|create|cr)
GLOBAL=false
NAME=""
for arg in "$@"; do
case "$arg" in
-g|--global) GLOBAL=true ;;
*) NAME="$arg" ;;
esac
done
# Strip .Chatfile extension if user accidentally included it
NAME="${NAME%.Chatfile}"
if [ -n "$NAME" ]; then
CHATFILE="${NAME}.Chatfile"
else
CHATFILE="Chatfile"
fi
if $GLOBAL; then
mkdir -p "$GLOBAL_DIR"
CHATFILE="$GLOBAL_DIR/$CHATFILE"
fi
if [ -f "$CHATFILE" ]; then
echo "Room already exists: $CHATFILE" >&2
exit 1
fi
printf '[system %s]: Chatroom "%s". Format: Name: msg. Append only.\n' "$(date '+%F %T')" "${NAME:-default}" > "$CHATFILE"
# Make append-only (requires sudo)
if sudo chattr +a "$CHATFILE" 2>/dev/null; then
echo "Created room: $CHATFILE (append-only)"
else
echo "Created room: $CHATFILE (append-only failed - run: sudo chattr +a $CHATFILE)"
fi
;;
list-rooms|list|ls)
shopt -s nullglob
# Local rooms
local_rooms=(*.Chatfile Chatfile)
if [ ${#local_rooms[@]} -gt 0 ]; then
echo "Local rooms:"
for f in "${local_rooms[@]}"; do
[ -f "$f" ] && echo " $f"
done
fi
# Global rooms
if [ -d "$GLOBAL_DIR" ]; then
global_rooms=("$GLOBAL_DIR"/*.Chatfile "$GLOBAL_DIR"/Chatfile)
has_global=false
for f in "${global_rooms[@]}"; do
[ -f "$f" ] && has_global=true && break
done
if $has_global; then
echo "Global rooms (~/.chatfiles):"
for f in "${global_rooms[@]}"; do
[ -f "$f" ] && echo " $(basename "$f")"
done
fi
fi
shopt -u nullglob
;;
delete-room|delete|del|rm)
CHATFILE="${1:-}"
[ -z "$CHATFILE" ] && { echo "Usage: cf delete-room <chatfile>" >&2; exit 1; }
# Try to find the chatfile: local first, then global
if [ -f "$CHATFILE" ]; then
:
elif [ -f "${CHATFILE}.Chatfile" ]; then
CHATFILE="${CHATFILE}.Chatfile"
elif [ -f "$GLOBAL_DIR/$CHATFILE" ]; then
CHATFILE="$GLOBAL_DIR/$CHATFILE"
elif [ -f "$GLOBAL_DIR/${CHATFILE}.Chatfile" ]; then
CHATFILE="$GLOBAL_DIR/${CHATFILE}.Chatfile"
else
echo "Chatfile not found: $CHATFILE" >&2
exit 1
fi
# Check if file has append-only attribute and remove it
if lsattr "$CHATFILE" 2>/dev/null | cut -d' ' -f1 | grep -q 'a'; then
if sudo chattr -a "$CHATFILE" 2>/dev/null; then
rm "$CHATFILE"
echo "Deleted room: $CHATFILE (removed +a attr)"
else
echo "Failed to remove +a attribute from $CHATFILE" >&2
exit 1
fi
else
rm "$CHATFILE"
echo "Deleted room: $CHATFILE"
fi
# Clean up session if it was pointing to this file
if sf=$(find_session 2>/dev/null); then
read -r SESSION_CHATFILE < "$sf"
if [ "$SESSION_CHATFILE" = "$(realpath "$CHATFILE" 2>/dev/null || echo "$CHATFILE")" ]; then
rm "$sf"
echo "Cleared session"
fi
fi
;;
register|reg|r)
CHATFILE="${1:-Chatfile}"
# Try to find the chatfile: local first, then global
if [ -f "$CHATFILE" ]; then
CHATFILE=$(realpath "$CHATFILE")
elif [ -f "${CHATFILE}.Chatfile" ]; then
CHATFILE=$(realpath "${CHATFILE}.Chatfile")
elif [ -f "$GLOBAL_DIR/$CHATFILE" ]; then
CHATFILE="$GLOBAL_DIR/$CHATFILE"
elif [ -f "$GLOBAL_DIR/${CHATFILE}.Chatfile" ]; then
CHATFILE="$GLOBAL_DIR/${CHATFILE}.Chatfile"
else
echo "Chatfile not found: $CHATFILE" >&2
echo "Searched: ./, ~/.chatfiles/" >&2
exit 1
fi
ADJS=(swift bold calm keen sage wild bright dark quick slow)
NOUNS=(fox owl raven wolf bear hawk crane lynx deer hare)
attempts=0
while [ $attempts -lt 100 ]; do
adj="${ADJS[RANDOM % ${#ADJS[@]}]}"
noun="${NOUNS[RANDOM % ${#NOUNS[@]}]}"
suffix=$((RANDOM % 9000 + 1000))
MYNAME="${adj}-${noun}-${suffix}"
grep -q "^${MYNAME}:" "$CHATFILE" 2>/dev/null || break
# NB: `((attempts++))` evaluates to the *old* value, so on the first
# iteration it yields 0 — which bash reports as exit status 1, and
# under `set -e` (above) that aborted the whole script on the very
# first name collision. An assignment always returns success.
attempts=$((attempts + 1))
done
if [ "$attempts" -ge 100 ]; then
echo "Failed to generate a unique name after 100 attempts" >&2
exit 1
fi
JOINED=""
# Global rooms get a HOME-level session so any working directory can find it.
# Local rooms get a CWD-level session (directory-scoped).
if [[ "$CHATFILE" == "$GLOBAL_DIR"/* ]]; then
SESSION_FILE="$HOME/.cf_session"
else
SESSION_FILE=".cf_session"
fi
printf '%s\n%s\n%s\n' "$CHATFILE" "$MYNAME" "$JOINED" > "$SESSION_FILE"
echo "$MYNAME"
;;
join|j)
load_session
[ -n "$JOINED" ] && { echo "Already joined as $MYNAME" >&2; exit 1; }
JOINED="yes"
save_session
printf '[%s joined]\n' "$MYNAME" >> "$CHATFILE"
echo "Joined as $MYNAME"
;;
leave|l)
load_session
[ -z "$JOINED" ] && { echo "Not in room" >&2; exit 1; }
printf '[%s left]\n' "$MYNAME" >> "$CHATFILE"
JOINED=""
save_session
echo "Left room"
;;
send|s)
[ -z "$1" ] && { echo "Usage: cf send \"message\"" >&2; exit 1; }
load_session
[ -z "$JOINED" ] && { echo "Must join first: cf join" >&2; exit 1; }
# Collapse newlines so one send stays one line — the "Name: msg" invariant
# that read/await depend on. A literal newline would otherwise land as a
# prefix-less second line that await parses as a phantom sender.
printf '%s: %s\n' "$MYNAME" "${1//$'\n'/ }" >> "$CHATFILE"
;;
await|a|wait|w)
load_session
[ -z "$JOINED" ] && { echo "Must join first: cf join" >&2; exit 1; }
LAST=$(tail -n 1 "$CHATFILE" | cut -d: -f1)
if [ "$LAST" = "$MYNAME" ]; then
tail -f -n 0 "$CHATFILE" | head -n 1
else
tail -n 1 "$CHATFILE"
fi
;;
send-await|sa)
[ -z "$1" ] && { echo "Usage: cf send-await \"message\"" >&2; exit 1; }
load_session
[ -z "$JOINED" ] && { echo "Must join first: cf join" >&2; exit 1; }
printf '%s: %s\n' "$MYNAME" "${1//$'\n'/ }" >> "$CHATFILE"
tail -f -n 0 "$CHATFILE" | head -n 1
;;
read|cat)
load_session
tail -n "${1:-20}" "$CHATFILE"
;;
status|st)
if sf=$(find_session 2>/dev/null); then
read -r CHATFILE < "$sf"
read -r MYNAME < <(sed -n '2p' "$sf")
JOINED=$(sed -n '3p' "$sf")
echo "Session: $MYNAME"
echo "Chatfile: $CHATFILE"
echo "Joined: ${JOINED:-no}"
else
echo "No active session"
exit 1
fi
;;
help|--help|-h|*)
cat <<'EOF'
cf - Chatfile tool for Claude Code
Room management:
cf create-room [name] Create local room (name.Chatfile), append-only
cf create-room -g name Create global room in ~/.chatfiles/
cf list-rooms List local and global rooms
cf delete-room <file> Delete room (prompts sudo for +a files)
cf register <name> Register with chatfile (searches local & global)
cf join Join the room (announces entry)
cf leave Leave the room (announces exit)
Messaging:
cf send "message" Send a message
cf await Wait for next message
cf send-await "msg" Send and wait for reply
cf read [n] Show last n messages (default 20)
Info:
cf status Show current session
State stored in .cf_session (current directory)
Global rooms stored in ~/.chatfiles/
EOF
;;
esac