| 1 | ////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // mpirc.hxx |
| 4 | // |
| 5 | // started November 2020 |
| 6 | // Authors: Henning Stahlke, Michael Filhol |
| 7 | // |
| 8 | // This program is free software; you can redistribute it and/or |
| 9 | // modify it under the terms of the GNU General Public License as |
| 10 | // published by the Free Software Foundation; either version 2 of the |
| 11 | // License, or (at your option) any later version. |
| 12 | // |
| 13 | // This program is distributed in the hope that it will be useful, but |
| 14 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | // General Public License for more details. |
| 17 | // |
| 18 | // You should have received a copy of the GNU General Public License |
| 19 | // along with this program; if not, write to the Free Software |
| 20 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | // |
| 22 | // $Id$ |
| 23 | // |
| 24 | ////////////////////////////////////////////////////////////////////// |
| 25 | |
| 26 | #ifndef MPIRC_H |
| 27 | #define MPIRC_H |
| 28 | |
| 29 | #include <deque> |
| 30 | #include <string> |
| 31 | |
| 32 | #include <simgear/compiler.h> |
| 33 | #include <simgear/props/props.hxx> |
| 34 | #include <simgear/io/raw_socket.hxx> |
| 35 | #include <simgear/io/sg_socket.hxx> |
| 36 | |
| 37 | const std::string IRC_DEFAULT_PORT {"6667"}; |
| 38 | const int IRC_BUFFER_SIZE = 1024; |
| 39 | |
| 40 | struct IRCMessage { |
| 41 | std::string sender; |
| 42 | std::string textline; |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | IRCConnection implements a basic IRC client for transmitting and receiving |
| 47 | private messages via an IRC server |
| 48 | |
| 49 | In general it is possible to have multiple instances of this class but you |
| 50 | have to consider the following points: |
| 51 | - you cannot connect to a server with the same nickname more than once at a time |
| 52 | - if you want to expose status info to the property tree, you have to pass |
| 53 | an unique prefix to setupProperties() per instance |
| 54 | */ |
| 55 | class IRCConnection : SGSocket |
| 56 | { |
| 57 | public: |
| 58 | IRCConnection(const std::string &nickname, const std::string &servername, const std::string &port = IRC_DEFAULT_PORT); |
| 59 | ~IRCConnection(); |
| 60 | |
| 61 | void setupProperties(std::string path); |
| 62 | void update(); |
| 63 | |
| 64 | bool login(const std::string &nickname); |
| 65 | bool login(); |
| 66 | void quit(); |
| 67 | |
| 68 | bool sendPrivmsg(const std::string &recipient, const std::string &textline); |
| 69 | bool join(const std::string &channel); |
| 70 | bool part(const std::string &channel); |
| 71 | |
| 72 | |
| 73 | bool isConnected() const { return _connected; } |
| 74 | bool isReady() const { return _logged_in; } |
| 75 | bool hasMessage() const { return !_incoming_private_messages.empty(); } |
| 76 | IRCMessage getMessage(); |
| 77 | |
| 78 | private: |
| 79 | bool connect(); |
| 80 | void disconnect(); |
| 81 | void pong(const std::string &recipient); |
| 82 | bool parseReceivedLine(std::string irc_line); |
| 83 | |
| 84 | bool _connected {false}; // TCP session ok |
| 85 | bool _logged_in {false}; // IRC login completed |
| 86 | std::string _nickname {""}; |
| 87 | char _read_buffer[IRC_BUFFER_SIZE]; |
| 88 | std::deque<IRCMessage> _incoming_private_messages; |
| 89 | |
| 90 | SGPropertyNode *_pReadyFlag {nullptr}; |
| 91 | SGPropertyNode *_pMessageCountIn {nullptr}; |
| 92 | SGPropertyNode *_pMessageCountOut {nullptr}; |
| 93 | SGPropertyNode *_pIRCReturnCode {nullptr}; |
| 94 | }; |
| 95 | |
| 96 | #endif |