chatcommand.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "chatcommand.h"
  2. #include "stringbuilder.h"
  3. #include "message.h"
  4. #include "datastring.h"
  5. #include <stdio.h>
  6. #include <inttypes.h>
  7. #include "Debug.h"
  8. message *chatcommand::fromstringbuilder(stringbuilder &output)
  9. {
  10. // Converts a stringbuilder to a message object.
  11. message *new_message;
  12. new_message = new message();
  13. *new_message = output;
  14. return new_message;
  15. }
  16. void chatcommand::incorrect_system_password(chatclient *client,int64_t messageid)
  17. {
  18. message *new_message;
  19. stringbuilder output;
  20. output += "error(26,Incorrect system password";
  21. output += messageid;
  22. output += ")";
  23. new_message = new message();
  24. *new_message = output;
  25. client->add_message(new_message);
  26. idisposable::dereference((idisposable**)&new_message);
  27. client->callback_on_writable();
  28. return;
  29. }
  30. void chatcommand::success_message(chatclient *client,int64_t messageid)
  31. {
  32. stringbuilder output;
  33. message *new_message;
  34. new_message = new message();
  35. output += "success(";
  36. output += messageid;
  37. output += ")";
  38. *new_message = output;
  39. client->add_message(new_message);
  40. idisposable::dereference((idisposable**)&new_message);
  41. client->callback_on_writable();
  42. return;
  43. }
  44. void chatcommand::not_implemented(chatclient *client)
  45. {
  46. client->add_message("error(36,That command is not yet implemented.0)");
  47. client->callback_on_writable();
  48. return;
  49. }
  50. void chatcommand::parameters_not_correct(chatclient *client,message *received_message)
  51. {
  52. stringbuilder output;
  53. message *new_message;
  54. output += "erorr(";
  55. output += (45+received_message->actual_message.length);
  56. output += ",";
  57. output += "Syntax error in parameters. Original message:";
  58. output += received_message->actual_message;
  59. output += "0)";
  60. new_message = fromstringbuilder(output);
  61. client->add_message(new_message);
  62. idisposable::dereference((idisposable**)&new_message);
  63. client->callback_on_writable();
  64. return;
  65. }
  66. void chatcommand::message_not_understood(chatclient *client,datastring bad_message)
  67. {
  68. stringbuilder output;
  69. message *new_message;
  70. output += "error(";
  71. output += (bad_message.length + 32);
  72. output += ",That message was not understood:";
  73. output += bad_message;
  74. output += "0)";
  75. new_message = fromstringbuilder(output);
  76. client->add_message(new_message);
  77. idisposable::dereference((idisposable**)&new_message);
  78. client->callback_on_writable();
  79. return;
  80. }
  81. void chatcommand::error(chatclient *client,datastring error_message,int64_t messageid)
  82. {
  83. message *new_message;
  84. stringbuilder output;
  85. new_message = new message();
  86. output += "error(";
  87. output.addparameter(error_message);
  88. output += messageid;
  89. output +=")";
  90. *new_message = output;
  91. client->add_message(new_message);
  92. idisposable::dereference((idisposable**)&new_message);
  93. client->callback_on_writable();
  94. }
  95. void chatcommand::error(chatclient *client,datastring error_message)
  96. {
  97. stringbuilder output;
  98. message *new_message;
  99. output += "error(";
  100. output.addparameter(error_message);
  101. output +="0)";
  102. new_message = new message();
  103. *new_message = output;
  104. client->add_message(new_message);
  105. idisposable::dereference((idisposable**)&new_message);
  106. client->callback_on_writable();
  107. }
  108. message *chatcommand::userjoinedchatroom(
  109. int64_t messageid,int64_t chatroomid,int64_t chatclientid, user *new_user)
  110. {
  111. stringbuilder output;
  112. output += "userjoinedchatroom(";
  113. output.addparameter(messageid);
  114. output.addparameter(chatroomid);
  115. output.addparameter(chatclientid);
  116. if (new_user == nullptr) {
  117. output += "0,0,";
  118. } else {
  119. output.addparameter(new_user->userid);
  120. output.addparameter(*new_user->username);
  121. }
  122. output += ")";
  123. return fromstringbuilder(output);
  124. }
  125. message *chatcommand::userleftchatroom(int64_t messageid,int64_t chatroomid, int64_t chatclientid, user *old_user)
  126. {
  127. stringbuilder output;
  128. output += "userleftchatroom(";
  129. output.addparameter(messageid);
  130. output.addparameter(chatroomid);
  131. if (old_user == nullptr) {
  132. output += "0,0,";
  133. } else {
  134. output.addparameter(old_user->userid);
  135. output.addparameter(*old_user->username);
  136. }
  137. output += ")";
  138. return fromstringbuilder(output);
  139. }
  140. message *chatcommand::chatroomwasdeleted(int64_t messageid,int64_t chatroomid)
  141. {
  142. Debug debug(__FILE__,__func__,__LINE__);
  143. message *new_message;
  144. stringbuilder output;
  145. output += "chatroomwasdeleted(";
  146. debug = __LINE__;
  147. output.addparameter(messageid);
  148. debug = __LINE__;
  149. output.addparameter(chatroomid);
  150. debug = __LINE__;
  151. output += ")";
  152. debug = __LINE__;
  153. return fromstringbuilder(output);
  154. }
  155. message *chatcommand::chatroomcreated(int64_t messageid,int64_t chatroomid,datastring room_name,int info)
  156. {
  157. stringbuilder output_message;
  158. output_message += "chatroomcreated(";
  159. output_message.addparameter(messageid);
  160. output_message.addparameter(chatroomid);
  161. output_message.addparameter(room_name);
  162. output_message.addparameter(info);
  163. output_message += ")";
  164. return fromstringbuilder(output_message);
  165. }
  166. message *chatcommand::yourchatclientid(int64_t chatclientid)
  167. {
  168. stringbuilder output_message;
  169. output_message += "yourchatclientid(";
  170. output_message.addparameter(chatclientid);
  171. output_message += ")";
  172. return fromstringbuilder(output_message);
  173. }
  174. message *chatcommand::chat(int64_t messageid,int64_t chatroomid
  175. ,int64_t chatclientid,int64_t userid,datastring message_to_send)
  176. {
  177. stringbuilder output_message;
  178. output_message += "chat(";
  179. output_message.addparameter(messageid);
  180. output_message.addparameter(chatroomid);
  181. output_message.addparameter(chatclientid);
  182. output_message.addparameter(userid);
  183. output_message.addparameter(message_to_send);
  184. output_message += ")";
  185. return fromstringbuilder(output_message);
  186. }
  187. message *chatcommand::msg(int64_t messageid
  188. ,int64_t chatclientid,int64_t userid,datastring message_to_send)
  189. {
  190. stringbuilder output_message;
  191. output_message += "msg(";
  192. output_message.addparameter(messageid);
  193. output_message.addparameter(chatclientid);
  194. output_message.addparameter(userid);
  195. output_message.addparameter(message_to_send);
  196. output_message += ")";
  197. return fromstringbuilder(output_message);
  198. }
  199. message *chatcommand::simplegamestarted(int64_t messageid,datastring gameid)
  200. {
  201. stringbuilder output_message;
  202. output_message += "simplegamestarted(";
  203. output_message.addparameter(messageid);
  204. output_message.addparameter(gameid);
  205. output_message += ")";
  206. return fromstringbuilder(output_message);
  207. }
  208. message *chatcommand::simplegamedata(int64_t messageid,datastring gameid,datastring username,datastring gamedata)
  209. {
  210. stringbuilder output_message;
  211. output_message += "simplegamedata(";
  212. output_message.addparameter(messageid);
  213. output_message.addparameter(gameid);
  214. output_message.addparameter(username);
  215. output_message.addparameter(gamedata);
  216. output_message += ")";
  217. return fromstringbuilder(output_message);
  218. }
  219. message *chatcommand::simplegameleave(int64_t messageid,datastring gameid,datastring username,datastring gamedata)
  220. {
  221. stringbuilder output_message;
  222. output_message += "simplegameleave(";
  223. output_message.addparameter(messageid);
  224. output_message.addparameter(gameid);
  225. output_message.addparameter(username);
  226. output_message.addparameter(gamedata);
  227. output_message += ")";
  228. return fromstringbuilder(output_message);
  229. }
  230. message *chatcommand::simplegamechat(int64_t messageid,datastring gameid,datastring username,datastring chatmessage)
  231. {
  232. stringbuilder output_message;
  233. output_message += "simplegamechat(";
  234. output_message.addparameter(messageid);
  235. output_message.addparameter(gameid);
  236. output_message.addparameter(username);
  237. output_message.addparameter(chatmessage);
  238. output_message += ")";
  239. return fromstringbuilder(output_message);
  240. }
  241. message *chatcommand::simplegamejoined(int64_t messageid,datastring gameid,datastring gamedata,biglist<simplechatgameuser *> *userlist)
  242. {
  243. stringbuilder output_message;
  244. datastring upordown;
  245. output_message += "simplegamejoined(";
  246. output_message.addparameter(messageid);
  247. output_message.addparameter(gameid);
  248. output_message.addparameter(gamedata);
  249. biglist_iterator<simplechatgameuser *>userloop(userlist);
  250. while (!userloop.eof()) {
  251. output_message.addparameter(*(userloop.item->username));
  252. upordown = userloop.item->client == nullptr ? "Down" : "Up";
  253. output_message.addparameter(upordown);
  254. userloop.movenext();
  255. }
  256. output_message += ")";
  257. return fromstringbuilder(output_message);
  258. }
  259. message *chatcommand::simplegameusers(int64_t messageid,datastring gameid,biglist<simplechatgameuser *> *userlist)
  260. {
  261. stringbuilder output_message;
  262. output_message += "simplegameusers(";
  263. output_message.addparameter(messageid);
  264. output_message.addparameter(gameid);
  265. datastring upordown;
  266. if (userlist != nullptr) {
  267. biglist_iterator<simplechatgameuser *>userloop(userlist);
  268. while (!userloop.eof()) {
  269. output_message.addparameter(*(userloop.item->username));
  270. upordown = userloop.item->client == nullptr ? "Down" : "Up";
  271. output_message.addparameter(upordown);
  272. userloop.movenext();
  273. }
  274. }
  275. output_message += ")";
  276. return fromstringbuilder(output_message);
  277. }
  278. message *chatcommand::simplegameuserdata(int64_t messageid,datastring gameid,datastring username,datastring userdata)
  279. {
  280. // A message listing the user data for a user in a game.
  281. stringbuilder output_message;
  282. output_message += "simplegameuserdata(";
  283. output_message.addparameter(messageid);
  284. output_message.addparameter(gameid);
  285. output_message.addparameter(username);
  286. output_message.addparameter(userdata);
  287. output_message += ")";
  288. return fromstringbuilder(output_message);
  289. }