def _pre_process_message(self, contact, message, incomming, cedict, cepath, tstamp=None, mtype=None, cstyle=None): '''Create a new gui.Message and calculates if it's first message ''' msg = gui.Message.from_contact(contact, message, self.first, incomming, tstamp, mtype = mtype, mstyle=cstyle) if self.session.config.b_show_emoticons: msg.message = MarkupParser.replace_emotes(MarkupParser.escape(msg.message), cedict, cepath, msg.sender) msg.message = MarkupParser.urlify(msg.message) b_nick_check = bool(self.last_incoming_nickname != msg.display_name) if b_nick_check: self.last_incoming_nickname = msg.display_name if msg.incoming: if self.last_incoming is None: self.last_incoming = False msg.first = not self.last_incoming if self.last_incoming_account != msg.sender or b_nick_check: msg.first = True else: if self.last_incoming is None: self.last_incoming = True msg.first = self.last_incoming return msg
def replace(self, template, msg, style=None, cedict={}, cedir=None): '''replace the variables on template for the values on msg ''' msgtext = MarkupParser.replace_emotes(escape(msg.message), cedict, cedir) msgtext = MarkupParser.urlify(msgtext) if style is not None: msgtext = style_message(msgtext, style) template = template.replace('%sender%', escape(msg.alias)) template = template.replace('%senderScreenName%', escape(msg.sender)) template = template.replace('%senderDisplayName%', escape(msg.display_name)) template = template.replace('%userIconPath%', escape(msg.image_path)) template = template.replace('%senderStatusIcon%', escape(msg.status_path)) template = template.replace('%messageDirection%', escape(msg.direction)) template = template.replace('%message%', msgtext) template = template.replace('%time%', escape(time.strftime(self.timefmt))) template = re.sub("%time{(.*?)}%", replace_time, template) template = template.replace('%shortTime%', escape(time.strftime("%H:%M"))) template = template.replace('%service%', escape(msg.service)) template = template.replace('%messageClasses%', escape(msg.classes)) template = template.replace('%status%', escape(msg.status)) return template.replace('\n', '')
def _prepare_markup(self, markup, is_plus=True): if is_plus: text = Plus.msnplus_parse(markup) else: text = Plus.msnplus_strip(markup) text = MarkupParser.replace_markup(text) text_list = MarkupParser.replace_emoticons(text) return text_list
def _replace(self, template, msg): '''replace the variables on template for the values on msg ''' msg.alias = Plus.msnplus_strip(msg.alias) msg.display_name = Plus.msnplus_strip(msg.display_name) if (len(msg.alias) > DISPLAY_NAME_LIMIT): msg.alias = msg.alias.decode('utf-8')[0:DISPLAY_NAME_LIMIT] + "..." if (len(msg.display_name) > DISPLAY_NAME_LIMIT): msg.display_name = msg.display_name.decode( 'utf-8')[0:DISPLAY_NAME_LIMIT] + "..." image_path = escape(MarkupParser.path_to_url(msg.image_path)) status_path = escape(MarkupParser.path_to_url(msg.status_path)) if msg.style is not None: msg.message = style_message(msg.message, msg.style) if msg.alias: template = template.replace('%sender%', escape(msg.alias)) else: template = template.replace('%sender%', escape(msg.display_name)) template = template.replace('%senderScreenName%', escape(msg.sender)) template = template.replace('%senderDisplayName%', escape(msg.display_name)) template = template.replace('%userIconPath%', image_path) template = template.replace('%senderStatusIcon%', status_path) template = template.replace('%messageDirection%', escape(msg.direction)) template = template.replace('%message%', escape_no_xml(msg.message)) if msg.timestamp is None: template = template.replace('%time%', escape(time.strftime("%H:%M"))) else: def utc_to_local(t): secs = calendar.timegm(t) return time.localtime(secs) l_time = utc_to_local(msg.timestamp.timetuple()) #time.struct_time d_time = datetime.datetime.fromtimestamp(time.mktime(l_time)) template = template.replace('%time%', escape(d_time.strftime('%x %X'))) template = re.sub("%time{(.*?)}%", replace_time, template) template = template.replace('%shortTime%', escape(time.strftime("%H:%M"))) template = template.replace('%service%', escape(msg.service)) template = template.replace('%messageClasses%', escape(msg.classes)) template = template.replace('%status%', escape(msg.status)) return template.replace('\n', '')
def replace(self, template, msg, style=None, cedict={}, cedir=None): '''replace the variables on template for the values on msg ''' msg.alias = Plus.msnplus_strip(msg.alias) msg.display_name = Plus.msnplus_strip(msg.display_name) if(len(msg.alias) > DISPLAY_NAME_LIMIT): msg.alias = msg.alias.decode('utf-8')[0:DISPLAY_NAME_LIMIT] + "..." if(len(msg.display_name) > DISPLAY_NAME_LIMIT): msg.display_name = msg.display_name.decode('utf-8')[0:DISPLAY_NAME_LIMIT] + "..." msgtext = MarkupParser.replace_emotes(escape(msg.message), cedict, cedir, msg.sender) msgtext = MarkupParser.urlify(msgtext) image_path = escape(MarkupParser.path_to_url(msg.image_path)) status_path = escape(MarkupParser.path_to_url(msg.status_path)) if style is not None: msgtext = style_message(msgtext, style) if msg.alias: template = template.replace('%sender%', escape(msg.alias)) else: template = template.replace('%sender%', escape(msg.display_name)) template = template.replace('%senderScreenName%', escape(msg.sender)) template = template.replace('%senderDisplayName%', escape(msg.display_name)) template = template.replace('%userIconPath%', image_path) template = template.replace('%senderStatusIcon%', status_path) template = template.replace('%messageDirection%', escape(msg.direction)) template = template.replace('%message%', msgtext) if msg.timestamp is None: template = template.replace('%time%', escape(time.strftime("%H:%M"))) else: def utc_to_local(t): secs = calendar.timegm(t) return time.localtime(secs) l_time = utc_to_local(msg.timestamp.timetuple()) #time.struct_time d_time = datetime.datetime.fromtimestamp(time.mktime(l_time)) template = template.replace('%time%', escape(d_time.strftime('%x %X'))) template = re.sub("%time{(.*?)}%", replace_time, template) template = template.replace('%shortTime%', escape(time.strftime("%H:%M"))) template = template.replace('%service%', escape(msg.service)) template = template.replace('%messageClasses%', escape(msg.classes)) template = template.replace('%status%', escape(msg.status)) return template.replace('\n', '')
def _update_single_information(self, account): '''set the data of the conversation to the data of the account''' contact = self.session.contacts.safe_get(account) if contact.media == '': message = MarkupParser.escape(contact.message) else: message = MarkupParser.escape(contact.media) nick = MarkupParser.escape(contact.display_name) self.update_single_information(nick, message, account)
def replace(self, template, msg, style=None, cedict={}, cedir=None): '''replace the variables on template for the values on msg ''' msgtext = MarkupParser.replace_emotes(escape(msg.message), cedict, cedir, msg.sender) msgtext = MarkupParser.urlify(msgtext) image_path = escape(MarkupParser.path_to_url(msg.image_path)) status_path = escape(MarkupParser.path_to_url(msg.status_path)) if style is not None: msgtext = style_message(msgtext, style) plus_parser = Plus.MsnPlusMarkupMohrtutchy() if msg.alias: template = template.replace( '%sender%', escape(plus_parser.removeMarkup(msg.alias))) else: template = template.replace( '%sender%', escape(plus_parser.removeMarkup(msg.display_name))) template = template.replace('%senderScreenName%', escape(msg.sender)) template = template.replace('%senderDisplayName%', escape(msg.display_name)) template = template.replace('%userIconPath%', image_path) template = template.replace('%senderStatusIcon%', status_path) template = template.replace('%messageDirection%', escape(msg.direction)) template = template.replace('%message%', msgtext) if msg.timestamp is None: template = template.replace('%time%', escape(time.strftime(self.timefmt))) else: def utc_to_local(t): secs = calendar.timegm(t) return time.localtime(secs) l_time = utc_to_local(msg.timestamp.timetuple()) #time.struct_time d_time = datetime.datetime.fromtimestamp(time.mktime(l_time)) template = template.replace('%time%', escape(d_time.strftime('%x %X'))) template = re.sub("%time{(.*?)}%", replace_time, template) template = template.replace('%shortTime%', escape(time.strftime("%H:%M"))) template = template.replace('%service%', escape(msg.service)) template = template.replace('%messageClasses%', escape(msg.classes)) template = template.replace('%status%', escape(msg.status)) return template.replace('\n', '')
def _update_single_information(self, account): """set the data of the conversation to the data of the account""" contact = self.session.contacts.get(account) if contact: message = MarkupParser.escape(contact.message) nick = MarkupParser.escape(contact.display_name) else: message = "" nick = account self.update_single_information(nick, message, account)
def add_style_to_message(text, stl, escape=True): '''add the style in a xhtml like syntax to text''' style_start = '' style_end = '' style = 'color: #' + stl.color.to_hex() + ';' if stl.bold: style_start = style_start + '<b>' style_end = '</b>' + style_end if stl.italic: style_start = style_start + '<i>' style_end = '</i>' + style_end if stl.underline: style_start = style_start + '<u>' style_end = '</u>' + style_end if stl.strike: style_start = style_start + '<s>' style_end = '</s>' + style_end if stl.font: style += 'font-family: ' + stl.font style_start += '<span style="%s; ">' % (style, ) style_end = '</span>' + style_end if escape: text = MarkupParser.escape(text) return style_start + text + style_end
def _on_message(self, cid, account, message, cedict=None): '''called when a message is received''' conversation = self.conversations.get(float(cid), None) if conversation is None: (exists, conversation) = self.new_conversation(cid, [account]) contact = self.session.contacts.get(account) if contact: nick = contact.display_name else: nick = account if message.type == e3.Message.TYPE_MESSAGE: (is_raw, consecutive, outgoing, first, last) = \ conversation.formatter.format(contact) middle = MarkupParser.escape(message.body) if not is_raw: middle = self.format_from_message(message) conversation.output.append(first + middle + last, cedict,self.session.config.b_allow_auto_scroll) conversation.play_send() elif message.type == e3.Message.TYPE_NUDGE: conversation.output.append( conversation.formatter.format_information( '%s just sent you a nudge!' % (nick,)),self.session.config.b_allow_auto_scroll) conversation.play_nudge() if message.type != e3.Message.TYPE_TYPING: self.set_message_waiting(conversation, True)
def _on_send_message(self, text, cedict=None): '''method called when the user press enter on the input text''' self.session.send_message(self.cid, text, self.cstyle) nick = self.session.contacts.me.display_name (is_raw, consecutive, outgoing, first, last) = \ self.formatter.format(self.session.contacts.me) if is_raw: middle = MarkupParser.escape(text) else: middle = MarkupParser.escape(text) middle = e3.common.add_style_to_message(middle, self.cstyle, False) all = first + middle + last self.output.append(all, cedict,self.session.config.b_allow_auto_scroll) self.play_type()
def pre_process_message(self, contact, message, incomming, cedict, cepath, tstamp=None, mtype=None, cstyle=None): '''Create a new gui.Message ''' msg = gui.Message.from_contact(contact, message, self.first, incomming, tstamp, mtype=mtype, mstyle=cstyle) msg.message = MarkupParser.escape(msg.message) if self.config.b_show_emoticons: msg.message = MarkupParser.replace_emotes(msg.message, cedict, cepath, msg.sender) msg.message = MarkupParser.urlify(msg.message) b_nick_check = bool(self.last_incoming_nickname != msg.display_name) if b_nick_check: self.last_incoming_nickname = msg.display_name if msg.incoming: if self.last_incoming is None: self.last_incoming = False msg.first = not self.last_incoming if self.last_incoming_account != msg.sender or b_nick_check: msg.first = True else: if self.last_incoming is None: self.last_incoming = True msg.first = self.last_incoming return msg
def replace(self, template, msg, style=None, cedict={}, cedir=None): """replace the variables on template for the values on msg """ msg.alias = Plus.msnplus_strip(msg.alias) msg.display_name = Plus.msnplus_strip(msg.display_name) msgtext = MarkupParser.replace_emotes(escape(msg.message), cedict, cedir, msg.sender) msgtext = MarkupParser.urlify(msgtext) image_path = escape(MarkupParser.path_to_url(msg.image_path)) status_path = escape(MarkupParser.path_to_url(msg.status_path)) if style is not None: msgtext = style_message(msgtext, style) if msg.alias: template = template.replace("%sender%", escape(msg.alias)) else: template = template.replace("%sender%", escape(msg.display_name)) template = template.replace("%senderScreenName%", escape(msg.sender)) template = template.replace("%senderDisplayName%", escape(msg.display_name)) template = template.replace("%userIconPath%", image_path) template = template.replace("%senderStatusIcon%", status_path) template = template.replace("%messageDirection%", escape(msg.direction)) template = template.replace("%message%", msgtext) if msg.timestamp is None: template = template.replace("%time%", escape(time.strftime(self.timefmt))) else: def utc_to_local(t): secs = calendar.timegm(t) return time.localtime(secs) l_time = utc_to_local(msg.timestamp.timetuple()) # time.struct_time d_time = datetime.datetime.fromtimestamp(time.mktime(l_time)) template = template.replace("%time%", escape(d_time.strftime("%x %X"))) template = re.sub("%time{(.*?)}%", replace_time, template) template = template.replace("%shortTime%", escape(time.strftime("%H:%M"))) template = template.replace("%service%", escape(msg.service)) template = template.replace("%messageClasses%", escape(msg.classes)) template = template.replace("%status%", escape(msg.status)) return template.replace("\n", "")
def on_contact_left(self, account): '''called when a contact leaves the conversation''' if len(self.members) > 1 and account in self.members: self.members.remove(account) self.update_data() contact = self.session.contacts.safe_get(account) message = e3.base.Message(e3.base.Message.TYPE_MESSAGE, \ _('%s has left the conversation') % (MarkupParser.escape(contact.display_name)), \ account) msg = gui.Message.from_information(contact, message) self.output.information(msg) self.conv_status.post_process_message(msg)
def on_contact_joined(self, account): '''called when a contact joins the conversation''' if account not in self.members: self.members.append(account) contact = self.session.contacts.safe_get(account) if len(self.members) > 1: message = e3.base.Message(e3.base.Message.TYPE_MESSAGE, \ _('%s has joined the conversation') % (MarkupParser.escape(contact.display_name)), \ account) msg = gui.Message.from_information(contact, message) self.output.information(msg) self.conv_status.post_process_message(msg) self.update_data()
def on_receive_message(self, message, account, received_custom_emoticons): '''method called when a message arrives to the conversation''' for callback in self.session.cb_gui_recv_message.sorted(): callback(message) contact = self.session.contacts.safe_get(account) if message.type == e3.Message.TYPE_MESSAGE or \ message.type == e3.Message.TYPE_FLNMSG: if self.session.config.b_override_text_color: message.style.color = \ e3.base.Color.from_hex(self.session.config.override_text_color) user_emcache = self.caches.get_emoticon_cache(account) #XXX: when we send messages from the web iface we get those here, so show them propertly if contact.account == self.session.contacts.me.account: self.output_message(message, None) return self.input_message(message, contact, received_custom_emoticons, user_emcache.path) self.play_type() elif message.type == e3.Message.TYPE_NUDGE: message.body = _('%s just sent you a nudge!') % (MarkupParser.escape(contact.display_name),) msg = gui.Message.from_information(contact, message) self.output.information(msg) self.conv_status.post_process_message(msg) self.play_nudge() elif message.type == e3.Message.TYPE_INFO: msg = gui.Message.from_information(contact, message) self.output.information(msg) self.conv_status.post_process_message(msg) elif message.type == e3.Message.TYPE_PICTURE: msg = gui.Message.from_contact(contact, message) self.output.information(msg) self.conv_status.post_process_message(msg) self.conv_status.update_status()
def get_body(self, source, target, target_display, source_img, target_img): """return the template to put as html content """ # first try load custom Template.html from theme path = urljoin(self.resources_path, "Template.html") if not os.path.exists(path): path = urljoin("gui", "base", "template.html") template = read_file(path) resources_url = MarkupParser.path_to_url(self.resources_path) css_path = urljoin(resources_url, "main.css") template = template.replace("%@", resources_url + "/", 1) template = template.replace("%@", css_path, 1) if self.variant is not None: variant_css_path = urljoin(resources_url, "Variants", self.variant + ".css") variant_tag = ( '<style id="mainStyle" type="text/css"' + 'media="screen,print"> @import url( "' + variant_css_path + '" ); </style>' ) else: variant_tag = "" template = template.replace("%@", variant_tag, 1) target = Plus.msnplus_strip(target) target_display = Plus.msnplus_strip(target_display) header = read_file(self.resources_path, "Header.html") or "" if header: header = self.replace_header_or_footer(header, source, target, target_display, source_img, target_img) template = template.replace("%@", header, 1) footer = read_file(self.resources_path, "Footer.html") or "" if footer: footer = self.replace_header_or_footer(footer, source, target, target_display, source_img, target_img) template = template.replace("%@", footer, 1) return template
def get_body(self, source, target, target_display, source_img, target_img): '''return the template to put as html content ''' #first try load custom Template.html from theme path = urljoin(self.resources_path, 'Template.html') if not os.path.exists(path): path = urljoin("gui", "base", "template.html") template = read_file(path) resources_url = MarkupParser.path_to_url(self.resources_path) css_path = urljoin(resources_url, "main.css") if self.variant: variant_name = self.variant else: variant_name = self.info.get('DefaultVariant', None) template = template.replace("%@", resources_url + "/", 1) template = template.replace("%@", css_path, 1) if variant_name is not None: variant_css_path = urljoin(resources_url, "Variants", variant_name + ".css") variant_tag = '<style id="mainStyle" type="text/css"' + \ 'media="screen,print"> @import url( "' + variant_css_path + '" ); </style>' else: variant_tag = "" template = template.replace("%@", variant_tag, 1) header = read_file(self.resources_path, 'Header.html') or "" if header: header = self.replace_header_or_footer(header, source, target, target_display, source_img, target_img) template = template.replace("%@", header, 1) footer = read_file(self.resources_path, 'Footer.html') or "" if footer: footer = self.replace_header_or_footer(footer, source, target, target_display, source_img, target_img) template = template.replace("%@", footer, 1) return template
def get_body(self, source, target, target_display, source_img, target_img): '''return the template to put as html content ''' #first try load custom Template.html from theme path = urljoin(self.resources_path, 'Template.html') if not os.path.exists(path): path = urljoin("gui", "base", "template.html") template = read_file(path) resources_url = MarkupParser.path_to_url(self.resources_path) css_path = urljoin(resources_url, "main.css") variant_name = self.info.get('DefaultVariant', None) template = template.replace("%@", resources_url + "/", 1) template = template.replace("%@", css_path, 1) if variant_name is not None: variant_css_path = "file://" + urljoin(resources_url, "Variants", variant_name + ".css") variant_tag = '<style id="mainStyle" type="text/css"' + \ 'media="screen,print"> @import url( "' + variant_css_path + '" ); </style>' else: variant_tag = "" template = template.replace("%@", variant_tag, 1) header = read_file(self.resources_path, 'Header.html') or "" if header: header = self.replace_header_or_footer(header, source, target, target_display, source_img, target_img) template = template.replace("%@", header, 1) footer = read_file(self.resources_path, 'Footer.html') or "" if footer: footer = self.replace_header_or_footer(footer, source, target, target_display, source_img, target_img) template = template.replace("%@", footer, 1) return template
def get_body(self, source, target, target_display, source_img, target_img): '''return the template to put as html content ''' template = self.template resources_url = MarkupParser.path_to_url(self.resources_path) css_path = urljoin(resources_url, "main.css") template = template.replace("%@", resources_url + "/", 1) template = template.replace("%@", css_path, 1) if self.variant is not None: variant_css_path = urljoin(resources_url, "Variants", self.variant + ".css") variant_tag = '<style id="mainStyle" type="text/css"' + \ 'media="screen,print"> @import url( "' + variant_css_path + '" ); </style>' else: variant_tag = "" template = template.replace("%@", variant_tag, 1) target = Plus.msnplus_strip(target) target_display = Plus.msnplus_strip(target_display) header = self.header if header: header = self._replace_header_or_footer(header, source, target, target_display, source_img, target_img) template = template.replace("%@", header, 1) footer = self.footer if footer: footer = self._replace_header_or_footer(footer, source, target, target_display, source_img, target_img) template = template.replace("%@", footer, 1) return template
def msnplus_to_list(text, plus=True): '''parse text and return a list of strings and gtk.gdk.Pixbufs''' text = plus_text_parse(text, plus) text = MarkupParser.replace_markup(text) text_list = MarkupParser.replace_emoticons(text) return text_list
def format(self, contact, message_type=None): '''format the message according to the template''' if message_type is None: message_type=e3.Message.TYPE_MESSAGE outgoing = False consecutive = False if self.contact.account == contact.account: outgoing = True if self.last_message_sender and \ self.last_message_sender.account == contact.account: consecutive = True timestamp = time.time() self.last_message_sender = contact self.last_message_time = timestamp if message_type == e3.Message.TYPE_MESSAGE: if consecutive: if outgoing: template = self.consecutive_outgoing else: template = self.consecutive_incoming else: if outgoing: template = self.outgoing else: template = self.incoming if message_type == e3.Message.TYPE_NUDGE: template = self.nudge self.last_message_sender = None formated_time = time.strftime('%c', time.gmtime(timestamp)) template = template.replace('%NICK%', MarkupParser.escape(contact.nick)) template = template.replace('%ALIAS%', MarkupParser.escape(contact.alias)) template = template.replace('%ACCOUNT%', MarkupParser.escape(contact.account)) template = template.replace('%DISPLAYNAME%', MarkupParser.escape(contact.display_name)) template = template.replace('%TIME%', MarkupParser.escape(formated_time)) template = template.replace('%STATUS%', MarkupParser.escape(e3.status.STATUS[contact.status])) template = template.replace('%PERSONALMESSAGE%', MarkupParser.escape(contact.message)) template = template.replace('%NL%', self.new_line) is_raw = False if '%MESSAGE%' in template: (first, last) = template.split('%MESSAGE%') elif '%RAWMESSAGE%' in template: (first, last) = template.split('%RAWMESSAGE%') is_raw = True else: first = template last = '' return (is_raw, consecutive, outgoing, first, last)
def msnplus_to_list(text): """parse text and return a list of strings and gtk.gdk.Pixbufs""" text = plus_text_parse(text) text = MarkupParser.replace_markup(text) text_list = MarkupParser.replace_emoticons(text) return text_list