ircclient.h 2.7 KB

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