1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3 
4     SPDX-FileCopyrightText: 2004 Peter Simonsson <psn@linux.se>
5     SPDX-FileCopyrightText: 2006-2008 Eike Hein <hein@kde.org>
6 */
7 
8 #ifndef COMMON_H
9 #define COMMON_H
10 
11 #include <QByteArray>
12 #include <QHash>
13 #include <QRegularExpression>
14 #include <QStringList>
15 
16 class QString;
17 
18 namespace Konversation
19 {
20     static QRegularExpression ircMarkupsRegExp(QStringLiteral("[\\o{0000}-\\o{0037}]"));
21 
22     static QRegExp colorRegExp(QStringLiteral("((\003([0-9]|0[0-9]|1[0-5])(,([0-9]|0[0-9]|1[0-5])|)|\017)|\x02|\x03|\x09|\x13|\x15|\x16|\x1d|\x1e|\x1f)"));
23 
24     static QRegularExpression urlPattern(QStringLiteral("\\b((?:(?:([a-z][\\w\\.-]+:/{1,3})|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|\\}\\]|[^\\s`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6])|[a-z0-9.\\-+_]+@[a-z0-9.\\-]+[.][a-z]{1,5}[^\\s/`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6]))").arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019)));
25 
26     static QRegularExpression chanExp(QStringLiteral("(^|\\s|^\"|\\s\"|,|'|\\(|\\:|!|@|%|\\+)(#[^,\\s;\\)\\:\\/\\(\\<\\>]*[^.,\\s;\\)\\:\\/\\(\"\''\\<\\>?%1%2%3%4%5%6])").arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019)));
27 
28     enum TabNotifyType
29     {
30         tnfNick,
31         tnfHighlight,
32         tnfPrivate,
33         tnfNormal,
34         tnfSystem,
35         tnfControl,
36         tnfNone
37     };
38 
39     enum ConnectionState
40     {
41         SSNeverConnected,
42         SSDeliberatelyDisconnected,
43         SSInvoluntarilyDisconnected,
44         SSScheduledToConnect,
45         SSConnecting,
46         SSConnected
47     };
48 
49     enum ConnectionFlag
50     {
51         SilentlyReuseConnection,
52         PromptToReuseConnection,
53         CreateNewConnection
54     };
55 
56     struct TextUrlData
57     {
58         QList<QPair<int, int> > urlRanges;
59         QStringList fixedUrls;
60     };
61 
62     struct TextChannelData
63     {
64         QList<QPair<int, int> > channelRanges;
65         QStringList fixedChannels;
66     };
67 
68     QString removeIrcMarkup(const QString& text);
69     QString doVarExpansion(const QString& text);
70     QString replaceFormattingCodes(const QString& text);
71     QString replaceIRCMarkups(const QString& text);
hasIRCMarkups(const QString & text)72     inline bool hasIRCMarkups(const QString& text)
73     {
74         return text.contains(ircMarkupsRegExp);
75     }
76 
77     QList<QPair<int, int> > getUrlRanges(const QString& text);
78     QList<QPair<int, int> > getChannelRanges(const QString& text);
79     TextUrlData extractUrlData(const QString& string, bool doUrlFixup = true);
80     TextChannelData extractChannelData(const QString& text, bool doChannelFixup = true);
81     bool isUrl(const QString& text);
82     QString extractColorCodes(const QString& text);
83 
84     bool isUtf8(const QByteArray& text);
85     uint colorForNick(const QString& nickname);
86 
87     static QHash<QChar,QString> m_modesHash;
88     QHash<QChar,QString> getChannelModesHash();
89 
90     QString sterilizeUnicode(const QString& s);
91     QString& sterilizeUnicode(QString& s);
92     QStringList& sterilizeUnicode(QStringList& list);
93     QStringList sterilizeUnicode(const QStringList& inVal);
94 }
95 #endif
96