File indexing completed on 2025-03-09 04:54:14

0001 /* SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
0002 
0003    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 #pragma once
0006 
0007 #include "kmime/kmime_header_parsing.h"
0008 #include "kmime/kmime_message.h"
0009 #include "messagecore_export.h"
0010 
0011 #include <QList>
0012 #include <QStringList>
0013 class QUrl;
0014 
0015 namespace KMime
0016 {
0017 namespace Types
0018 {
0019 struct Address;
0020 using AddressList = QList<Address>;
0021 class Mailbox;
0022 }
0023 namespace Headers
0024 {
0025 namespace Generics
0026 {
0027 class MailboxList;
0028 class AddressList;
0029 }
0030 }
0031 }
0032 
0033 namespace MessageCore
0034 {
0035 /**
0036  * This namespace contain helper functions for string manipulation
0037  */
0038 namespace StringUtil
0039 {
0040 /**
0041  * Parses a mailto: url and extracts the information in the QMap (field name as key).
0042  */
0043 [[nodiscard]] MESSAGECORE_EXPORT QList<QPair<QString, QString>> parseMailtoUrl(const QUrl &url);
0044 
0045 /**
0046  * Strips the signature blocks from a message text. "-- " is considered as a signature block separator.
0047  *
0048  * @param message The message to remove the signature block from.
0049  */
0050 [[nodiscard]] MESSAGECORE_EXPORT QString stripSignature(const QString &message);
0051 
0052 /**
0053  * Splits the given address list @p text into separate addresses.
0054  */
0055 [[nodiscard]] MESSAGECORE_EXPORT KMime::Types::AddressList splitAddressField(const QByteArray &text);
0056 
0057 /**
0058  * Generates the Message-Id. It uses either the Message-Id @p suffix
0059  * defined by the user or the given email address as suffix. The @p address
0060  * must be given as addr-spec as defined in RFC 2822.
0061  */
0062 [[nodiscard]] MESSAGECORE_EXPORT QString generateMessageId(const QString &address, const QString &suffix);
0063 
0064 /**
0065  * Quotes the following characters which have a special meaning in HTML:
0066  * '<'  '>'  '&'  '"'. Additionally '\\n' is converted to "<br />" if
0067  * @p removeLineBreaks is false. If @p removeLineBreaks is true, then
0068  * '\\n' is removed. Last but not least '\\r' is removed.
0069  */
0070 [[nodiscard]] MESSAGECORE_EXPORT QString quoteHtmlChars(const QString &text, bool removeLineBreaks = false);
0071 
0072 /**
0073  * Removes all private header fields (e.g. *Status: and X-KMail-*) from the given @p message.
0074  * if cleanUpHeader is false don't remove X-KMail-Identity and X-KMail-Dictionary which is useful when we want restore mail.
0075  */
0076 MESSAGECORE_EXPORT void removePrivateHeaderFields(const KMime::Message::Ptr &message, bool cleanUpHeader = true);
0077 
0078 /**
0079  * Returns the @p message contents with the headers that should not be sent stripped off.
0080  */
0081 [[nodiscard]] MESSAGECORE_EXPORT QByteArray asSendableString(const KMime::Message::Ptr &message);
0082 
0083 /**
0084  * Return the message header with the headers that should not be sent stripped off.
0085  */
0086 [[nodiscard]] MESSAGECORE_EXPORT QByteArray headerAsSendableString(const KMime::Message::Ptr &message);
0087 
0088 /**
0089  * Used to determine if the visible part of the anchor contains
0090  * only the name part and not the given emailAddr or the full address.
0091  */
0092 enum Display {
0093     DisplayNameOnly,
0094     DisplayFullAddress,
0095 };
0096 
0097 /**
0098  * Used to determine if the address should be a link or not.
0099  */
0100 enum Link {
0101     ShowLink,
0102     HideLink,
0103 };
0104 
0105 /**
0106  * Used to determine if the address field should be expandable/collapsible.
0107  */
0108 enum AddressMode {
0109     ExpandableAddresses,
0110     FullAddresses,
0111 };
0112 
0113 /**
0114  * Converts the email address(es) to (a) nice HTML mailto: anchor(s).
0115  * @p display determines if only the name part or the entire address should be returned.
0116  * @p cssStyle a custom css template.
0117  * @p link determines if the result should be a html link or not.
0118  * @p expandable determines if a long list of addresses should be expandable or shown
0119  * in full.
0120  * @p fieldName the name that the divs should be based on if expandable is set to ExpanableAddesses.
0121  * @p The number of addresses to show before collapsing the rest, if expandable is set to
0122  * ExpandableAddresses.
0123  */
0124 [[nodiscard]] MESSAGECORE_EXPORT QString emailAddrAsAnchor(const KMime::Headers::Generics::MailboxList *mailboxList,
0125                                                            Display display = DisplayNameOnly,
0126                                                            const QString &cssStyle = QString(),
0127                                                            Link link = ShowLink,
0128                                                            AddressMode expandable = FullAddresses,
0129                                                            const QString &fieldName = QString(),
0130                                                            int collapseNumber = 4);
0131 
0132 /**
0133  * Same as above method, only for AddressList headers.
0134  */
0135 [[nodiscard]] MESSAGECORE_EXPORT QString emailAddrAsAnchor(const KMime::Headers::Generics::AddressList *addressList,
0136                                                            Display display = DisplayNameOnly,
0137                                                            const QString &cssStyle = QString(),
0138                                                            Link link = ShowLink,
0139                                                            AddressMode expandable = FullAddresses,
0140                                                            const QString &fieldName = QString(),
0141                                                            int collapseNumber = 4);
0142 
0143 /**
0144  * Same as the above, only for Mailbox::List types.
0145  */
0146 [[nodiscard]] MESSAGECORE_EXPORT QString emailAddrAsAnchor(const QList<KMime::Types::Mailbox> &mailboxList,
0147                                                            Display display = DisplayNameOnly,
0148                                                            const QString &cssStyle = QString(),
0149                                                            Link link = ShowLink,
0150                                                            AddressMode expandable = FullAddresses,
0151                                                            const QString &fieldName = QString(),
0152                                                            int collapseNumber = 4);
0153 
0154 /**
0155  * Returns true if the given address is contained in the given address list.
0156  */
0157 [[nodiscard]] MESSAGECORE_EXPORT bool addressIsInAddressList(const QString &address, const QStringList &addresses);
0158 
0159 /**
0160  * Uses the hostname as domain part and tries to determine the real name
0161  * from the entries in the password file.
0162  */
0163 [[nodiscard]] MESSAGECORE_EXPORT QString guessEmailAddressFromLoginName(const QString &userName);
0164 
0165 /**
0166  *  Relayouts the given string so that the individual lines don't exceed the given
0167  *  maximal length.
0168  *
0169  *  As the name of the function implies, it is smart, which means it deals with quoting
0170  *  correctly. This means if a line already starts with quote characters and needs to be
0171  *  broken, the same quote characters are prepended to the next line as well.
0172  *
0173  *  This does _not_ add new quote characters in front of every line, that is the responsibility
0174  *  of the caller.
0175  *
0176  *  @param message The string which it to be relayouted
0177  *  @param maxLineLength reformat text to be this amount of columns at maximum. Note that this
0178  *                       also includes the trailing \n!
0179  */
0180 [[nodiscard]] MESSAGECORE_EXPORT QString smartQuote(const QString &message, int maxLineLength);
0181 
0182 /**
0183  * Convert quote wildcards into the final quote prefix.
0184  * @param wildString the string to be converted
0185  * @param fromDisplayString displayable string of the from email address
0186  */
0187 [[nodiscard]] MESSAGECORE_EXPORT QString formatQuotePrefix(const QString &wildString, const QString &fromDisplayString);
0188 
0189 /**
0190  * Cleans a filename by replacing characters not allowed or wanted on the filesystem
0191  *  e.g. ':', '/', '\' with '_'
0192  */
0193 [[nodiscard]] MESSAGECORE_EXPORT QString cleanFileName(const QString &fileName);
0194 
0195 /**
0196  * Removes the forward and reply marks (e.g. Re: or Fwd:) from a @p subject string.
0197  * Additional markers to act on can be specified in the MessageCore::GlobalSettings
0198  * object.
0199  */
0200 [[nodiscard]] MESSAGECORE_EXPORT QString stripOffPrefixes(const QString &subject);
0201 
0202 MESSAGECORE_EXPORT void setEncodingFile(QUrl &url, const QString &encoding);
0203 
0204 /** Check for prefixes @p prefixRegExps in #subject(). If none
0205       is found, @p newPrefix + ' ' is prepended to the subject and the
0206       resulting string is returned. If @p replace is true, any
0207       sequence of whitespace-delimited prefixes at the beginning of
0208       #subject() is replaced by @p newPrefix
0209   **/
0210 [[nodiscard]] MESSAGECORE_EXPORT QString cleanSubject(KMime::Message *msg, const QStringList &prefixRegExps, bool replace, const QString &newPrefix);
0211 
0212 /** Return this mails subject, with all "forward" and "reply"
0213       prefixes removed */
0214 [[nodiscard]] MESSAGECORE_EXPORT QString cleanSubject(KMime::Message *msg);
0215 
0216 /** Return this mails subject, formatted for "forward" mails */
0217 [[nodiscard]] MESSAGECORE_EXPORT QString forwardSubject(KMime::Message *msg);
0218 
0219 /** Return this mails subject, formatted for "reply" mails */
0220 [[nodiscard]] MESSAGECORE_EXPORT QString replySubject(KMime::Message *msg);
0221 /** Check for prefixes @p prefixRegExps in @p str. If none
0222       is found, @p newPrefix + ' ' is prepended to @p str and the
0223       resulting string is returned. If @p replace is true, any
0224       sequence of whitespace-delimited prefixes at the beginning of
0225       @p str is replaced by @p newPrefix.
0226   **/
0227 [[nodiscard]] MESSAGECORE_EXPORT QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, bool replace, const QString &newPrefix);
0228 }
0229 }