import { NextRequest, NextResponse } from 'next/server' import nodemailer from 'nodemailer' export async function POST(request: NextRequest) { try { const body = await request.json() const { channelName, category, url, notes, email } = body // Validate required fields if (!channelName || !email) { return NextResponse.json( { error: 'Channel name and email are required' }, { status: 400 } ) } // Email validation const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ if (!emailRegex.test(email)) { return NextResponse.json( { error: 'Invalid email format' }, { status: 400 } ) } const smtpHost = process.env.SMTP_HOST const smtpUser = process.env.SMTP_USER const smtpPass = process.env.SMTP_PASS if (!smtpHost || !smtpUser || !smtpPass) { console.error('SMTP credentials not configured') return NextResponse.json( { error: 'Email service not configured' }, { status: 500 } ) } const adminEmail = process.env.ADMIN_EMAIL || 'jeff@jeffemmett.com' const transporter = nodemailer.createTransport({ host: smtpHost, port: Number(process.env.SMTP_PORT) || 587, secure: false, auth: { user: smtpUser, pass: smtpPass }, tls: { rejectUnauthorized: false }, }) await transporter.sendMail({ from: `Jefflix <${smtpUser}>`, to: adminEmail, subject: `[Jefflix] Channel Request: ${escapeHtml(channelName)}`, html: `

New Channel Request

Someone has requested a new Live TV channel:

Channel: ${escapeHtml(channelName)}
Category: ${escapeHtml(category || 'Not specified')}
URL/Link: ${url ? `${escapeHtml(url)}` : 'Not provided'}
Notes: ${escapeHtml(notes || 'None')}
Email: ${escapeHtml(email)}
Requested: ${new Date().toLocaleString()}

To add this channel:

  1. Check iptv-org for an existing stream
  2. If found, add to Threadfin channel list and map the stream
  3. If not in iptv-org, add as a custom M3U source in Threadfin
  4. Reply to ${escapeHtml(email)} to let them know it's been added

This is an automated message from Jefflix.

`, }) return NextResponse.json({ success: true }) } catch (error) { console.error('Channel request error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } } function escapeHtml(text: string): string { const map: Record = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', } return text.replace(/[&<>"']/g, (char) => map[char]) }