39IRCConnection::IRCConnection(
const std::string &nickname,
const std::string &servername,
const std::string &port) : SGSocket(servername, port,
"tcp"),
51 if (path.back() !=
'/') path.push_back(
'/');
52 if (!_pReadyFlag) _pReadyFlag =
fgGetNode(path +
"irc-ready",
true);
53 _pReadyFlag->setBoolValue(_logged_in);
55 if (!_pMessageCountIn) _pMessageCountIn =
fgGetNode(path +
"msg-count-in",
true);
56 if (!_pMessageCountOut) _pMessageCountOut =
fgGetNode(path +
"msg-count-out",
true);
57 if (!_pIRCReturnCode) _pIRCReturnCode =
fgGetNode(path +
"last-return-code",
true);
63 if (!_connected && !connect()) {
66 if (!nickname.empty()) {
69 SG_LOG(SG_NETWORK, SG_WARN,
"IRC login requires nickname argument.");
73 std::string lines(
"NICK ");
81 return writestring(lines.c_str());
87 return login(_nickname);
93 if (!_connected)
return;
94 writestring(
"QUIT goodbye\r\n");
101 SG_LOG(SG_NETWORK, SG_WARN,
"IRC 'privmsg' command unvailable. Login first!");
104 std::string line(
"PRIVMSG ");
109 if (writestring(line.c_str())) {
110 if (_pMessageCountOut) _pMessageCountOut->setIntValue(_pMessageCountOut->getIntValue() + 1);
111 if (_pIRCReturnCode) _pIRCReturnCode->setStringValue(
"");
114 SG_LOG(SG_NETWORK, SG_WARN,
"IRC send privmsg failed.");
123 SG_LOG(SG_NETWORK, SG_WARN,
"IRC 'join' command unvailable. Login first!");
126 std::string lines(
"JOIN ");
129 return writestring(lines.c_str());
136 SG_LOG(SG_NETWORK, SG_WARN,
"IRC 'part' command unvailable. Login first!");
139 std::string lines(
"PART ");
142 return writestring(lines.c_str());
154 if (_connected && readline(_read_buffer,
sizeof(_read_buffer) - 1) > 0) {
155 std::string line(_read_buffer);
156 parseReceivedLine(line);
165bool IRCConnection::connect()
171 _connected = open(SG_IO_OUT);
176 SG_LOG(SG_NETWORK, SG_WARN,
"IRCConnection::connect error");
181void IRCConnection::disconnect()
184 if (_pReadyFlag) _pReadyFlag->setBoolValue(_logged_in);
189 SG_LOG(SG_NETWORK, SG_INFO,
"IRCConnection::disconnect");
193void IRCConnection::pong(
const std::string &recipient)
195 if (!_connected)
return;
196 std::string line(
"PONG ");
199 writestring(line.c_str());
202bool IRCConnection::parseReceivedLine(std::string line)
244 std::size_t pos = line.find(
" ", 1);
246 if (line.at(0) ==
':') {
247 prefix = line.substr(1, pos - 1);
248 std::size_t end = line.find(
" ", pos + 1);
249 command = line.substr(pos + 1, end - pos - 1);
254 params = line.substr(pos + 1);
261 if (_pMessageCountIn) _pMessageCountIn->setIntValue(_pMessageCountIn->getIntValue() + 1);
262 std::string recipient = params.substr(0, params.find(
" :"));
264 if (recipient == _nickname) {
265 struct IRCMessage rcv;
266 rcv.sender = prefix.substr(0, prefix.find(
"!"));
267 rcv.textline = params.substr(params.find(
":") + 1);
268 _incoming_private_messages.push_back(rcv);
273 SG_LOG(SG_NETWORK, SG_DEV_WARN,
"Ignoring PRIVMSG to '" + recipient +
"' (should be '" + _nickname +
"')");
275 }
else if (
command ==
"PING") {
277 std::string server = params.substr(0, params.find(
" "));
279 }
else if (
command ==
"JOIN") {
281 std::string channel = params.substr(0, params.find(
" "));
282 SG_LOG(SG_NETWORK, SG_DEV_WARN,
"Joined IRC channel " + channel);
286 if (_pReadyFlag) _pReadyFlag->setBoolValue(1);
302 if (_pIRCReturnCode) _pIRCReturnCode->setStringValue(params);
322 if (!_incoming_private_messages.empty()) {
323 entry = _incoming_private_messages.front();
324 _incoming_private_messages.pop_front();
bool sendPrivmsg(const std::string &recipient, const std::string &textline)
bool part(const std::string &channel)
bool join(const std::string &channel)
IRCConnection(const std::string &nickname, const std::string &servername, const std::string &port=IRC_DEFAULT_PORT)
void setupProperties(std::string path)
SGCommandMgr::command_t command
const std::string IRC_RPL_YOURID
const std::string IRC_RPL_WELCOME
const std::string IRC_MSG_TERMINATOR
const std::string IRC_RPL_MOTDSTART
const std::string IRC_RPL_ENDOFMOTD
const std::string IRC_RPL_MOTD
const std::string IRC_ERR_NOSUCHNICK
const std::string IRC_TEST_CHANNEL
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.