chatcommand_sendto.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "chatcommand_sendto.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "stringbuilder.h"
  6. #include "message.h"
  7. #include "Debug.h"
  8. #include <inttypes.h>
  9. // sendto(messageid,chatclientid,userid,message)
  10. // Send a message to a specific user.
  11. // Returns chat(messageid,chatroomid,chatclientid,userid,message) or error(message,messageid);
  12. // If user isn't logged in, userid = 0.
  13. bool chatcommand_sendto::processmessage(char first_letter,message *received_message,chatclient *client)
  14. {
  15. if ((first_letter != 's')
  16. || (received_message->actual_message.substr(0,7)!="sendto(")) {
  17. return false;
  18. }
  19. Debug debug(__FILE__,__func__,__LINE__);
  20. message *new_message;
  21. datastring method_parameters;
  22. parameters parameters_parsed;
  23. bool parameter_success = true;
  24. int64_t messageid;
  25. int64_t chatclientid;
  26. datastring message_to_send;
  27. int64_t userid;
  28. user *loop_user;
  29. datastring error_message;
  30. debug = __LINE__;
  31. method_parameters = received_message->actual_message.substr(7,received_message->actual_message.length-8);
  32. method_parameters.null_terminate(); // Used to comply with snprintf.
  33. parameters_parsed.long_parameter(method_parameters,parameter_success);
  34. parameters_parsed.long_parameter(method_parameters,parameter_success);
  35. parameters_parsed.long_parameter(method_parameters,parameter_success);
  36. parameters_parsed.string_parameter(method_parameters,parameter_success);
  37. debug = __LINE__;
  38. if (parameter_success) {
  39. // Give the parameters nice names.
  40. debug = __LINE__;
  41. messageid = parameters_parsed.long_parameters[0];
  42. chatclientid = parameters_parsed.long_parameters[1];
  43. userid = parameters_parsed.long_parameters[2];
  44. message_to_send = parameters_parsed.string_parameters[3];
  45. // find the user. Look in chatclients.
  46. debug = __LINE__;
  47. biglist_iterator<chatclient *>loop(&the_websocket->chatclients);
  48. while (!loop.eof())
  49. {
  50. debug = __LINE__;
  51. if (userid != 0) {
  52. loop_user = loop.item->logged_in_user;
  53. debug = __LINE__;
  54. if ((loop_user != nullptr) && (loop_user->userid == userid)) {
  55. break;
  56. }
  57. } else {
  58. debug = __LINE__;
  59. if (loop.item->chatclientid == chatclientid) {
  60. break;
  61. }
  62. }
  63. loop.movenext();
  64. }
  65. debug = __LINE__;
  66. if (loop.eof()) {
  67. debug = __LINE__;
  68. error_message = "That person is not online.";
  69. error(client,error_message,messageid);
  70. return true;
  71. }
  72. debug = __LINE__;
  73. new_message = msg(messageid,client->chatclientid,client->logged_in_user == nullptr ? 0 : client->logged_in_user->userid,message_to_send);
  74. loop.item->push_message(&new_message);
  75. } else {
  76. debug = __LINE__;
  77. parameters_not_correct(client,received_message);
  78. }
  79. debug = __LINE__;
  80. return true;
  81. }