<s> from O365 . attachment import Attachment <EOL> from O365 . contact import Contact <EOL> from O365 . group import Group <EOL> import logging <EOL> import json <EOL> import requests <EOL> logging . basicConfig ( filename = '<STR_LIT>' , level = logging . DEBUG ) <EOL> log = logging . getLogger ( __name__ ) <EOL> class Message ( object ) : <EOL> '''<STR_LIT>''' <EOL> att_url = '<STR_LIT>' <EOL> send_url = '<STR_LIT>' <EOL> draft_url = '<STR_LIT>' <EOL> update_url = '<STR_LIT>' <EOL> def __init__ ( self , json = None , auth = None ) : <EOL> '''<STR_LIT>''' <EOL> if json : <EOL> self . json = json <EOL> self . hasAttachments = json [ '<STR_LIT>' ] <EOL> else : <EOL> self . json = { '<STR_LIT>' : { '<STR_LIT>' : { } } , '<STR_LIT>' : { } } <EOL> self . hasAttachments = False <EOL> self . auth = auth <EOL> self . attachments = [ ] <EOL> self . reciever = None <EOL> def fetchAttachments ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not self . hasAttachments : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return False <EOL> response = requests . get ( self . att_url . format ( self . json [ '<STR_LIT>' ] ) , auth = self . auth ) <EOL> log . info ( '<STR_LIT>' , str ( response ) ) <EOL> json = response . json ( ) <EOL> for att in json [ '<STR_LIT:value>' ] : <EOL> try : <EOL> self . attachments . append ( Attachment ( att ) ) <EOL> log . debug ( '<STR_LIT>' , self . auth [ <NUM_LIT:0> ] ) <EOL> except Exception as e : <EOL> log . info ( '<STR_LIT>' , self . auth [ <NUM_LIT:0> ] ) <EOL> return len ( self . attachments ) <EOL> def sendMessage ( self ) : <EOL> '''<STR_LIT>''' <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> try : <EOL> data = { '<STR_LIT>' : { '<STR_LIT>' : { } } } <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . json [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = self . json [ '<STR_LIT>' ] <EOL> data [ '<STR_LIT>' ] [ '<STR_LIT>' ] = [ att . json for att in self . attachments ] <EOL> data [ '<STR_LIT>' ] = "<STR_LIT:false>" <EOL> data = json . dumps ( data ) <EOL> log . debug ( str ( data ) ) <EOL> except Exception as e : <EOL> log . error ( str ( e ) ) <EOL> return False <EOL> response = requests . post ( self . send_url , data , headers = headers , auth = self . auth ) <EOL> log . debug ( '<STR_LIT>' + str ( response ) ) <EOL> if response . status_code != <NUM_LIT> : <EOL> return False <EOL> return True <EOL> def markAsRead ( self ) : <EOL> '''<STR_LIT>''' <EOL> read = '<STR_LIT>' <EOL> headers = { '<STR_LIT>' : '<STR_LIT:application/json>' , '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> try : <EOL> response = requests . patch ( self . update_url . format ( self . json [ '<STR_LIT>' ] ) , read , headers = headers , auth = self . auth ) <EOL> except : <EOL> return False <EOL> return True <EOL> def getSender ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . json [ '<STR_LIT>' ] <EOL> def getSenderEmail ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def getSenderName ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT:Name>' ] <EOL> except : <EOL> return '<STR_LIT>' <EOL> def getSubject ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . json [ '<STR_LIT>' ] <EOL> def getBody ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> def setRecipients ( self , val ) : <EOL> '''<STR_LIT>''' <EOL> self . json [ '<STR_LIT>' ] = [ ] <EOL> if isinstance ( val , list ) : <EOL> for con in val : <EOL> if isinstance ( con , Contact ) : <EOL> self . addRecipient ( con ) <EOL> elif isinstance ( con , str ) : <EOL> if '<STR_LIT:@>' in con : <EOL> self . addRecipient ( con ) <EOL> elif isinstance ( con , dict ) : <EOL> self . json [ '<STR_LIT>' ] . append ( con ) <EOL> elif isinstance ( val , dict ) : <EOL> self . json [ '<STR_LIT>' ] = [ val ] <EOL> elif isinstance ( val , str ) : <EOL> if '<STR_LIT:@>' in val : <EOL> self . addRecipient ( val ) <EOL> elif isinstance ( val , Contact ) : <EOL> self . addRecipient ( val ) <EOL> elif isinstance ( val , Group ) : <EOL> for person in val : <EOL> self . addRecipient ( person ) <EOL> else : <EOL> return False <EOL> return True <EOL> def addRecipient ( self , address , name = None ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( address , Contact ) : <EOL> self . json [ '<STR_LIT>' ] . append ( address . getFirstEmailAddress ( ) ) <EOL> elif isinstance ( address , Group ) : <EOL> for con in address . contacts : <EOL> self . json [ '<STR_LIT>' ] . append ( address . getFirstEmailAddress ( ) ) <EOL> else : <EOL> if name is None : <EOL> name = address [ : address . index ( '<STR_LIT:@>' ) ] <EOL> self . json [ '<STR_LIT>' ] . append ( { '<STR_LIT>' : { '<STR_LIT>' : address , '<STR_LIT:Name>' : name } } ) <EOL> def setSubject ( self , val ) : <EOL> '''<STR_LIT>''' <EOL> self . json [ '<STR_LIT>' ] = val <EOL> def setBody ( self , val ) : <EOL> '''<STR_LIT>''' <EOL> cont = False <EOL> while not cont : <EOL> try : <EOL> self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] = val <EOL> self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> cont = True <EOL> except : <EOL> self . json [ '<STR_LIT>' ] = { } <EOL> def setBodyHTML ( self , val = None ) : <EOL> '''<STR_LIT>''' <EOL> self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if val : <EOL> self . json [ '<STR_LIT>' ] [ '<STR_LIT>' ] = val </s>
