IRCClient.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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_privmsg(const String& target, const String& text)
  281. {
  282. send(String::format("PRIVMSG %s :%s\r\n", target.characters(), text.characters()));
  283. }
  284. void IRCClient::send_notice(const String& target, const String& text)
  285. {
  286. send(String::format("NOTICE %s :%s\r\n", target.characters(), text.characters()));
  287. }
  288. void IRCClient::handle_user_input_in_channel(const String& channel_name, const String& input)
  289. {
  290. if (input.is_empty())
  291. return;
  292. if (input[0] == '/')
  293. return handle_user_command(input);
  294. ensure_channel(channel_name).say(input);
  295. }
  296. void IRCClient::handle_user_input_in_query(const String& query_name, const String& input)
  297. {
  298. if (input.is_empty())
  299. return;
  300. if (input[0] == '/')
  301. return handle_user_command(input);
  302. ensure_query(query_name).say(input);
  303. }
  304. void IRCClient::handle_user_input_in_server(const String& input)
  305. {
  306. if (input.is_empty())
  307. return;
  308. if (input[0] == '/')
  309. return handle_user_command(input);
  310. }
  311. bool IRCClient::is_nick_prefix(char ch) const
  312. {
  313. switch (ch) {
  314. case '@':
  315. case '+':
  316. case '~':
  317. case '&':
  318. case '%':
  319. return true;
  320. }
  321. return false;
  322. }
  323. static bool has_ctcp_payload(const StringView& string)
  324. {
  325. return string.length() >= 2 && string[0] == 0x01 && string[string.length() - 1] == 0x01;
  326. }
  327. void IRCClient::handle_privmsg_or_notice(const Message& msg, PrivmsgOrNotice type)
  328. {
  329. if (msg.arguments.size() < 2)
  330. return;
  331. if (msg.prefix.is_empty())
  332. return;
  333. auto parts = msg.prefix.split('!');
  334. auto sender_nick = parts[0];
  335. auto target = msg.arguments[0];
  336. bool is_ctcp = has_ctcp_payload(msg.arguments[1]);
  337. #ifdef IRC_DEBUG
  338. printf("handle_privmsg_or_notice: type='%s'%s, sender_nick='%s', target='%s'\n",
  339. type == PrivmsgOrNotice::Privmsg ? "privmsg" : "notice",
  340. is_ctcp ? " (ctcp)" : "",
  341. sender_nick.characters(),
  342. target.characters());
  343. #endif
  344. if (sender_nick.is_empty())
  345. return;
  346. char sender_prefix = 0;
  347. if (is_nick_prefix(sender_nick[0])) {
  348. sender_prefix = sender_nick[0];
  349. sender_nick = sender_nick.substring(1, sender_nick.length() - 1);
  350. }
  351. String message_text = msg.arguments[1];
  352. auto message_color = Color::Black;
  353. if (is_ctcp) {
  354. auto ctcp_payload = msg.arguments[1].substring_view(1, msg.arguments[1].length() - 2);
  355. if (type == PrivmsgOrNotice::Privmsg)
  356. handle_ctcp_request(sender_nick, ctcp_payload);
  357. else
  358. handle_ctcp_response(sender_nick, ctcp_payload);
  359. StringBuilder builder;
  360. builder.append("(CTCP) ");
  361. builder.append(ctcp_payload);
  362. message_text = builder.to_string();
  363. message_color = Color::Blue;
  364. }
  365. {
  366. auto it = m_channels.find(target);
  367. if (it != m_channels.end()) {
  368. (*it).value->add_message(sender_prefix, sender_nick, message_text, message_color);
  369. return;
  370. }
  371. }
  372. // For NOTICE or CTCP messages, only put them in query if one already exists.
  373. // Otherwise, put them in the server window. This seems to match other clients.
  374. IRCQuery* query = nullptr;
  375. if (is_ctcp || type == PrivmsgOrNotice::Notice) {
  376. query = query_with_name(sender_nick);
  377. } else {
  378. query = &ensure_query(sender_nick);
  379. }
  380. if (query)
  381. query->add_message(sender_prefix, sender_nick, message_text, message_color);
  382. else {
  383. add_server_message(String::format("<%s> %s", sender_nick.characters(), message_text.characters()), message_color);
  384. }
  385. }
  386. IRCQuery* IRCClient::query_with_name(const String& name)
  387. {
  388. return const_cast<IRCQuery*>(m_queries.get(name).value_or(nullptr));
  389. }
  390. IRCQuery& IRCClient::ensure_query(const String& name)
  391. {
  392. auto it = m_queries.find(name);
  393. if (it != m_queries.end())
  394. return *(*it).value;
  395. auto query = IRCQuery::create(*this, name);
  396. auto& query_reference = *query;
  397. m_queries.set(name, query);
  398. return query_reference;
  399. }
  400. IRCChannel& IRCClient::ensure_channel(const String& name)
  401. {
  402. auto it = m_channels.find(name);
  403. if (it != m_channels.end())
  404. return *(*it).value;
  405. auto channel = IRCChannel::create(*this, name);
  406. auto& channel_reference = *channel;
  407. m_channels.set(name, channel);
  408. return channel_reference;
  409. }
  410. void IRCClient::handle_ping(const Message& msg)
  411. {
  412. if (msg.arguments.size() < 1)
  413. return;
  414. m_log->add_message(0, "", "Ping? Pong!");
  415. send_pong(msg.arguments[0]);
  416. }
  417. void IRCClient::handle_join(const Message& msg)
  418. {
  419. if (msg.arguments.size() != 1)
  420. return;
  421. auto prefix_parts = msg.prefix.split('!');
  422. if (prefix_parts.size() < 1)
  423. return;
  424. auto nick = prefix_parts[0];
  425. auto& channel_name = msg.arguments[0];
  426. ensure_channel(channel_name).handle_join(nick, msg.prefix);
  427. }
  428. void IRCClient::handle_part(const Message& msg)
  429. {
  430. if (msg.arguments.size() < 1)
  431. return;
  432. auto prefix_parts = msg.prefix.split('!');
  433. if (prefix_parts.size() < 1)
  434. return;
  435. auto nick = prefix_parts[0];
  436. auto& channel_name = msg.arguments[0];
  437. ensure_channel(channel_name).handle_part(nick, msg.prefix);
  438. }
  439. void IRCClient::handle_nick(const Message& msg)
  440. {
  441. auto prefix_parts = msg.prefix.split('!');
  442. if (prefix_parts.size() < 1)
  443. return;
  444. auto old_nick = prefix_parts[0];
  445. if (msg.arguments.size() != 1)
  446. return;
  447. auto& new_nick = msg.arguments[0];
  448. if (old_nick == m_nickname)
  449. m_nickname = new_nick;
  450. add_server_message(String::format("~ %s changed nickname to %s", old_nick.characters(), new_nick.characters()));
  451. if (on_nickname_changed)
  452. on_nickname_changed(new_nick);
  453. for (auto& it : m_channels) {
  454. it.value->notify_nick_changed(old_nick, new_nick);
  455. }
  456. }
  457. void IRCClient::handle_topic(const Message& msg)
  458. {
  459. if (msg.arguments.size() != 2)
  460. return;
  461. auto prefix_parts = msg.prefix.split('!');
  462. if (prefix_parts.size() < 1)
  463. return;
  464. auto nick = prefix_parts[0];
  465. auto& channel_name = msg.arguments[0];
  466. ensure_channel(channel_name).handle_topic(nick, msg.arguments[1]);
  467. }
  468. void IRCClient::handle_rpl_topic(const Message& msg)
  469. {
  470. if (msg.arguments.size() < 3)
  471. return;
  472. auto& channel_name = msg.arguments[1];
  473. auto& topic = msg.arguments[2];
  474. ensure_channel(channel_name).handle_topic({}, topic);
  475. // FIXME: Handle RPL_TOPICWHOTIME so we can know who set it and when.
  476. }
  477. void IRCClient::handle_rpl_namreply(const Message& msg)
  478. {
  479. if (msg.arguments.size() < 4)
  480. return;
  481. auto& channel_name = msg.arguments[2];
  482. auto& channel = ensure_channel(channel_name);
  483. auto members = msg.arguments[3].split(' ');
  484. quick_sort(members.begin(), members.end(), [](auto& a, auto& b) {
  485. return strcasecmp(a.characters(), b.characters()) < 0;
  486. });
  487. for (auto& member : members) {
  488. if (member.is_empty())
  489. continue;
  490. char prefix = 0;
  491. if (is_nick_prefix(member[0]))
  492. prefix = member[0];
  493. channel.add_member(member, prefix);
  494. }
  495. }
  496. void IRCClient::handle_rpl_endofnames(const Message&)
  497. {
  498. }
  499. void IRCClient::handle_rpl_endofwhois(const Message&)
  500. {
  501. add_server_message("// End of WHOIS");
  502. }
  503. void IRCClient::handle_rpl_whoisoperator(const Message& msg)
  504. {
  505. if (msg.arguments.size() < 2)
  506. return;
  507. auto& nick = msg.arguments[1];
  508. add_server_message(String::format("* %s is an IRC operator", nick.characters()));
  509. }
  510. void IRCClient::handle_rpl_whoisserver(const Message& msg)
  511. {
  512. if (msg.arguments.size() < 3)
  513. return;
  514. auto& nick = msg.arguments[1];
  515. auto& server = msg.arguments[2];
  516. add_server_message(String::format("* %s is using server %s", nick.characters(), server.characters()));
  517. }
  518. void IRCClient::handle_rpl_whoisuser(const Message& msg)
  519. {
  520. if (msg.arguments.size() < 6)
  521. return;
  522. auto& nick = msg.arguments[1];
  523. auto& username = msg.arguments[2];
  524. auto& host = msg.arguments[3];
  525. auto& asterisk = msg.arguments[4];
  526. auto& realname = msg.arguments[5];
  527. (void)asterisk;
  528. add_server_message(String::format("* %s is %s@%s, real name: %s",
  529. nick.characters(),
  530. username.characters(),
  531. host.characters(),
  532. realname.characters()));
  533. }
  534. void IRCClient::handle_rpl_whoisidle(const Message& msg)
  535. {
  536. if (msg.arguments.size() < 3)
  537. return;
  538. auto& nick = msg.arguments[1];
  539. auto& secs = msg.arguments[2];
  540. add_server_message(String::format("* %s is %s seconds idle", nick.characters(), secs.characters()));
  541. }
  542. void IRCClient::handle_rpl_whoischannels(const Message& msg)
  543. {
  544. if (msg.arguments.size() < 3)
  545. return;
  546. auto& nick = msg.arguments[1];
  547. auto& channel_list = msg.arguments[2];
  548. add_server_message(String::format("* %s is in channels %s", nick.characters(), channel_list.characters()));
  549. }
  550. void IRCClient::handle_rpl_topicwhotime(const Message& msg)
  551. {
  552. if (msg.arguments.size() < 4)
  553. return;
  554. auto& channel_name = msg.arguments[1];
  555. auto& nick = msg.arguments[2];
  556. auto setat = msg.arguments[3];
  557. bool ok;
  558. time_t setat_time = setat.to_uint(ok);
  559. if (ok)
  560. setat = Core::DateTime::from_timestamp(setat_time).to_string();
  561. ensure_channel(channel_name).add_message(String::format("*** (set by %s at %s)", nick.characters(), setat.characters()), Color::Blue);
  562. }
  563. void IRCClient::register_subwindow(IRCWindow& subwindow)
  564. {
  565. if (subwindow.type() == IRCWindow::Server) {
  566. m_server_subwindow = &subwindow;
  567. subwindow.set_log_buffer(*m_log);
  568. }
  569. m_windows.append(&subwindow);
  570. m_client_window_list_model->update();
  571. }
  572. void IRCClient::unregister_subwindow(IRCWindow& subwindow)
  573. {
  574. if (subwindow.type() == IRCWindow::Server) {
  575. m_server_subwindow = &subwindow;
  576. }
  577. for (size_t i = 0; i < m_windows.size(); ++i) {
  578. if (m_windows.at(i) == &subwindow) {
  579. m_windows.remove(i);
  580. break;
  581. }
  582. }
  583. m_client_window_list_model->update();
  584. }
  585. void IRCClient::handle_user_command(const String& input)
  586. {
  587. auto parts = input.split_view(' ');
  588. if (parts.is_empty())
  589. return;
  590. auto command = String(parts[0]).to_uppercase();
  591. if (command == "/NICK") {
  592. if (parts.size() >= 2)
  593. change_nick(parts[1]);
  594. return;
  595. }
  596. if (command == "/JOIN") {
  597. if (parts.size() >= 2)
  598. join_channel(parts[1]);
  599. return;
  600. }
  601. if (command == "/PART") {
  602. if (parts.size() >= 2)
  603. part_channel(parts[1]);
  604. return;
  605. }
  606. if (command == "/QUERY") {
  607. if (parts.size() >= 2) {
  608. auto& query = ensure_query(parts[1]);
  609. IRCAppWindow::the().set_active_window(query.window());
  610. }
  611. return;
  612. }
  613. if (command == "/MSG") {
  614. if (parts.size() < 3)
  615. return;
  616. auto nick = parts[1];
  617. auto& query = ensure_query(nick);
  618. IRCAppWindow::the().set_active_window(query.window());
  619. query.say(input.view().substring_view_starting_after_substring(nick));
  620. return;
  621. }
  622. if (command == "/WHOIS") {
  623. if (parts.size() >= 2)
  624. send_whois(parts[1]);
  625. return;
  626. }
  627. }
  628. void IRCClient::change_nick(const String& nick)
  629. {
  630. send(String::format("NICK %s\r\n", nick.characters()));
  631. }
  632. void IRCClient::handle_whois_action(const String& nick)
  633. {
  634. send_whois(nick);
  635. }
  636. void IRCClient::handle_open_query_action(const String& nick)
  637. {
  638. ensure_query(nick);
  639. }
  640. void IRCClient::handle_change_nick_action(const String& nick)
  641. {
  642. change_nick(nick);
  643. }
  644. void IRCClient::handle_close_query_action(const String& nick)
  645. {
  646. m_queries.remove(nick);
  647. m_client_window_list_model->update();
  648. }
  649. void IRCClient::handle_join_action(const String& channel)
  650. {
  651. join_channel(channel);
  652. }
  653. void IRCClient::handle_part_action(const String& channel)
  654. {
  655. part_channel(channel);
  656. }
  657. void IRCClient::did_part_from_channel(Badge<IRCChannel>, IRCChannel& channel)
  658. {
  659. if (on_part_from_channel)
  660. on_part_from_channel(channel);
  661. }
  662. void IRCClient::send_ctcp_response(const StringView& peer, const StringView& payload)
  663. {
  664. StringBuilder builder;
  665. builder.append(0x01);
  666. builder.append(payload);
  667. builder.append(0x01);
  668. auto message = builder.to_string();
  669. send_notice(peer, message);
  670. }
  671. void IRCClient::handle_ctcp_request(const StringView& peer, const StringView& payload)
  672. {
  673. dbg() << "handle_ctcp_request: " << payload;
  674. if (payload == "VERSION") {
  675. send_ctcp_response(peer, "VERSION IRC Client [x86] / Serenity OS");
  676. return;
  677. }
  678. if (payload.starts_with("PING")) {
  679. send_ctcp_response(peer, payload);
  680. return;
  681. }
  682. }
  683. void IRCClient::handle_ctcp_response(const StringView& peer, const StringView& payload)
  684. {
  685. dbg() << "handle_ctcp_response(" << peer << "): " << payload;
  686. }