serverpackethandler.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. /*
  2. Minetest
  3. Copyright (C) 2015 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "chatmessage.h"
  17. #include "server.h"
  18. #include "log.h"
  19. #include "content_sao.h"
  20. #include "emerge.h"
  21. #include "mapblock.h"
  22. #include "modchannels.h"
  23. #include "nodedef.h"
  24. #include "remoteplayer.h"
  25. #include "rollback_interface.h"
  26. #include "scripting_server.h"
  27. #include "settings.h"
  28. #include "tool.h"
  29. #include "version.h"
  30. #include "network/connection.h"
  31. #include "network/networkprotocol.h"
  32. #include "network/serveropcodes.h"
  33. #include "util/auth.h"
  34. #include "util/base64.h"
  35. #include "util/pointedthing.h"
  36. #include "util/serialize.h"
  37. #include "util/srp.h"
  38. void Server::handleCommand_Deprecated(NetworkPacket* pkt)
  39. {
  40. infostream << "Server: " << toServerCommandTable[pkt->getCommand()].name
  41. << " not supported anymore" << std::endl;
  42. }
  43. void Server::handleCommand_Init(NetworkPacket* pkt)
  44. {
  45. if(pkt->getSize() < 1)
  46. return;
  47. RemoteClient* client = getClient(pkt->getPeerId(), CS_Created);
  48. std::string addr_s;
  49. try {
  50. Address address = getPeerAddress(pkt->getPeerId());
  51. addr_s = address.serializeString();
  52. }
  53. catch (con::PeerNotFoundException &e) {
  54. /*
  55. * no peer for this packet found
  56. * most common reason is peer timeout, e.g. peer didn't
  57. * respond for some time, your server was overloaded or
  58. * things like that.
  59. */
  60. infostream << "Server::ProcessData(): Canceling: peer "
  61. << pkt->getPeerId() << " not found" << std::endl;
  62. return;
  63. }
  64. // If net_proto_version is set, this client has already been handled
  65. if (client->getState() > CS_Created) {
  66. verbosestream << "Server: Ignoring multiple TOSERVER_INITs from "
  67. << addr_s << " (peer_id=" << pkt->getPeerId() << ")" << std::endl;
  68. return;
  69. }
  70. verbosestream << "Server: Got TOSERVER_INIT from " << addr_s << " (peer_id="
  71. << pkt->getPeerId() << ")" << std::endl;
  72. // Do not allow multiple players in simple singleplayer mode.
  73. // This isn't a perfect way to do it, but will suffice for now
  74. if (m_simple_singleplayer_mode && m_clients.getClientIDs().size() > 1) {
  75. infostream << "Server: Not allowing another client (" << addr_s
  76. << ") to connect in simple singleplayer mode" << std::endl;
  77. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SINGLEPLAYER);
  78. return;
  79. }
  80. // First byte after command is maximum supported
  81. // serialization version
  82. u8 client_max;
  83. u16 supp_compr_modes;
  84. u16 min_net_proto_version = 0;
  85. u16 max_net_proto_version;
  86. std::string playerName;
  87. *pkt >> client_max >> supp_compr_modes >> min_net_proto_version
  88. >> max_net_proto_version >> playerName;
  89. u8 our_max = SER_FMT_VER_HIGHEST_READ;
  90. // Use the highest version supported by both
  91. u8 depl_serial_v = std::min(client_max, our_max);
  92. // If it's lower than the lowest supported, give up.
  93. if (depl_serial_v < SER_FMT_VER_LOWEST_READ)
  94. depl_serial_v = SER_FMT_VER_INVALID;
  95. if (depl_serial_v == SER_FMT_VER_INVALID) {
  96. actionstream << "Server: A mismatched client tried to connect from "
  97. << addr_s << std::endl;
  98. infostream<<"Server: Cannot negotiate serialization version with "
  99. << addr_s << std::endl;
  100. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_VERSION);
  101. return;
  102. }
  103. client->setPendingSerializationVersion(depl_serial_v);
  104. /*
  105. Read and check network protocol version
  106. */
  107. u16 net_proto_version = 0;
  108. // Figure out a working version if it is possible at all
  109. if (max_net_proto_version >= SERVER_PROTOCOL_VERSION_MIN ||
  110. min_net_proto_version <= SERVER_PROTOCOL_VERSION_MAX) {
  111. // If maximum is larger than our maximum, go with our maximum
  112. if (max_net_proto_version > SERVER_PROTOCOL_VERSION_MAX)
  113. net_proto_version = SERVER_PROTOCOL_VERSION_MAX;
  114. // Else go with client's maximum
  115. else
  116. net_proto_version = max_net_proto_version;
  117. }
  118. verbosestream << "Server: " << addr_s << ": Protocol version: min: "
  119. << min_net_proto_version << ", max: " << max_net_proto_version
  120. << ", chosen: " << net_proto_version << std::endl;
  121. client->net_proto_version = net_proto_version;
  122. if (g_settings->getBool("strict_protocol_version_checking") ||
  123. net_proto_version < SERVER_PROTOCOL_VERSION_MIN ||
  124. net_proto_version > SERVER_PROTOCOL_VERSION_MAX) {
  125. actionstream << "Server: A mismatched client tried to connect from "
  126. << addr_s << std::endl;
  127. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_VERSION);
  128. return;
  129. }
  130. /*
  131. Validate player name
  132. */
  133. const char* playername = playerName.c_str();
  134. size_t pns = playerName.size();
  135. if (pns == 0 || pns > PLAYERNAME_SIZE) {
  136. actionstream << "Server: Player with "
  137. << ((pns > PLAYERNAME_SIZE) ? "a too long" : "an empty")
  138. << " name tried to connect from " << addr_s << std::endl;
  139. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_NAME);
  140. return;
  141. }
  142. if (!string_allowed(playerName, PLAYERNAME_ALLOWED_CHARS)) {
  143. actionstream << "Server: Player with an invalid name "
  144. << "tried to connect from " << addr_s << std::endl;
  145. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_CHARS_IN_NAME);
  146. return;
  147. }
  148. m_clients.setPlayerName(pkt->getPeerId(), playername);
  149. //TODO (later) case insensitivity
  150. std::string legacyPlayerNameCasing = playerName;
  151. if (!isSingleplayer() && strcasecmp(playername, "singleplayer") == 0) {
  152. actionstream << "Server: Player with the name \"singleplayer\" "
  153. << "tried to connect from " << addr_s << std::endl;
  154. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_NAME);
  155. return;
  156. }
  157. {
  158. std::string reason;
  159. if (m_script->on_prejoinplayer(playername, addr_s, &reason)) {
  160. actionstream << "Server: Player with the name \"" << playerName << "\" "
  161. << "tried to connect from " << addr_s << " "
  162. << "but it was disallowed for the following reason: "
  163. << reason << std::endl;
  164. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_CUSTOM_STRING, reason);
  165. return;
  166. }
  167. }
  168. infostream << "Server: New connection: \"" << playerName << "\" from "
  169. << addr_s << " (peer_id=" << pkt->getPeerId() << ")" << std::endl;
  170. // Enforce user limit.
  171. // Don't enforce for users that have some admin right or mod permits it.
  172. if (m_clients.isUserLimitReached() &&
  173. playername != g_settings->get("name") &&
  174. !m_script->can_bypass_userlimit(playername, addr_s)) {
  175. actionstream << "Server: " << playername << " tried to join from "
  176. << addr_s << ", but there" << " are already max_users="
  177. << g_settings->getU16("max_users") << " players." << std::endl;
  178. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_TOO_MANY_USERS);
  179. return;
  180. }
  181. /*
  182. Compose auth methods for answer
  183. */
  184. std::string encpwd; // encrypted Password field for the user
  185. bool has_auth = m_script->getAuth(playername, &encpwd, NULL);
  186. u32 auth_mechs = 0;
  187. client->chosen_mech = AUTH_MECHANISM_NONE;
  188. if (has_auth) {
  189. std::vector<std::string> pwd_components = str_split(encpwd, '#');
  190. if (pwd_components.size() == 4) {
  191. if (pwd_components[1] == "1") { // 1 means srp
  192. auth_mechs |= AUTH_MECHANISM_SRP;
  193. client->enc_pwd = encpwd;
  194. } else {
  195. actionstream << "User " << playername
  196. << " tried to log in, but password field"
  197. << " was invalid (unknown mechcode)." << std::endl;
  198. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
  199. return;
  200. }
  201. } else if (base64_is_valid(encpwd)) {
  202. auth_mechs |= AUTH_MECHANISM_LEGACY_PASSWORD;
  203. client->enc_pwd = encpwd;
  204. } else {
  205. actionstream << "User " << playername
  206. << " tried to log in, but password field"
  207. << " was invalid (invalid base64)." << std::endl;
  208. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
  209. return;
  210. }
  211. } else {
  212. std::string default_password = g_settings->get("default_password");
  213. if (default_password.length() == 0) {
  214. auth_mechs |= AUTH_MECHANISM_FIRST_SRP;
  215. } else {
  216. // Take care of default passwords.
  217. client->enc_pwd = get_encoded_srp_verifier(playerName, default_password);
  218. auth_mechs |= AUTH_MECHANISM_SRP;
  219. // Allocate player in db, but only on successful login.
  220. client->create_player_on_auth_success = true;
  221. }
  222. }
  223. /*
  224. Answer with a TOCLIENT_HELLO
  225. */
  226. verbosestream << "Sending TOCLIENT_HELLO with auth method field: "
  227. << auth_mechs << std::endl;
  228. NetworkPacket resp_pkt(TOCLIENT_HELLO, 1 + 4
  229. + legacyPlayerNameCasing.size(), pkt->getPeerId());
  230. u16 depl_compress_mode = NETPROTO_COMPRESSION_NONE;
  231. resp_pkt << depl_serial_v << depl_compress_mode << net_proto_version
  232. << auth_mechs << legacyPlayerNameCasing;
  233. Send(&resp_pkt);
  234. client->allowed_auth_mechs = auth_mechs;
  235. client->setDeployedCompressionMode(depl_compress_mode);
  236. m_clients.event(pkt->getPeerId(), CSE_Hello);
  237. }
  238. void Server::handleCommand_Init2(NetworkPacket* pkt)
  239. {
  240. verbosestream << "Server: Got TOSERVER_INIT2 from "
  241. << pkt->getPeerId() << std::endl;
  242. m_clients.event(pkt->getPeerId(), CSE_GotInit2);
  243. u16 protocol_version = m_clients.getProtocolVersion(pkt->getPeerId());
  244. std::string lang;
  245. if (pkt->getSize() > 0)
  246. *pkt >> lang;
  247. /*
  248. Send some initialization data
  249. */
  250. infostream << "Server: Sending content to "
  251. << getPlayerName(pkt->getPeerId()) << std::endl;
  252. // Send player movement settings
  253. SendMovement(pkt->getPeerId());
  254. // Send item definitions
  255. SendItemDef(pkt->getPeerId(), m_itemdef, protocol_version);
  256. // Send node definitions
  257. SendNodeDef(pkt->getPeerId(), m_nodedef, protocol_version);
  258. m_clients.event(pkt->getPeerId(), CSE_SetDefinitionsSent);
  259. // Send media announcement
  260. sendMediaAnnouncement(pkt->getPeerId(), lang);
  261. // Send detached inventories
  262. sendDetachedInventories(pkt->getPeerId());
  263. // Send time of day
  264. u16 time = m_env->getTimeOfDay();
  265. float time_speed = g_settings->getFloat("time_speed");
  266. SendTimeOfDay(pkt->getPeerId(), time, time_speed);
  267. SendCSMFlavourLimits(pkt->getPeerId());
  268. // Warnings about protocol version can be issued here
  269. if (getClient(pkt->getPeerId())->net_proto_version < LATEST_PROTOCOL_VERSION) {
  270. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  271. L"# Server: WARNING: YOUR CLIENT'S VERSION MAY NOT BE FULLY COMPATIBLE "
  272. L"WITH THIS SERVER!"));
  273. }
  274. }
  275. void Server::handleCommand_RequestMedia(NetworkPacket* pkt)
  276. {
  277. std::vector<std::string> tosend;
  278. u16 numfiles;
  279. *pkt >> numfiles;
  280. infostream << "Sending " << numfiles << " files to "
  281. << getPlayerName(pkt->getPeerId()) << std::endl;
  282. verbosestream << "TOSERVER_REQUEST_MEDIA: " << std::endl;
  283. for (u16 i = 0; i < numfiles; i++) {
  284. std::string name;
  285. *pkt >> name;
  286. tosend.push_back(name);
  287. verbosestream << "TOSERVER_REQUEST_MEDIA: requested file "
  288. << name << std::endl;
  289. }
  290. sendRequestedMedia(pkt->getPeerId(), tosend);
  291. }
  292. void Server::handleCommand_ClientReady(NetworkPacket* pkt)
  293. {
  294. session_t peer_id = pkt->getPeerId();
  295. PlayerSAO* playersao = StageTwoClientInit(peer_id);
  296. if (playersao == NULL) {
  297. actionstream
  298. << "TOSERVER_CLIENT_READY stage 2 client init failed for peer_id: "
  299. << peer_id << std::endl;
  300. DisconnectPeer(peer_id);
  301. return;
  302. }
  303. if (pkt->getSize() < 8) {
  304. errorstream
  305. << "TOSERVER_CLIENT_READY client sent inconsistent data, disconnecting peer_id: "
  306. << peer_id << std::endl;
  307. DisconnectPeer(peer_id);
  308. return;
  309. }
  310. u8 major_ver, minor_ver, patch_ver, reserved;
  311. std::string full_ver;
  312. *pkt >> major_ver >> minor_ver >> patch_ver >> reserved >> full_ver;
  313. m_clients.setClientVersion(
  314. peer_id, major_ver, minor_ver, patch_ver,
  315. full_ver);
  316. const std::vector<std::string> &players = m_clients.getPlayerNames();
  317. NetworkPacket list_pkt(TOCLIENT_UPDATE_PLAYER_LIST, 0, peer_id);
  318. list_pkt << (u8) PLAYER_LIST_INIT << (u16) players.size();
  319. for (const std::string &player: players) {
  320. list_pkt << player;
  321. }
  322. m_clients.send(peer_id, 0, &list_pkt, true);
  323. NetworkPacket notice_pkt(TOCLIENT_UPDATE_PLAYER_LIST, 0, PEER_ID_INEXISTENT);
  324. // (u16) 1 + std::string represents a pseudo vector serialization representation
  325. notice_pkt << (u8) PLAYER_LIST_ADD << (u16) 1 << std::string(playersao->getPlayer()->getName());
  326. m_clients.sendToAll(&notice_pkt);
  327. m_clients.event(peer_id, CSE_SetClientReady);
  328. m_script->on_joinplayer(playersao);
  329. // Send shutdown timer if shutdown has been scheduled
  330. if (m_shutdown_timer > 0.0f) {
  331. std::wstringstream ws;
  332. ws << L"*** Server shutting down in "
  333. << duration_to_string(myround(m_shutdown_timer)).c_str() << ".";
  334. SendChatMessage(pkt->getPeerId(), ws.str());
  335. }
  336. }
  337. void Server::handleCommand_GotBlocks(NetworkPacket* pkt)
  338. {
  339. if (pkt->getSize() < 1)
  340. return;
  341. /*
  342. [0] u16 command
  343. [2] u8 count
  344. [3] v3s16 pos_0
  345. [3+6] v3s16 pos_1
  346. ...
  347. */
  348. u8 count;
  349. *pkt >> count;
  350. RemoteClient *client = getClient(pkt->getPeerId());
  351. if ((s16)pkt->getSize() < 1 + (int)count * 6) {
  352. throw con::InvalidIncomingDataException
  353. ("GOTBLOCKS length is too short");
  354. }
  355. for (u16 i = 0; i < count; i++) {
  356. v3s16 p;
  357. *pkt >> p;
  358. client->GotBlock(p);
  359. }
  360. }
  361. void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
  362. NetworkPacket *pkt)
  363. {
  364. if (pkt->getRemainingBytes() < 12 + 12 + 4 + 4 + 4 + 1 + 1)
  365. return;
  366. v3s32 ps, ss;
  367. s32 f32pitch, f32yaw;
  368. u8 f32fov;
  369. *pkt >> ps;
  370. *pkt >> ss;
  371. *pkt >> f32pitch;
  372. *pkt >> f32yaw;
  373. f32 pitch = (f32)f32pitch / 100.0f;
  374. f32 yaw = (f32)f32yaw / 100.0f;
  375. u32 keyPressed = 0;
  376. // default behavior (in case an old client doesn't send these)
  377. f32 fov = 0;
  378. u8 wanted_range = 0;
  379. *pkt >> keyPressed;
  380. *pkt >> f32fov;
  381. fov = (f32)f32fov / 80.0f;
  382. *pkt >> wanted_range;
  383. v3f position((f32)ps.X / 100.0f, (f32)ps.Y / 100.0f, (f32)ps.Z / 100.0f);
  384. v3f speed((f32)ss.X / 100.0f, (f32)ss.Y / 100.0f, (f32)ss.Z / 100.0f);
  385. pitch = modulo360f(pitch);
  386. yaw = wrapDegrees_0_360(yaw);
  387. playersao->setBasePosition(position);
  388. player->setSpeed(speed);
  389. playersao->setPitch(pitch);
  390. playersao->setYaw(yaw);
  391. playersao->setFov(fov);
  392. playersao->setWantedRange(wanted_range);
  393. player->keyPressed = keyPressed;
  394. player->control.up = (keyPressed & 1);
  395. player->control.down = (keyPressed & 2);
  396. player->control.left = (keyPressed & 4);
  397. player->control.right = (keyPressed & 8);
  398. player->control.jump = (keyPressed & 16);
  399. player->control.aux1 = (keyPressed & 32);
  400. player->control.sneak = (keyPressed & 64);
  401. player->control.LMB = (keyPressed & 128);
  402. player->control.RMB = (keyPressed & 256);
  403. if (playersao->checkMovementCheat()) {
  404. // Call callbacks
  405. m_script->on_cheat(playersao, "moved_too_fast");
  406. SendMovePlayer(pkt->getPeerId());
  407. }
  408. }
  409. void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
  410. {
  411. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  412. if (player == NULL) {
  413. errorstream << "Server::ProcessData(): Canceling: "
  414. "No player for peer_id=" << pkt->getPeerId()
  415. << " disconnecting peer!" << std::endl;
  416. DisconnectPeer(pkt->getPeerId());
  417. return;
  418. }
  419. PlayerSAO *playersao = player->getPlayerSAO();
  420. if (playersao == NULL) {
  421. errorstream << "Server::ProcessData(): Canceling: "
  422. "No player object for peer_id=" << pkt->getPeerId()
  423. << " disconnecting peer!" << std::endl;
  424. DisconnectPeer(pkt->getPeerId());
  425. return;
  426. }
  427. // If player is dead we don't care of this packet
  428. if (playersao->isDead()) {
  429. verbosestream << "TOSERVER_PLAYERPOS: " << player->getName()
  430. << " is dead. Ignoring packet";
  431. return;
  432. }
  433. process_PlayerPos(player, playersao, pkt);
  434. }
  435. void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt)
  436. {
  437. if (pkt->getSize() < 1)
  438. return;
  439. /*
  440. [0] u16 command
  441. [2] u8 count
  442. [3] v3s16 pos_0
  443. [3+6] v3s16 pos_1
  444. ...
  445. */
  446. u8 count;
  447. *pkt >> count;
  448. RemoteClient *client = getClient(pkt->getPeerId());
  449. if ((s16)pkt->getSize() < 1 + (int)count * 6) {
  450. throw con::InvalidIncomingDataException
  451. ("DELETEDBLOCKS length is too short");
  452. }
  453. for (u16 i = 0; i < count; i++) {
  454. v3s16 p;
  455. *pkt >> p;
  456. client->SetBlockNotSent(p);
  457. }
  458. }
  459. void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
  460. {
  461. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  462. if (player == NULL) {
  463. errorstream << "Server::ProcessData(): Canceling: "
  464. "No player for peer_id=" << pkt->getPeerId()
  465. << " disconnecting peer!" << std::endl;
  466. DisconnectPeer(pkt->getPeerId());
  467. return;
  468. }
  469. PlayerSAO *playersao = player->getPlayerSAO();
  470. if (playersao == NULL) {
  471. errorstream << "Server::ProcessData(): Canceling: "
  472. "No player object for peer_id=" << pkt->getPeerId()
  473. << " disconnecting peer!" << std::endl;
  474. DisconnectPeer(pkt->getPeerId());
  475. return;
  476. }
  477. // Strip command and create a stream
  478. std::string datastring(pkt->getString(0), pkt->getSize());
  479. verbosestream << "TOSERVER_INVENTORY_ACTION: data=" << datastring
  480. << std::endl;
  481. std::istringstream is(datastring, std::ios_base::binary);
  482. // Create an action
  483. InventoryAction *a = InventoryAction::deSerialize(is);
  484. if (!a) {
  485. infostream << "TOSERVER_INVENTORY_ACTION: "
  486. << "InventoryAction::deSerialize() returned NULL"
  487. << std::endl;
  488. return;
  489. }
  490. // If something goes wrong, this player is to blame
  491. RollbackScopeActor rollback_scope(m_rollback,
  492. std::string("player:")+player->getName());
  493. /*
  494. Note: Always set inventory not sent, to repair cases
  495. where the client made a bad prediction.
  496. */
  497. /*
  498. Handle restrictions and special cases of the move action
  499. */
  500. if (a->getType() == IAction::Move) {
  501. IMoveAction *ma = (IMoveAction*)a;
  502. ma->from_inv.applyCurrentPlayer(player->getName());
  503. ma->to_inv.applyCurrentPlayer(player->getName());
  504. setInventoryModified(ma->from_inv, false);
  505. setInventoryModified(ma->to_inv, false);
  506. bool from_inv_is_current_player =
  507. (ma->from_inv.type == InventoryLocation::PLAYER) &&
  508. (ma->from_inv.name == player->getName());
  509. bool to_inv_is_current_player =
  510. (ma->to_inv.type == InventoryLocation::PLAYER) &&
  511. (ma->to_inv.name == player->getName());
  512. /*
  513. Disable moving items out of craftpreview
  514. */
  515. if (ma->from_list == "craftpreview") {
  516. infostream << "Ignoring IMoveAction from "
  517. << (ma->from_inv.dump()) << ":" << ma->from_list
  518. << " to " << (ma->to_inv.dump()) << ":" << ma->to_list
  519. << " because src is " << ma->from_list << std::endl;
  520. delete a;
  521. return;
  522. }
  523. /*
  524. Disable moving items into craftresult and craftpreview
  525. */
  526. if (ma->to_list == "craftpreview" || ma->to_list == "craftresult") {
  527. infostream << "Ignoring IMoveAction from "
  528. << (ma->from_inv.dump()) << ":" << ma->from_list
  529. << " to " << (ma->to_inv.dump()) << ":" << ma->to_list
  530. << " because dst is " << ma->to_list << std::endl;
  531. delete a;
  532. return;
  533. }
  534. // Disallow moving items in elsewhere than player's inventory
  535. // if not allowed to interact
  536. if (!checkPriv(player->getName(), "interact") &&
  537. (!from_inv_is_current_player ||
  538. !to_inv_is_current_player)) {
  539. infostream << "Cannot move outside of player's inventory: "
  540. << "No interact privilege" << std::endl;
  541. delete a;
  542. return;
  543. }
  544. }
  545. /*
  546. Handle restrictions and special cases of the drop action
  547. */
  548. else if (a->getType() == IAction::Drop) {
  549. IDropAction *da = (IDropAction*)a;
  550. da->from_inv.applyCurrentPlayer(player->getName());
  551. setInventoryModified(da->from_inv, false);
  552. /*
  553. Disable dropping items out of craftpreview
  554. */
  555. if (da->from_list == "craftpreview") {
  556. infostream << "Ignoring IDropAction from "
  557. << (da->from_inv.dump()) << ":" << da->from_list
  558. << " because src is " << da->from_list << std::endl;
  559. delete a;
  560. return;
  561. }
  562. // Disallow dropping items if not allowed to interact
  563. if (!checkPriv(player->getName(), "interact")) {
  564. delete a;
  565. return;
  566. }
  567. // Disallow dropping items if dead
  568. if (playersao->isDead()) {
  569. infostream << "Ignoring IDropAction from "
  570. << (da->from_inv.dump()) << ":" << da->from_list
  571. << " because player is dead." << std::endl;
  572. delete a;
  573. return;
  574. }
  575. }
  576. /*
  577. Handle restrictions and special cases of the craft action
  578. */
  579. else if (a->getType() == IAction::Craft) {
  580. ICraftAction *ca = (ICraftAction*)a;
  581. ca->craft_inv.applyCurrentPlayer(player->getName());
  582. setInventoryModified(ca->craft_inv, false);
  583. //bool craft_inv_is_current_player =
  584. // (ca->craft_inv.type == InventoryLocation::PLAYER) &&
  585. // (ca->craft_inv.name == player->getName());
  586. // Disallow crafting if not allowed to interact
  587. if (!checkPriv(player->getName(), "interact")) {
  588. infostream << "Cannot craft: "
  589. << "No interact privilege" << std::endl;
  590. delete a;
  591. return;
  592. }
  593. }
  594. // Do the action
  595. a->apply(this, playersao, this);
  596. // Eat the action
  597. delete a;
  598. SendInventory(playersao);
  599. }
  600. void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
  601. {
  602. /*
  603. u16 command
  604. u16 length
  605. wstring message
  606. */
  607. u16 len;
  608. *pkt >> len;
  609. std::wstring message;
  610. for (u16 i = 0; i < len; i++) {
  611. u16 tmp_wchar;
  612. *pkt >> tmp_wchar;
  613. message += (wchar_t)tmp_wchar;
  614. }
  615. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  616. if (player == NULL) {
  617. errorstream << "Server::ProcessData(): Canceling: "
  618. "No player for peer_id=" << pkt->getPeerId()
  619. << " disconnecting peer!" << std::endl;
  620. DisconnectPeer(pkt->getPeerId());
  621. return;
  622. }
  623. // Get player name of this client
  624. std::string name = player->getName();
  625. std::wstring wname = narrow_to_wide(name);
  626. std::wstring answer_to_sender = handleChat(name, wname, message, true, player);
  627. if (!answer_to_sender.empty()) {
  628. // Send the answer to sender
  629. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_NORMAL,
  630. answer_to_sender, wname));
  631. }
  632. }
  633. void Server::handleCommand_Damage(NetworkPacket* pkt)
  634. {
  635. u8 damage;
  636. *pkt >> damage;
  637. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  638. if (player == NULL) {
  639. errorstream << "Server::ProcessData(): Canceling: "
  640. "No player for peer_id=" << pkt->getPeerId()
  641. << " disconnecting peer!" << std::endl;
  642. DisconnectPeer(pkt->getPeerId());
  643. return;
  644. }
  645. PlayerSAO *playersao = player->getPlayerSAO();
  646. if (playersao == NULL) {
  647. errorstream << "Server::ProcessData(): Canceling: "
  648. "No player object for peer_id=" << pkt->getPeerId()
  649. << " disconnecting peer!" << std::endl;
  650. DisconnectPeer(pkt->getPeerId());
  651. return;
  652. }
  653. if (g_settings->getBool("enable_damage")) {
  654. if (playersao->isDead()) {
  655. verbosestream << "Server::ProcessData(): Info: "
  656. "Ignoring damage as player " << player->getName()
  657. << " is already dead." << std::endl;
  658. return;
  659. }
  660. actionstream << player->getName() << " damaged by "
  661. << (int)damage << " hp at " << PP(playersao->getBasePosition() / BS)
  662. << std::endl;
  663. playersao->setHP(playersao->getHP() - damage);
  664. SendPlayerHPOrDie(playersao);
  665. }
  666. }
  667. void Server::handleCommand_Password(NetworkPacket* pkt)
  668. {
  669. if (pkt->getSize() != PASSWORD_SIZE * 2)
  670. return;
  671. std::string oldpwd;
  672. std::string newpwd;
  673. // Deny for clients using the new protocol
  674. RemoteClient* client = getClient(pkt->getPeerId(), CS_Created);
  675. if (client->net_proto_version >= 25) {
  676. infostream << "Server::handleCommand_Password(): Denying change: "
  677. << " Client protocol version for peer_id=" << pkt->getPeerId()
  678. << " too new!" << std::endl;
  679. return;
  680. }
  681. for (u16 i = 0; i < PASSWORD_SIZE - 1; i++) {
  682. char c = pkt->getChar(i);
  683. if (c == 0)
  684. break;
  685. oldpwd += c;
  686. }
  687. for (u16 i = 0; i < PASSWORD_SIZE - 1; i++) {
  688. char c = pkt->getChar(PASSWORD_SIZE + i);
  689. if (c == 0)
  690. break;
  691. newpwd += c;
  692. }
  693. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  694. if (player == NULL) {
  695. errorstream << "Server::ProcessData(): Canceling: "
  696. "No player for peer_id=" << pkt->getPeerId()
  697. << " disconnecting peer!" << std::endl;
  698. DisconnectPeer(pkt->getPeerId());
  699. return;
  700. }
  701. if (!base64_is_valid(newpwd)) {
  702. infostream<<"Server: " << player->getName() <<
  703. " supplied invalid password hash" << std::endl;
  704. // Wrong old password supplied!!
  705. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  706. L"Invalid new password hash supplied. Password NOT changed."));
  707. return;
  708. }
  709. infostream << "Server: Client requests a password change from "
  710. << "'" << oldpwd << "' to '" << newpwd << "'" << std::endl;
  711. std::string playername = player->getName();
  712. std::string checkpwd;
  713. m_script->getAuth(playername, &checkpwd, NULL);
  714. if (oldpwd != checkpwd) {
  715. infostream << "Server: invalid old password" << std::endl;
  716. // Wrong old password supplied!!
  717. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  718. L"Invalid old password supplied. Password NOT changed."));
  719. return;
  720. }
  721. bool success = m_script->setPassword(playername, newpwd);
  722. if (success) {
  723. actionstream << player->getName() << " changes password" << std::endl;
  724. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  725. L"Password change successful."));
  726. } else {
  727. actionstream << player->getName() << " tries to change password but "
  728. << "it fails" << std::endl;
  729. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  730. L"Password change failed or unavailable."));
  731. }
  732. }
  733. void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
  734. {
  735. if (pkt->getSize() < 2)
  736. return;
  737. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  738. if (player == NULL) {
  739. errorstream << "Server::ProcessData(): Canceling: "
  740. "No player for peer_id=" << pkt->getPeerId()
  741. << " disconnecting peer!" << std::endl;
  742. DisconnectPeer(pkt->getPeerId());
  743. return;
  744. }
  745. PlayerSAO *playersao = player->getPlayerSAO();
  746. if (playersao == NULL) {
  747. errorstream << "Server::ProcessData(): Canceling: "
  748. "No player object for peer_id=" << pkt->getPeerId()
  749. << " disconnecting peer!" << std::endl;
  750. DisconnectPeer(pkt->getPeerId());
  751. return;
  752. }
  753. u16 item;
  754. *pkt >> item;
  755. playersao->setWieldIndex(item);
  756. }
  757. void Server::handleCommand_Respawn(NetworkPacket* pkt)
  758. {
  759. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  760. if (player == NULL) {
  761. errorstream << "Server::ProcessData(): Canceling: "
  762. "No player for peer_id=" << pkt->getPeerId()
  763. << " disconnecting peer!" << std::endl;
  764. DisconnectPeer(pkt->getPeerId());
  765. return;
  766. }
  767. PlayerSAO *playersao = player->getPlayerSAO();
  768. assert(playersao);
  769. if (!playersao->isDead())
  770. return;
  771. RespawnPlayer(pkt->getPeerId());
  772. actionstream << player->getName() << " respawns at "
  773. << PP(playersao->getBasePosition() / BS) << std::endl;
  774. // ActiveObject is added to environment in AsyncRunStep after
  775. // the previous addition has been successfully removed
  776. }
  777. void Server::handleCommand_Interact(NetworkPacket* pkt)
  778. {
  779. /*
  780. [0] u16 command
  781. [2] u8 action
  782. [3] u16 item
  783. [5] u32 length of the next item (plen)
  784. [9] serialized PointedThing
  785. [9 + plen] player position information
  786. actions:
  787. 0: start digging (from undersurface) or use
  788. 1: stop digging (all parameters ignored)
  789. 2: digging completed
  790. 3: place block or item (to abovesurface)
  791. 4: use item
  792. 5: rightclick air ("activate")
  793. */
  794. u8 action;
  795. u16 item_i;
  796. *pkt >> action;
  797. *pkt >> item_i;
  798. std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
  799. PointedThing pointed;
  800. pointed.deSerialize(tmp_is);
  801. verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item="
  802. << item_i << ", pointed=" << pointed.dump() << std::endl;
  803. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  804. if (player == NULL) {
  805. errorstream << "Server::ProcessData(): Canceling: "
  806. "No player for peer_id=" << pkt->getPeerId()
  807. << " disconnecting peer!" << std::endl;
  808. DisconnectPeer(pkt->getPeerId());
  809. return;
  810. }
  811. PlayerSAO *playersao = player->getPlayerSAO();
  812. if (playersao == NULL) {
  813. errorstream << "Server::ProcessData(): Canceling: "
  814. "No player object for peer_id=" << pkt->getPeerId()
  815. << " disconnecting peer!" << std::endl;
  816. DisconnectPeer(pkt->getPeerId());
  817. return;
  818. }
  819. if (playersao->isDead()) {
  820. actionstream << "Server: NoCheat: " << player->getName()
  821. << " tried to interact while dead; ignoring." << std::endl;
  822. if (pointed.type == POINTEDTHING_NODE) {
  823. // Re-send block to revert change on client-side
  824. RemoteClient *client = getClient(pkt->getPeerId());
  825. v3s16 blockpos = getNodeBlockPos(pointed.node_undersurface);
  826. client->SetBlockNotSent(blockpos);
  827. }
  828. // Call callbacks
  829. m_script->on_cheat(playersao, "interacted_while_dead");
  830. return;
  831. }
  832. process_PlayerPos(player, playersao, pkt);
  833. v3f player_pos = playersao->getLastGoodPosition();
  834. // Update wielded item
  835. playersao->setWieldIndex(item_i);
  836. // Get pointed to node (undefined if not POINTEDTYPE_NODE)
  837. v3s16 p_under = pointed.node_undersurface;
  838. v3s16 p_above = pointed.node_abovesurface;
  839. // Get pointed to object (NULL if not POINTEDTYPE_OBJECT)
  840. ServerActiveObject *pointed_object = NULL;
  841. if (pointed.type == POINTEDTHING_OBJECT) {
  842. pointed_object = m_env->getActiveObject(pointed.object_id);
  843. if (pointed_object == NULL) {
  844. verbosestream << "TOSERVER_INTERACT: "
  845. "pointed object is NULL" << std::endl;
  846. return;
  847. }
  848. }
  849. v3f pointed_pos_under = player_pos;
  850. v3f pointed_pos_above = player_pos;
  851. if (pointed.type == POINTEDTHING_NODE) {
  852. pointed_pos_under = intToFloat(p_under, BS);
  853. pointed_pos_above = intToFloat(p_above, BS);
  854. }
  855. else if (pointed.type == POINTEDTHING_OBJECT) {
  856. pointed_pos_under = pointed_object->getBasePosition();
  857. pointed_pos_above = pointed_pos_under;
  858. }
  859. /*
  860. Make sure the player is allowed to do it
  861. */
  862. if (!checkPriv(player->getName(), "interact")) {
  863. actionstream<<player->getName()<<" attempted to interact with "
  864. <<pointed.dump()<<" without 'interact' privilege"
  865. <<std::endl;
  866. // Re-send block to revert change on client-side
  867. RemoteClient *client = getClient(pkt->getPeerId());
  868. // Digging completed -> under
  869. if (action == 2) {
  870. v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
  871. client->SetBlockNotSent(blockpos);
  872. }
  873. // Placement -> above
  874. if (action == 3) {
  875. v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
  876. client->SetBlockNotSent(blockpos);
  877. }
  878. return;
  879. }
  880. /*
  881. Check that target is reasonably close
  882. (only when digging or placing things)
  883. */
  884. static thread_local const bool enable_anticheat =
  885. !g_settings->getBool("disable_anticheat");
  886. if ((action == 0 || action == 2 || action == 3 || action == 4) &&
  887. (enable_anticheat && !isSingleplayer())) {
  888. float d = player_pos.getDistanceFrom(pointed_pos_under);
  889. const ItemDefinition &playeritem_def =
  890. playersao->getWieldedItem().getDefinition(m_itemdef);
  891. float max_d = BS * playeritem_def.range;
  892. InventoryList *hlist = playersao->getInventory()->getList("hand");
  893. const ItemDefinition &hand_def =
  894. hlist ? (hlist->getItem(0).getDefinition(m_itemdef)) : (m_itemdef->get(""));
  895. float max_d_hand = BS * hand_def.range;
  896. if (max_d < 0 && max_d_hand >= 0)
  897. max_d = max_d_hand;
  898. else if (max_d < 0)
  899. max_d = BS * 4.0f;
  900. // cube diagonal: sqrt(3) = 1.73
  901. if (d > max_d * 1.73) {
  902. actionstream << "Player " << player->getName()
  903. << " tried to access " << pointed.dump()
  904. << " from too far: "
  905. << "d=" << d <<", max_d=" << max_d
  906. << ". ignoring." << std::endl;
  907. // Re-send block to revert change on client-side
  908. RemoteClient *client = getClient(pkt->getPeerId());
  909. v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
  910. client->SetBlockNotSent(blockpos);
  911. // Call callbacks
  912. m_script->on_cheat(playersao, "interacted_too_far");
  913. // Do nothing else
  914. return;
  915. }
  916. }
  917. /*
  918. If something goes wrong, this player is to blame
  919. */
  920. RollbackScopeActor rollback_scope(m_rollback,
  921. std::string("player:")+player->getName());
  922. /*
  923. 0: start digging or punch object
  924. */
  925. if (action == 0) {
  926. if (pointed.type == POINTEDTHING_NODE) {
  927. MapNode n(CONTENT_IGNORE);
  928. bool pos_ok;
  929. n = m_env->getMap().getNodeNoEx(p_under, &pos_ok);
  930. if (!pos_ok) {
  931. infostream << "Server: Not punching: Node not found."
  932. << " Adding block to emerge queue."
  933. << std::endl;
  934. m_emerge->enqueueBlockEmerge(pkt->getPeerId(),
  935. getNodeBlockPos(p_above), false);
  936. }
  937. if (n.getContent() != CONTENT_IGNORE)
  938. m_script->node_on_punch(p_under, n, playersao, pointed);
  939. // Cheat prevention
  940. playersao->noCheatDigStart(p_under);
  941. }
  942. else if (pointed.type == POINTEDTHING_OBJECT) {
  943. // Skip if object can't be interacted with anymore
  944. if (pointed_object->isGone())
  945. return;
  946. actionstream<<player->getName()<<" punches object "
  947. <<pointed.object_id<<": "
  948. <<pointed_object->getDescription()<<std::endl;
  949. ItemStack punchitem = playersao->getWieldedItemOrHand();
  950. ToolCapabilities toolcap =
  951. punchitem.getToolCapabilities(m_itemdef);
  952. v3f dir = (pointed_object->getBasePosition() -
  953. (playersao->getBasePosition() + playersao->getEyeOffset())
  954. ).normalize();
  955. float time_from_last_punch =
  956. playersao->resetTimeFromLastPunch();
  957. s16 src_original_hp = pointed_object->getHP();
  958. s16 dst_origin_hp = playersao->getHP();
  959. pointed_object->punch(dir, &toolcap, playersao,
  960. time_from_last_punch);
  961. // If the object is a player and its HP changed
  962. if (src_original_hp != pointed_object->getHP() &&
  963. pointed_object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
  964. SendPlayerHPOrDie((PlayerSAO *)pointed_object);
  965. }
  966. // If the puncher is a player and its HP changed
  967. if (dst_origin_hp != playersao->getHP())
  968. SendPlayerHPOrDie(playersao);
  969. }
  970. } // action == 0
  971. /*
  972. 1: stop digging
  973. */
  974. else if (action == 1) {
  975. } // action == 1
  976. /*
  977. 2: Digging completed
  978. */
  979. else if (action == 2) {
  980. // Only digging of nodes
  981. if (pointed.type == POINTEDTHING_NODE) {
  982. bool pos_ok;
  983. MapNode n = m_env->getMap().getNodeNoEx(p_under, &pos_ok);
  984. if (!pos_ok) {
  985. infostream << "Server: Not finishing digging: Node not found."
  986. << " Adding block to emerge queue."
  987. << std::endl;
  988. m_emerge->enqueueBlockEmerge(pkt->getPeerId(),
  989. getNodeBlockPos(p_above), false);
  990. }
  991. /* Cheat prevention */
  992. bool is_valid_dig = true;
  993. if (enable_anticheat && !isSingleplayer()) {
  994. v3s16 nocheat_p = playersao->getNoCheatDigPos();
  995. float nocheat_t = playersao->getNoCheatDigTime();
  996. playersao->noCheatDigEnd();
  997. // If player didn't start digging this, ignore dig
  998. if (nocheat_p != p_under) {
  999. infostream << "Server: NoCheat: " << player->getName()
  1000. << " started digging "
  1001. << PP(nocheat_p) << " and completed digging "
  1002. << PP(p_under) << "; not digging." << std::endl;
  1003. is_valid_dig = false;
  1004. // Call callbacks
  1005. m_script->on_cheat(playersao, "finished_unknown_dig");
  1006. }
  1007. // Get player's wielded item
  1008. ItemStack playeritem = playersao->getWieldedItemOrHand();
  1009. ToolCapabilities playeritem_toolcap =
  1010. playeritem.getToolCapabilities(m_itemdef);
  1011. // Get diggability and expected digging time
  1012. DigParams params = getDigParams(m_nodedef->get(n).groups,
  1013. &playeritem_toolcap);
  1014. // If can't dig, try hand
  1015. if (!params.diggable) {
  1016. InventoryList *hlist = playersao->getInventory()->getList("hand");
  1017. const ItemDefinition &hand =
  1018. hlist ? hlist->getItem(0).getDefinition(m_itemdef) : m_itemdef->get("");
  1019. const ToolCapabilities *tp = hand.tool_capabilities;
  1020. if (tp)
  1021. params = getDigParams(m_nodedef->get(n).groups, tp);
  1022. }
  1023. // If can't dig, ignore dig
  1024. if (!params.diggable) {
  1025. infostream << "Server: NoCheat: " << player->getName()
  1026. << " completed digging " << PP(p_under)
  1027. << ", which is not diggable with tool. not digging."
  1028. << std::endl;
  1029. is_valid_dig = false;
  1030. // Call callbacks
  1031. m_script->on_cheat(playersao, "dug_unbreakable");
  1032. }
  1033. // Check digging time
  1034. // If already invalidated, we don't have to
  1035. if (!is_valid_dig) {
  1036. // Well not our problem then
  1037. }
  1038. // Clean and long dig
  1039. else if (params.time > 2.0 && nocheat_t * 1.2 > params.time) {
  1040. // All is good, but grab time from pool; don't care if
  1041. // it's actually available
  1042. playersao->getDigPool().grab(params.time);
  1043. }
  1044. // Short or laggy dig
  1045. // Try getting the time from pool
  1046. else if (playersao->getDigPool().grab(params.time)) {
  1047. // All is good
  1048. }
  1049. // Dig not possible
  1050. else {
  1051. infostream << "Server: NoCheat: " << player->getName()
  1052. << " completed digging " << PP(p_under)
  1053. << "too fast; not digging." << std::endl;
  1054. is_valid_dig = false;
  1055. // Call callbacks
  1056. m_script->on_cheat(playersao, "dug_too_fast");
  1057. }
  1058. }
  1059. /* Actually dig node */
  1060. if (is_valid_dig && n.getContent() != CONTENT_IGNORE)
  1061. m_script->node_on_dig(p_under, n, playersao);
  1062. v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
  1063. RemoteClient *client = getClient(pkt->getPeerId());
  1064. // Send unusual result (that is, node not being removed)
  1065. if (m_env->getMap().getNodeNoEx(p_under).getContent() != CONTENT_AIR) {
  1066. // Re-send block to revert change on client-side
  1067. client->SetBlockNotSent(blockpos);
  1068. }
  1069. else {
  1070. client->ResendBlockIfOnWire(blockpos);
  1071. }
  1072. }
  1073. } // action == 2
  1074. /*
  1075. 3: place block or right-click object
  1076. */
  1077. else if (action == 3) {
  1078. ItemStack item = playersao->getWieldedItem();
  1079. // Reset build time counter
  1080. if (pointed.type == POINTEDTHING_NODE &&
  1081. item.getDefinition(m_itemdef).type == ITEM_NODE)
  1082. getClient(pkt->getPeerId())->m_time_from_building = 0.0;
  1083. if (pointed.type == POINTEDTHING_OBJECT) {
  1084. // Right click object
  1085. // Skip if object can't be interacted with anymore
  1086. if (pointed_object->isGone())
  1087. return;
  1088. actionstream << player->getName() << " right-clicks object "
  1089. << pointed.object_id << ": "
  1090. << pointed_object->getDescription() << std::endl;
  1091. // Do stuff
  1092. pointed_object->rightClick(playersao);
  1093. }
  1094. else if (m_script->item_OnPlace(
  1095. item, playersao, pointed)) {
  1096. // Placement was handled in lua
  1097. // Apply returned ItemStack
  1098. if (playersao->setWieldedItem(item)) {
  1099. SendInventory(playersao);
  1100. }
  1101. }
  1102. // If item has node placement prediction, always send the
  1103. // blocks to make sure the client knows what exactly happened
  1104. RemoteClient *client = getClient(pkt->getPeerId());
  1105. v3s16 blockpos = getNodeBlockPos(floatToInt(pointed_pos_above, BS));
  1106. v3s16 blockpos2 = getNodeBlockPos(floatToInt(pointed_pos_under, BS));
  1107. if (!item.getDefinition(m_itemdef).node_placement_prediction.empty()) {
  1108. client->SetBlockNotSent(blockpos);
  1109. if (blockpos2 != blockpos) {
  1110. client->SetBlockNotSent(blockpos2);
  1111. }
  1112. }
  1113. else {
  1114. client->ResendBlockIfOnWire(blockpos);
  1115. if (blockpos2 != blockpos) {
  1116. client->ResendBlockIfOnWire(blockpos2);
  1117. }
  1118. }
  1119. } // action == 3
  1120. /*
  1121. 4: use
  1122. */
  1123. else if (action == 4) {
  1124. ItemStack item = playersao->getWieldedItem();
  1125. actionstream << player->getName() << " uses " << item.name
  1126. << ", pointing at " << pointed.dump() << std::endl;
  1127. if (m_script->item_OnUse(
  1128. item, playersao, pointed)) {
  1129. // Apply returned ItemStack
  1130. if (playersao->setWieldedItem(item)) {
  1131. SendInventory(playersao);
  1132. }
  1133. }
  1134. } // action == 4
  1135. /*
  1136. 5: rightclick air
  1137. */
  1138. else if (action == 5) {
  1139. ItemStack item = playersao->getWieldedItem();
  1140. actionstream << player->getName() << " activates "
  1141. << item.name << std::endl;
  1142. if (m_script->item_OnSecondaryUse(
  1143. item, playersao)) {
  1144. if( playersao->setWieldedItem(item)) {
  1145. SendInventory(playersao);
  1146. }
  1147. }
  1148. }
  1149. /*
  1150. Catch invalid actions
  1151. */
  1152. else {
  1153. warningstream << "Server: Invalid action "
  1154. << action << std::endl;
  1155. }
  1156. }
  1157. void Server::handleCommand_RemovedSounds(NetworkPacket* pkt)
  1158. {
  1159. u16 num;
  1160. *pkt >> num;
  1161. for (u16 k = 0; k < num; k++) {
  1162. s32 id;
  1163. *pkt >> id;
  1164. std::unordered_map<s32, ServerPlayingSound>::iterator i =
  1165. m_playing_sounds.find(id);
  1166. if (i == m_playing_sounds.end())
  1167. continue;
  1168. ServerPlayingSound &psound = i->second;
  1169. psound.clients.erase(pkt->getPeerId());
  1170. if (psound.clients.empty())
  1171. m_playing_sounds.erase(i++);
  1172. }
  1173. }
  1174. void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt)
  1175. {
  1176. v3s16 p;
  1177. std::string formname;
  1178. u16 num;
  1179. *pkt >> p >> formname >> num;
  1180. StringMap fields;
  1181. for (u16 k = 0; k < num; k++) {
  1182. std::string fieldname;
  1183. *pkt >> fieldname;
  1184. fields[fieldname] = pkt->readLongString();
  1185. }
  1186. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  1187. if (player == NULL) {
  1188. errorstream << "Server::ProcessData(): Canceling: "
  1189. "No player for peer_id=" << pkt->getPeerId()
  1190. << " disconnecting peer!" << std::endl;
  1191. DisconnectPeer(pkt->getPeerId());
  1192. return;
  1193. }
  1194. PlayerSAO *playersao = player->getPlayerSAO();
  1195. if (playersao == NULL) {
  1196. errorstream << "Server::ProcessData(): Canceling: "
  1197. "No player object for peer_id=" << pkt->getPeerId()
  1198. << " disconnecting peer!" << std::endl;
  1199. DisconnectPeer(pkt->getPeerId());
  1200. return;
  1201. }
  1202. // If something goes wrong, this player is to blame
  1203. RollbackScopeActor rollback_scope(m_rollback,
  1204. std::string("player:")+player->getName());
  1205. // Check the target node for rollback data; leave others unnoticed
  1206. RollbackNode rn_old(&m_env->getMap(), p, this);
  1207. m_script->node_on_receive_fields(p, formname, fields, playersao);
  1208. // Report rollback data
  1209. RollbackNode rn_new(&m_env->getMap(), p, this);
  1210. if (rollback() && rn_new != rn_old) {
  1211. RollbackAction action;
  1212. action.setSetNode(p, rn_old, rn_new);
  1213. rollback()->reportAction(action);
  1214. }
  1215. }
  1216. void Server::handleCommand_InventoryFields(NetworkPacket* pkt)
  1217. {
  1218. std::string formname;
  1219. u16 num;
  1220. *pkt >> formname >> num;
  1221. StringMap fields;
  1222. for (u16 k = 0; k < num; k++) {
  1223. std::string fieldname;
  1224. *pkt >> fieldname;
  1225. fields[fieldname] = pkt->readLongString();
  1226. }
  1227. RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
  1228. if (player == NULL) {
  1229. errorstream << "Server::ProcessData(): Canceling: "
  1230. "No player for peer_id=" << pkt->getPeerId()
  1231. << " disconnecting peer!" << std::endl;
  1232. DisconnectPeer(pkt->getPeerId());
  1233. return;
  1234. }
  1235. PlayerSAO *playersao = player->getPlayerSAO();
  1236. if (playersao == NULL) {
  1237. errorstream << "Server::ProcessData(): Canceling: "
  1238. "No player object for peer_id=" << pkt->getPeerId()
  1239. << " disconnecting peer!" << std::endl;
  1240. DisconnectPeer(pkt->getPeerId());
  1241. return;
  1242. }
  1243. m_script->on_playerReceiveFields(playersao, formname, fields);
  1244. }
  1245. void Server::handleCommand_FirstSrp(NetworkPacket* pkt)
  1246. {
  1247. RemoteClient* client = getClient(pkt->getPeerId(), CS_Invalid);
  1248. ClientState cstate = client->getState();
  1249. std::string playername = client->getName();
  1250. std::string salt;
  1251. std::string verification_key;
  1252. std::string addr_s = getPeerAddress(pkt->getPeerId()).serializeString();
  1253. u8 is_empty;
  1254. *pkt >> salt >> verification_key >> is_empty;
  1255. verbosestream << "Server: Got TOSERVER_FIRST_SRP from " << addr_s
  1256. << ", with is_empty=" << (is_empty == 1) << std::endl;
  1257. // Either this packet is sent because the user is new or to change the password
  1258. if (cstate == CS_HelloSent) {
  1259. if (!client->isMechAllowed(AUTH_MECHANISM_FIRST_SRP)) {
  1260. actionstream << "Server: Client from " << addr_s
  1261. << " tried to set password without being "
  1262. << "authenticated, or the username being new." << std::endl;
  1263. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1264. return;
  1265. }
  1266. if (!isSingleplayer() &&
  1267. g_settings->getBool("disallow_empty_password") &&
  1268. is_empty == 1) {
  1269. actionstream << "Server: " << playername
  1270. << " supplied empty password from " << addr_s << std::endl;
  1271. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_EMPTY_PASSWORD);
  1272. return;
  1273. }
  1274. std::string initial_ver_key;
  1275. initial_ver_key = encode_srp_verifier(verification_key, salt);
  1276. m_script->createAuth(playername, initial_ver_key);
  1277. acceptAuth(pkt->getPeerId(), false);
  1278. } else {
  1279. if (cstate < CS_SudoMode) {
  1280. infostream << "Server::ProcessData(): Ignoring TOSERVER_FIRST_SRP from "
  1281. << addr_s << ": " << "Client has wrong state " << cstate << "."
  1282. << std::endl;
  1283. return;
  1284. }
  1285. m_clients.event(pkt->getPeerId(), CSE_SudoLeave);
  1286. std::string pw_db_field = encode_srp_verifier(verification_key, salt);
  1287. bool success = m_script->setPassword(playername, pw_db_field);
  1288. if (success) {
  1289. actionstream << playername << " changes password" << std::endl;
  1290. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  1291. L"Password change successful."));
  1292. } else {
  1293. actionstream << playername << " tries to change password but "
  1294. << "it fails" << std::endl;
  1295. SendChatMessage(pkt->getPeerId(), ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
  1296. L"Password change failed or unavailable."));
  1297. }
  1298. }
  1299. }
  1300. void Server::handleCommand_SrpBytesA(NetworkPacket* pkt)
  1301. {
  1302. RemoteClient* client = getClient(pkt->getPeerId(), CS_Invalid);
  1303. ClientState cstate = client->getState();
  1304. bool wantSudo = (cstate == CS_Active);
  1305. if (!((cstate == CS_HelloSent) || (cstate == CS_Active))) {
  1306. actionstream << "Server: got SRP _A packet in wrong state "
  1307. << cstate << " from "
  1308. << getPeerAddress(pkt->getPeerId()).serializeString()
  1309. << ". Ignoring." << std::endl;
  1310. return;
  1311. }
  1312. if (client->chosen_mech != AUTH_MECHANISM_NONE) {
  1313. actionstream << "Server: got SRP _A packet, while auth"
  1314. << "is already going on with mech " << client->chosen_mech
  1315. << " from " << getPeerAddress(pkt->getPeerId()).serializeString()
  1316. << " (wantSudo=" << wantSudo << "). Ignoring." << std::endl;
  1317. if (wantSudo) {
  1318. DenySudoAccess(pkt->getPeerId());
  1319. return;
  1320. }
  1321. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1322. return;
  1323. }
  1324. std::string bytes_A;
  1325. u8 based_on;
  1326. *pkt >> bytes_A >> based_on;
  1327. infostream << "Server: TOSERVER_SRP_BYTES_A received with "
  1328. << "based_on=" << int(based_on) << " and len_A="
  1329. << bytes_A.length() << "." << std::endl;
  1330. AuthMechanism chosen = (based_on == 0) ?
  1331. AUTH_MECHANISM_LEGACY_PASSWORD : AUTH_MECHANISM_SRP;
  1332. if (wantSudo) {
  1333. if (!client->isSudoMechAllowed(chosen)) {
  1334. actionstream << "Server: Player \"" << client->getName()
  1335. << "\" at " << getPeerAddress(pkt->getPeerId()).serializeString()
  1336. << " tried to change password using unallowed mech "
  1337. << chosen << "." << std::endl;
  1338. DenySudoAccess(pkt->getPeerId());
  1339. return;
  1340. }
  1341. } else {
  1342. if (!client->isMechAllowed(chosen)) {
  1343. actionstream << "Server: Client tried to authenticate from "
  1344. << getPeerAddress(pkt->getPeerId()).serializeString()
  1345. << " using unallowed mech " << chosen << "." << std::endl;
  1346. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1347. return;
  1348. }
  1349. }
  1350. client->chosen_mech = chosen;
  1351. std::string salt;
  1352. std::string verifier;
  1353. if (based_on == 0) {
  1354. generate_srp_verifier_and_salt(client->getName(), client->enc_pwd,
  1355. &verifier, &salt);
  1356. } else if (!decode_srp_verifier_and_salt(client->enc_pwd, &verifier, &salt)) {
  1357. // Non-base64 errors should have been catched in the init handler
  1358. actionstream << "Server: User " << client->getName()
  1359. << " tried to log in, but srp verifier field"
  1360. << " was invalid (most likely invalid base64)." << std::endl;
  1361. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
  1362. return;
  1363. }
  1364. char *bytes_B = 0;
  1365. size_t len_B = 0;
  1366. client->auth_data = srp_verifier_new(SRP_SHA256, SRP_NG_2048,
  1367. client->getName().c_str(),
  1368. (const unsigned char *) salt.c_str(), salt.size(),
  1369. (const unsigned char *) verifier.c_str(), verifier.size(),
  1370. (const unsigned char *) bytes_A.c_str(), bytes_A.size(),
  1371. NULL, 0,
  1372. (unsigned char **) &bytes_B, &len_B, NULL, NULL);
  1373. if (!bytes_B) {
  1374. actionstream << "Server: User " << client->getName()
  1375. << " tried to log in, SRP-6a safety check violated in _A handler."
  1376. << std::endl;
  1377. if (wantSudo) {
  1378. DenySudoAccess(pkt->getPeerId());
  1379. return;
  1380. }
  1381. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1382. return;
  1383. }
  1384. NetworkPacket resp_pkt(TOCLIENT_SRP_BYTES_S_B, 0, pkt->getPeerId());
  1385. resp_pkt << salt << std::string(bytes_B, len_B);
  1386. Send(&resp_pkt);
  1387. }
  1388. void Server::handleCommand_SrpBytesM(NetworkPacket* pkt)
  1389. {
  1390. RemoteClient* client = getClient(pkt->getPeerId(), CS_Invalid);
  1391. ClientState cstate = client->getState();
  1392. bool wantSudo = (cstate == CS_Active);
  1393. verbosestream << "Server: Received TOCLIENT_SRP_BYTES_M." << std::endl;
  1394. if (!((cstate == CS_HelloSent) || (cstate == CS_Active))) {
  1395. actionstream << "Server: got SRP _M packet in wrong state "
  1396. << cstate << " from "
  1397. << getPeerAddress(pkt->getPeerId()).serializeString()
  1398. << ". Ignoring." << std::endl;
  1399. return;
  1400. }
  1401. if (client->chosen_mech != AUTH_MECHANISM_SRP &&
  1402. client->chosen_mech != AUTH_MECHANISM_LEGACY_PASSWORD) {
  1403. actionstream << "Server: got SRP _M packet, while auth"
  1404. << "is going on with mech " << client->chosen_mech
  1405. << " from " << getPeerAddress(pkt->getPeerId()).serializeString()
  1406. << " (wantSudo=" << wantSudo << "). Denying." << std::endl;
  1407. if (wantSudo) {
  1408. DenySudoAccess(pkt->getPeerId());
  1409. return;
  1410. }
  1411. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1412. return;
  1413. }
  1414. std::string bytes_M;
  1415. *pkt >> bytes_M;
  1416. if (srp_verifier_get_session_key_length((SRPVerifier *) client->auth_data)
  1417. != bytes_M.size()) {
  1418. actionstream << "Server: User " << client->getName()
  1419. << " at " << getPeerAddress(pkt->getPeerId()).serializeString()
  1420. << " sent bytes_M with invalid length " << bytes_M.size() << std::endl;
  1421. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_UNEXPECTED_DATA);
  1422. return;
  1423. }
  1424. unsigned char *bytes_HAMK = 0;
  1425. srp_verifier_verify_session((SRPVerifier *) client->auth_data,
  1426. (unsigned char *)bytes_M.c_str(), &bytes_HAMK);
  1427. if (!bytes_HAMK) {
  1428. if (wantSudo) {
  1429. actionstream << "Server: User " << client->getName()
  1430. << " at " << getPeerAddress(pkt->getPeerId()).serializeString()
  1431. << " tried to change their password, but supplied wrong"
  1432. << " (SRP) password for authentication." << std::endl;
  1433. DenySudoAccess(pkt->getPeerId());
  1434. return;
  1435. }
  1436. actionstream << "Server: User " << client->getName()
  1437. << " at " << getPeerAddress(pkt->getPeerId()).serializeString()
  1438. << " supplied wrong password (auth mechanism: SRP)."
  1439. << std::endl;
  1440. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_WRONG_PASSWORD);
  1441. return;
  1442. }
  1443. if (client->create_player_on_auth_success) {
  1444. std::string playername = client->getName();
  1445. m_script->createAuth(playername, client->enc_pwd);
  1446. std::string checkpwd; // not used, but needed for passing something
  1447. if (!m_script->getAuth(playername, &checkpwd, NULL)) {
  1448. actionstream << "Server: " << playername << " cannot be authenticated"
  1449. << " (auth handler does not work?)" << std::endl;
  1450. DenyAccess(pkt->getPeerId(), SERVER_ACCESSDENIED_SERVER_FAIL);
  1451. return;
  1452. }
  1453. client->create_player_on_auth_success = false;
  1454. }
  1455. acceptAuth(pkt->getPeerId(), wantSudo);
  1456. }
  1457. /*
  1458. * Mod channels
  1459. */
  1460. void Server::handleCommand_ModChannelJoin(NetworkPacket *pkt)
  1461. {
  1462. std::string channel_name;
  1463. *pkt >> channel_name;
  1464. NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL, 1 + 2 + channel_name.size(),
  1465. pkt->getPeerId());
  1466. // Send signal to client to notify join succeed or not
  1467. if (g_settings->getBool("enable_mod_channels") &&
  1468. m_modchannel_mgr->joinChannel(channel_name, pkt->getPeerId())) {
  1469. resp_pkt << (u8) MODCHANNEL_SIGNAL_JOIN_OK;
  1470. infostream << "Peer " << pkt->getPeerId() << " joined channel " << channel_name
  1471. << std::endl;
  1472. }
  1473. else {
  1474. resp_pkt << (u8)MODCHANNEL_SIGNAL_JOIN_FAILURE;
  1475. infostream << "Peer " << pkt->getPeerId() << " tried to join channel "
  1476. << channel_name << ", but was already registered." << std::endl;
  1477. }
  1478. resp_pkt << channel_name;
  1479. Send(&resp_pkt);
  1480. }
  1481. void Server::handleCommand_ModChannelLeave(NetworkPacket *pkt)
  1482. {
  1483. std::string channel_name;
  1484. *pkt >> channel_name;
  1485. NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL, 1 + 2 + channel_name.size(),
  1486. pkt->getPeerId());
  1487. // Send signal to client to notify join succeed or not
  1488. if (g_settings->getBool("enable_mod_channels") &&
  1489. m_modchannel_mgr->leaveChannel(channel_name, pkt->getPeerId())) {
  1490. resp_pkt << (u8)MODCHANNEL_SIGNAL_LEAVE_OK;
  1491. infostream << "Peer " << pkt->getPeerId() << " left channel " << channel_name
  1492. << std::endl;
  1493. } else {
  1494. resp_pkt << (u8) MODCHANNEL_SIGNAL_LEAVE_FAILURE;
  1495. infostream << "Peer " << pkt->getPeerId() << " left channel " << channel_name
  1496. << ", but was not registered." << std::endl;
  1497. }
  1498. resp_pkt << channel_name;
  1499. Send(&resp_pkt);
  1500. }
  1501. void Server::handleCommand_ModChannelMsg(NetworkPacket *pkt)
  1502. {
  1503. std::string channel_name, channel_msg;
  1504. *pkt >> channel_name >> channel_msg;
  1505. verbosestream << "Mod channel message received from peer " << pkt->getPeerId()
  1506. << " on channel " << channel_name << " message: " << channel_msg << std::endl;
  1507. // If mod channels are not enabled, discard message
  1508. if (!g_settings->getBool("enable_mod_channels")) {
  1509. return;
  1510. }
  1511. // If channel not registered, signal it and ignore message
  1512. if (!m_modchannel_mgr->channelRegistered(channel_name)) {
  1513. NetworkPacket resp_pkt(TOCLIENT_MODCHANNEL_SIGNAL, 1 + 2 + channel_name.size(),
  1514. pkt->getPeerId());
  1515. resp_pkt << (u8)MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED << channel_name;
  1516. Send(&resp_pkt);
  1517. return;
  1518. }
  1519. // @TODO: filter, rate limit
  1520. broadcastModChannelMessage(channel_name, channel_msg, pkt->getPeerId());
  1521. }