def _get_target_url(self, target): target_url = self.config['target_url'].strip() if target_url: try: target_url = mailer.render_message_template( target_url, self.application.config, target=target) except jinja2.exceptions.TemplateSyntaxError as error: self.logger.error( "jinja2 syntax error ({0}) in target url: {1}".format( error.message, target_url)) self.text_insert( "Jinja2 syntax error ({0}) in target url: {1}\n".format( error.message, target_url)) return None except ValueError as error: self.logger.error( "value error ({0}) in target url: {1}".format( error, target_url)) self.text_insert( "Value error ({0}) in target url: {1}\n".format( error, target_url)) return None else: target_url = self.application.config['mailer.webserver_url'] if target is not None: target_url += '?id=' + target.uid return target_url
def load_html_file(self): """ Load the configured HTML file into the WebKit engine so the contents can be previewed. """ html_file = self.config.get('mailer.html_file') if not (html_file and os.path.isfile(html_file) and os.access(html_file, os.R_OK)): return with codecs.open(html_file, 'r', encoding='utf-8') as file_h: html_data = file_h.read() try: html_data = mailer.render_message_template(html_data, self.config) except jinja2.TemplateSyntaxError as error: self.info_bar_label.set_text("Template syntax error: {error.message} on line {error.lineno}.".format(error=error)) self.info_bar.show() except jinja2.UndefinedError as error: self.info_bar_label.set_text("Template undefined error: {error.message}.".format(error=error)) self.info_bar.show() except TypeError as error: self.info_bar_label.set_text("Template type error: {0}.".format(error.args[0])) self.info_bar.show() else: html_file_uri = urllib.parse.urlparse(html_file, 'file').geturl() self.webview.load_html_data(html_data, html_file_uri) self.info_bar.hide()
def process_attachment_file(self, input_path, output_path, target=None): output_path, _ = os.path.splitext(output_path) output_path += '.pdf' try: with codecs.open(input_path, 'r', encoding='utf-8') as file_: msg_template = file_.read() except UnicodeDecodeError as error: gui_utilities.show_dialog_error( 'PDF Build Error', self.application.get_active_window(), "HTML template not in UTF-8 format.\n\n{error}".format(error=error) ) return try: formatted_message = mailer.render_message_template(msg_template, self.application.config, target) except jinja2.exceptions.TemplateSyntaxError as error: gui_utilities.show_dialog_error( 'PDF Build Error', self.application.get_active_window(), "Template syntax error: {error.message} on line {error.lineno}.".format(error=error) ) return except jinja2.exceptions.UndefinedError as error: gui_utilities.show_dialog_error( 'PDF Build Error', self.application.get_active_window(), "Template undefined error: {error.message}.".format(error=error) ) return except TypeError as error: gui_utilities.show_dialog_error( 'PDF Build Error', self.application.get_active_window(), "Template type error: {0}.".format(error.args[0]) ) return css_style = self.config.get('css_stylesheet') if css_style: css_style = css_style.strip() if not (os.path.isfile(css_style) and os.access(css_style, os.R_OK)): self.logger.warning('invalid css file path: ' + css_style) css_style = None weasyprint_html = HTML(string=formatted_message, base_url=os.path.dirname(input_path)) weasyprint_html.write_pdf( output_path, stylesheets=[css_style] if css_style else None, presentational_hints=True ) return output_path
def expand_path(self, output_file, *args, **kwargs): expanded_path = _expand_path(output_file, *args, **kwargs) try: expanded_path = mailer.render_message_template(expanded_path, self.application.config) except jinja2.exceptions.TemplateSyntaxError as error: self.logger.error("jinja2 syntax error ({0}) in directory: {1}".format(error.message, output_file)) gui_utilities.show_dialog_error('Error', self.application.get_active_window(), 'Error creating the HTML file.') return None except ValueError as error: self.logger.error("value error ({0}) in directory: {1}".format(error, output_file)) gui_utilities.show_dialog_error('Error', self.application.get_active_window(), 'Error creating the HTML file.') return None return expanded_path
def _expand_path(self, path, *args, **kwargs): expanded_path = _expand_path(path, *args, **kwargs) try: expanded_path = mailer.render_message_template(expanded_path, self.application.config) except jinja2.exceptions.TemplateSyntaxError as error: self.logger.error("jinja2 syntax error ({0}) in directory: {1}".format(error.message, path)) self.text_insert("Jinja2 syntax error ({0}) in directory: {1}\n".format(error.message, path)) return None except ValueError as error: self.logger.error("value error ({0}) in directory: {1}".format(error, path)) self.text_insert("Value error ({0}) in directory: {1}\n".format(error, path)) return None return expanded_path
def render_template_string(self, template_string, target=None, description='string', log_to_mailer=True): """ Render the specified *template_string* in the message environment. If an error occurs during the rendering process, a message will be logged and ``None`` will be returned. If *log_to_mailer* is set to ``True`` then a message will also be displayed in the message send tab of the client. .. versionadded:: 1.9.0b5 :param str template_string: The string to render as a template. :param target: An optional target to pass to the rendering environment. :type target: :py:class:`~king_phisher.client.mailer.MessageTarget` :param str description: A keyword to use to identify the template string in error messages. :param bool log_to_mailer: Whether or not to log to the message send tab as well. :return: The rendered string or ``None`` if an error occurred. :rtype: str """ mailer_tab = self.application.main_tabs['mailer'] text_insert = mailer_tab.tabs['send_messages'].text_insert try: template_string = mailer.render_message_template( template_string, self.application.config, target=target) except jinja2.exceptions.TemplateSyntaxError as error: self.logger.error("jinja2 syntax error ({0}) in {1}: {2}".format( error.message, description, template_string)) if log_to_mailer: text_insert("Jinja2 syntax error ({0}) in {1}: {2}\n".format( error.message, description, template_string)) return None except jinja2.exceptions.UndefinedError as error: self.logger.error("jinj2 undefined error ({0}) in {1}: {2}".format( error.message, description, template_string)) if log_to_mailer: text_insert("Jinja2 undefined error ({0}) in {1}: {2}".format( error.message, description, template_string)) return None except ValueError as error: self.logger.error("value error ({0}) in {1}: {2}".format( error, description, template_string)) if log_to_mailer: text_insert("Value error ({0}) in {1}: {2}\n".format( error, description, template_string)) return None return template_string
def _expand_path(self, path, *args, **kwargs): expanded_path = _expand_path(path, *args, **kwargs) try: expanded_path = mailer.render_message_template( expanded_path, self.application.config) except jinja2.exceptions.TemplateSyntaxError as error: self.logger.error( "jinja2 syntax error ({0}) in directory: {1}".format( error.message, path)) self.text_insert( "Jinja2 syntax error ({0}) in directory: {1}\n".format( error.message, path)) return None except ValueError as error: self.logger.error("value error ({0}) in directory: {1}".format( error, path)) self.text_insert("Value error ({0}) in directory: {1}\n".format( error, path)) return None return expanded_path
def render_template_string(self, template_string, target=None, description='string', log_to_mailer=True): """ Render the specified *template_string* in the message environment. If an error occurs during the rendering process, a message will be logged and ``None`` will be returned. If *log_to_mailer* is set to ``True`` then a message will also be displayed in the message send tab of the client. .. versionadded:: 1.9.0b5 :param str template_string: The string to render as a template. :param target: An optional target to pass to the rendering environment. :type target: :py:class:`~king_phisher.client.mailer.MessageTarget` :param str description: A keyword to use to identify the template string in error messages. :param bool log_to_mailer: Whether or not to log to the message send tab as well. :return: The rendered string or ``None`` if an error occurred. :rtype: str """ mailer_tab = self.application.main_tabs['mailer'] text_insert = mailer_tab.tabs['send_messages'].text_insert try: template_string = mailer.render_message_template(template_string, self.application.config, target=target) except jinja2.exceptions.TemplateSyntaxError as error: self.logger.error("jinja2 syntax error ({0}) in {1}: {2}".format(error.message, description, template_string)) if log_to_mailer: text_insert("Jinja2 syntax error ({0}) in {1}: {2}\n".format(error.message, description, template_string)) return None except jinja2.exceptions.UndefinedError as error: self.logger.error("jinj2 undefined error ({0}) in {1}: {2}".format(error.message, description, template_string)) if log_to_mailer: text_insert("Jinja2 undefined error ({0}) in {1}: {2}".format(error.message, description, template_string)) return None except ValueError as error: self.logger.error("value error ({0}) in {1}: {2}".format(error, description, template_string)) if log_to_mailer: text_insert("Value error ({0}) in {1}: {2}\n".format(error, description, template_string)) return None return template_string