eric6/Network/IRC/IrcUtilities.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing functions used by several IRC objects.
8 """
9
10 from __future__ import unicode_literals
11
12 import re
13
14 from PyQt5.QtCore import QTime, QCoreApplication
15 from PyQt5.QtWidgets import QApplication
16
17 import Utilities
18 import Preferences
19
20
21 __UrlRe = re.compile(
22 r"""((?:http|ftp|https):\/\/[\w\-_]+(?:\.[\w\-_]+)+"""
23 r"""(?:[\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)""")
24 __ColorRe = re.compile(
25 r"""((?:\x03(?:0[0-9]|1[0-5]|[0-9])?(?:,(?:0[0-9]|1[0-5]|[0-9]))?)"""
26 r"""|\x02|\x03|\x13|\x15|\x16|\x17|\x1d|\x1f)""")
27
28
29 def ircTimestamp():
30 """
31 Module method to generate a time stamp string.
32
33 @return time stamp (string)
34 """
35 if Preferences.getIrc("ShowTimestamps"):
36 if Preferences.getIrc("TimestampIncludeDate"):
37 if QApplication.isLeftToRight():
38 f = "{0} {1}"
39 else:
40 f = "{1} {0}"
41 formatString = f.format(Preferences.getIrc("DateFormat"),
42 Preferences.getIrc("TimeFormat"))
43 else:
44 formatString = Preferences.getIrc("TimeFormat")
45 return '<font color="{0}">[{1}]</font> '.format(
46 Preferences.getIrc("TimestampColour"),
47 QTime.currentTime().toString(formatString))
48 else:
49 return ""
50
51
52 def ircFilter(msg):
53 """
54 Module method to make the message HTML compliant and detect URLs.
55
56 @param msg message to process (string)
57 @return processed message (string)
58 """
59 # step 1: cleanup message
60 msg = Utilities.html_encode(msg)
61
62 # step 2: replace IRC formatting characters
63 openTags = []
64 parts = __ColorRe.split(msg)
65 msgParts = []
66 for part in parts:
67 if part == "\x02": # bold
68 if openTags and openTags[-1] == "b":
69 msgParts.append("</" + openTags.pop(-1) + ">")
70 else:
71 msgParts.append("<b>")
72 openTags.append("b")
73 elif part in ["\x03", "\x17"]:
74 if Preferences.getIrc("EnableIrcColours"):
75 if openTags and openTags[-1] == "span":
76 msgParts.append("</" + openTags.pop(-1) + ">")
77 else:
78 continue
79 else:
80 continue
81 elif part == "\x0f": # reset
82 while openTags:
83 msgParts.append("</" + openTags.pop(-1) + ">")
84 elif part == "\x13": # strikethru
85 if openTags and openTags[-1] == "s":
86 msgParts.append("</" + openTags.pop(-1) + ">")
87 else:
88 msgParts.append("<s>")
89 openTags.append("s")
90 elif part in ["\x15", "\x1f"]: # underline
91 if openTags and openTags[-1] == "u":
92 msgParts.append("</" + openTags.pop(-1) + ">")
93 else:
94 msgParts.append("<u>")
95 openTags.append("u")
96 elif part == "\x16":
97 # revert color not supported
98 continue
99 elif part == "\x1d": # italic
100 if openTags and openTags[-1] == "i":
101 msgParts.append("</" + openTags.pop(-1) + ">")
102 else:
103 msgParts.append("<i>")
104 openTags.append("i")
105 elif part.startswith("\x03"):
106 if Preferences.getIrc("EnableIrcColours"):
107 colors = part[1:].split(",", 1)
108 if len(colors) == 1:
109 # foreground color only
110 tag = '<span style="color:{0}">'.format(Preferences.getIrc(
111 "IrcColor{0}".format(int(colors[0]))))
112 else:
113 if colors[0]:
114 # foreground and background
115 tag = '<span style="background-color:{0};color={1}">'\
116 .format(Preferences.getIrc(
117 "IrcColor{0}".format(int(colors[0]))),
118 Preferences.getIrc(
119 "IrcColor{0}".format(int(colors[1]))))
120 else:
121 # background only
122 tag = '<span style="background-color:{0}">'.format(
123 Preferences.getIrc(
124 "IrcColor{0}".format(int(colors[1]))))
125 msgParts.append(tag)
126 openTags.append("span")
127 else:
128 continue
129 else:
130 msgParts.append(part)
131 msg = "".join(msgParts)
132
133 # step 3: find http and https links
134 parts = __UrlRe.split(msg)
135 msgParts = []
136 for part in parts:
137 if part.startswith(("http://", "https://", "ftp://")):
138 msgParts.append('<a href="{0}" style="color:{1}">{0}</a>'.format(
139 part, Preferences.getIrc("HyperlinkColour")))
140 else:
141 msgParts.append(part)
142
143 return "".join(msgParts)
144
145
146 __channelModesDict = None
147
148
149 def __initChannelModesDict():
150 """
151 Private module function to initialize the channels modes dictionary.
152 """
153 global __channelModesDict
154
155 modesDict = {
156 "a": QCoreApplication.translate("IrcUtilities", "anonymous"),
157 "b": QCoreApplication.translate("IrcUtilities", "ban mask"),
158 "c": QCoreApplication.translate("IrcUtilities", "no colors allowed"),
159 "e": QCoreApplication.translate("IrcUtilities", "ban exception mask"),
160 "i": QCoreApplication.translate("IrcUtilities", "invite only"),
161 "k": QCoreApplication.translate("IrcUtilities", "password protected"),
162 "l": QCoreApplication.translate("IrcUtilities", "user limit"),
163 "m": QCoreApplication.translate("IrcUtilities", "moderated"),
164 "n": QCoreApplication.translate("IrcUtilities",
165 "no messages from outside"),
166 "p": QCoreApplication.translate("IrcUtilities", "private"),
167 "q": QCoreApplication.translate("IrcUtilities", "quiet"),
168 "r": QCoreApplication.translate("IrcUtilities", "reop channel"),
169 "s": QCoreApplication.translate("IrcUtilities", "secret"),
170 "t": QCoreApplication.translate("IrcUtilities", "topic protection"),
171 "I": QCoreApplication.translate("IrcUtilities", "invitation mask"),
172 }
173 __channelModesDict = modesDict
174
175
176 def getChannelModesDict():
177 """
178 Module function to get the dictionary with the channel modes mappings.
179
180 @return dictionary with channel modes mapping (dict)
181 """
182 global __channelModesDict
183
184 if __channelModesDict is None:
185 __initChannelModesDict()
186
187 return __channelModesDict

eric ide

mercurial