def sara_debug(string, eobj=None): if eobj is not None: body = mail.get_body(eobj) if body is not None: logger.debug('[SARA]:: From:%s To:%s Subject:%s Body:%s::%s' %(eobj['from'],eobj['to'], eobj['subject'], EmailReplyParser.parse_reply(body), string)) else: logger.debug('[SARA]:: From:%s To:%s Subject:%s ::%s' %(eobj['from'],eobj['to'], eobj['subject'], string)) else: logger.debug('[SARA]::%s'%(string))
def sara_debug(string, eobj=None): if eobj is not None: body = mail.get_body(eobj) if body is not None: logger.debug('[SARA]:: From:%s To:%s Subject:%s Body:%s::%s' % (eobj['from'], eobj['to'], eobj['subject'], EmailReplyParser.parse_reply(body), string)) else: logger.debug('[SARA]:: From:%s To:%s Subject:%s ::%s' % (eobj['from'], eobj['to'], eobj['subject'], string)) else: logger.debug('[SARA]::%s' % (string))
def sara_handle(message_id, thread_id): sara_debug('[HANDLE]: message_id=%s, thread_id=%s' % (message_id, thread_id)) message = google.GetMessage(gm, 'me', message_id) raw_email = base64.urlsafe_b64decode(message['raw'].encode("utf-8")) eobj = email.message_from_string(raw_email) header = eobj['headers'] from_addr = mail.strip(eobj['from']) # is a list to_addrs = mail.strip(eobj['to']) # is a list sub = eobj['subject'] if sub is None or sub == '': sara_debug('Subject missing', eobj) if mail.get_body(eobj) is None: sara_debug('No body present', eobj) if eobj['cc'] is None or eobj['cc'] == '': sara_debug('No CC') cc_addrs = [] else: cc_addrs = mail.strip(eobj['cc']) # all to+cc addresses without sara to_plus_cc_addrs = [addr for addr in (to_addrs + cc_addrs) if addr != SARA] # --------------------------------------------------------------------- # RFC 2822 # https://tools.ietf.org/html/rfc2822#section-3.6.4 # --------------------------------------------------------------------- references = eobj['References'] if references is not None: raw_id = references.split(' ')[0] else: raw_id = eobj.get('Message-ID') prev_elist, prev_mlist = [], [] record = db.find_one({'thread_id': thread_id}) if record is not None: sara_debug('Existing thread', eobj) prev_elist = record['elist'] prev_mlist = record['mlist'] # anyone in the To/CC field apart from busy person # and Sara is the free person # TODO: Also remove other busy users who are also using Sara # dont update if we recieve the same message # will happen when a history push notification was not # acknowledged if message_id not in record['mlist']: prev_elist.append(eobj.as_string()) prev_mlist.append(message_id) else: sara_debug('Existing message', eobj) # return fulist = record['fu'] bu = record['bu'] for addr in to_plus_cc_addrs: if (addr not in fulist) and (addr != bu): fulist.append(addr) else: sara_debug('New thread/message', eobj) # who ever first sent a mail including Sara is the Busy person bu = from_addr[0] fulist = [] for addr in to_plus_cc_addrs: fulist.append(addr) prev_elist.append(eobj.as_string()) prev_mlist.append(message_id) # update database res = db.update({'thread_id': thread_id}, { 'thread_id': thread_id, 'elist': prev_elist, 'mlist': prev_mlist, 'bu': bu, 'fu': fulist, }, upsert=True) # do nothing for loopback messages if from_addr[0] != SARA: receive(from_addr[0], to_plus_cc_addrs, eobj, thread_id, fulist, bu) else: sara_debug('BCC message', eobj)
def receive(from_addr, to_plus_cc_addrs, current_email, thread_id, fulist, bu): try: # the following is simply to fix the state among all free users. If a new # free guy is added by the busy guy, please take the most recent state # among the free guys and add him to that. <-- this adding a free guy in the # middle has not been included in the below snippet within the if statement if from_addr in fulist: audience = [addr for addr in to_plus_cc_addrs if addr != bu] if audience and audience != fulist: adding_others_reply(fulist, current_email, thread_id) to_plus_cc_addrs = fulist person_list = [] for item in to_plus_cc_addrs: person_list.append({ 'email': item, 'first_name': address_dict[item][0] }) from_entry = { 'email': from_addr, 'first_name': address_dict[from_addr][0] } input_obj = { 'email': { 'from': from_entry, 'to': person_list, 'body': EmailReplyParser.parse_reply(mail.get_body(current_email)), }, 'availability': { 'dt': get_free_slots(bu), # list of tuples of dt objects 'loc': 'Location', } } sara_debug('INPUTTT' + input_obj.__str__()) # dsobj = ds(thread_id) # if tid is None ds will pass a brand new object dsobj = gds(thread_id) output_obj = dsobj.take_turn(input_obj) sara_debug('OUTPUTTT ' + output_obj.__str__()) if output_obj is not None: for each_email in output_obj['emails']: to_addrs = list(each_email['to']) sara_debug("INPUTTTTTTTT" + ','.join(to_addrs)) to_addrs = [address_dict[item][1] for item in to_addrs] body = each_email['body'] send(to_addrs, body, thread_id) if output_obj['meeting'] is not None: send_invite(SARA_F, list(output_obj['meeting']['to']), output_obj['meeting']['loc'], output_obj['meeting']['dt']['start'], output_obj['meeting']['dt']['end']) sara_debug("Finished receiving...Returning") return 'success' except Exception as e: print e print sys.exc_info()[0] sara_debug("Failed receiving...Returning") return 'Failure'
def sara_handle(message_id, thread_id): sara_debug('[HANDLE]: message_id=%s, thread_id=%s'%(message_id, thread_id)) message = google.GetMessage(gm, 'me', message_id) raw_email = base64.urlsafe_b64decode(message['raw'].encode("utf-8")) eobj = email.message_from_string(raw_email) header = eobj['headers'] from_addr = mail.strip(eobj['from']) # is a list to_addrs = mail.strip(eobj['to']) # is a list sub = eobj['subject'] if sub is None or sub == '': sara_debug('Subject missing', eobj) if mail.get_body(eobj) is None: sara_debug('No body present', eobj) if eobj['cc'] is None or eobj['cc'] == '': sara_debug('No CC') cc_addrs = [] else: cc_addrs = mail.strip(eobj['cc']) # all to+cc addresses without sara to_plus_cc_addrs = [addr for addr in (to_addrs + cc_addrs) if addr != SARA] # --------------------------------------------------------------------- # RFC 2822 # https://tools.ietf.org/html/rfc2822#section-3.6.4 # --------------------------------------------------------------------- references = eobj['References'] if references is not None: raw_id = references.split(' ')[0] else: raw_id = eobj.get('Message-ID') prev_elist, prev_mlist = [], [] record = db.find_one({'thread_id': thread_id}) if record is not None: sara_debug('Existing thread', eobj) prev_elist = record['elist'] prev_mlist = record['mlist'] # anyone in the To/CC field apart from busy person # and Sara is the free person # TODO: Also remove other busy users who are also using Sara # dont update if we recieve the same message # will happen when a history push notification was not # acknowledged if message_id not in record['mlist']: prev_elist.append(eobj.as_string()) prev_mlist.append(message_id) else: sara_debug('Existing message', eobj) # return fulist = record['fu'] bu = record['bu'] for addr in to_plus_cc_addrs: if (addr not in fulist) and (addr != bu): fulist.append(addr) else: sara_debug('New thread/message', eobj) # who ever first sent a mail including Sara is the Busy person bu = from_addr[0] fulist = [] for addr in to_plus_cc_addrs: fulist.append(addr) prev_elist.append(eobj.as_string()) prev_mlist.append(message_id) # update database res = db.update( {'thread_id': thread_id}, { 'thread_id': thread_id, 'elist': prev_elist, 'mlist': prev_mlist, 'bu': bu, 'fu': fulist, }, upsert = True ) # do nothing for loopback messages if from_addr[0] != SARA: receive(from_addr[0], to_plus_cc_addrs, eobj, thread_id, fulist, bu) else: sara_debug('BCC message', eobj)
def receive(from_addr, to_plus_cc_addrs, current_email, thread_id, fulist, bu): try: # the following is simply to fix the state among all free users. If a new # free guy is added by the busy guy, please take the most recent state # among the free guys and add him to that. <-- this adding a free guy in the # middle has not been included in the below snippet within the if statement if from_addr in fulist: audience = [addr for addr in to_plus_cc_addrs if addr != bu] if audience and audience != fulist: adding_others_reply(fulist, current_email, thread_id) to_plus_cc_addrs = fulist person_list = [] for item in to_plus_cc_addrs: person_list.append({ 'email': item, 'first_name': address_dict[item][0] } ) from_entry = { 'email': from_addr, 'first_name': address_dict[from_addr][0] } input_obj = { 'email': { 'from': from_entry, 'to': person_list, 'body': EmailReplyParser.parse_reply(mail.get_body(current_email)), }, 'availability': { 'dt': get_free_slots(bu), # list of tuples of dt objects 'loc': 'Location', } } sara_debug('INPUTTT'+input_obj.__str__()) # dsobj = ds(thread_id) # if tid is None ds will pass a brand new object dsobj = gds(thread_id) output_obj = dsobj.take_turn(input_obj) sara_debug('OUTPUTTT '+output_obj.__str__()) if output_obj is not None: for each_email in output_obj['emails']: to_addrs = list(each_email['to']) sara_debug("INPUTTTTTTTT"+','.join(to_addrs)) to_addrs = [address_dict[item][1] for item in to_addrs] body = each_email['body'] send(to_addrs, body, thread_id) if output_obj['meeting'] is not None: send_invite(SARA_F, list(output_obj['meeting']['to']), output_obj['meeting']['loc'], output_obj['meeting']['dt']['start'], output_obj['meeting']['dt']['end']) sara_debug("Finished receiving...Returning") return 'success' except Exception as e: print e print sys.exc_info()[0] sara_debug("Failed receiving...Returning") return 'Failure'