# Chat System

KayakNet Chat provides end-to-end encrypted messaging with full privacy protection.

## Overview

* **End-to-End Encrypted** - Messages only readable by participants
* **Room-Based** - Public and private chat rooms
* **Direct Messages** - Private 1-on-1 conversations
* **Media Support** - Share images and files
* **Persistent History** - Messages saved locally
* **Real-Time** - Instant message delivery via PubSub

## Accessing Chat

1. Start your KayakNet node with `--proxy`
2. Configure browser proxy
3. Navigate to `http://chat.kyk`

Or access via `http://home.kyk` → Chat tab

## Features

### Public Rooms

Join rooms to chat with multiple people:

```
┌─────────────────────────────────────────┐
│  #general                          [3]  │
├─────────────────────────────────────────┤
│  alice: Hello everyone!                 │
│  bob: Hey alice, welcome!               │
│  you: Thanks for having me              │
├─────────────────────────────────────────┤
│  [Type a message...]           [Send]   │
└─────────────────────────────────────────┘
```

**Creating a Room:**

1. Click "NEW ROOM"
2. Enter room name
3. Optionally set as private
4. Share the room name with others

**Joining a Room:**

1. Enter room name in search
2. Click "JOIN"
3. Start chatting

### Direct Messages (DMs)

Private conversations between two users:

1. Click on a user's name
2. Select "Send Direct Message"
3. Or enter their Node ID manually
4. Messages are E2E encrypted

### Media Sharing

Share images in chat:

1. Click the image icon 📷
2. Select file from device
3. Image is encrypted and sent
4. Recipients see inline preview

### User Presence

See who's online:

* 🟢 Online - Active in last 5 minutes
* 🟡 Away - Inactive
* ⚫ Offline - Not connected

### Message Status

* ✓ Sent - Message left your node
* ✓✓ Delivered - Reached recipient's node
* 👁 Read - Recipient opened message

## Encryption

### Room Messages

Room messages use:

1. **Room Key** - Symmetric key shared by members
2. **ChaCha20-Poly1305** - AEAD encryption
3. **Gossip Protocol** - Distributed delivery

### Direct Messages

DMs use:

1. **X25519** - Key exchange with recipient
2. **ChaCha20-Poly1305** - Encrypt message
3. **Ed25519** - Sign for authenticity

### Key Rotation

Keys are rotated:

* Room keys: When members change
* DM keys: Per message (perfect forward secrecy)

## Commands

| Command       | Description         |
| ------------- | ------------------- |
| `/join #room` | Join a room         |
| `/leave`      | Leave current room  |
| `/nick name`  | Set display name    |
| `/dm user`    | Start DM with user  |
| `/members`    | List room members   |
| `/clear`      | Clear local history |
| `/help`       | Show all commands   |

## Privacy Features

### What's Protected

✅ Message content - E2E encrypted ✅ Sender identity - Pseudonymous ✅ Recipient identity - Hidden by onion routing ✅ Message timing - Obfuscated ✅ Chat history - Stored locally only

### What's Visible

⚠️ Room names - Public rooms are discoverable ⚠️ Online status - Can be disabled ⚠️ Message metadata - Size (padded), timestamp (fuzzy)

## Settings

Access settings via gear icon:

| Setting               | Description                  |
| --------------------- | ---------------------------- |
| Display Name          | Your visible name            |
| Show Online Status    | Toggle presence visibility   |
| Read Receipts         | Send read confirmations      |
| Message Notifications | Desktop notifications        |
| Auto-Delete           | Delete messages after X days |

## Technical Details

### Message Format

```json
{
  "id": "abc123...",
  "type": "room" | "dm",
  "room": "#general",
  "sender": "node_id",
  "sender_name": "alice",
  "content": "<encrypted>",
  "timestamp": 1234567890,
  "signature": "ed25519_sig"
}
```

### Delivery

Messages are delivered via:

1. **Direct** - If recipient is a peer
2. **DHT** - Store and forward
3. **Gossip** - Flood to room members

### Storage

Chat history is stored in:

```
./data/chat/
├── rooms/
│   └── #general.json
└── dms/
    └── <recipient_id>.json
```

## Troubleshooting

### Messages Not Sending

* Check node is connected (peer count > 0)
* Verify recipient is online or using DHT fallback
* Try restarting node

### Can't Join Room

* Room name is case-sensitive
* Room may be private
* Network connectivity issue

### History Not Loading

* Check data directory permissions
* May be syncing from network
* Try clearing cache

## Best Practices

1. **Use unique names** - Avoid impersonation
2. **Verify contacts** - Check Node IDs match
3. **Clear history** - When done with sensitive chats
4. **Private rooms** - For confidential discussions
5. **No personal info** - Keep anonymity intact
