Mercurial > libervia-backend
annotate frontends/src/tools/strings.py @ 1963:a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
/!\ several features are temporarily disabled, like notifications in frontends
next step in refactoring, with the following changes:
- jp: updated jp message to follow changes in backend/bridge
- jp: added --lang, --subject, --subject_lang, and --type options to jp message + fixed unicode handling for jid
- quick_frontend (QuickApp, QuickChat):
- follow backend changes
- refactored chat, message are now handled in OrderedDict and uid are kept so they can be updated
- Message and Occupant classes handle metadata, so frontend just have to display them
- Primitivus (Chat):
- follow backend/QuickFrontend changes
- info & standard messages are handled in the same MessageWidget class
- improved/simplified handling of messages, removed update() method
- user joined/left messages are merged when next to each other
- a separator is shown when message is received while widget is out of focus, so user can quickly see the new messages
- affiliation/role are shown (in a basic way for now) in occupants panel
- removed "/me" messages handling, as it will be done by a backend plugin
- message language is displayed when available (only one language per message for now)
- fixed :history and :search commands
- core (constants): new constants for messages type, XML namespace, entity type
- core: *Message methods renamed to follow new code sytle (e.g. sendMessageToBridge => messageSendToBridge)
- core (messages handling): fixed handling of language
- core (messages handling): mes_data['from'] and ['to'] are now jid.JID
- core (core.xmpp): reorganised message methods, added getNick() method to client.roster
- plugin text commands: fixed plugin and adapted to new messages behaviour. client is now used in arguments instead of profile
- plugins: added information for cancellation reason in CancelError calls
- plugin XEP-0045: various improvments, but this plugin still need work:
- trigger is used to avoid message already handled by the plugin to be handled a second time
- changed the way to handle history, the last message from DB is checked and we request only messages since this one, in seconds (thanks Poezio folks :))
- subject reception is waited before sending the roomJoined signal, this way we are sure that everything including history is ready
- cmd_* method now follow the new convention with client instead of profile
- roomUserJoined and roomUserLeft messages are removed, the events are now handled with info message with a "ROOM_USER_JOINED" info subtype
- probably other forgotten stuffs :p
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 20 Jun 2016 18:41:53 +0200 |
parents | 1128feb54180 |
children | 2c31ddf633e5 |
rev | line source |
---|---|
1934
2daf7b4c6756
use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents:
1879
diff
changeset
|
1 #!/usr/bin/env python2 |
690
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
3 |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
4 # SAT helpers methods for plugins |
1766 | 5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org) |
690
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
6 |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
11 |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
16 |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
19 |
769
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
20 import re |
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
21 |
690
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
22 |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
23 def getURLParams(url): |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
24 """This comes from pyjamas.Location.makeUrlDict with a small change |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
25 to also parse full URLs, and parameters with no value specified |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
26 (in that case the default value "" is used). |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
27 @param url: any URL with or without parameters |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
28 @return: a dictionary of the parameters, if any was given, or {} |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
29 """ |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
30 dict_ = {} |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
31 if "/" in url: |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
32 # keep the part after the last "/" |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
33 url = url[url.rindex("/") + 1:] |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
34 if url.startswith("?"): |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
35 # remove the first "?" |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
36 url = url[1:] |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
37 pairs = url.split("&") |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
38 for pair in pairs: |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
39 if len(pair) < 3: |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
40 continue |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
41 kv = pair.split("=", 1) |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
42 dict_[kv[0]] = kv[1] if len(kv) > 1 else "" |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
43 return dict_ |
d8e7a58eaa00
misc: added a file for frontend's string operations:
souliane <souliane@mailoo.org>
parents:
diff
changeset
|
44 |
769
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
45 |
1790
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
46 def addURLToText(string, new_target=True): |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
47 """Check a text for what looks like an URL and make it clickable. |
769
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
48 |
1790
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
49 @param string (unicode): text to process |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
50 @param new_target (bool): if True, make the link open in a new window |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
51 """ |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
52 # XXX: report any change to libervia.browser.strings.addURLToText |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
53 # Regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls |
769
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
54 def repl(match): |
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
55 url = match.group(0) |
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
56 if not re.match(r"""[a-z]{3,}://|mailto:|xmpp:""", url): |
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
57 url = "http://" + url |
1790
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
58 target = ' target="_blank"' if new_target else '' |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
59 return '<a href="%s"%s class="url">%s</a>' % (url, target, match.group(0)) |
769
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
60 pattern = r"""(?i)\b((?:[a-z]{3,}://|(www|ftp)\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/|mailto:|xmpp:)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))""" |
8b6137f7b4e1
tools: addURLToText moved from libervia to sat_frontends/tools/strings
souliane <souliane@mailoo.org>
parents:
719
diff
changeset
|
61 return re.sub(pattern, repl, string) |
842
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
62 |
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
63 |
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
64 def addURLToImage(string): |
1790
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
65 """Check a XHTML text for what looks like an imageURL and make it clickable. |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
66 |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
67 @param string (unicode): text to process |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
68 """ |
bf7e468fe440
tools (strings): add a parameter "new_target" to addURLToText
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
69 # XXX: report any change to libervia.browser.strings.addURLToImage |
842
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
70 def repl(match): |
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
71 url = match.group(1) |
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
72 return '<a href="%s" target="_blank">%s</a>' % (url, match.group(0)) |
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
73 pattern = r"""<img[^>]* src="([^"]+)"[^>]*>""" |
429c6a0ef73d
frontends (tools): addURLToImage makes an image clickable
souliane <souliane@mailoo.org>
parents:
811
diff
changeset
|
74 return re.sub(pattern, repl, string) |
1801
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
75 |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
76 |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
77 def fixXHTMLLinks(xhtml): |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
78 """Add http:// if the scheme is missing and force opening in a new window. |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
79 |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
80 @param string (unicode): XHTML Content |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
81 """ |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
82 subs = [] |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
83 for match in re.finditer(r'<a( \w+="[^"]*")* ?/?>', xhtml): |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
84 tag = match.group(0) |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
85 url = re.search(r'href="([^"]*)"', tag) |
1879
b43ee22eac98
frontends (tools/strings): fixXHTMLLinks skips internal anchors (links starting with "#")
souliane <souliane@mailoo.org>
parents:
1801
diff
changeset
|
86 if url and not url.group(1).startswith("#"): # skip internal anchor |
1801
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
87 if not re.search(r'target="([^"]*)"', tag): # no target |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
88 subs.append((tag, '<a target="_blank"%s' % tag[2:])) |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
89 if not re.match(r"^\w+://", url.group(1)): # no scheme |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
90 subs.append((url.group(0), 'href="http://%s"' % url.group(1))) |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
91 |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
92 for url, new_url in subs: |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
93 xhtml = xhtml.replace(url, new_url) |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
94 return xhtml |
07390a9d1c09
tools (strings): add method fixXHTMLLinks to add a scheme if missing, and force opening in new tab
souliane <souliane@mailoo.org>
parents:
1790
diff
changeset
|
95 |