irc.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef IRC_H
  2. #define IRC_H
  3. #include <boost/asio.hpp>
  4. #include <boost/asio/ssl.hpp>
  5. #include <boost/format.hpp>
  6. #include <boost/lexical_cast.hpp>
  7. #include <boost/optional.hpp>
  8. #include <boost/signals2/mutex.hpp>
  9. #include <chrono>
  10. #include <iomanip> // put_time
  11. #include <string>
  12. // #include <vector>
  13. #include <algorithm>
  14. #include <ctime> // time_t
  15. #include <fstream>
  16. #include <set>
  17. #include <boost/asio/io_context.hpp>
  18. #define SENDQ
  19. std::string base64encode(const std::string &str);
  20. void string_toupper(std::string &str);
  21. std::vector<std::string> split_limit(std::string &text, int max = -1);
  22. std::vector<std::string> irc_split(std::string &text);
  23. std::string parse_nick(std::string &name);
  24. void remove_channel_modes(std::string &nick);
  25. class message_stamp {
  26. public:
  27. message_stamp() { time(&stamp); }
  28. std::time_t stamp;
  29. std::vector<std::string> buffer;
  30. };
  31. // using error_code = boost::system::error_code;
  32. class ircClient {
  33. using error_code = boost::system::error_code;
  34. public:
  35. ircClient(boost::asio::io_context &io_context);
  36. // async startup
  37. void begin(void);
  38. // thread-safe write to IRC
  39. void write(std::string output);
  40. #ifdef SENDQ
  41. // queued writer
  42. void write_queue(std::string target, std::string output);
  43. void on_sendq(error_code error);
  44. #endif
  45. // configuration
  46. std::string hostname;
  47. std::string port;
  48. std::string server_password;
  49. std::string nick;
  50. std::string sasl_plain_password;
  51. std::string username = "bzbz";
  52. std::string realname;
  53. std::string autojoin;
  54. std::string version;
  55. // filename to use for logfile
  56. std::string debug_output;
  57. std::ofstream debug_file;
  58. protected:
  59. boost::signals2::mutex talkto_lock;
  60. std::string _talkto;
  61. public:
  62. std::string talkto(void) {
  63. talkto_lock.lock();
  64. std::string ret{_talkto};
  65. talkto_lock.unlock();
  66. return ret;
  67. };
  68. void talkto(std::string talkvalue) {
  69. talkto_lock.lock();
  70. _talkto = talkvalue;
  71. talkto_lock.unlock();
  72. };
  73. // channels / users
  74. std::atomic<bool> channels_updated;
  75. boost::signals2::mutex channels_lock;
  76. std::map<std::string, std::set<std::string>> channels;
  77. std::atomic<int> max_nick_length;
  78. void message(std::string msg);
  79. std::atomic<bool> shutdown;
  80. // thread-safe messages access
  81. virtual void message_append(message_stamp &msg);
  82. boost::optional<message_stamp> message_pop(void);
  83. std::vector<std::string> errors;
  84. std::atomic<bool> registered;
  85. private:
  86. void find_max_nick_length(void);
  87. boost::signals2::mutex lock;
  88. std::vector<message_stamp> messages;
  89. std::string original_nick;
  90. int nick_retry;
  91. bool logging;
  92. std::ofstream &log(void);
  93. // async callbacks
  94. void on_resolve(error_code error,
  95. boost::asio::ip::tcp::resolver::results_type results);
  96. void on_connect(error_code error,
  97. boost::asio::ip::tcp::endpoint const &endpoint);
  98. void on_handshake(error_code error);
  99. void on_write(error_code error, std::size_t bytes_transferred);
  100. void read_until(error_code error, std::size_t bytes);
  101. void on_shutdown(error_code error);
  102. // end async callback
  103. void receive(std::string &text);
  104. std::string registration(void);
  105. // initialization order matters for socket, ssl_context!
  106. //
  107. // socket up here causes segmentation fault!
  108. // boost::asio::ssl::stream<ip::tcp::socket> socket;
  109. boost::asio::ip::tcp::resolver resolver;
  110. boost::asio::ssl::context ssl_context;
  111. boost::asio::streambuf response;
  112. boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket;
  113. #ifdef SENDQ
  114. boost::asio::high_resolution_timer sendq_timer;
  115. std::map<std::string, std::vector<std::string>> sendq;
  116. std::vector<std::string> sendq_targets;
  117. int sendq_current;
  118. std::atomic<bool> sendq_active;
  119. int sendq_ms;
  120. #endif
  121. boost::asio::io_context &context;
  122. };
  123. #endif