# Path: simone/models.py
# class ImapServer(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     address = models.CharField(max_length=255,null=True)
#     port = models.IntegerField(default=143,null=True)
#     username = models.CharField(max_length=255,null=True)
#     passwd = models.CharField(max_length=255,null=True)
#     ssl = models.BooleanField(default=False)
#     def __str__(self):
#         if not self.address and not self.port and not self.username and not self.passwd:
#             return 'None'
#         if self.ssl:
#             return '%s@imaps://%s:%s' % (self.username, self.address, self.port)
#         return '%s@imap://%s:%s' % (self.username, self.address, self.port)
# 
# class SmtpServer(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     address = models.CharField(max_length=255,null=True)
#     port = models.IntegerField(default=25,null=True)
#     username = models.CharField(max_length=255,null=True)
#     passwd = models.CharField("Password", max_length=255,null=True)
#     def __str__(self):
#         if not self.address and not self.port and not self.username and not self.passwd:
#             return 'None'
#         return '%s@smtp://%s:%s' % (self.username, self.address, self.port)
# 
# Path: simone/forms.py
# class ImapServerForm(forms.ModelForm):
#     class Meta:
#         model = ImapServer
#         exclude = ['user']
#         widgets = {
#             'passwd': forms.PasswordInput(),
#         }
# 
# class SmtpServerForm(forms.ModelForm):
#     class Meta:
#         model = SmtpServer
#         exclude = ['user']
#         widgets = {
#             'passwd': forms.PasswordInput(),
#         }


from django.views.generic import (
    CreateView,
    DeleteView,
    DetailView,
    UpdateView,
    ListView
)
from .models import (
	ImapServer,
	SmtpServer,
)
from .forms import (
    ImapServerForm,
    SmtpServerForm,
)
from pprint import pprint
from django.shortcuts import render, render_to_response, HttpResponse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.urls import reverse
    from django.core.mail import send_mail, BadHeaderError#, EmailMultiAlternatives
import codecs
import email
import json
import smtplib
import sys
    import imapclient
    import imaplib
    import ssl
        body = mailmsg.get_payload(decode=True)
    else:
        for part in mailmsg.walk():
            if(part.get_content_type() == 'text/plain'):
                body = part.get_payload(decode=True)
                break # FIXME this assumes the first text/plain part is what we want
    if body == "":
        body = "There is no plain text version of this message."
        #body = body + "Raw Source:<br />" + mailmsg

    i.logout()

    if request.GET.get('json') == "true":
        return HttpResponse(json.dumps({'headers':mailmsg.items(), 'body':body}))

    data = {
        'mailmsg':mailmsg,
        'body':body,
        'folder': folder,
        'uid': uid,
        'server': server,
    }
    return render(request, 'mail/viewmsg.html', data)

@login_required
def prefs(request):
    """preferences dialog

    :param request: request object from Django
    """

# Path: simone/models.py
# class ImapServer(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     address = models.CharField(max_length=255,null=True)
#     port = models.IntegerField(default=143,null=True)
#     username = models.CharField(max_length=255,null=True)
#     passwd = models.CharField(max_length=255,null=True)
#     ssl = models.BooleanField(default=False)
#     def __str__(self):
#         if not self.address and not self.port and not self.username and not self.passwd:
#             return 'None'
#         if self.ssl:
#             return '%s@imaps://%s:%s' % (self.username, self.address, self.port)
#         return '%s@imap://%s:%s' % (self.username, self.address, self.port)
# 
# class SmtpServer(models.Model):
#     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
#     address = models.CharField(max_length=255,null=True)
#     port = models.IntegerField(default=25,null=True)
#     username = models.CharField(max_length=255,null=True)
#     passwd = models.CharField("Password", max_length=255,null=True)
#     def __str__(self):
#         if not self.address and not self.port and not self.username and not self.passwd:
#             return 'None'
#         return '%s@smtp://%s:%s' % (self.username, self.address, self.port)
# 
# Path: simone/forms.py
# class ImapServerForm(forms.ModelForm):
#     class Meta:
#         model = ImapServer
#         exclude = ['user']
#         widgets = {
#             'passwd': forms.PasswordInput(),
#         }
# 
# class SmtpServerForm(forms.ModelForm):
#     class Meta:
#         model = SmtpServer
#         exclude = ['user']
#         widgets = {
#             'passwd': forms.PasswordInput(),
#         }


# Path: simone/views.py
from django.views.generic import (
    CreateView,
    DeleteView,
    DetailView,
    UpdateView,
    ListView
)
from .models import (
	ImapServer,
	SmtpServer,
)
from .forms import (
    ImapServerForm,
    SmtpServerForm,
)
from pprint import pprint
from django.shortcuts import render, render_to_response, HttpResponse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect
from django.template import RequestContext
from django.urls import reverse
    from django.core.mail import send_mail, BadHeaderError#, EmailMultiAlternatives
import codecs
import email
import json
import smtplib
import sys
    import imapclient
    import imaplib
    import ssl

        body = mailmsg.get_payload(decode=True)
    else:
        for part in mailmsg.walk():
            if(part.get_content_type() == 'text/plain'):
                body = part.get_payload(decode=True)
                break # FIXME this assumes the first text/plain part is what we want
    if body == "":
        body = "There is no plain text version of this message."
        #body = body + "Raw Source:<br />" + mailmsg

    i.logout()

    if request.GET.get('json') == "true":
        return HttpResponse(json.dumps({'headers':mailmsg.items(), 'body':body}))

    data = {
        'mailmsg':mailmsg,
        'body':body,
        'folder': folder,
        'uid': uid,
        'server': server,
    }
    return render(request, 'mail/viewmsg.html', data)

@login_required
def prefs(request):
    """preferences dialog

    :param request: request object from Django
    """

    smtpform = SmtpServerForm(initial={'address':'localhost', 'port':'25'})