# IMC Vibe

A self-sufficient web application to manage an ISPmail (Postfix, Dovecot, Rspamd) mail server.

## Features

- **Domain Management** - Add, view, and delete mail domains
- **User Management** - Create and manage mail users per domain
- **Alias Management** - Create and manage email aliases per domain
- **Mail Queue** - View, requeue, and delete queued emails
- **Mail Logs** - View postfix logs with filtering
- **Password Management** - Change password for admin users

## Tech Stack

- **Backend**: Go with net/http (no framework), GORM for database
- **Frontend**: SvelteKit
- **Database**: MariaDB/MySQL (shared with mail server ISPmail schema)
- **Auth**: JWT-based authentication

## Architecture

- Single binary with embedded frontend (no separate web server needed)
- All database tables use `imc_` prefix to avoid conflicts with ISPmail schema
- Permissions controlled via `imc_users2domains` table
- Admin users have access to all domains; non-admin users only to assigned domains

## Quick Start

### 1. Build

```bash
make build
```

### 2. Create Admin User

```bash
./build/imc-vibe --reset-admin-password
# This creates an admin user or resets the password
# Output: Username: admin, Password: (randomly generated)
```

### 3. Run

```bash
./build/imc-vibe --bind=0.0.0.0 --port=8080
```

### 4. Access

Open `http://your-server:8080` and login with the admin credentials.

## Configuration

### Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `DB_HOST` | Database host | `localhost` |
| `DB_PORT` | Database port | `3306` |
| `DB_USER` | Database user | `mailadmin` |
| `DB_PASSWORD` | Database password | (required) |
| `DB_NAME` | Database name | `mailserver` |
| `BIND` | IP to bind to | `0.0.0.0` |
| `PORT` | Port to listen on | `8080` |
| `JWT_SECRET` | JWT signing secret (min 32 chars) | (required) |

### CLI Flags

```bash
./imc-vibe --help
```

```
  -bind string
        IP address to bind to (default: 0.0.0.0)
  -port string
        Port to listen on (default: 8080)
  -reset-admin-password
        Reset admin password to a random value and exit
```

## Database Tables

The app creates these tables automatically:

- `imc_users` - App admin users
- `imc_login_attempts` - Brute force protection
- `imc_users2domains` - User-domain permissions

Existing ISPmail tables (`virtual_domains`, `virtual_users`, `virtual_aliases`) are used for mail data.

## Systemd Service

Example service file at `/etc/systemd/system/imc-vibe.service`:

```ini
[Unit]
Description=IMC Vibe Mail Admin
After=network.target mariadb.service postfix.service

[Service]
Type=simple
User=root
Environment=DB_HOST=localhost
Environment=DB_PORT=3306
Environment=DB_USER=mailadmin
Environment=DB_PASSWORD=your_password
Environment=DB_NAME=mailserver
Environment=JWT_SECRET=your_secret
Environment=BIND=0.0.0.0
Environment=PORT=8080
ExecStart=/opt/imc-vibe/imc-vibe

[Install]
WantedBy=multi-user.target
```

## Requirements

- Go 1.21+
- Bun (for frontend development)
- MariaDB/MySQL
- Postfix (with postqueue/postsuper)
- systemd-journald (for log viewing)

## Development

```bash
# Build frontend and backend
make build

# Run backend only (uses filesystem frontend)
cd backend && go run ./cmd/server

# Run frontend dev server
cd frontend && bun run dev
```

## License

MIT
