#!/usr/bin/python3
# -*- coding: latin-1 -*-

import cgi, os, re, smtplib, ssl
from datetime import datetime

smtp_server = "localhost"
smtp_port = 1587  # For SSL
sender_email = "site@rallumons-les-etoiles.eu"
sender_password = "Communication_avec_le_site"
receiver_email = "david@rallumons-les-etoiles.eu"

print("content-type: text/html\n\n")

msg = {
    "de" : "Nachricht erhalten, wir melden uns umgehend bei Ihnen.",
    "en" : "Message received, we will contact you shortly.",
    "es" : "Mensaje recibido, en breve nos pondremos en contacto con usted.",
    "fr" : "Message reçu, nous vous contactons rapidement.",
    "it" : "Messaggio ricevuto, vi contatteremo a breve.",
    "ro" : "Mesaj primit, vă vom contacta în scurt timp.",
    "ru" : "Сообщение получено, мы свяжемся с вами в ближайшее время.",
    "sv" : "Meddelande mottaget, vi kommer att kontakta dig inom kort.",
    }
#get data
formulaire = cgi.FieldStorage()

N = format(formulaire.getvalue("nom"))
E = format(formulaire.getvalue("email"))
P = format(formulaire.getvalue("pays"))
V = format(formulaire.getvalue("ville"))
T = format(formulaire.getvalue("tel"))

now = datetime.today().strftime('%Y%m%d %H:%M:%S')
ref = os.environ.get('HTTP_REFERER')
# https://rallumons-les-etoiles.eu/fr/rejoindre.html

#C = re.fullmatch('https:\/\/rallumons-les-etoiles\.eu\/\w\w\/rejoindre\.html',ref)
C = re.search('\/[defirs][ensrtouv]\/',ref)
if hasattr(C, 'group'):
 C = format(C.group(0)).replace('/','')
else:
 C = 'none'

if C in msg:
  print("<p>" + msg[C] + "</p>")
  texte = now + " / " + N + " / " + E + " / " + P + " / " + V + " / " + T + " / " + C
  with open('/media/localbackup/web/rle-demandes.txt', 'a') as fichier:
    fichier.write(texte +"\n")
  texte = "Date de la demande : " + now + "\nNom   : " + N + "\nEmail : " + E + "\nVille : " + V + "\nPays  : " + P + "\nTel   : " + T +"\n"
  message = """Subject: Rejoindre RLE - demande sur le site
From: """ + E +"""\


  
""" + texte + """\
souhaite rejoindre le collectif.\n
Demande faite depuis le site en langue """ + C 
  context = ssl._create_unverified_context()
  with smtplib.SMTP(smtp_server, smtp_port) as server:
      server.ehlo()  # Can be omitted
      server.starttls(context=context)
      server.ehlo()  # Can be omitted
      server.login(sender_email, sender_password)
      server.sendmail(sender_email, receiver_email, message)

            sender_email = "contact@rallumons-les-etoiles.eu"
      receiver_email = E

      message = """Bonjour,
      Merci de nous avoir contacté.
      Vous pouvez nous rejoindre dans nos discussions ici :
      https://rallumons-les-etoiles.eu/fr/a-propos.html
      Bonne journée !
      Rallumons les étoiles
        """

      server.sendmail(sender_email, receiver_email, message)
else:
  print("<p>ok.</p>")

