WOLChannel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/WWOnline/WOLChannel.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * $Author: Denzil_l $
  27. *
  28. * VERSION INFO
  29. * $Revision: 23 $
  30. * $Modtime: 1/17/02 12:04p $
  31. *
  32. ******************************************************************************/
  33. #include "always.h"
  34. #include <stdlib.h>
  35. #include "WOLChannel.h"
  36. #include "WOLString.h"
  37. #include <WOLAPI\ChatDefs.h>
  38. #include <WWDebug\WWDebug.h>
  39. namespace WWOnline {
  40. /******************************************************************************
  41. *
  42. * NAME
  43. * ChannelData::Create(WOL::Channel)
  44. *
  45. * DESCRIPTION
  46. * Create Channel Data object from a WOLAPI channel.
  47. *
  48. * INPUTS
  49. * WOLChannel - WOLAPI channel structure.
  50. *
  51. * RESULT
  52. * Channel - Channel data object.
  53. *
  54. ******************************************************************************/
  55. RefPtr<ChannelData> ChannelData::Create(const WOL::Channel& wolChannel)
  56. {
  57. return new ChannelData(wolChannel);
  58. }
  59. /******************************************************************************
  60. *
  61. * NAME
  62. * ChannelData::Create(Name, Password, Type)
  63. *
  64. * DESCRIPTION
  65. * Create a new Channel Data object
  66. *
  67. * INPUTS
  68. * Name - Name of channel.
  69. * Password - Password needed to join channel
  70. * Type - Type of channel
  71. *
  72. * RESULT
  73. * Channel - Channel data object.
  74. *
  75. ******************************************************************************/
  76. RefPtr<ChannelData> ChannelData::Create(const wchar_t* name, const wchar_t* password, int type)
  77. {
  78. if (name && wcslen(name))
  79. {
  80. return new ChannelData(name, password, type);
  81. }
  82. return NULL;
  83. }
  84. /******************************************************************************
  85. *
  86. * NAME
  87. * ChannelData::ChannelData(WOL::Channel)
  88. *
  89. * DESCRIPTION
  90. * WOLAPI channel constructor
  91. *
  92. * INPUTS
  93. * WOLChannel - WOLAPI channel structure.
  94. *
  95. * RESULT
  96. * NONE
  97. *
  98. ******************************************************************************/
  99. ChannelData::ChannelData(const WOL::Channel& channel) :
  100. mChannelName((char*)channel.name)
  101. {
  102. memcpy(&mData, &channel, sizeof(mData));
  103. mData.next = NULL;
  104. }
  105. /******************************************************************************
  106. *
  107. * NAME
  108. * ChannelData::ChannelData(Name, Password, Type)
  109. *
  110. * DESCRIPTION
  111. * Named channel constructor
  112. *
  113. * INPUTS
  114. * Name - Name of channel.
  115. * Password - Password needed to join channel
  116. * Type - Type of channel
  117. *
  118. * RESULT
  119. * NONE
  120. *
  121. ******************************************************************************/
  122. ChannelData::ChannelData(const wchar_t* name, const wchar_t* password, int type) :
  123. mChannelName(name)
  124. {
  125. memset(&mData, 0, sizeof(mData));
  126. mData.type = type;
  127. WWASSERT(name && "NULL channel name");
  128. wcstombs((char*)mData.name, name, sizeof(mData.name));
  129. mData.name[sizeof(mData.name) - 1] = 0;
  130. if (password)
  131. {
  132. wcstombs((char*)mData.key, password, sizeof(mData.key));
  133. }
  134. }
  135. /******************************************************************************
  136. *
  137. * NAME
  138. * ChannelData::~ChannelData
  139. *
  140. * DESCRIPTION
  141. * Destructor
  142. *
  143. * INPUTS
  144. * NONE
  145. *
  146. * RESULT
  147. * NONE
  148. *
  149. ******************************************************************************/
  150. ChannelData::~ChannelData()
  151. {
  152. }
  153. /******************************************************************************
  154. *
  155. * NAME
  156. * ChannelData::UpdateData
  157. *
  158. * DESCRIPTION
  159. * Update channel data with the WOLAPI channel
  160. *
  161. * INPUTS
  162. *
  163. * RESULT
  164. * NONE
  165. *
  166. ******************************************************************************/
  167. void ChannelData::UpdateData(const WOL::Channel& wolChannel)
  168. {
  169. wchar_t name[64];
  170. mbstowcs(name, (const char*)wolChannel.name, sizeof(wolChannel.name));
  171. name[sizeof(wolChannel.name) - 1] = 0;
  172. WWASSERT(wcslen(name));
  173. bool isValid = (!mChannelName.Is_Empty() && mChannelName.Compare_No_Case(name) == 0);
  174. WWASSERT(isValid && "WOLChannelData::UpdateData() channel mismatch");
  175. if (isValid)
  176. {
  177. memcpy(&mData, &wolChannel, sizeof(mData));
  178. mData.next = NULL;
  179. }
  180. }
  181. /******************************************************************************
  182. *
  183. * NAME
  184. * ChannelData::SetLatency
  185. *
  186. * DESCRIPTION
  187. * Update the channel latency.
  188. *
  189. * INPUTS
  190. * Latency - Channel latency
  191. *
  192. * RESULT
  193. * NONE
  194. *
  195. ******************************************************************************/
  196. void ChannelData::SetLatency(int latency)
  197. {
  198. mData.latency = latency;
  199. }
  200. /******************************************************************************
  201. *
  202. * NAME
  203. * ChannelData::SetTopic
  204. *
  205. * DESCRIPTION
  206. * Set the channel's topic
  207. *
  208. * INPUTS
  209. * Topic - New channel topic string.
  210. *
  211. * RESULT
  212. * NONE
  213. *
  214. ******************************************************************************/
  215. void ChannelData::SetTopic(const char* topic)
  216. {
  217. strncpy((char*)mData.topic, topic, sizeof(mData.topic));
  218. mData.topic[sizeof(mData.topic) - 1] = 0;
  219. }
  220. /******************************************************************************
  221. *
  222. * NAME
  223. * ChannelData::SetExtraInfo
  224. *
  225. * DESCRIPTION
  226. * Set the channel's extra information.
  227. *
  228. * INPUTS
  229. * ExInfo - Data to be kept in the channel's ExInfo field.
  230. *
  231. * RESULT
  232. * NONE
  233. *
  234. ******************************************************************************/
  235. void ChannelData::SetExtraInfo(const char* exInfo)
  236. {
  237. strncpy((char*)mData.exInfo, exInfo, sizeof(mData.exInfo));
  238. mData.exInfo[sizeof(mData.exInfo) - 1] = 0;
  239. }
  240. /******************************************************************************
  241. *
  242. * NAME
  243. * ChannelData::IsPassworded
  244. *
  245. * DESCRIPTION
  246. * Check if the channel requires a password to join.
  247. *
  248. * INPUTS
  249. * NONE
  250. *
  251. * RESULT
  252. * True if password required.
  253. *
  254. ******************************************************************************/
  255. bool ChannelData::IsPassworded(void) const
  256. {
  257. return ((mData.flags & CHAN_MODE_KEY) == CHAN_MODE_KEY);
  258. }
  259. /******************************************************************************
  260. *
  261. * NAME
  262. * ChannelData::SetMinMaxUsers
  263. *
  264. * DESCRIPTION
  265. * Set the number of users that can join this channel.
  266. *
  267. * INPUTS
  268. * MinUsers - Minimum number of users required.
  269. * MaxUsers - Maximum number of users allowed.
  270. *
  271. * RESULT
  272. * NONE
  273. *
  274. ******************************************************************************/
  275. void ChannelData::SetMinMaxUsers(unsigned int minUsers, unsigned int maxUsers)
  276. {
  277. mData.minUsers = minUsers;
  278. mData.maxUsers = maxUsers;
  279. }
  280. /******************************************************************************
  281. *
  282. * NAME
  283. * ChannelData::SetOfficial
  284. *
  285. * DESCRIPTION
  286. * Change the official state of this channel.
  287. *
  288. * INPUTS
  289. * Official - Flag indicating if this channel is official or not.
  290. *
  291. * RESULT
  292. * NONE
  293. *
  294. ******************************************************************************/
  295. void ChannelData::SetOfficial(bool official)
  296. {
  297. mData.official = official;
  298. }
  299. /******************************************************************************
  300. *
  301. * NAME
  302. * ChannelData::SetTournament
  303. *
  304. * DESCRIPTION
  305. * Set the type of tournament game this channel hosting.
  306. *
  307. * INPUTS
  308. * Type - Type of tournament (0 = none)
  309. *
  310. * RESULT
  311. * NONE
  312. *
  313. ******************************************************************************/
  314. void ChannelData::SetTournament(unsigned int tournamentType)
  315. {
  316. mData.tournament = tournamentType;
  317. }
  318. /******************************************************************************
  319. *
  320. * NAME
  321. * GetChannelStatusFromHRESULT
  322. *
  323. * DESCRIPTION
  324. * Get a channel status from the provided HRESULT
  325. *
  326. * INPUTS
  327. * HRESULT - Error / Status code.
  328. *
  329. * RESULT
  330. * ChannelStatus - Channel status
  331. *
  332. ******************************************************************************/
  333. ChannelStatus GetChannelStatusFromHResult(HRESULT result)
  334. {
  335. switch (result)
  336. {
  337. case CHAT_E_CHANNELEXISTS:
  338. return ChannelExists;
  339. break;
  340. case CHAT_E_CHANNELDOESNOTEXIST:
  341. return ChannelNotFound;
  342. break;
  343. case CHAT_E_BADCHANNELPASSWORD:
  344. return ChannelBadPassword;
  345. break;
  346. case CHAT_E_CHANNELFULL:
  347. return ChannelFull;
  348. break;
  349. case CHAT_E_BANNED:
  350. return ChannelBanned;
  351. break;
  352. default:
  353. break;
  354. }
  355. return ChannelError;
  356. }
  357. /******************************************************************************
  358. *
  359. * NAME
  360. * GetChannelStatusDescription
  361. *
  362. * DESCRIPTION
  363. * Get a channel status description for its status code.
  364. *
  365. * INPUTS
  366. * Status - Channel status enumeration
  367. *
  368. * RESULT
  369. * Description - Text description of channel status.
  370. *
  371. ******************************************************************************/
  372. const wchar_t* GetChannelStatusDescription(ChannelStatus status)
  373. {
  374. static const char* _statusDesc[] =
  375. {
  376. "WOL_CHANNELERROR",
  377. "WOL_CHANNELLEFT",
  378. "WOL_CHANNELJOINED",
  379. "WOL_CHANNELCREATED",
  380. "WOL_CHANNELNOTFOUND",
  381. "WOL_CHANNELEXISTS",
  382. "WOL_BADPASSWORD",
  383. "WOL_CHANNELFULL",
  384. "WOL_CHANNELBANNED",
  385. "WOL_CHANNELKICKED",
  386. "WOL_BLANK", // ChannelNewData
  387. "WOL_CHANNELLEAVE"
  388. };
  389. WWASSERT(status >= 0 && status <= ChannelLeaving);
  390. return WOLSTRING(_statusDesc[status + 1]);
  391. }
  392. // Find the channel node by name
  393. ChannelList::iterator FindChannelNode(ChannelList& list, const char* name)
  394. {
  395. if (name == NULL)
  396. {
  397. return list.end();
  398. }
  399. ChannelList::iterator node = list.begin();
  400. while (node != list.end())
  401. {
  402. WOL::Channel& wolChannel = (*node)->GetData();
  403. if (stricmp(name, (const char*)wolChannel.name) == 0)
  404. {
  405. break;
  406. }
  407. node++;
  408. }
  409. return node;
  410. }
  411. // Find a channel in the specified list by ANSI name
  412. RefPtr<ChannelData> FindChannelInList(ChannelList& list, const char* name)
  413. {
  414. if (name)
  415. {
  416. ChannelList::iterator node = list.begin();
  417. while (node != list.end())
  418. {
  419. WOL::Channel& wolChannel = (*node)->GetData();
  420. if (stricmp(name, (const char*)wolChannel.name) == 0)
  421. {
  422. return *node;
  423. }
  424. node++;
  425. }
  426. }
  427. return NULL;
  428. }
  429. // Find a channel in the specified list by Unicode name.
  430. RefPtr<ChannelData> FindChannelInList(ChannelList& list, const wchar_t* name)
  431. {
  432. if (name)
  433. {
  434. ChannelList::iterator node = list.begin();
  435. while (node != list.end())
  436. {
  437. const WideStringClass& channame = (*node)->GetName();
  438. if (channame.Compare_No_Case(name) == 0)
  439. {
  440. return *node;
  441. }
  442. node++;
  443. }
  444. }
  445. return NULL;
  446. }
  447. } // namespace WWOnline