ircclient.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef IRCCLIENT_H
  2. #define IRCCLIENT_H
  3. #include "connectiondata.h"
  4. #include <QRegularExpression>
  5. #include <QSharedPointer>
  6. #include <QTcpSocket>
  7. #include <QThread>
  8. #include <QTimer>
  9. #include <map>
  10. constexpr int NICK_RECOVER_TIMER = 60000; // 1 minute
  11. constexpr int THREAD_SLEEP_ON_RECONNECT_TIMER = 3; // seconds
  12. constexpr int USER_LIST_ACTIALIZE_TIMER = 1800000; // 30 minutes
  13. constexpr int WRITING_STOP_TIMER = 1100; // 1.1 second
  14. constexpr int PING_TIMEOUT_TIMER = 361000; // 361 seconds
  15. const QString TRIGGER_CHANNEL_FOR_URL_PATTERN = "%CHANNEL_FOR_URL%";
  16. class IrcClient : public QObject
  17. {
  18. Q_OBJECT
  19. public:
  20. IrcClient(const ConnectionData& config, QObject *parent = nullptr);
  21. ~IrcClient();
  22. QStringList getOnlineUsers(const QString& channel);
  23. void connectToServer();
  24. private:
  25. enum class IrcCode {
  26. Failed = -1,
  27. ChannelTopic = 332,
  28. NamesList = 353,
  29. EndOfNamesList = 366,
  30. NickNameIsAlreadyInUse = 433
  31. };
  32. void write(const QString& message, bool log = true);
  33. void process(const QString& message);
  34. IrcCode getServerCode(const QString& message);
  35. QString getChannelName(const QString& message);
  36. QString getNickname(const QString& message);
  37. void toTrigger(const QString& channel, const QString& nickname, const QString& message);
  38. void toChatLog(QString channel, const QString& nick, const QString& message);
  39. void consoleLog(const QString& message);
  40. void errorLog(const QString& message);
  41. QTcpSocket* m_socket;
  42. ConnectionData m_connectionData;
  43. QTimer m_timerToJoin;
  44. QTimer m_nickRecover;
  45. QTimer m_usersActualize; // to get all users status (op, owner, etc)
  46. QTimer m_pingTimeout;
  47. QTimer m_stopWriting; // for output messages flow control (one in second as maximum)
  48. const QRegularExpression m_rgxPrivmsg {"^[^\\s]*\\sPRIVMSG"};
  49. const QRegularExpression m_rgxJoin {"^[^\\s]*\\sJOIN"};
  50. const QRegularExpression m_rgxPart {"^[^\\s]*\\sPART"};
  51. const QRegularExpression m_rgxNick {"^[^\\s]*\\sNICK"};
  52. const QRegularExpression m_rgxMode {"^[^\\s]*\\sMODE"};
  53. const QRegularExpression m_rgxKick {"^[^\\s]*\\sKICK"};
  54. const QRegularExpression m_rgxQuit {"^[^\\s]*\\sQUIT"};
  55. const QRegularExpression m_rgxTopic {"^[^\\s]*\\sTOPIC"};
  56. bool m_reconnectReport;
  57. bool m_connected;
  58. bool m_triggersIsStopped;
  59. std::map<QString, QStringList> m_online; // channel, nicks
  60. QStringList m_readNamesList; // channel in reading
  61. QString m_readingBuffer;
  62. bool m_shouldAppendMessage;
  63. private slots:
  64. void onRead();
  65. void onLogin();
  66. void onDisconnected();
  67. void pingTimedOut();
  68. void actualizeUsersList();
  69. void nickRecover();
  70. signals:
  71. void startInfo(QString server, QStringList channels);
  72. void userOnline(QString server, QString channel, QStringList users);
  73. void topicChanged(QString server, QString channel, QString topic);
  74. void myOnline(QString server, quint8 status);
  75. void myNickname(QString server, QString nick);
  76. void newMessage(QString server, QString channel, QString nick, QString text);
  77. };
  78. #endif // IRCCLIENT_H