IRCClient.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "IRCClient.h"
  27. #include "IRCAppWindow.h"
  28. #include "IRCChannel.h"
  29. #include "IRCLogBuffer.h"
  30. #include "IRCQuery.h"
  31. #include "IRCWindow.h"
  32. #include "IRCWindowListModel.h"
  33. #include <AK/QuickSort.h>
  34. #include <AK/StringBuilder.h>
  35. #include <LibCore/DateTime.h>
  36. #include <LibCore/Notifier.h>
  37. #include <arpa/inet.h>
  38. #include <netinet/in.h>
  39. #include <stdio.h>
  40. #include <sys/socket.h>
  41. #include <time.h>
  42. #include <unistd.h>
  43. #define IRC_DEBUG
  44. enum IRCNumeric {
  45. RPL_WHOISUSER = 311,
  46. RPL_WHOISSERVER = 312,
  47. RPL_WHOISOPERATOR = 313,
  48. RPL_WHOISIDLE = 317,
  49. RPL_ENDOFWHOIS = 318,
  50. RPL_WHOISCHANNELS = 319,
  51. RPL_TOPIC = 332,
  52. RPL_TOPICWHOTIME = 333,
  53. RPL_NAMREPLY = 353,
  54. RPL_ENDOFNAMES = 366,
  55. };
  56. IRCClient::IRCClient()
  57. : m_nickname("seren1ty")
  58. , m_client_window_list_model(IRCWindowListModel::create(*this))
  59. , m_log(IRCLogBuffer::create())
  60. , m_config(Core::ConfigFile::get_for_app("IRCClient"))
  61. {
  62. m_socket = Core::TCPSocket::construct(this);
  63. m_nickname = m_config->read_entry("User", "Nickname", "seren1ty");
  64. m_hostname = m_config->read_entry("Connection", "Server", "");
  65. m_port = m_config->read_num_entry("Connection", "Port", 6667);
  66. }
  67. IRCClient::~IRCClient()
  68. {
  69. }
  70. void IRCClient::set_server(const String& hostname, int port)
  71. {
  72. m_hostname = hostname;
  73. m_port = port;
  74. m_config->write_entry("Connection", "Server", hostname);
  75. m_config->write_num_entry("Connection", "Port", port);
  76. m_config->sync();
  77. }
  78. void IRCClient::on_socket_connected()
  79. {
  80. m_notifier = Core::Notifier::construct(m_socket->fd(), Core::Notifier::Read);
  81. m_notifier->on_ready_to_read = [this] { receive_from_server(); };
  82. send_user();
  83. send_nick();
  84. auto channel_str = m_config->read_entry("Connection", "AutoJoinChannels", "#test");
  85. dbgprintf("IRCClient: Channels to autojoin: %s\n", channel_str.characters());
  86. auto channels = channel_str.split(',');
  87. for (auto& channel : channels) {
  88. join_channel(channel);
  89. dbgprintf("IRCClient: Auto joining channel: %s\n", channel.characters());
  90. }
  91. }
  92. bool IRCClient::connect()
  93. {
  94. if (m_socket->is_connected())
  95. ASSERT_NOT_REACHED();
  96. m_socket->on_connected = [this] { on_socket_connected(); };
  97. bool success = m_socket->connect(m_hostname, m_port);
  98. if (!success)
  99. return false;
  100. return true;
  101. }
  102. void IRCClient::receive_from_server()
  103. {
  104. while (m_socket->can_read_line()) {
  105. auto line = m_socket->read_line(PAGE_SIZE);
  106. if (line.is_null()) {
  107. if (!m_socket->is_connected()) {
  108. printf("IRCClient: Connection closed!\n");
  109. exit(1);
  110. }
  111. ASSERT_NOT_REACHED();
  112. }
  113. process_line(move(line));
  114. }
  115. }
  116. void IRCClient::process_line(ByteBuffer&& line)
  117. {
  118. Message msg;
  119. Vector<char, 32> prefix;
  120. Vector<char, 32> command;
  121. Vector<char, 256> current_parameter;
  122. enum {
  123. Start,
  124. InPrefix,
  125. InCommand,
  126. InStartOfParameter,
  127. InParameter,
  128. InTrailingParameter,
  129. } state
  130. = Start;
  131. for (size_t i = 0; i < line.size(); ++i) {
  132. char ch = line[i];
  133. if (ch == '\r')
  134. continue;
  135. if (ch == '\n')
  136. break;
  137. switch (state) {
  138. case Start:
  139. if (ch == ':') {
  140. state = InPrefix;
  141. continue;
  142. }
  143. state = InCommand;
  144. [[fallthrough]];
  145. case InCommand:
  146. if (ch == ' ') {
  147. state = InStartOfParameter;
  148. continue;
  149. }
  150. command.append(ch);
  151. continue;
  152. case InPrefix:
  153. if (ch == ' ') {
  154. state = InCommand;
  155. continue;
  156. }
  157. prefix.append(ch);
  158. continue;
  159. case InStartOfParameter:
  160. if (ch == ':') {
  161. state = InTrailingParameter;
  162. continue;
  163. }
  164. state = InParameter;
  165. [[fallthrough]];
  166. case InParameter:
  167. if (ch == ' ') {
  168. if (!current_parameter.is_empty())
  169. msg.arguments.append(String(current_parameter.data(), current_parameter.size()));
  170. current_parameter.clear_with_capacity();
  171. state = InStartOfParameter;
  172. continue;
  173. }
  174. current_parameter.append(ch);
  175. continue;
  176. case InTrailingParameter:
  177. current_parameter.append(ch);
  178. continue;
  179. }
  180. }
  181. if (!current_parameter.is_empty())
  182. msg.arguments.append(String::copy(current_parameter));
  183. msg.prefix = String::copy(prefix);
  184. msg.command = String::copy(command);
  185. handle(msg);
  186. }
  187. void IRCClient::send(const String& text)
  188. {
  189. if (!m_socket->send(ByteBuffer::wrap(text.characters(), text.length()))) {
  190. perror("send");
  191. exit(1);
  192. }
  193. }
  194. void IRCClient::send_user()
  195. {
  196. send(String::format("USER %s 0 * :%s\r\n", m_nickname.characters(), m_nickname.characters()));
  197. }
  198. void IRCClient::send_nick()
  199. {
  200. send(String::format("NICK %s\r\n", m_nickname.characters()));
  201. }
  202. void IRCClient::send_pong(const String& server)
  203. {
  204. send(String::format("PONG %s\r\n", server.characters()));
  205. sleep(1);
  206. }
  207. void IRCClient::join_channel(const String& channel_name)
  208. {
  209. send(String::format("JOIN %s\r\n", channel_name.characters()));
  210. }
  211. void IRCClient::part_channel(const String& channel_name)
  212. {
  213. send(String::format("PART %s\r\n", channel_name.characters()));
  214. }
  215. void IRCClient::send_whois(const String& nick)
  216. {
  217. send(String::format("WHOIS %s\r\n", nick.characters()));
  218. }
  219. void IRCClient::handle(const Message& msg)
  220. {
  221. #ifdef IRC_DEBUG
  222. printf("IRCClient::execute: prefix='%s', command='%s', arguments=%zu\n",
  223. msg.prefix.characters(),
  224. msg.command.characters(),
  225. msg.arguments.size());
  226. int i = 0;
  227. for (auto& arg : msg.arguments) {
  228. printf(" [%d]: %s\n", i, arg.characters());
  229. ++i;
  230. }
  231. #endif
  232. bool is_numeric;
  233. int numeric = msg.command.to_uint(is_numeric);
  234. if (is_numeric) {
  235. switch (numeric) {
  236. case RPL_WHOISCHANNELS:
  237. return handle_rpl_whoischannels(msg);
  238. case RPL_ENDOFWHOIS:
  239. return handle_rpl_endofwhois(msg);
  240. case RPL_WHOISOPERATOR:
  241. return handle_rpl_whoisoperator(msg);
  242. case RPL_WHOISSERVER:
  243. return handle_rpl_whoisserver(msg);
  244. case RPL_WHOISUSER:
  245. return handle_rpl_whoisuser(msg);
  246. case RPL_WHOISIDLE:
  247. return handle_rpl_whoisidle(msg);
  248. case RPL_TOPICWHOTIME:
  249. return handle_rpl_topicwhotime(msg);
  250. case RPL_TOPIC:
  251. return handle_rpl_topic(msg);
  252. case RPL_NAMREPLY:
  253. return handle_rpl_namreply(msg);
  254. case RPL_ENDOFNAMES:
  255. return handle_rpl_endofnames(msg);
  256. }
  257. }
  258. if (msg.command == "PING")
  259. return handle_ping(msg);
  260. if (msg.command == "JOIN")
  261. return handle_join(msg);
  262. if (msg.command == "PART")
  263. return handle_part(msg);
  264. if (msg.command == "TOPIC")
  265. return handle_topic(msg);
  266. if (msg.command == "PRIVMSG")
  267. return handle_privmsg_or_notice(msg, PrivmsgOrNotice::Privmsg);
  268. if (msg.command == "NOTICE")
  269. return handle_privmsg_or_notice(msg, PrivmsgOrNotice::Notice);
  270. if (msg.command == "NICK")
  271. return handle_nick(msg);
  272. if (msg.arguments.size() >= 2)
  273. add_server_message(String::format("[%s] %s", msg.command.characters(), msg.arguments[1].characters()));
  274. }
  275. void IRCClient::add_server_message(const String& text, Color color)
  276. {
  277. m_log->add_message(0, "", text, color);
  278. m_server_subwindow->did_add_message();
  279. }
  280. void IRCClient::send_topic(const String& channel_name, const String& text)
  281. {
  282. send(String::format("TOPIC %s :%s\r\n", channel_name.characters(), text.characters()));
  283. }
  284. void IRCClient::send_privmsg(const String& target, const String& text)
  285. {
  286. send(String::format("PRIVMSG %s :%s\r\n", target.characters(), text.characters()));
  287. }
  288. void IRCClient::send_notice(const String& target, const String& text)
  289. {
  290. send(String::format("NOTICE %s :%s\r\n", target.characters(), text.characters()));
  291. }
  292. void IRCClient::handle_user_input_in_channel(const String& channel_name, const String& input)
  293. {
  294. if (input.is_empty())
  295. return;
  296. if (input[0] == '/')
  297. return handle_user_command(input);
  298. ensure_channel(channel_name).say(input);
  299. }
  300. void IRCClient::handle_user_input_in_query(const String& query_name, const String& input)
  301. {
  302. if (input.is_empty())
  303. return;
  304. if (input[0] == '/')
  305. return handle_user_command(input);
  306. ensure_query(query_name).say(input);
  307. }
  308. void IRCClient::handle_user_input_in_server(const String& input)
  309. {
  310. if (input.is_empty())
  311. return;
  312. if (input[0] == '/')
  313. return handle_user_command(input);
  314. }
  315. bool IRCClient::is_nick_prefix(char ch) const
  316. {
  317. switch (ch) {
  318. case '@':
  319. case '+':
  320. case '~':
  321. case '&':
  322. case '%':
  323. return true;
  324. }
  325. return false;
  326. }
  327. static bool has_ctcp_payload(const StringView& string)
  328. {
  329. return string.length() >= 2 && string[0] == 0x01 && string[string.length() - 1] == 0x01;
  330. }
  331. void IRCClient::handle_privmsg_or_notice(const Message& msg, PrivmsgOrNotice type)
  332. {
  333. if (msg.arguments.size() < 2)
  334. return;
  335. if (msg.prefix.is_empty())
  336. return;
  337. auto parts = msg.prefix.split('!');
  338. auto sender_nick = parts[0];
  339. auto target = msg.arguments[0];
  340. bool is_ctcp = has_ctcp_payload(msg.arguments[1]);
  341. #ifdef IRC_DEBUG
  342. printf("handle_privmsg_or_notice: type='%s'%s, sender_nick='%s', target='%s'\n",
  343. type == PrivmsgOrNotice::Privmsg ? "privmsg" : "notice",
  344. is_ctcp ? " (ctcp)" : "",
  345. sender_nick.characters(),
  346. target.characters());
  347. #endif
  348. if (sender_nick.is_empty())
  349. return;
  350. char sender_prefix = 0;
  351. if (is_nick_prefix(sender_nick[0])) {
  352. sender_prefix = sender_nick[0];
  353. sender_nick = sender_nick.substring(1, sender_nick.length() - 1);
  354. }
  355. String message_text = msg.arguments[1];
  356. auto message_color = Color::Black;
  357. if (is_ctcp) {
  358. auto ctcp_payload = msg.arguments[1].substring_view(1, msg.arguments[1].length() - 2);
  359. if (type == PrivmsgOrNotice::Privmsg)
  360. handle_ctcp_request(sender_nick, ctcp_payload);
  361. else
  362. handle_ctcp_response(sender_nick, ctcp_payload);
  363. StringBuilder builder;
  364. builder.append("(CTCP) ");
  365. builder.append(ctcp_payload);
  366. message_text = builder.to_string();
  367. message_color = Color::Blue;
  368. }
  369. {
  370. auto it = m_channels.find(target);
  371. if (it != m_channels.end()) {
  372. (*it).value->add_message(sender_prefix, sender_nick, message_text, message_color);
  373. return;
  374. }
  375. }
  376. // For NOTICE or CTCP messages, only put them in query if one already exists.
  377. // Otherwise, put them in the server window. This seems to match other clients.
  378. IRCQuery* query = nullptr;
  379. if (is_ctcp || type == PrivmsgOrNotice::Notice) {
  380. query = query_with_name(sender_nick);
  381. } else {
  382. query = &ensure_query(sender_nick);
  383. }
  384. if (query)
  385. query->add_message(sender_prefix, sender_nick, message_text, message_color);
  386. else {
  387. add_server_message(String::format("<%s> %s", sender_nick.characters(), message_text.characters()), message_color);
  388. }
  389. }
  390. IRCQuery* IRCClient::query_with_name(const String& name)
  391. {
  392. return const_cast<IRCQuery*>(m_queries.get(name).value_or(nullptr));
  393. }
  394. IRCQuery& IRCClient::ensure_query(const String& name)
  395. {
  396. auto it = m_queries.find(name);
  397. if (it != m_queries.end())
  398. return *(*it).value;
  399. auto query = IRCQuery::create(*this, name);
  400. auto& query_reference = *query;
  401. m_queries.set(name, query);
  402. return query_reference;
  403. }
  404. IRCChannel& IRCClient::ensure_channel(const String& name)
  405. {
  406. auto it = m_channels.find(name);
  407. if (it != m_channels.end())
  408. return *(*it).value;
  409. auto channel = IRCChannel::create(*this, name);
  410. auto& channel_reference = *channel;
  411. m_channels.set(name, channel);
  412. return channel_reference;
  413. }
  414. void IRCClient::handle_ping(const Message& msg)
  415. {
  416. if (msg.arguments.size() < 1)
  417. return;
  418. m_log->add_message(0, "", "Ping? Pong!");
  419. send_pong(msg.arguments[0]);
  420. }
  421. void IRCClient::handle_join(const Message& msg)
  422. {
  423. if (msg.arguments.size() != 1)
  424. return;
  425. auto prefix_parts = msg.prefix.split('!');
  426. if (prefix_parts.size() < 1)
  427. return;
  428. auto nick = prefix_parts[0];
  429. auto& channel_name = msg.arguments[0];
  430. ensure_channel(channel_name).handle_join(nick, msg.prefix);
  431. }
  432. void IRCClient::handle_part(const Message& msg)
  433. {
  434. if (msg.arguments.size() < 1)
  435. return;
  436. auto prefix_parts = msg.prefix.split('!');
  437. if (prefix_parts.size() < 1)
  438. return;
  439. auto nick = prefix_parts[0];
  440. auto& channel_name = msg.arguments[0];
  441. ensure_channel(channel_name).handle_part(nick, msg.prefix);
  442. }
  443. void IRCClient::handle_nick(const Message& msg)
  444. {
  445. auto prefix_parts = msg.prefix.split('!');
  446. if (prefix_parts.size() < 1)
  447. return;
  448. auto old_nick = prefix_parts[0];
  449. if (msg.arguments.size() != 1)
  450. return;
  451. auto& new_nick = msg.arguments[0];
  452. if (old_nick == m_nickname)
  453. m_nickname = new_nick;
  454. add_server_message(String::format("~ %s changed nickname to %s", old_nick.characters(), new_nick.characters()));
  455. if (on_nickname_changed)
  456. on_nickname_changed(new_nick);
  457. for (auto& it : m_channels) {
  458. it.value->notify_nick_changed(old_nick, new_nick);
  459. }
  460. }
  461. void IRCClient::handle_topic(const Message& msg)
  462. {
  463. if (msg.arguments.size() != 2)
  464. return;
  465. auto prefix_parts = msg.prefix.split('!');
  466. if (prefix_parts.size() < 1)
  467. return;
  468. auto nick = prefix_parts[0];
  469. auto& channel_name = msg.arguments[0];
  470. ensure_channel(channel_name).handle_topic(nick, msg.arguments[1]);
  471. }
  472. void IRCClient::handle_rpl_topic(const Message& msg)
  473. {
  474. if (msg.arguments.size() < 3)
  475. return;
  476. auto& channel_name = msg.arguments[1];
  477. auto& topic = msg.arguments[2];
  478. ensure_channel(channel_name).handle_topic({}, topic);
  479. // FIXME: Handle RPL_TOPICWHOTIME so we can know who set it and when.
  480. }
  481. void IRCClient::handle_rpl_namreply(const Message& msg)
  482. {
  483. if (msg.arguments.size() < 4)
  484. return;
  485. auto& channel_name = msg.arguments[2];
  486. auto& channel = ensure_channel(channel_name);
  487. auto members = msg.arguments[3].split(' ');
  488. quick_sort(members, [](auto& a, auto& b) {
  489. return strcasecmp(a.characters(), b.characters()) < 0;
  490. });
  491. for (auto& member : members) {
  492. if (member.is_empty())
  493. continue;
  494. char prefix = 0;
  495. if (is_nick_prefix(member[0]))
  496. prefix = member[0];
  497. channel.add_member(member, prefix);
  498. }
  499. }
  500. void IRCClient::handle_rpl_endofnames(const Message&)
  501. {
  502. }
  503. void IRCClient::handle_rpl_endofwhois(const Message&)
  504. {
  505. add_server_message("// End of WHOIS");
  506. }
  507. void IRCClient::handle_rpl_whoisoperator(const Message& msg)
  508. {
  509. if (msg.arguments.size() < 2)
  510. return;
  511. auto& nick = msg.arguments[1];
  512. add_server_message(String::format("* %s is an IRC operator", nick.characters()));
  513. }
  514. void IRCClient::handle_rpl_whoisserver(const Message& msg)
  515. {
  516. if (msg.arguments.size() < 3)
  517. return;
  518. auto& nick = msg.arguments[1];
  519. auto& server = msg.arguments[2];
  520. add_server_message(String::format("* %s is using server %s", nick.characters(), server.characters()));
  521. }
  522. void IRCClient::handle_rpl_whoisuser(const Message& msg)
  523. {
  524. if (msg.arguments.size() < 6)
  525. return;
  526. auto& nick = msg.arguments[1];
  527. auto& username = msg.arguments[2];
  528. auto& host = msg.arguments[3];
  529. auto& asterisk = msg.arguments[4];
  530. auto& realname = msg.arguments[5];
  531. (void)asterisk;
  532. add_server_message(String::format("* %s is %s@%s, real name: %s",
  533. nick.characters(),
  534. username.characters(),
  535. host.characters(),
  536. realname.characters()));
  537. }
  538. void IRCClient::handle_rpl_whoisidle(const Message& msg)
  539. {
  540. if (msg.arguments.size() < 3)
  541. return;
  542. auto& nick = msg.arguments[1];
  543. auto& secs = msg.arguments[2];
  544. add_server_message(String::format("* %s is %s seconds idle", nick.characters(), secs.characters()));
  545. }
  546. void IRCClient::handle_rpl_whoischannels(const Message& msg)
  547. {
  548. if (msg.arguments.size() < 3)
  549. return;
  550. auto& nick = msg.arguments[1];
  551. auto& channel_list = msg.arguments[2];
  552. add_server_message(String::format("* %s is in channels %s", nick.characters(), channel_list.characters()));
  553. }
  554. void IRCClient::handle_rpl_topicwhotime(const Message& msg)
  555. {
  556. if (msg.arguments.size() < 4)
  557. return;
  558. auto& channel_name = msg.arguments[1];
  559. auto& nick = msg.arguments[2];
  560. auto setat = msg.arguments[3];
  561. bool ok;
  562. time_t setat_time = setat.to_uint(ok);
  563. if (ok)
  564. setat = Core::DateTime::from_timestamp(setat_time).to_string();
  565. ensure_channel(channel_name).add_message(String::format("*** (set by %s at %s)", nick.characters(), setat.characters()), Color::Blue);
  566. }
  567. void IRCClient::register_subwindow(IRCWindow& subwindow)
  568. {
  569. if (subwindow.type() == IRCWindow::Server) {
  570. m_server_subwindow = &subwindow;
  571. subwindow.set_log_buffer(*m_log);
  572. }
  573. m_windows.append(&subwindow);
  574. m_client_window_list_model->update();
  575. }
  576. void IRCClient::unregister_subwindow(IRCWindow& subwindow)
  577. {
  578. if (subwindow.type() == IRCWindow::Server) {
  579. m_server_subwindow = &subwindow;
  580. }
  581. for (size_t i = 0; i < m_windows.size(); ++i) {
  582. if (m_windows.at(i) == &subwindow) {
  583. m_windows.remove(i);
  584. break;
  585. }
  586. }
  587. m_client_window_list_model->update();
  588. }
  589. void IRCClient::handle_user_command(const String& input)
  590. {
  591. auto parts = input.split_view(' ');
  592. if (parts.is_empty())
  593. return;
  594. auto command = String(parts[0]).to_uppercase();
  595. if (command == "/NICK") {
  596. if (parts.size() >= 2)
  597. change_nick(parts[1]);
  598. return;
  599. }
  600. if (command == "/JOIN") {
  601. if (parts.size() >= 2)
  602. join_channel(parts[1]);
  603. return;
  604. }
  605. if (command == "/PART") {
  606. if (parts.size() >= 2)
  607. part_channel(parts[1]);
  608. return;
  609. }
  610. if (command == "/HOP") {
  611. if (parts.size() >= 2) {
  612. part_channel(parts[1]);
  613. join_channel(parts[1]);
  614. }
  615. return;
  616. }
  617. if (command == "/TOPIC") {
  618. if (parts.size() < 3)
  619. return;
  620. auto channel = parts[1];
  621. auto topic = input.view().substring_view_starting_after_substring(channel);
  622. send_topic(channel, topic);
  623. return;
  624. }
  625. if (command == "/QUERY") {
  626. if (parts.size() >= 2) {
  627. auto& query = ensure_query(parts[1]);
  628. IRCAppWindow::the().set_active_window(query.window());
  629. }
  630. return;
  631. }
  632. if (command == "/MSG") {
  633. if (parts.size() < 3)
  634. return;
  635. auto nick = parts[1];
  636. auto& query = ensure_query(nick);
  637. IRCAppWindow::the().set_active_window(query.window());
  638. query.say(input.view().substring_view_starting_after_substring(nick));
  639. return;
  640. }
  641. if (command == "/WHOIS") {
  642. if (parts.size() >= 2)
  643. send_whois(parts[1]);
  644. return;
  645. }
  646. }
  647. void IRCClient::change_nick(const String& nick)
  648. {
  649. send(String::format("NICK %s\r\n", nick.characters()));
  650. }
  651. void IRCClient::handle_whois_action(const String& nick)
  652. {
  653. send_whois(nick);
  654. }
  655. void IRCClient::handle_open_query_action(const String& nick)
  656. {
  657. ensure_query(nick);
  658. }
  659. void IRCClient::handle_change_nick_action(const String& nick)
  660. {
  661. change_nick(nick);
  662. }
  663. void IRCClient::handle_close_query_action(const String& nick)
  664. {
  665. m_queries.remove(nick);
  666. m_client_window_list_model->update();
  667. }
  668. void IRCClient::handle_join_action(const String& channel)
  669. {
  670. join_channel(channel);
  671. }
  672. void IRCClient::handle_part_action(const String& channel)
  673. {
  674. part_channel(channel);
  675. }
  676. void IRCClient::did_part_from_channel(Badge<IRCChannel>, IRCChannel& channel)
  677. {
  678. if (on_part_from_channel)
  679. on_part_from_channel(channel);
  680. }
  681. void IRCClient::send_ctcp_response(const StringView& peer, const StringView& payload)
  682. {
  683. StringBuilder builder;
  684. builder.append(0x01);
  685. builder.append(payload);
  686. builder.append(0x01);
  687. auto message = builder.to_string();
  688. send_notice(peer, message);
  689. }
  690. void IRCClient::handle_ctcp_request(const StringView& peer, const StringView& payload)
  691. {
  692. dbg() << "handle_ctcp_request: " << payload;
  693. if (payload == "VERSION") {
  694. send_ctcp_response(peer, "VERSION IRC Client [x86] / Serenity OS");
  695. return;
  696. }
  697. if (payload.starts_with("PING")) {
  698. send_ctcp_response(peer, payload);
  699. return;
  700. }
  701. }
  702. void IRCClient::handle_ctcp_response(const StringView& peer, const StringView& payload)
  703. {
  704. dbg() << "handle_ctcp_response(" << peer << "): " << payload;
  705. }