'''
Created on June 28, 2013

__author__ = "Elizabeth 'pidge' Flanagan"
__copyright__ = "Copyright 2012-2013, Intel Corp."
__credits__ = ["Elizabeth Flanagan"]
__license__ = "GPL"
__version__ = "2.0"
__maintainer__ = "Elizabeth Flanagan"
__email__ = "elizabeth.flanagan@intel.com"
'''
from buildbot.status.mail import MailNotifier
from buildbot.status.mail import *
from buildbot import interfaces, util, config
from buildbot.process.users import users
from buildbot.status import base
from buildbot.status.results import FAILURE, SUCCESS, WARNINGS, EXCEPTION, Results
from twisted.python import log

class YoctoMailNotifier(MailNotifier):
    def __init__(self, fromaddr, mode=("failing"),
                 categories=None, branches=[], yoctorepos=[], builders=None, addLogs=False,
                 relayhost="localhost", buildSetSummary=False,
                 subject="buildbot %(result)s in %(title)s on %(builder)s",
                 lookup=None, extraRecipients=[],
                 sendToInterestedUsers=True, customMesg=None,
                 messageFormatter=defaultMessage, extraHeaders=None,
                 addPatch=True, useTls=False, 
                 smtpUser=None, smtpPassword=None, smtpPort=25):
        self.fromaddr = fromaddr
        self.mode=mode
        self.categories = categories
        self.branches = branches
        self.yoctorepos = yoctorepos
        self.builders = builders
        self.addLogs = addLogs
        self.relayhost = relayhost
        self.buildSetSummary = buildSetSummary
        self.subject = subject
        self.lookup = lookup
        self.extraRecipients = extraRecipients
        self.sendToInterestedUsers = sendToInterestedUsers
        self.customMesg = customMesg
        self.messageFormatter = messageFormatter
        self.extraHeaders = extraHeaders
        self.addPatch = addPatch
        self.useTls = useTls
        self.smtpUser = smtpUser
        self.smtpPassword = smtpPassword
        self.smtpPort = smtpPort
        MailNotifier.__init__(self, fromaddr, mode=self.mode,
                categories = self.categories, builders = self.builders,
                addLogs = self.addLogs, relayhost = self.relayhost,
                buildSetSummary = self.buildSetSummary,
                subject = self.subject,
                lookup = self.lookup,
                extraRecipients = self.extraRecipients,
                sendToInterestedUsers = self.sendToInterestedUsers,
                customMesg = self.customMesg, 
                messageFormatter = self.messageFormatter,
                extraHeaders = self.extraHeaders, addPatch = self.addPatch,
                useTls = self.useTls, smtpUser = self.smtpUser,
                smtpPassword = self.smtpPassword, smtpPort = self.smtpPort)

    def isMailNeeded(self, build, results):
        # here is where we actually do something.
        builder = build.getBuilder()
        repo=build.getProperty("repository")
        branch=build.getProperty("branch")
        log.msg(repo)
        log.msg(branch)

        if self.builders is not None and builder.name not in self.builders:
            return False # ignore this build
        if self.categories is not None and \
               builder.category not in self.categories:
            return False # ignore this build

        if self.yoctorepos is not None and repo not in self.yoctorepos:
            return False # ignore this build
        elif self.yoctorepos is not None and repo in self.yoctorepos and self.branches is None:
            return True
        elif self.yoctorepos is None and self.branches is not None and branch in self.branches:
            return False
        else:
            return True


        prev = build.getPreviousBuild()
        if "change" in self.mode:
            if prev and prev.getResults() != results:
                return True
        if "failing" in self.mode and results == FAILURE:
            return True
        if "passing" in self.mode and results == SUCCESS:
            return True
        if "problem" in self.mode and results == FAILURE:
            if prev and prev.getResults() != FAILURE:
                return True
        if "warnings" in self.mode and results == WARNINGS:
            return True
        if "exception" in self.mode and results == EXCEPTION:
            return True
        return False


@@ -3026,16 +3026,39 @@
 branch)%0A
+        buildme = False
 %0A       
@@ -3439,128 +3439,8 @@
 ild%0A
-        elif self.yoctorepos is not None and repo in self.yoctorepos and self.branches is None:%0A            return True%0A
     
@@ -3570,38 +3570,188 @@
   el
-se:%0A            return True%0A%0A%0A
+if self.yoctorepos is not None and repo in self.yoctorepos and self.branches is None:%0A            buildme = True%0A        else:%0A%09    buildme = True%0A%0A        if buildme is True:%0A    
     
@@ -3790,24 +3790,28 @@
 d()%0A        
+    
 if %22change%22 
@@ -3828,32 +3828,36 @@
 de:%0A            
+   
+ 
 if prev and prev
@@ -3890,32 +3890,36 @@
                 
+    
 return True%0A    
@@ -3918,24 +3918,28 @@
 rue%0A        
+  
+  
 if %22failing%22
@@ -3980,32 +3980,36 @@
 RE:%0A            
+    
 return True%0A    
@@ -4008,24 +4008,28 @@
 rue%0A        
+ 
+   
 if %22passing%22
@@ -4066,16 +4066,20 @@
 UCCESS:%0A
+    
         
@@ -4098,24 +4098,28 @@
 rue%0A        
+    
 if %22problem%22
@@ -4160,32 +4160,36 @@
 RE:%0A            
+  
+  
 if prev and prev
@@ -4222,32 +4222,36 @@
                 
+    
 return True%0A    
@@ -4250,24 +4250,28 @@
 rue%0A        
+ 
+   
 if %22warnings
@@ -4310,16 +4310,20 @@
 RNINGS:%0A
+    
         
@@ -4342,24 +4342,28 @@
 rue%0A        
+    
 if %22exceptio
@@ -4408,32 +4408,36 @@
 ON:%0A            
+    
 return True%0A    
@@ -4424,32 +4424,36 @@
     return True%0A
+    
         return F
@@ -4457,9 +4457,8 @@
 n False%0A
-%0A

Fix disappearing mails.