def _connect(self) -> None: # TODO - G.M - 2017-11-15 Verify connection/disconnection # Are old connexion properly close this way ? if self._connection: logger.debug(self, 'Disconnect from IMAP') self._disconnect() # TODO - G.M - 2017-11-23 Support for predefined SSLContext ? # without ssl_context param, tracim use default security configuration # which is great in most case. if self.use_ssl: logger.debug( self, 'Connect IMAP {}:{} using SSL'.format( self.host, self.port, )) self._connection = imaplib.IMAP4_SSL(self.host, self.port) else: logger.debug(self, 'Connect IMAP {}:{}'.format( self.host, self.port, )) self._connection = imaplib.IMAP4(self.host, self.port) try: logger.debug(self, 'Login IMAP with login {}'.format(self.user, )) self._connection.login(self.user, self.password) except Exception as e: log = 'Error during execution: {}' logger.error(self, log.format(e.__str__()), exc_info=1)
def post(self, label, parent_id=None, can_contain_folders=False, can_contain_threads=False, can_contain_files=False, can_contain_pages=False): # TODO - SECURE THIS workspace = tmpl_context.workspace api = ContentApi(tmpl_context.current_user) redirect_url_tmpl = '/workspaces/{}/folders/{}' redirect_url = '' try: parent = None if parent_id: parent = api.get_one(int(parent_id), ContentType.Folder, workspace) folder = api.create(ContentType.Folder, workspace, parent, label) subcontent = dict( folder=True if can_contain_folders == 'on' else False, thread=True if can_contain_threads == 'on' else False, file=True if can_contain_files == 'on' else False, page=True if can_contain_pages == 'on' else False) api.set_allowed_content(folder, subcontent) api.save(folder) tg.flash(_('Folder created'), CST.STATUS_OK) redirect_url = redirect_url_tmpl.format(tmpl_context.workspace_id, folder.content_id) except Exception as e: logger.error( self, 'An unexpected exception has been catched. Look at the traceback below.' ) traceback.print_exc() tb = sys.exc_info()[2] tg.flash( _('Folder not created: {}').format(e.with_traceback(tb)), CST.STATUS_ERROR) if parent_id: redirect_url = redirect_url_tmpl.format( tmpl_context.workspace_id, parent_id) else: redirect_url = '/workspaces/{}'.format( tmpl_context.workspace_id) #### # # INFO - D.A. - 2014-10-22 - Do not put redirect in a # try/except block as redirect is using exceptions! # tg.redirect(tg.url(redirect_url))
def post(self, label, parent_id=None, can_contain_folders=False, can_contain_threads=False, can_contain_files=False, can_contain_pages=False): # TODO - SECURE THIS workspace = tmpl_context.workspace api = ContentApi(tmpl_context.current_user) redirect_url_tmpl = '/workspaces/{}/folders/{}' redirect_url = '' try: parent = None if parent_id: parent = api.get_one(int(parent_id), ContentType.Folder, workspace) folder = api.create(ContentType.Folder, workspace, parent, label) subcontent = dict( folder = True if can_contain_folders=='on' else False, thread = True if can_contain_threads=='on' else False, file = True if can_contain_files=='on' else False, page = True if can_contain_pages=='on' else False ) api.set_allowed_content(folder, subcontent) api.save(folder) tg.flash(_('Folder created'), CST.STATUS_OK) redirect_url = redirect_url_tmpl.format(tmpl_context.workspace_id, folder.content_id) except Exception as e: logger.error(self, 'An unexpected exception has been catched. Look at the traceback below.') traceback.print_exc() tg.flash(_('Folder not created: {}').format(e.with_traceback()), CST.STATUS_ERROR) if parent_id: redirect_url = redirect_url_tmpl.format(tmpl_context.workspace_id, parent_id) else: redirect_url = '/workspaces/{}'.format(tmpl_context.workspace_id) #### # # INFO - D.A. - 2014-10-22 - Do not put redirect in a # try/except block as redirect is using exceptions! # tg.redirect(tg.url(redirect_url))
def _unset_flag( self, uid: int, flag: str, ) -> None: assert uid is not None rv, data = self._connection.store( uid, '-FLAGS', flag, ) if rv == 'OK': log = 'Message {uid} unset as {flag}.'.format(uid=uid, flag=flag) logger.debug(self, log) else: log = 'Can not unset Message {uid} as {flag} : {rv}'.format( uid=uid, flag=flag, rv=rv) logger.error(self, log)
def _notify_tracim( self, mails: typing.List[DecodedMail], ) -> None: """ Send http request to tracim endpoint :param mails: list of mails to send :return: unsended mails """ logger.debug( self, 'Notify tracim about {} new responses'.format(len(mails), )) unsended_mails = [] # TODO BS 20171124: Look around mail.get_from_address(), mail.get_key() # , mail.get_body() etc ... for raise InvalidEmailError if missing # required informations (actually get_from_address raise IndexError # if no from address for example) and catch it here while mails: mail = mails.pop() msg = { 'token': self.token, 'user_mail': mail.get_from_address(), 'content_id': mail.get_key(), 'payload': { 'content': mail.get_body(use_html_parsing=self.use_html_parsing, use_txt_parsing=self.use_txt_parsing), } } try: logger.debug( self, 'Contact API on {} with body {}'.format( self.endpoint, json.dumps(msg), ), ) r = requests.post(self.endpoint, json=msg) if r.status_code not in [200, 204]: details = r.json().get('msg') log = 'bad status code {} response when sending mail to tracim: {}' # nopep8 logger.error(self, log.format( str(r.status_code), details, )) # Flag all correctly checked mail, unseen the others if r.status_code in [200, 204, 400]: self._set_flag(mail.uid, IMAP_CHECKED_FLAG) else: self._unset_flag(mail.uid, IMAP_SEEN_FLAG) # TODO - G.M - Verify exception correctly works except requests.exceptions.Timeout as e: log = 'Timeout error to transmit fetched mail to tracim : {}' logger.error(self, log.format(str(e))) unsended_mails.append(mail) self._unset_flag(mail.uid, IMAP_SEEN_FLAG) except requests.exceptions.RequestException as e: log = 'Fail to transmit fetched mail to tracim : {}' logger.error(self, log.format(str(e))) self._unset_flag(mail.uid, IMAP_SEEN_FLAG)
def _fetch(self) -> typing.List[MessageContainer]: """ Get news message from mailbox :return: list of new mails """ messages = [] # select mailbox logger.debug(self, 'Fetch messages from folder {}'.format(self.folder, )) rv, data = self._connection.select(self.folder) logger.debug(self, 'Response status {}'.format(rv, )) if rv == 'OK': # get mails # TODO - G.M - 2017-11-15 Which files to select as new file ? # Unseen file or All file from a directory (old one should be # moved/ deleted from mailbox during this process) ? logger.debug(self, 'Fetch unseen messages') rv, data = self._connection.search(None, "(UNSEEN)") logger.debug(self, 'Response status {}'.format(rv, )) if rv == 'OK': # get mail content logger.debug( self, 'Found {} unseen mails'.format(len(data[0].split()), )) for uid in data[0].split(): # INFO - G.M - 2017-12-08 - Fetch BODY.PEEK[] # Retrieve all mail(body and header) but don't set mail # as seen because of PEEK # see rfc3501 logger.debug(self, 'Fetch mail "{}"'.format(uid, )) rv, data = self._connection.fetch(uid, 'BODY.PEEK[]') logger.debug(self, 'Response status {}'.format(rv, )) if rv == 'OK': msg = message_from_bytes(data[0][1]) msg_container = MessageContainer(msg, uid) messages.append(msg_container) self._set_flag(uid, IMAP_SEEN_FLAG) else: log = 'IMAP : Unable to get mail : {}' logger.error(self, log.format(str(rv))) else: log = 'IMAP : Unable to get unseen mail : {}' logger.error(self, log.format(str(rv))) else: log = 'IMAP : Unable to open mailbox : {}' logger.error(self, log.format(str(rv))) return messages
def get_one(self, file_id, revision_id=None): file_id = int(file_id) cache_path = CFG.get_instance().PREVIEW_CACHE_DIR preview_manager = PreviewManager(cache_path, create_folder=True) user = tmpl_context.current_user workspace = tmpl_context.workspace current_user_content = Context(CTX.CURRENT_USER, current_user=user).toDict(user) current_user_content.roles.sort(key=lambda role: role.workspace.name) content_api = ContentApi(user, show_archived=True, show_deleted=True) if revision_id: file = content_api.get_one_from_revision(file_id, self._item_type, workspace, revision_id) else: file = content_api.get_one(file_id, self._item_type, workspace) revision_id = file.revision_id file_path = content_api.get_one_revision_filepath(revision_id) nb_page = 0 enable_pdf_buttons = False # type: bool preview_urls = [] try: nb_page = preview_manager.get_page_nb(file_path=file_path) for page in range(int(nb_page)): url_str = '/previews/{}/pages/{}?revision_id={}' url = url_str.format(file_id, page, revision_id) preview_urls.append(url) enable_pdf_buttons = \ preview_manager.has_pdf_preview(file_path=file_path) except PreviewGeneratorException as e: # INFO - A.P - Silently intercepts preview exception # As preview generation isn't mandatory, just register it logger.debug(self, 'Preview Generator Exception: {}'.format(e.__str__)) except Exception as e: # INFO - D.A - 2017-08-11 - Make Tracim robust to pg exceptions # Preview generator may potentially raise any type of exception # so we prevent user interface crashes by catching all exceptions logger.error( self, 'Preview Generator Generic Exception: {}'.format(e.__str__)) pdf_available = 'true' if enable_pdf_buttons else 'false' # type: str fake_api_breadcrumb = self.get_breadcrumb(file_id) fake_api_content = DictLikeClass(breadcrumb=fake_api_breadcrumb, current_user=current_user_content) fake_api = Context(CTX.FOLDER, current_user=user)\ .toDict(fake_api_content) dictified_file = Context(self._get_one_context, current_user=user).toDict(file, 'file') result = DictLikeClass(result=dictified_file, fake_api=fake_api, nb_page=nb_page, url=preview_urls, pdf_available=pdf_available) return result
def put(self, new_profile): # FIXME - Allow only self password or operation for managers current_user = tmpl_context.current_user user = tmpl_context.user group_api = GroupApi(current_user) if current_user.user_id == user.user_id: tg.flash(_('You can\'t change your own profile'), CST.STATUS_ERROR) tg.redirect(self.parent_controller.url()) redirect_url = self.parent_controller.url(skip_id=True) if new_profile not in self.allowed_profiles: tg.flash(_('Unknown profile'), CST.STATUS_ERROR) tg.redirect(redirect_url) pod_user_group = group_api.get_one(Group.TIM_USER) pod_manager_group = group_api.get_one(Group.TIM_MANAGER) pod_admin_group = group_api.get_one(Group.TIM_ADMIN) # this is the default value ; should never appear flash_message = _('User updated.') if new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_USER: if pod_user_group not in user.groups: user.groups.append(pod_user_group) try: user.groups.remove(pod_manager_group) except: pass try: user.groups.remove(pod_admin_group) except: pass flash_message = _('User {} is now a basic user').format(user.get_display_name()) elif new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER: if pod_user_group not in user.groups: user.groups.append(pod_user_group) if pod_manager_group not in user.groups: user.groups.append(pod_manager_group) try: user.groups.remove(pod_admin_group) except: pass flash_message = _('User {} can now workspaces').format(user.get_display_name()) elif new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN: if pod_user_group not in user.groups: user.groups.append(pod_user_group) if pod_manager_group not in user.groups: user.groups.append(pod_manager_group) if pod_admin_group not in user.groups: user.groups.append(pod_admin_group) flash_message = _('User {} is now an administrator').format(user.get_display_name()) else: error_msg = \ 'Trying to change user {} profile with unexpected profile {}' logger.error(self, error_msg.format(user.user_id, new_profile)) tg.flash(_('Unknown profile'), CST.STATUS_ERROR) tg.redirect(redirect_url) DBSession.flush() tg.flash(flash_message, CST.STATUS_OK) tg.redirect(redirect_url)
def put(self, new_profile): # FIXME - Allow only self password or operation for managers current_user = tmpl_context.current_user user = tmpl_context.user group_api = GroupApi(current_user) if current_user.user_id==user.user_id: tg.flash(_('You can\'t change your own profile'), CST.STATUS_ERROR) tg.redirect(self.parent_controller.url()) redirect_url = self.parent_controller.url(skip_id=True) if new_profile not in self.allowed_profiles: tg.flash(_('Unknown profile'), CST.STATUS_ERROR) tg.redirect(redirect_url) pod_user_group = group_api.get_one(Group.TIM_USER) pod_manager_group = group_api.get_one(Group.TIM_MANAGER) pod_admin_group = group_api.get_one(Group.TIM_ADMIN) flash_message = _('User updated.') # this is the default value ; should never appear if new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_USER: if pod_user_group not in user.groups: user.groups.append(pod_user_group) try: user.groups.remove(pod_manager_group) except: pass try: user.groups.remove(pod_admin_group) except: pass flash_message = _('User {} is now a basic user').format(user.get_display_name()) elif new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER: if pod_user_group not in user.groups: user.groups.append(pod_user_group) if pod_manager_group not in user.groups: user.groups.append(pod_manager_group) try: user.groups.remove(pod_admin_group) except: pass flash_message = _('User {} can now workspaces').format(user.get_display_name()) elif new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN: if pod_user_group not in user.groups: user.groups.append(pod_user_group) if pod_manager_group not in user.groups: user.groups.append(pod_manager_group) if pod_admin_group not in user.groups: user.groups.append(pod_admin_group) flash_message = _('User {} is now an administrator').format(user.get_display_name()) else: logger.error(self, 'Trying to change user {} profile with unexpected profile {}'.format(user.user_id, new_profile)) tg.flash(_('Unknown profile'), CST.STATUS_ERROR) tg.redirect(redirect_url) DBSession.flush() tg.flash(flash_message, CST.STATUS_OK) tg.redirect(redirect_url)
def notify_content_update(self, content: Content): # TODO: Find a way to import properly without cyclic import from tracim.config.app_cfg import CFG global_config = CFG.get_instance() if content.get_last_action().id not \ in global_config.EMAIL_NOTIFICATION_NOTIFIED_EVENTS: logger.info( self, 'Skip email notification for update of content {}' 'by user {} (the action is {})'.format( content.content_id, # below: 0 means "no user" self._user.user_id if self._user else 0, content.get_last_action().id ) ) return logger.info(self, 'About to email-notify update' 'of content {} by user {}'.format( content.content_id, # Below: 0 means "no user" self._user.user_id if self._user else 0 ) ) if content.type not \ in global_config.EMAIL_NOTIFICATION_NOTIFIED_CONTENTS: logger.info( self, 'Skip email notification for update of content {}' 'by user {} (the content type is {})'.format( content.type, # below: 0 means "no user" self._user.user_id if self._user else 0, content.get_last_action().id ) ) return logger.info(self, 'About to email-notify update' 'of content {} by user {}'.format( content.content_id, # Below: 0 means "no user" self._user.user_id if self._user else 0 ) ) #### # # INFO - D.A. - 2014-11-05 - Emails are sent through asynchronous jobs. # For that reason, we do not give SQLAlchemy objects but ids only # (SQLA objects are related to a given thread/session) # try: if global_config.EMAIL_NOTIFICATION_PROCESSING_MODE.lower()==global_config.CST.ASYNC.lower(): logger.info(self, 'Sending email in ASYNC mode') # TODO - D.A - 2014-11-06 # This feature must be implemented in order to be able to scale to large communities raise NotImplementedError('Sending emails through ASYNC mode is not working yet') asyncjob_perform(EmailNotifier(self._smtp_config, global_config).notify_content_update, self._user.user_id, content.content_id) else: logger.info(self, 'Sending email in SYNC mode') EmailNotifier(self._smtp_config, global_config).notify_content_update(self._user.user_id, content.content_id) except Exception as e: logger.error(self, 'Exception catched during email notification: {}'.format(e.__str__()))
def _build_email_body(self, mako_template_filepath: str, role: UserRoleInWorkspace, content: Content, actor: User) -> str: """ Build an email body and return it as a string :param mako_template_filepath: the absolute path to the mako template to be used for email body building :param role: the role related to user to whom the email must be sent. The role is required (and not the user only) in order to show in the mail why the user receive the notification :param content: the content item related to the notification :param actor: the user at the origin of the action / notification (for example the one who wrote a comment :param config: the global configuration :return: the built email body as string. In case of multipart email, this method must be called one time for text and one time for html """ logger.debug(self, 'Building email content from MAKO template {}'.format(mako_template_filepath)) template = Template(filename=mako_template_filepath) # TODO - D.A. - 2014-11-06 - move this # Import is here for circular import problem import tracim.lib.helpers as helpers dictified_item = Context(CTX.EMAIL_NOTIFICATION, self._global_config.WEBSITE_BASE_URL).toDict(content) dictified_actor = Context(CTX.DEFAULT).toDict(actor) main_title = dictified_item.label content_intro = '' content_text = '' call_to_action_text = '' action = content.get_last_action().id if ActionDescription.COMMENT == action: content_intro = _('<span id="content-intro-username">{}</span> added a comment:').format(actor.display_name) content_text = content.description call_to_action_text = _('Answer') elif ActionDescription.CREATION == action: # Default values (if not overriden) content_text = content.description call_to_action_text = _('View online') if ContentType.Thread == content.type: call_to_action_text = _('Answer') content_intro = _('<span id="content-intro-username">{}</span> started a thread entitled:').format(actor.display_name) content_text = '<p id="content-body-intro">{}</p>'.format(content.label) + \ content.get_last_comment_from(actor).description elif ContentType.File == content.type: content_intro = _('<span id="content-intro-username">{}</span> added a file entitled:').format(actor.display_name) if content.description: content_text = content.description else: content_text = '<span id="content-body-only-title">{}</span>'.format(content.label) elif ContentType.Page == content.type: content_intro = _('<span id="content-intro-username">{}</span> added a page entitled:').format(actor.display_name) content_text = '<span id="content-body-only-title">{}</span>'.format(content.label) elif ActionDescription.REVISION == action: content_text = content.description call_to_action_text = _('View online') if ContentType.File == content.type: content_intro = _('<span id="content-intro-username">{}</span> uploaded a new revision.').format(actor.display_name) content_text = '' elif ContentType.Page == content.type: content_intro = _('<span id="content-intro-username">{}</span> updated this page.').format(actor.display_name) previous_revision = content.get_previous_revision() title_diff = '' if previous_revision.label != content.label: title_diff = htmldiff(previous_revision.label, content.label) content_text = _('<p id="content-body-intro">Here is an overview of the changes:</p>')+ \ title_diff + \ htmldiff(previous_revision.description, content.description) elif ContentType.Thread == content.type: content_intro = _('<span id="content-intro-username">{}</span> updated the thread description.').format(actor.display_name) previous_revision = content.get_previous_revision() title_diff = '' if previous_revision.label != content.label: title_diff = htmldiff(previous_revision.label, content.label) content_text = _('<p id="content-body-intro">Here is an overview of the changes:</p>')+ \ title_diff + \ htmldiff(previous_revision.description, content.description) # elif ContentType.Thread == content.type: # content_intro = _('<span id="content-intro-username">{}</span> updated this page.').format(actor.display_name) # previous_revision = content.get_previous_revision() # content_text = _('<p id="content-body-intro">Here is an overview of the changes:</p>')+ \ # htmldiff(previous_revision.description, content.description) elif ActionDescription.EDITION == action: call_to_action_text = _('View online') if ContentType.File == content.type: content_intro = _('<span id="content-intro-username">{}</span> updated the file description.').format(actor.display_name) content_text = '<p id="content-body-intro">{}</p>'.format(content.get_label()) + \ content.description if '' == content_intro and content_text == '': # Skip notification, but it's not normal logger.error( self, 'A notification is being sent but no content. ' 'Here are some debug informations: [content_id: {cid}]' '[action: {act}][author: {actor}]'.format( cid=content.content_id, act=action, actor=actor ) ) raise ValueError('Unexpected empty notification') # Import done here because cyclic import from tracim.config.app_cfg import CFG body_content = template.render( base_url=self._global_config.WEBSITE_BASE_URL, _=_, h=helpers, user_display_name=role.user.display_name, user_role_label=role.role_as_label(), workspace_label=role.workspace.label, content_intro=content_intro, content_text=content_text, main_title=main_title, call_to_action_text=call_to_action_text, result = DictLikeClass(item=dictified_item, actor=dictified_actor), CFG=CFG.get_instance(), ) return body_content
def notify_content_update(self, content: Content): # TODO: Find a way to import properly without cyclic import from tracim.config.app_cfg import CFG global_config = CFG.get_instance() if content.get_last_action().id not \ in global_config.EMAIL_NOTIFICATION_NOTIFIED_EVENTS: logger.info( self, 'Skip email notification for update of content {}' 'by user {} (the action is {})'.format( content.content_id, # below: 0 means "no user" self._user.user_id if self._user else 0, content.get_last_action().id)) return logger.info( self, 'About to email-notify update' 'of content {} by user {}'.format( content.content_id, # Below: 0 means "no user" self._user.user_id if self._user else 0)) if content.type not \ in global_config.EMAIL_NOTIFICATION_NOTIFIED_CONTENTS: logger.info( self, 'Skip email notification for update of content {}' 'by user {} (the content type is {})'.format( content.type, # below: 0 means "no user" self._user.user_id if self._user else 0, content.get_last_action().id)) return logger.info( self, 'About to email-notify update' 'of content {} by user {}'.format( content.content_id, # Below: 0 means "no user" self._user.user_id if self._user else 0)) #### # # INFO - D.A. - 2014-11-05 - Emails are sent through asynchronous jobs. # For that reason, we do not give SQLAlchemy objects but ids only # (SQLA objects are related to a given thread/session) # try: if global_config.EMAIL_NOTIFICATION_PROCESSING_MODE.lower( ) == global_config.CST.ASYNC.lower(): logger.info(self, 'Sending email in ASYNC mode') # TODO - D.A - 2014-11-06 # This feature must be implemented in order to be able to scale to large communities raise NotImplementedError( 'Sending emails through ASYNC mode is not working yet') else: logger.info(self, 'Sending email in SYNC mode') EmailNotifier(self._smtp_config, global_config).notify_content_update( self._user.user_id, content.content_id) except Exception as e: logger.error( self, 'Exception catched during email notification: {}'.format( e.__str__()))
def _build_email_body(self, mako_template_filepath: str, role: UserRoleInWorkspace, content: Content, actor: User) -> str: """ Build an email body and return it as a string :param mako_template_filepath: the absolute path to the mako template to be used for email body building :param role: the role related to user to whom the email must be sent. The role is required (and not the user only) in order to show in the mail why the user receive the notification :param content: the content item related to the notification :param actor: the user at the origin of the action / notification (for example the one who wrote a comment :param config: the global configuration :return: the built email body as string. In case of multipart email, this method must be called one time for text and one time for html """ logger.debug( self, 'Building email content from MAKO template {}'.format( mako_template_filepath)) template = Template(filename=mako_template_filepath) # TODO - D.A. - 2014-11-06 - move this # Import is here for circular import problem import tracim.lib.helpers as helpers dictified_item = Context( CTX.EMAIL_NOTIFICATION, self._global_config.WEBSITE_BASE_URL).toDict(content) dictified_actor = Context(CTX.DEFAULT).toDict(actor) main_title = dictified_item.label content_intro = '' content_text = '' call_to_action_text = '' action = content.get_last_action().id if ActionDescription.COMMENT == action: content_intro = l_( '<span id="content-intro-username">{}</span> added a comment:' ).format(actor.display_name) content_text = content.description call_to_action_text = l_('Answer') elif ActionDescription.CREATION == action: # Default values (if not overriden) content_text = content.description call_to_action_text = l_('View online') if ContentType.Thread == content.type: call_to_action_text = l_('Answer') content_intro = l_( '<span id="content-intro-username">{}</span> started a thread entitled:' ).format(actor.display_name) content_text = '<p id="content-body-intro">{}</p>'.format(content.label) + \ content.get_last_comment_from(actor).description elif ContentType.File == content.type: content_intro = l_( '<span id="content-intro-username">{}</span> added a file entitled:' ).format(actor.display_name) if content.description: content_text = content.description else: content_text = '<span id="content-body-only-title">{}</span>'.format( content.label) elif ContentType.Page == content.type: content_intro = l_( '<span id="content-intro-username">{}</span> added a page entitled:' ).format(actor.display_name) content_text = '<span id="content-body-only-title">{}</span>'.format( content.label) elif ActionDescription.REVISION == action: content_text = content.description call_to_action_text = l_('View online') if ContentType.File == content.type: content_intro = l_( '<span id="content-intro-username">{}</span> uploaded a new revision.' ).format(actor.display_name) content_text = '' elif ContentType.Page == content.type: content_intro = l_( '<span id="content-intro-username">{}</span> updated this page.' ).format(actor.display_name) previous_revision = content.get_previous_revision() title_diff = '' if previous_revision.label != content.label: title_diff = htmldiff(previous_revision.label, content.label) content_text = str(l_('<p id="content-body-intro">Here is an overview of the changes:</p>'))+ \ title_diff + \ htmldiff(previous_revision.description, content.description) elif ContentType.Thread == content.type: content_intro = l_( '<span id="content-intro-username">{}</span> updated the thread description.' ).format(actor.display_name) previous_revision = content.get_previous_revision() title_diff = '' if previous_revision.label != content.label: title_diff = htmldiff(previous_revision.label, content.label) content_text = str(l_('<p id="content-body-intro">Here is an overview of the changes:</p>'))+ \ title_diff + \ htmldiff(previous_revision.description, content.description) # elif ContentType.Thread == content.type: # content_intro = l_('<span id="content-intro-username">{}</span> updated this page.').format(actor.display_name) # previous_revision = content.get_previous_revision() # content_text = l_('<p id="content-body-intro">Here is an overview of the changes:</p>')+ \ # htmldiff(previous_revision.description, content.description) elif ActionDescription.EDITION == action: call_to_action_text = l_('View online') if ContentType.File == content.type: content_intro = l_( '<span id="content-intro-username">{}</span> updated the file description.' ).format(actor.display_name) content_text = '<p id="content-body-intro">{}</p>'.format(content.get_label()) + \ content.description elif ActionDescription.STATUS_UPDATE == action: call_to_action_text = l_('View online') intro_user_msg = l_('<span id="content-intro-username">{}</span> ' 'updated the following status:') content_intro = intro_user_msg.format(actor.display_name) intro_body_msg = '<p id="content-body-intro">{}: {}</p>' content_text = intro_body_msg.format( content.get_label(), content.get_status().label, ) if '' == content_intro and content_text == '': # Skip notification, but it's not normal logger.error( self, 'A notification is being sent but no content. ' 'Here are some debug informations: [content_id: {cid}]' '[action: {act}][author: {actor}]'.format( cid=content.content_id, act=action, actor=actor)) raise ValueError('Unexpected empty notification') # Import done here because cyclic import from tracim.config.app_cfg import CFG body_content = template.render( base_url=self._global_config.WEBSITE_BASE_URL, _=l_, h=helpers, user_display_name=role.user.display_name, user_role_label=role.role_as_label(), workspace_label=role.workspace.label, content_intro=content_intro, content_text=content_text, main_title=main_title, call_to_action_text=call_to_action_text, result=DictLikeClass(item=dictified_item, actor=dictified_actor), CFG=CFG.get_instance(), ) return body_content
def run(self) -> None: logger.info(self, 'Starting MailFetcher') while self._is_active: imapc = None sleep_after_connection = True try: imapc = imapclient.IMAPClient( self.host, self.port, ssl=self.use_ssl, timeout=MAIL_FETCHER_CONNECTION_TIMEOUT) imapc.login(self.user, self.password) logger.debug(self, 'Select folder {}'.format(self.folder, )) imapc.select_folder(self.folder) # force renew connection when deadline is reached deadline = time.time() + self.connection_max_lifetime while True: if not self._is_active: logger.warning(self, 'Mail Fetcher process aborted') sleep_after_connection = False break if time.time() > deadline: logger.debug( self, "MailFetcher Connection Lifetime limit excess" ", Try Re-new connection") sleep_after_connection = False break # check for new mails self._check_mail(imapc) if self.use_idle and imapc.has_capability('IDLE'): # IDLE_mode wait until event from server logger.debug(self, 'wail for event(IDLE)') imapc.idle() imapc.idle_check( timeout=MAIL_FETCHER_IDLE_RESPONSE_TIMEOUT) imapc.idle_done() else: if self.use_idle and not imapc.has_capability('IDLE'): log = 'IDLE mode activated but server do not' \ 'support it, use polling instead.' logger.warning(self, log) # normal polling mode : sleep a define duration logger.debug(self, 'sleep for {}'.format(self.heartbeat)) time.sleep(self.heartbeat) # Socket except (socket.error, socket.gaierror, socket.herror) as e: log = 'Socket fail with IMAP connection {}' logger.error(self, log.format(e.__str__())) except socket.timeout as e: log = 'Socket timeout on IMAP connection {}' logger.error(self, log.format(e.__str__())) # SSL except ssl.SSLError as e: log = 'SSL error on IMAP connection' logger.error(self, log.format(e.__str__())) except ssl.CertificateError as e: log = 'SSL Certificate verification failed on IMAP connection' logger.error(self, log.format(e.__str__())) # Filelock except filelock.Timeout as e: log = 'Mail Fetcher Lock Timeout {}' logger.warning(self, log.format(e.__str__())) # IMAP # TODO - G.M - 10-01-2017 - Support imapclient exceptions # when Imapclient stable will be 2.0+ except BadIMAPFetchResponse as e: log = 'Imap Fetch command return bad response.' \ 'Is someone else connected to the mailbox ?: ' \ '{}' logger.error(self, log.format(e.__str__())) # Others except Exception as e: log = 'Mail Fetcher error {}' logger.error(self, log.format(e.__str__())) finally: # INFO - G.M - 2018-01-09 - Connection closing # Properly close connection according to # https://github.com/mjs/imapclient/pull/279/commits/043e4bd0c5c775c5a08cb5f1baa93876a46732ee # TODO : Use __exit__ method instead when imapclient stable will # be 2.0+ . if imapc: logger.debug(self, 'Try logout') try: imapc.logout() except Exception: try: imapc.shutdown() except Exception as e: log = "Can't logout, connection broken ? {}" logger.error(self, log.format(e.__str__())) if sleep_after_connection: logger.debug(self, 'sleep for {}'.format(self.heartbeat)) time.sleep(self.heartbeat) log = 'Mail Fetcher stopped' logger.debug(self, log)