void IRCSession::on_message_received(const IRCMessage &message) { /* // Do not remove this code. It is useful to see exactly what the server writes to us. IRCRawString s = IRCMessage::create_line(message.get_prefix().to_raw(), message.get_command(), message.get_params()); cb_system_text(IRCText::from_raw(s.substr(0, s.length()-2))); */ switch (message.get_type()) { case IRCMessage::type_numeric_reply: on_numeric_reply(IRCNumericReply(message)); break; case IRCMessage::type_nick: on_nick(IRCNickMessage(message)); break; case IRCMessage::type_join: on_join(IRCJoinMessage(message)); break; case IRCMessage::type_part: on_part(IRCPartMessage(message)); break; case IRCMessage::type_kick: on_kick(IRCKickMessage(message)); break; case IRCMessage::type_quit: on_quit(IRCQuitMessage(message)); break; case IRCMessage::type_channel_mode: on_channel_mode(IRCChannelModeMessage(message)); break; case IRCMessage::type_nick_mode: on_nick_mode(IRCNickModeMessage(message)); break; case IRCMessage::type_topic: on_topic(IRCTopicMessage(message)); break; case IRCMessage::type_privmsg: on_privmsg(IRCPrivateMessage(message)); break; case IRCMessage::type_notice: on_notice(IRCNoticeMessage(message)); break; case IRCMessage::type_ping: on_ping(IRCPingMessage(message)); break; case IRCMessage::type_unknown: default: on_unknown_message(message); break; } }
void client::readMessage() { QByteArray message = socket->readAll(); qDebug() << "reading..."; //qDebug() << message; QList<QByteArray> message_list= message.split('\n'); qDebug() << message_list; QJsonDocument json_document; QJsonObject json_object; QJsonValue type; for (int i=0; i<message_list.size(); i++){ json_document = QJsonDocument::fromJson(message_list.at(i)); json_object = json_document.object(); type = json_object.value("type"); if (type == "login"){ player_id = json_object.value("id").toInt(); emit on_login(); } else if (type == "response"){ if (json_object.value("object") == "rooms"){ rooms = json_object.value("data").toArray(); emit on_refresh_rooms(rooms); } else if (json_object.value("object") == "players"){ players = json_object.value("data").toArray(); emit on_refresh_players(players); } else if (json_object.value("object") == "board"){ board = json_object.value("data").toArray(); emit on_refresh_board(board); } } else if (type == "newroom"){ emit on_create_room(json_object.value("rid").toInt()); } else if (type == "join"){ emit on_join(json_object.value("rid").toInt()); } else if (type == "closegame") { emit on_close_game(); } else if (type == "startgame") { emit on_start_game(); } else if (type == "play") { emit on_update_game(json_object); } else if (type == "win") { emit on_game_over(json_object); } else if (type == "highlight") { emit on_highlight(json_object); } else if (type == "chat") { emit on_chat(json_object); } else if (type == "spectate") { emit on_spectate(json_object.value("rid").toInt()); } } }
/** * process_incomming_data: handles a raw irc line minus the \r\n that * has already been removed in the handle_receive event. * @param line */ void Client::process_incomming_data(std::string &line) { session_t session; int pos; line = trim(line); //std::cout << "server->'" << line << "'" << std::endl; // Get the prefix if (line.at(0) == ':') { if ((pos = line.find(' ')) > 0) { session.prefix = line.substr(1, pos - 1); line = line.substr(pos); } } line = ltrim(line); // Get the code or the command session.code = 0; if (isdigit(line.at(0)) && isdigit(line.at(1)) && isdigit(line.at(2))) { session.code = atoi(line.substr(0, 3).c_str()); line = line.substr(3); } else if ((pos = line.find(' ')) > 0) { session.command = line.substr(0, pos); line = line.substr(pos); } else { // just drop line for now. return; } line = ltrim(line); // Get the params session.param_count = 0; while (line.length() > 0 && session.param_count < 15) { // Message if (line.at(0) == ':') { session.trailing = line.substr(1); break; } // Params if ((pos = line.find(' ')) > 0) { session.params[session.param_count] = line.substr(0, pos); session.param_count++; line = line.substr(pos); line = ltrim(line); } else { session.params[session.param_count] = line; session.param_count++; break; } } //std::cout << "prefix: '" << session.prefix << "', command: '" << session.command << "', code: " << session.code << ", message: '" << session.trailing << "', param_count: " << session.param_count << ", params[0]: '" << session.params[0] << "', params[1]: '" << session.params[1] << "', params[2]: '" << session.params[2] << "'" << std::endl; // Handle Codes switch (session.code) { // We use the MOTD as a signal that we are fully connected to IRC. case RPL_ENDOFMOTD: case ERR_NOMOTD: on_connected(); break; case ERR_NONICKNAMEGIVEN: case ERR_ERRONEUSNICKNAME: case ERR_NICKNAMEINUSE: case ERR_NICKCOLLISION: on_nick_error(session); } // Handle Commands if (session.command == "JOIN") on_join(session); else if (session.command == "NICK") on_nick(session); else if (session.command == "NOTICE") on_notice(session); else if (session.command == "PART") on_part(session); else if (session.command == "PING") on_ping(session); else if (session.command == "PRIVMSG") on_privmsg(session); else if (session.command == "QUIT") on_quit(session); //std::cout << "---------------------------------------------------------------------------------------------------------------------------" << std::endl; }