#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of VilfredoReloadedCore.
#
# Copyright © 2009-2013 Pietro Speroni di Fenizio / Derek Paterson.
#
# VilfredoReloadedCore is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation version 3 of the License.
#
# VilfredoReloadedCore is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License
# for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with VilfredoReloadedCore.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################


'''
Emails
'''

from . import app
from email.mime.text import MIMEText
from subprocess import Popen, PIPE

def send_email(subject, sender_email, recipient_email, text_body):
    msg = MIMEText(text_body)
    msg["From"] = sender_email
    msg["To"] = recipient_email
    msg["Subject"] = subject
    p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
    p.communicate(msg.as_string())

def email_question_email_invite(sender, recipient_email, question):
    # print "Sending email:", sender.username, question.title
    send_email("Vilfredo - Invitation to participate",
               'admin@vilfredo.org',
               recipient_email,
               "User %s invites you to participate in question %s"
               % (sender.username, question.title))

def email_question_invite(sender, recipient, question):
    # print "Sending email:", sender.username, question.title
    send_email("Vilfredo - Invitation to participate",
               'admin@vilfredo.org',
               recipient,
               "User %s invites you to participate in question %s"
               % (sender.username, question.title))

@@ -1256,37 +1256,36 @@
 _string())%0A%0Adef 
-email
+send
 _question_email_
@@ -1286,24 +1286,30 @@
 email_invite
+_email
 (sender, rec
@@ -1322,32 +1322,39 @@
 _email, question
+, token
 ):%0A    # print %22
@@ -1410,194 +1410,544 @@
     
-send_email(%22Vilfredo - Invitation to participate%22,%0A    
+body_template = %5C%0A    %22%22%22%0A    Vilfredo user %25s invites you to participate in the question %22%25s%22.%0A
     
+%0A
     
-   'admin@vilfredo.org',%0A         
+If you are already a member of Vilfredo please sign in then click on the Join Question link below.%0A
     
+%0A 
   
-recipient_email,%0A       
+ If you are not yet signed up then please go here http://%25s and create an account, then once you have logged in click on the %0A    Join Question link below.%0A
     
+%0A
     
-%22User %25s invites you to participate in question %25s%22
+Join Question: http://%25s%0A    %22%22%22%0A    send_email(%22Vilfredo - Invitation to participate%22,%0A               app.config%5B'ADMINS'%5D%5B0%5D,%0A               recipient_email,
 %0A   
@@ -1950,32 +1950,46 @@
 %0A               
+body_template 
 %25 (sender.userna
@@ -2006,24 +2006,164 @@
 on.title
+, %0A                                app.config%5B'SITE_DOMAIN'%5D,%0A                                app.config%5B'SITE_DOMAIN'%5D+'/invitation/'+token
 ))%0A%0Adef 
 email_qu
@@ -2154,21 +2154,20 @@
 ))%0A%0Adef 
-email
+send
 _questio
@@ -2174,16 +2174,22 @@
 n_invite
+_email
 (sender,
@@ -2269,24 +2269,143 @@
 stion.title%0A
+    body_template = %5C%0A    %22%22%22%0A    Hi %25s, Vilfredo user %25s has invited you to participate in the question %22%25s%22.%0A    %22%22%22%0A
     send_ema
@@ -2529,78 +2529,45 @@
     
-%22User %25s invites you to participate in question %25s%22%0A               %25 (
+body_template %25 (recipient.username, 
 send

Generate invitation emails.