The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCMethods.cs 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. namespace Combot
  8. {
  9. internal partial class IRCService
  10. {
  11. // ----------------------- //
  12. // Public Mapped Functions //
  13. // ----------------------- //
  14. /// <summary>
  15. /// Send a message to a nick
  16. /// </summary>
  17. /// <param name="nick"></param>
  18. /// <param name="message"></param>
  19. /// <returns></returns>
  20. public bool SendMessage(Nick nick, string message)
  21. {
  22. bool success = false;
  23. if (CanSendMessage())
  24. {
  25. IRCSendPrivMessage(nick.Nickname, message);
  26. success = true;
  27. }
  28. return success;
  29. }
  30. /// <summary>
  31. /// Send a message to a channel
  32. /// </summary>
  33. /// <param name="channel"></param>
  34. /// <param name="message"></param>
  35. /// <returns></returns>
  36. public bool SendMessage(Channel channel, string message)
  37. {
  38. bool success = false;
  39. if (CanSendMessage())
  40. {
  41. IRCSendPrivMessage(channel.Name, message);
  42. success = true;
  43. }
  44. return success;
  45. }
  46. /// <summary>
  47. /// Send a message to multiple nicks
  48. /// </summary>
  49. /// <param name="nicks"></param>
  50. /// <param name="message"></param>
  51. /// <returns></returns>
  52. public bool SendMessage(List<Nick> nicks, string message)
  53. {
  54. bool success = false;
  55. if (CanSendMessage())
  56. {
  57. List<string> nicknames = new List<string>();
  58. foreach (Nick nick in nicks)
  59. {
  60. nicknames.Add(nick.Nickname);
  61. }
  62. IRCSendPrivMessage(nicknames, message);
  63. success = true;
  64. }
  65. return success;
  66. }
  67. /// <summary>
  68. /// Send a message to multiple channels
  69. /// </summary>
  70. /// <param name="channels"></param>
  71. /// <param name="message"></param>
  72. /// <returns></returns>
  73. public bool SendMessage(List<Channel> channels, string message)
  74. {
  75. bool success = false;
  76. if (CanSendMessage())
  77. {
  78. List<string> channelnames = new List<string>();
  79. foreach (Channel channel in channels)
  80. {
  81. channelnames.Add(channel.Name);
  82. }
  83. IRCSendPrivMessage(channelnames, message);
  84. success = true;
  85. }
  86. return success;
  87. }
  88. public bool CanSendMessage() { return true; }
  89. /// <summary>
  90. /// Send a notice to a nick
  91. /// </summary>
  92. /// <param name="nick"></param>
  93. /// <param name="message"></param>
  94. /// <returns></returns>
  95. public bool SendNotice(Nick nick, string message)
  96. {
  97. bool success = false;
  98. if (CanSendNotice())
  99. {
  100. IRCSendNotice(nick.Nickname, message);
  101. success = true;
  102. }
  103. return success;
  104. }
  105. /// <summary>
  106. /// Send a notice to a channel
  107. /// </summary>
  108. /// <param name="channel"></param>
  109. /// <param name="message"></param>
  110. /// <returns></returns>
  111. public bool SendNotice(Channel channel, string message)
  112. {
  113. bool success = false;
  114. if (CanSendNotice())
  115. {
  116. IRCSendNotice(channel.Name, message);
  117. success = true;
  118. }
  119. return success;
  120. }
  121. /// <summary>
  122. /// Send a notice to multiple nicks
  123. /// </summary>
  124. /// <param name="nicks"></param>
  125. /// <param name="message"></param>
  126. /// <returns></returns>
  127. public bool SendNotice(List<Nick> nicks, string message)
  128. {
  129. bool success = false;
  130. if (CanSendNotice())
  131. {
  132. List<string> nicknames = new List<string>();
  133. foreach (Nick nick in nicks)
  134. {
  135. nicknames.Add(nick.Nickname);
  136. }
  137. IRCSendNotice(nicknames, message);
  138. success = true;
  139. }
  140. return success;
  141. }
  142. /// <summary>
  143. /// Send a notice to multiple channels
  144. /// </summary>
  145. /// <param name="channels"></param>
  146. /// <param name="message"></param>
  147. /// <returns></returns>
  148. public bool SendNotice(List<Channel> channels, string message)
  149. {
  150. bool success = false;
  151. if (CanSendNotice())
  152. {
  153. List<string> channelnames = new List<string>();
  154. foreach (Channel channel in channels)
  155. {
  156. channelnames.Add(channel.Name);
  157. }
  158. IRCSendNotice(channelnames, message);
  159. success = true;
  160. }
  161. return success;
  162. }
  163. public bool CanSendNotice() { return true; }
  164. /// <summary>
  165. /// Set your connection password
  166. /// </summary>
  167. /// <param name="password"></param>
  168. /// <returns></returns>
  169. public bool SendPassword(string password)
  170. {
  171. bool success = false;
  172. if (CanSendPassword())
  173. {
  174. IRCSendPassword(password);
  175. success = true;
  176. }
  177. return success;
  178. }
  179. public bool CanSendPassword() { return true; }
  180. /// <summary>
  181. /// Set your nickname
  182. /// </summary>
  183. /// <param name="nick"></param>
  184. /// <returns></returns>
  185. public bool SetNick(Nick nick)
  186. {
  187. bool success = false;
  188. if (CanSetNick())
  189. {
  190. IRCSendNick(nick.Nickname);
  191. success = true;
  192. }
  193. return success;
  194. }
  195. public bool CanSetNick() { return true; }
  196. /// <summary>
  197. /// Sets the user for the client connection
  198. /// </summary>
  199. /// <param name="nick"></param>
  200. /// <returns></returns>
  201. public bool SetUser(Nick nick)
  202. {
  203. bool success = false;
  204. if (CanSetUser())
  205. {
  206. IRCSendUser(nick.Nickname, nick.Host, _Bot.Config.Name, nick.Realname);
  207. success = true;
  208. }
  209. return success;
  210. }
  211. public bool CanSetUser() { return true; }
  212. public bool Oper(string username, string password)
  213. {
  214. bool success = false;
  215. if (CanOper())
  216. {
  217. IRCSendOper(username, password);
  218. success = true;
  219. }
  220. return success;
  221. }
  222. public bool CanOper() { return true; }
  223. public bool Quit()
  224. {
  225. bool success = false;
  226. if (CanQuit())
  227. {
  228. IRCSendQuit();
  229. success = true;
  230. }
  231. return success;
  232. }
  233. public bool Quit(string message)
  234. {
  235. bool success = false;
  236. if (CanQuit())
  237. {
  238. IRCSendQuit(message);
  239. success = true;
  240. }
  241. return success;
  242. }
  243. public bool CanQuit() { return true; }
  244. public bool Join(Channel channel)
  245. {
  246. bool success = false;
  247. if (CanJoin())
  248. {
  249. IRCSendJoin(channel.Name, channel.Key);
  250. success = true;
  251. }
  252. return success;
  253. }
  254. public bool IRCSendJoin(List<Channel> channels)
  255. {
  256. bool success = false;
  257. if (CanJoin())
  258. {
  259. List<string> channel_names = new List<string>();
  260. List<string> keys = new List<string>();
  261. foreach (Channel channel in channels)
  262. {
  263. channel_names.Add(channel.Name);
  264. keys.Add(channel.Key);
  265. }
  266. IRCSendJoin(channel_names, keys);
  267. success = true;
  268. }
  269. return success;
  270. }
  271. public bool CanJoin() { return true; }
  272. public bool IRCSendPart(string channel)
  273. {
  274. SendTCPMessage(string.Format("PART {0}", channel));
  275. }
  276. public bool IRCSendPart(List<string> channels)
  277. {
  278. string channel_list = string.Empty;
  279. foreach (string channel in channels)
  280. {
  281. channel_list += channel + ",";
  282. }
  283. SendTCPMessage(string.Format("PART {0}", channel_list.TrimEnd(',')));
  284. }
  285. public bool IRCSendMode(string channel, ChannelModeInfo modeInfo)
  286. {
  287. string mode_set = modeInfo.Set ? "+" : "-";
  288. SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  289. }
  290. public bool IRCSendMode(string channel, List<ChannelModeInfo> modeInfos)
  291. {
  292. foreach (ChannelModeInfo modeInfo in modeInfos)
  293. {
  294. IRCSendMode(channel, modeInfo);
  295. }
  296. }
  297. public bool IRCSendMode(string nick, UserModeInfo modeInfo)
  298. {
  299. string mode_set = modeInfo.Set ? "+" : "-";
  300. SendTCPMessage(string.Format("MODE {0} {1} {2}", nick, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  301. }
  302. public bool IRCSendMode(string nick, List<UserModeInfo> modeInfos)
  303. {
  304. foreach (UserModeInfo modeInfo in modeInfos)
  305. {
  306. IRCSendMode(nick, modeInfo);
  307. }
  308. }
  309. public bool IRCSendTopic(string channel)
  310. {
  311. SendTCPMessage(string.Format("TOPIC {0}", channel));
  312. }
  313. public bool IRCSendTopic(string channel, string topic)
  314. {
  315. SendTCPMessage(string.Format("TOPIC {0} :{1}", channel, topic));
  316. }
  317. public bool IRCSendNames()
  318. {
  319. SendTCPMessage("NAMES");
  320. }
  321. public bool IRCSendNames(string channel)
  322. {
  323. SendTCPMessage(string.Format("NAMES {0}", channel));
  324. }
  325. public bool IRCSendNames(List<string> channels)
  326. {
  327. string channel_list = string.Empty;
  328. foreach (string channel in channels)
  329. {
  330. channel_list += channel + ",";
  331. }
  332. SendTCPMessage(string.Format("NAMES {0}", channel_list.TrimEnd(',')));
  333. }
  334. public bool IRCSendList()
  335. {
  336. SendTCPMessage("LIST");
  337. }
  338. public bool IRCSendList(string channel)
  339. {
  340. SendTCPMessage(string.Format("LIST {0}", channel));
  341. }
  342. public bool IRCSendList(List<string> channels)
  343. {
  344. string channel_list = string.Empty;
  345. foreach (string channel in channels)
  346. {
  347. channel_list += channel + ",";
  348. }
  349. SendTCPMessage(string.Format("LIST {0}", channel_list.TrimEnd(',')));
  350. }
  351. public bool IRCSendInvite(string channel, string nick)
  352. {
  353. SendTCPMessage(string.Format("INVITE {0} {1}", nick, channel));
  354. }
  355. public bool IRCSendKick(string channel, string nick)
  356. {
  357. SendTCPMessage(string.Format("KICK {0} {1}", channel, nick));
  358. }
  359. public bool IRCSendKick(string channel, string nick, string reason)
  360. {
  361. SendTCPMessage(string.Format("KICK {0} {1} :{2}", channel, nick, reason));
  362. }
  363. public bool IRCSendVersion(string server)
  364. {
  365. SendTCPMessage(string.Format("VERSION {0}", server));
  366. }
  367. public bool IRCSendStats(ServerStat stat)
  368. {
  369. SendTCPMessage(string.Format("STATS {0}", stat.ToString()));
  370. }
  371. public bool IRCSendStats(ServerStat stat, string parameter)
  372. {
  373. SendTCPMessage(string.Format("STATS {0} {1}", stat.ToString(), parameter));
  374. }
  375. public bool IRCSendLinks(string mask)
  376. {
  377. SendTCPMessage(string.Format("LINKS {0}", mask));
  378. }
  379. public bool IRCSendLinks(string server, string mask)
  380. {
  381. SendTCPMessage(string.Format("LINKS {0} {1}", mask, server));
  382. }
  383. public bool IRCSendTime()
  384. {
  385. SendTCPMessage("TIME");
  386. }
  387. public bool IRCSendTime(string server)
  388. {
  389. SendTCPMessage(string.Format("TIME {0}", server));
  390. }
  391. public bool IRCSendConnect(string server)
  392. {
  393. SendTCPMessage(string.Format("CONNECT {0}", server));
  394. }
  395. public bool IRCSendConnect(string server, string originator, int port)
  396. {
  397. SendTCPMessage(string.Format("CONNECT {0} {1} {2}", originator, port, server));
  398. }
  399. public bool IRCSendTrace(string target)
  400. {
  401. SendTCPMessage(string.Format("TRACE {0}", target));
  402. }
  403. public bool IRCSendAdmin()
  404. {
  405. SendTCPMessage("ADMIN");
  406. }
  407. public bool IRCSendAdmin(string host)
  408. {
  409. SendTCPMessage(string.Format("ADMIN {0}", host));
  410. }
  411. public bool IRCSendInfo(string host)
  412. {
  413. SendTCPMessage(string.Format("INFO {0}", host));
  414. }
  415. public bool IRCSendWho()
  416. {
  417. SendTCPMessage("WHO");
  418. }
  419. public bool IRCSendWho(string host, bool ops = false)
  420. {
  421. string msg = string.Empty;
  422. if (ops)
  423. {
  424. msg = string.Format("WHO {0} o", host);
  425. }
  426. else
  427. {
  428. msg = string.Format("WHO {0}", host);
  429. }
  430. SendTCPMessage(msg);
  431. }
  432. public bool IRCSendWhois(string nick)
  433. {
  434. SendTCPMessage(string.Format("WHOIS {0}", nick));
  435. }
  436. public bool IRCSendWhois(string nick, string server)
  437. {
  438. SendTCPMessage(string.Format("WHOIS {0} {1}", server, nick));
  439. }
  440. public bool IRCSendWhowas(string nick)
  441. {
  442. SendTCPMessage(string.Format("WHOIS {0}", nick));
  443. }
  444. public bool IRCSendWhowas(string nick, int entries)
  445. {
  446. SendTCPMessage(string.Format("WHOIS {0} {1}", nick, entries));
  447. }
  448. public bool IRCSendWhowas(string nick, int entries, string server)
  449. {
  450. SendTCPMessage(string.Format("WHOIS {0} {1} {2}", nick, entries, server));
  451. }
  452. public bool IRCSendKill(string nick, string comment)
  453. {
  454. SendTCPMessage(string.Format("KILL {0} {1}", nick, comment));
  455. }
  456. public bool IRCSendPing(string recipient)
  457. {
  458. SendTCPMessage(string.Format("PING {0}", recipient));
  459. }
  460. public bool IRCSendPong()
  461. {
  462. SendTCPMessage("PONG");
  463. }
  464. public bool IRCSendPong(string sender, string recipient)
  465. {
  466. SendTCPMessage(string.Format("PONG {0} {1}", sender, recipient));
  467. }
  468. public bool IRCSendAway()
  469. {
  470. SendTCPMessage("AWAY");
  471. }
  472. public bool IRCSendAway(string message)
  473. {
  474. SendTCPMessage(string.Format("AWAY {0}", message));
  475. }
  476. public bool IRCSendRehash()
  477. {
  478. SendTCPMessage("REHASH");
  479. }
  480. public bool IRCSendRestart()
  481. {
  482. SendTCPMessage("RESTART");
  483. }
  484. public bool IRCSendSummon()
  485. {
  486. SendTCPMessage("SUMMON");
  487. }
  488. public bool IRCSendSummon(string nick)
  489. {
  490. SendTCPMessage(string.Format("SUMMON {0}", nick));
  491. }
  492. public bool IRCSendSummon(string nick, string host)
  493. {
  494. SendTCPMessage(string.Format("SUMMON {0} {1}", nick, host));
  495. }
  496. public bool IRCSendUsers(string server)
  497. {
  498. SendTCPMessage(string.Format("USERS {0}", server));
  499. }
  500. public bool IRCSendWallops(string message)
  501. {
  502. SendTCPMessage(string.Format("WALLOPS :{0}", message));
  503. }
  504. public bool IRCSendUserhost(List<string> nicks)
  505. {
  506. string message = string.Empty;
  507. foreach (string nick in nicks)
  508. {
  509. message += " " + nick;
  510. }
  511. SendTCPMessage(string.Format("USERHOST {0}", message.Trim()));
  512. }
  513. public bool IRCSendIson(List<string> nicks)
  514. {
  515. string message = string.Empty;
  516. foreach (string nick in nicks)
  517. {
  518. message += " " + nick;
  519. }
  520. SendTCPMessage(string.Format("ISON {0}", message.Trim()));
  521. }
  522. // ------------------- //
  523. // Internal Functions //
  524. // ------------------- //
  525. /// <summary>
  526. /// Sends a private message to a nick or channel
  527. /// </summary>
  528. /// <param name="nick"></param>
  529. /// <param name="message"></param>
  530. protected void IRCSendPrivMessage(string recipient, string message)
  531. {
  532. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient, message));
  533. }
  534. protected void IRCSendPrivMessage(List<string> recipients, string message)
  535. {
  536. string recipient_list = string.Empty;
  537. foreach (string recipient in recipients)
  538. {
  539. recipient_list += recipient + ",";
  540. }
  541. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient_list.TrimEnd(','), message));
  542. }
  543. /// <summary>
  544. /// Sends a Notice to either a nick or channel
  545. /// </summary>
  546. /// <param name="nick"></param>
  547. /// <param name="message"></param>
  548. protected void IRCSendNotice(string recipient, string message)
  549. {
  550. SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient, message));
  551. }
  552. protected void IRCSendNotice(List<string> recipients, string message)
  553. {
  554. string recipient_list = string.Empty;
  555. foreach (string recipient in recipients)
  556. {
  557. recipient_list += recipient + ",";
  558. }
  559. SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient_list.TrimEnd(','), message));
  560. }
  561. /// <summary>
  562. /// Sends the connection password
  563. /// </summary>
  564. /// <param name="password"></param>
  565. protected void IRCSendPassword(string password)
  566. {
  567. SendTCPMessage(password);
  568. }
  569. /// <summary>
  570. /// Sends a Nick command to set the nickname
  571. /// </summary>
  572. /// <param name="nick"></param>
  573. protected void IRCSendNick(string nick)
  574. {
  575. SendTCPMessage(nick);
  576. }
  577. /// <summary>
  578. /// Sends the User command to set a user
  579. /// </summary>
  580. /// <param name="user"></param>
  581. protected void IRCSendUser(string username, string hostname, string servername, string realname)
  582. {
  583. SendTCPMessage(string.Format("USER {0} {1} {2} :{3}", username, hostname, servername, realname));
  584. }
  585. /// <summary>
  586. /// Sends the Oper command to authorize the client as a newtork Oper
  587. /// </summary>
  588. /// <param name="username"></param>
  589. /// <param name="password"></param>
  590. protected void IRCSendOper(string username, string password)
  591. {
  592. SendTCPMessage(string.Format("OPER {0} {1}", username, password));
  593. }
  594. /// <summary>
  595. /// Sends a Quit command to end the client session
  596. /// </summary>
  597. /// <param name="message"></param>
  598. protected void IRCSendQuit()
  599. {
  600. SendTCPMessage("QUIT");
  601. }
  602. protected void IRCSendQuit(string message)
  603. {
  604. SendTCPMessage(string.Format("QUIT :{0}", message));
  605. }
  606. /// <summary>
  607. /// Sends a Join command to join a channel
  608. /// </summary>
  609. /// <param name="channel"></param>
  610. protected void IRCSendJoin(string channel, string key = "")
  611. {
  612. string message = string.Empty;
  613. message = (key != string.Empty) ? string.Format("{0}; {1}", channel, key) : channel;
  614. SendTCPMessage(string.Format("JOIN {0}", message));
  615. }
  616. protected void IRCSendJoin(List<string> channels, List<string> keys)
  617. {
  618. string message = string.Empty;
  619. string channel_string = string.Empty;
  620. string key_string = string.Empty;
  621. foreach (string channel in channels)
  622. {
  623. channel_string += channel + ",";
  624. }
  625. foreach (string key in keys)
  626. {
  627. if (key != string.Empty)
  628. {
  629. key_string += key + ",";
  630. }
  631. }
  632. channel_string = channel_string.TrimEnd(',');
  633. key_string = key_string.TrimEnd(',');
  634. message = (key_string != string.Empty) ? string.Format("{0}; {1}", channel_string, key_string) : channel_string;
  635. SendTCPMessage(string.Format("JOIN {0}", message));
  636. }
  637. /// <summary>
  638. /// Sends a Part command to leave a channel
  639. /// </summary>
  640. /// <param name="channel"></param>
  641. protected void IRCSendPart(string channel)
  642. {
  643. SendTCPMessage(string.Format("PART {0}", channel));
  644. }
  645. protected void IRCSendPart(List<string> channels)
  646. {
  647. string channel_list = string.Empty;
  648. foreach (string channel in channels)
  649. {
  650. channel_list += channel + ",";
  651. }
  652. SendTCPMessage(string.Format("PART {0}", channel_list.TrimEnd(',')));
  653. }
  654. /// <summary>
  655. /// Sends a Mode command for either a channel mode or user mode
  656. /// </summary>
  657. /// <param name="channel"></param>
  658. /// <param name="mode"></param>
  659. protected void IRCSendMode(string channel, ChannelModeInfo modeInfo)
  660. {
  661. string mode_set = modeInfo.Set ? "+" : "-";
  662. SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  663. }
  664. protected void IRCSendMode(string channel, List<ChannelModeInfo> modeInfos)
  665. {
  666. foreach (ChannelModeInfo modeInfo in modeInfos)
  667. {
  668. IRCSendMode(channel, modeInfo);
  669. }
  670. }
  671. protected void IRCSendMode(string nick, UserModeInfo modeInfo)
  672. {
  673. string mode_set = modeInfo.Set ? "+" : "-";
  674. SendTCPMessage(string.Format("MODE {0} {1} {2}", nick, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  675. }
  676. protected void IRCSendMode(string nick, List<UserModeInfo> modeInfos)
  677. {
  678. foreach (UserModeInfo modeInfo in modeInfos)
  679. {
  680. IRCSendMode(nick, modeInfo);
  681. }
  682. }
  683. /// <summary>
  684. /// Sends a Topic command to change the channels topic or view the current one
  685. /// </summary>
  686. /// <param name="channel"></param>
  687. protected void IRCSendTopic(string channel)
  688. {
  689. SendTCPMessage(string.Format("TOPIC {0}", channel));
  690. }
  691. protected void IRCSendTopic(string channel, string topic)
  692. {
  693. SendTCPMessage(string.Format("TOPIC {0} :{1}", channel, topic));
  694. }
  695. /// <summary>
  696. /// Sends a Names command to get a list of visible users
  697. /// </summary>
  698. protected void IRCSendNames()
  699. {
  700. SendTCPMessage("NAMES");
  701. }
  702. protected void IRCSendNames(string channel)
  703. {
  704. SendTCPMessage(string.Format("NAMES {0}", channel));
  705. }
  706. protected void IRCSendNames(List<string> channels)
  707. {
  708. string channel_list = string.Empty;
  709. foreach (string channel in channels)
  710. {
  711. channel_list += channel + ",";
  712. }
  713. SendTCPMessage(string.Format("NAMES {0}", channel_list.TrimEnd(',')));
  714. }
  715. /// <summary>
  716. /// Sends a List command to get the topic of channels
  717. /// </summary>
  718. protected void IRCSendList()
  719. {
  720. SendTCPMessage("LIST");
  721. }
  722. protected void IRCSendList(string channel)
  723. {
  724. SendTCPMessage(string.Format("LIST {0}", channel));
  725. }
  726. protected void IRCSendList(List<string> channels)
  727. {
  728. string channel_list = string.Empty;
  729. foreach (string channel in channels)
  730. {
  731. channel_list += channel + ",";
  732. }
  733. SendTCPMessage(string.Format("LIST {0}", channel_list.TrimEnd(',')));
  734. }
  735. /// <summary>
  736. /// Sends an Invite command that invites the specified nick to the channel
  737. /// </summary>
  738. /// <param name="channel"></param>
  739. /// <param name="nick"></param>
  740. protected void IRCSendInvite(string channel, string nick)
  741. {
  742. SendTCPMessage(string.Format("INVITE {0} {1}", nick, channel));
  743. }
  744. /// <summary>
  745. /// Sends a Kick command to remove a user from a channel
  746. /// </summary>
  747. /// <param name="channel"></param>
  748. /// <param name="nick"></param>
  749. protected void IRCSendKick(string channel, string nick)
  750. {
  751. SendTCPMessage(string.Format("KICK {0} {1}", channel, nick));
  752. }
  753. protected void IRCSendKick(string channel, string nick, string reason)
  754. {
  755. SendTCPMessage(string.Format("KICK {0} {1} :{2}", channel, nick, reason));
  756. }
  757. /// <summary>
  758. /// Sends a Version command to the server to get a Version reply
  759. /// </summary>
  760. /// <param name="server"></param>
  761. protected void IRCSendVersion(string server)
  762. {
  763. SendTCPMessage(string.Format("VERSION {0}", server));
  764. }
  765. /// <summary>
  766. /// Sends a Stats command to view Server information and statistics
  767. /// </summary>
  768. /// <param name="stat"></param>
  769. protected void IRCSendStats(ServerStat stat)
  770. {
  771. SendTCPMessage(string.Format("STATS {0}", stat.ToString()));
  772. }
  773. protected void IRCSendStats(ServerStat stat, string parameter)
  774. {
  775. SendTCPMessage(string.Format("STATS {0} {1}", stat.ToString(), parameter));
  776. }
  777. /// <summary>
  778. /// Sends a Links command to list all servers matching a mask
  779. /// </summary>
  780. /// <param name="mask"></param>
  781. protected void IRCSendLinks(string mask)
  782. {
  783. SendTCPMessage(string.Format("LINKS {0}", mask));
  784. }
  785. protected void IRCSendLinks(string server, string mask)
  786. {
  787. SendTCPMessage(string.Format("LINKS {0} {1}", mask, server));
  788. }
  789. /// <summary>
  790. /// Sends a Time command to query the local server time
  791. /// </summary>
  792. protected void IRCSendTime()
  793. {
  794. SendTCPMessage("TIME");
  795. }
  796. protected void IRCSendTime(string server)
  797. {
  798. SendTCPMessage(string.Format("TIME {0}", server));
  799. }
  800. /// <summary>
  801. /// Senda a Connect command to have the server try to connect to another server
  802. /// </summary>
  803. /// <param name="server"></param>
  804. protected void IRCSendConnect(string server)
  805. {
  806. SendTCPMessage(string.Format("CONNECT {0}", server));
  807. }
  808. protected void IRCSendConnect(string server, string originator, int port)
  809. {
  810. SendTCPMessage(string.Format("CONNECT {0} {1} {2}", originator, port, server));
  811. }
  812. /// <summary>
  813. /// Sends a Trace command to find the route to the target (nick or server)
  814. /// </summary>
  815. /// <param name="target"></param>
  816. protected void IRCSendTrace(string target)
  817. {
  818. SendTCPMessage(string.Format("TRACE {0}", target));
  819. }
  820. /// <summary>
  821. /// Sends an Admin command to get the name of the server Administrator
  822. /// </summary>
  823. protected void IRCSendAdmin()
  824. {
  825. SendTCPMessage("ADMIN");
  826. }
  827. protected void IRCSendAdmin(string host)
  828. {
  829. SendTCPMessage(string.Format("ADMIN {0}", host));
  830. }
  831. /// <summary>
  832. /// Sends an Info command for a specific server or nick
  833. /// </summary>
  834. /// <param name="host"></param>
  835. protected void IRCSendInfo(string host)
  836. {
  837. SendTCPMessage(string.Format("INFO {0}", host));
  838. }
  839. /// <summary>
  840. /// Sends a Who command to list all public users or matching a mask
  841. /// </summary>
  842. protected void IRCSendWho()
  843. {
  844. SendTCPMessage("WHO");
  845. }
  846. protected void IRCSendWho(string host, bool ops = false)
  847. {
  848. string msg = string.Empty;
  849. if (ops)
  850. {
  851. msg = string.Format("WHO {0} o", host);
  852. }
  853. else
  854. {
  855. msg = string.Format("WHO {0}", host);
  856. }
  857. SendTCPMessage(msg);
  858. }
  859. /// <summary>
  860. /// Sends a Whois command to get info about a user
  861. /// </summary>
  862. /// <param name="nick"></param>
  863. protected void IRCSendWhois(string nick)
  864. {
  865. SendTCPMessage(string.Format("WHOIS {0}", nick));
  866. }
  867. protected void IRCSendWhois(string nick, string server)
  868. {
  869. SendTCPMessage(string.Format("WHOIS {0} {1}", server, nick));
  870. }
  871. /// <summary>
  872. /// Sends a Whowas command to get the nick history of a user
  873. /// </summary>
  874. /// <param name="nick"></param>
  875. protected void IRCSendWhowas(string nick)
  876. {
  877. SendTCPMessage(string.Format("WHOIS {0}", nick));
  878. }
  879. protected void IRCSendWhowas(string nick, int entries)
  880. {
  881. SendTCPMessage(string.Format("WHOIS {0} {1}", nick, entries));
  882. }
  883. protected void IRCSendWhowas(string nick, int entries, string server)
  884. {
  885. SendTCPMessage(string.Format("WHOIS {0} {1} {2}", nick, entries, server));
  886. }
  887. /// <summary>
  888. /// Sends a Kill command to disconnect a nick
  889. /// </summary>
  890. /// <param name="nick"></param>
  891. /// <param name="comment"></param>
  892. protected void IRCSendKill(string nick, string comment)
  893. {
  894. SendTCPMessage(string.Format("KILL {0} {1}", nick, comment));
  895. }
  896. /// <summary>
  897. /// Sends a Ping command to the recipient
  898. /// </summary>
  899. /// <param name="recipient"></param>
  900. protected void IRCSendPing(string recipient)
  901. {
  902. SendTCPMessage(string.Format("PING {0}", recipient));
  903. }
  904. /// <summary>
  905. /// Sends a PONG response to respond to a Ping
  906. /// </summary>
  907. /// <param name="sender"></param>
  908. /// <param name="recipient"></param>
  909. protected void IRCSendPong()
  910. {
  911. SendTCPMessage("PONG");
  912. }
  913. protected void IRCSendPong(string sender, string recipient)
  914. {
  915. SendTCPMessage(string.Format("PONG {0} {1}", sender, recipient));
  916. }
  917. /// <summary>
  918. /// Sends an Away command to unset away status
  919. /// </summary>
  920. protected void IRCSendAway()
  921. {
  922. SendTCPMessage("AWAY");
  923. }
  924. /// <summary>
  925. /// Sends an Away comand to set away status with auto-reply message
  926. /// </summary>
  927. /// <param name="message"></param>
  928. protected void IRCSendAway(string message)
  929. {
  930. SendTCPMessage(string.Format("AWAY {0}", message));
  931. }
  932. /// <summary>
  933. /// Sends a Rehash command to the server to reload it's configuration file
  934. /// </summary>
  935. protected void IRCSendRehash()
  936. {
  937. SendTCPMessage("REHASH");
  938. }
  939. /// <summary>
  940. /// Sends a Restart command to the server to restart
  941. /// </summary>
  942. protected void IRCSendRestart()
  943. {
  944. SendTCPMessage("RESTART");
  945. }
  946. /// <summary>
  947. /// Sends a Summon command to summon a nick to the server
  948. /// </summary>
  949. /// <param name="nick"></param>
  950. protected void IRCSendSummon()
  951. {
  952. SendTCPMessage("SUMMON");
  953. }
  954. protected void IRCSendSummon(string nick)
  955. {
  956. SendTCPMessage(string.Format("SUMMON {0}", nick));
  957. }
  958. protected void IRCSendSummon(string nick, string host)
  959. {
  960. SendTCPMessage(string.Format("SUMMON {0} {1}", nick, host));
  961. }
  962. /// <summary>
  963. /// Sends a Users command to get a list of Users from a server
  964. /// </summary>
  965. /// <param name="server"></param>
  966. protected void IRCSendUsers(string server)
  967. {
  968. SendTCPMessage(string.Format("USERS {0}", server));
  969. }
  970. /// <summary>
  971. /// Sends a Wallops command which sends a message to all connected ops
  972. /// </summary>
  973. /// <param name="message"></param>
  974. protected void IRCSendWallops(string message)
  975. {
  976. SendTCPMessage(string.Format("WALLOPS :{0}", message));
  977. }
  978. /// <summary>
  979. /// Sends an Userhost command to up to 5 nicknames to return information about each nick
  980. /// </summary>
  981. /// <param name="nicks"></param>
  982. protected void IRCSendUserhost(List<string> nicks)
  983. {
  984. string message = string.Empty;
  985. foreach (string nick in nicks)
  986. {
  987. message += " " + nick;
  988. }
  989. SendTCPMessage(string.Format("USERHOST {0}", message.Trim()));
  990. }
  991. /// <summary>
  992. /// Sends an IsOn command to get a return if the nicks specified are online
  993. /// </summary>
  994. /// <param name="nicks"></param>
  995. protected void IRCSendIson(List<string> nicks)
  996. {
  997. string message = string.Empty;
  998. foreach (string nick in nicks)
  999. {
  1000. message += " " + nick;
  1001. }
  1002. SendTCPMessage(string.Format("ISON {0}", message.Trim()));
  1003. }
  1004. }
  1005. }