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.

IRCSend.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. using System.Threading;
  8. namespace Combot.IRCServices
  9. {
  10. // TODO Make long message splits based on words
  11. public partial class IRC
  12. {
  13. /// <summary>
  14. /// Sends a private message to a nick or channel
  15. /// </summary>
  16. /// <param name="nick"></param>
  17. /// <param name="message"></param>
  18. public void SendPrivateMessage(string recipient, string message)
  19. {
  20. TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
  21. if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
  22. {
  23. Thread.Sleep((int) sinceLastMessage.TotalMilliseconds);
  24. SendPrivateMessage(recipient, message);
  25. }
  26. else
  27. {
  28. LastMessageSend = DateTime.Now;
  29. if (message.Length > MaxMessageLength)
  30. {
  31. string subMessage = message.Substring(0, MaxMessageLength);
  32. string nextMessage = message.Remove(0, MaxMessageLength);
  33. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient, subMessage));
  34. SendPrivateMessage(recipient, nextMessage);
  35. }
  36. else
  37. {
  38. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient, message));
  39. }
  40. }
  41. }
  42. public void SendPrivateMessage(List<string> recipients, string message)
  43. {
  44. string recipient_list = string.Empty;
  45. foreach (string recipient in recipients)
  46. {
  47. recipient_list += recipient + ",";
  48. }
  49. SendPrivateMessage(recipient_list.TrimEnd(','), message);
  50. }
  51. /// <summary>
  52. /// Sends a Notice to either a nick or channel
  53. /// </summary>
  54. /// <param name="nick"></param>
  55. /// <param name="message"></param>
  56. public void SendNotice(string recipient, string message)
  57. {
  58. TimeSpan sinceLastMessage = (DateTime.Now - LastMessageSend);
  59. if (sinceLastMessage.TotalMilliseconds < MessageSendDelay)
  60. {
  61. Thread.Sleep((int) sinceLastMessage.TotalMilliseconds);
  62. SendNotice(recipient, message);
  63. }
  64. else
  65. {
  66. LastMessageSend = DateTime.Now;
  67. if (message.Length > MaxMessageLength)
  68. {
  69. string subMessage = message.Substring(0, MaxMessageLength);
  70. string nextMessage = message.Remove(0, MaxMessageLength);
  71. SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient, message));
  72. SendNotice(recipient, nextMessage);
  73. }
  74. else
  75. {
  76. SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient, message));
  77. }
  78. }
  79. }
  80. public void SendNotice(List<string> recipients, string message)
  81. {
  82. string recipient_list = string.Empty;
  83. foreach (string recipient in recipients)
  84. {
  85. recipient_list += recipient + ",";
  86. }
  87. SendNotice(recipient_list.TrimEnd(','), message);
  88. }
  89. /// <summary>
  90. /// Sends a CTCP command and optional message to a nick or channel
  91. /// </summary>
  92. /// <param name="recipient"></param>
  93. /// <param name="command"></param>
  94. /// <param name="message"></param>
  95. public void SendCTCPMessage(string recipient, string command, string message = "")
  96. {
  97. if (message != string.Empty)
  98. {
  99. message = " " + message;
  100. }
  101. SendTCPMessage(string.Format("PRIVMSG {0} :\u0001{1}{2}\u0001", recipient, command, message));
  102. }
  103. public void SendCTCPMessage(List<string> recipients, string command, string message)
  104. {
  105. string recipient_list = string.Empty;
  106. foreach (string recipient in recipients)
  107. {
  108. recipient_list += recipient + ",";
  109. }
  110. if (message != string.Empty)
  111. {
  112. message = " " + message;
  113. }
  114. SendCTCPMessage(recipient_list.TrimEnd(','), command, message);
  115. }
  116. /// <summary>
  117. /// Sends a CTCP command and optional message to a nick or channel
  118. /// </summary>
  119. /// <param name="recipient"></param>
  120. /// <param name="command"></param>
  121. /// <param name="message"></param>
  122. public void SendCTCPNotice(string recipient, string command, string message = "")
  123. {
  124. if (message != string.Empty)
  125. {
  126. message = " " + message;
  127. }
  128. SendTCPMessage(string.Format("NOTICE {0} :\u0001{1}{2}\u0001", recipient, command, message));
  129. }
  130. public void SendCTCPNotice(List<string> recipients, string command, string message)
  131. {
  132. string recipient_list = string.Empty;
  133. foreach (string recipient in recipients)
  134. {
  135. recipient_list += recipient + ",";
  136. }
  137. if (message != string.Empty)
  138. {
  139. message = " " + message;
  140. }
  141. SendCTCPNotice(recipient_list.TrimEnd(','), command, message);
  142. }
  143. /// <summary>
  144. /// Sends the connection password
  145. /// </summary>
  146. /// <param name="password"></param>
  147. public void SendPassword(string password)
  148. {
  149. SendTCPMessage(string.Format("PASSWORD {0}", password));
  150. }
  151. /// <summary>
  152. /// Sends a Nick command to set the nickname
  153. /// </summary>
  154. /// <param name="nick"></param>
  155. public void SendNick(string nick)
  156. {
  157. SendTCPMessage(string.Format("NICK {0}", nick));
  158. }
  159. /// <summary>
  160. /// Sends the User command to set a user
  161. /// </summary>
  162. /// <param name="user"></param>
  163. public void SendUser(string username, string hostname, string servername, string realname)
  164. {
  165. SendTCPMessage(string.Format("USER {0} {1} {2} :{3}", username, hostname, servername, realname));
  166. }
  167. /// <summary>
  168. /// Sends the Oper command to authorize the client as a newtork Oper
  169. /// </summary>
  170. /// <param name="username"></param>
  171. /// <param name="password"></param>
  172. public void SendOper(string username, string password)
  173. {
  174. SendTCPMessage(string.Format("OPER {0} {1}", username, password));
  175. }
  176. /// <summary>
  177. /// Sends a Quit command to end the client session
  178. /// </summary>
  179. /// <param name="message"></param>
  180. public void SendQuit()
  181. {
  182. SendTCPMessage("QUIT");
  183. }
  184. public void SendQuit(string message)
  185. {
  186. SendTCPMessage(string.Format("QUIT :{0}", message));
  187. }
  188. /// <summary>
  189. /// Sends a Join command to join a channel
  190. /// </summary>
  191. /// <param name="channel"></param>
  192. public void SendJoin(string channel, string key = "")
  193. {
  194. string message = string.Empty;
  195. message = (key != string.Empty) ? string.Format("{0} {1}", channel, key) : channel;
  196. SendTCPMessage(string.Format("JOIN {0}", message));
  197. }
  198. public void SendJoin(List<string> channels, List<string> keys)
  199. {
  200. string message = string.Empty;
  201. string channel_string = string.Empty;
  202. string key_string = string.Empty;
  203. foreach (string channel in channels)
  204. {
  205. channel_string += channel + ",";
  206. }
  207. foreach (string key in keys)
  208. {
  209. if (key != string.Empty)
  210. {
  211. key_string += key + ",";
  212. }
  213. }
  214. channel_string = channel_string.TrimEnd(',');
  215. key_string = key_string.TrimEnd(',');
  216. message = (key_string != string.Empty) ? string.Format("{0} {1}", channel_string, key_string) : channel_string;
  217. SendTCPMessage(string.Format("JOIN {0}", message));
  218. }
  219. /// <summary>
  220. /// Sends a Part command to leave a channel
  221. /// </summary>
  222. /// <param name="channel"></param>
  223. public void SendPart(string channel)
  224. {
  225. SendTCPMessage(string.Format("PART {0}", channel));
  226. }
  227. public void SendPart(List<string> channels)
  228. {
  229. string channel_list = string.Empty;
  230. foreach (string channel in channels)
  231. {
  232. channel_list += channel + ",";
  233. }
  234. SendPart(channel_list.TrimEnd(','));
  235. }
  236. /// <summary>
  237. /// Sends a Mode command for either a channel mode or user mode
  238. /// </summary>
  239. /// <param name="channel"></param>
  240. /// <param name="mode"></param>
  241. public void SendMode(string channel, ChannelModeInfo modeInfo)
  242. {
  243. string mode_set = modeInfo.Set ? "+" : "-";
  244. SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  245. }
  246. public void SendMode(string channel, List<ChannelModeInfo> modeInfos)
  247. {
  248. foreach (ChannelModeInfo modeInfo in modeInfos)
  249. {
  250. SendMode(channel, modeInfo);
  251. }
  252. }
  253. public void SendMode(string nick, UserModeInfo modeInfo)
  254. {
  255. string mode_set = modeInfo.Set ? "+" : "-";
  256. SendTCPMessage(string.Format("MODE {0} {1}", nick, mode_set + modeInfo.Mode.ToString()));
  257. }
  258. public void SendMode(string nick, List<UserModeInfo> modeInfos)
  259. {
  260. foreach (UserModeInfo modeInfo in modeInfos)
  261. {
  262. SendMode(nick, modeInfo);
  263. }
  264. }
  265. /// <summary>
  266. /// Sends a Topic command to change the channels topic or view the current one
  267. /// </summary>
  268. /// <param name="channel"></param>
  269. public void SendTopic(string channel)
  270. {
  271. SendTCPMessage(string.Format("TOPIC {0}", channel));
  272. }
  273. public void SendTopic(string channel, string topic)
  274. {
  275. SendTCPMessage(string.Format("TOPIC {0} :{1}", channel, topic));
  276. }
  277. /// <summary>
  278. /// Sends a Names command to get a list of visible users
  279. /// </summary>
  280. public void SendNames()
  281. {
  282. SendTCPMessage("NAMES");
  283. }
  284. public void SendNames(string channel)
  285. {
  286. SendTCPMessage(string.Format("NAMES {0}", channel));
  287. }
  288. public void SendNames(List<string> channels)
  289. {
  290. string channel_list = string.Empty;
  291. foreach (string channel in channels)
  292. {
  293. channel_list += channel + ",";
  294. }
  295. SendNames(channel_list.TrimEnd(','));
  296. }
  297. /// <summary>
  298. /// Sends a List command to get the topic of channels
  299. /// </summary>
  300. public void SendList()
  301. {
  302. SendTCPMessage("LIST");
  303. }
  304. public void SendList(string channel)
  305. {
  306. SendTCPMessage(string.Format("LIST {0}", channel));
  307. }
  308. public void SendList(List<string> channels)
  309. {
  310. string channel_list = string.Empty;
  311. foreach (string channel in channels)
  312. {
  313. channel_list += channel + ",";
  314. }
  315. SendList(channel_list.TrimEnd(','));
  316. }
  317. /// <summary>
  318. /// Sends an Invite command that invites the specified nick to the channel
  319. /// </summary>
  320. /// <param name="channel"></param>
  321. /// <param name="nick"></param>
  322. public void SendInvite(string channel, string nick)
  323. {
  324. SendTCPMessage(string.Format("INVITE {0} {1}", nick, channel));
  325. }
  326. /// <summary>
  327. /// Sends a Kick command to remove a user from a channel
  328. /// </summary>
  329. /// <param name="channel"></param>
  330. /// <param name="nick"></param>
  331. public void SendKick(string channel, string nick)
  332. {
  333. SendTCPMessage(string.Format("KICK {0} {1}", channel, nick));
  334. }
  335. public void SendKick(string channel, string nick, string reason)
  336. {
  337. SendTCPMessage(string.Format("KICK {0} {1} :{2}", channel, nick, reason));
  338. }
  339. /// <summary>
  340. /// Sends a Version command to the server to get a Version reply
  341. /// </summary>
  342. /// <param name="server"></param>
  343. public void SendVersion(string server)
  344. {
  345. SendTCPMessage(string.Format("VERSION {0}", server));
  346. }
  347. /// <summary>
  348. /// Sends a Stats command to view Server information and statistics
  349. /// </summary>
  350. /// <param name="stat"></param>
  351. public void SendStats(ServerStat stat)
  352. {
  353. SendTCPMessage(string.Format("STATS {0}", stat.ToString()));
  354. }
  355. public void SendStats(ServerStat stat, string parameter)
  356. {
  357. SendTCPMessage(string.Format("STATS {0} {1}", stat.ToString(), parameter));
  358. }
  359. /// <summary>
  360. /// Sends a Links command to list all servers matching a mask
  361. /// </summary>
  362. /// <param name="mask"></param>
  363. public void SendLinks(string mask)
  364. {
  365. SendTCPMessage(string.Format("LINKS {0}", mask));
  366. }
  367. public void SendLinks(string server, string mask)
  368. {
  369. SendTCPMessage(string.Format("LINKS {0} {1}", mask, server));
  370. }
  371. /// <summary>
  372. /// Sends a Time command to query the local server time
  373. /// </summary>
  374. public void SendTime()
  375. {
  376. SendTCPMessage("TIME");
  377. }
  378. public void SendTime(string server)
  379. {
  380. SendTCPMessage(string.Format("TIME {0}", server));
  381. }
  382. /// <summary>
  383. /// Senda a Connect command to have the server try to connect to another server
  384. /// </summary>
  385. /// <param name="server"></param>
  386. public void SendConnect(string server)
  387. {
  388. SendTCPMessage(string.Format("CONNECT {0}", server));
  389. }
  390. public void SendConnect(string server, string originator, int port)
  391. {
  392. SendTCPMessage(string.Format("CONNECT {0} {1} {2}", originator, port, server));
  393. }
  394. /// <summary>
  395. /// Sends a Trace command to find the route to the target (nick or server)
  396. /// </summary>
  397. /// <param name="target"></param>
  398. public void SendTrace(string target)
  399. {
  400. SendTCPMessage(string.Format("TRACE {0}", target));
  401. }
  402. /// <summary>
  403. /// Sends an Admin command to get the name of the server Administrator
  404. /// </summary>
  405. public void SendAdmin()
  406. {
  407. SendTCPMessage("ADMIN");
  408. }
  409. public void SendAdmin(string host)
  410. {
  411. SendTCPMessage(string.Format("ADMIN {0}", host));
  412. }
  413. /// <summary>
  414. /// Sends an Info command for a specific server or nick
  415. /// </summary>
  416. /// <param name="host"></param>
  417. public void SendInfo(string host)
  418. {
  419. SendTCPMessage(string.Format("INFO {0}", host));
  420. }
  421. /// <summary>
  422. /// Sends a Who command to list all public users or matching a mask
  423. /// </summary>
  424. public void SendWho()
  425. {
  426. SendTCPMessage("WHO");
  427. }
  428. public void SendWho(string host, bool ops = false)
  429. {
  430. string msg = string.Empty;
  431. if (ops)
  432. {
  433. msg = string.Format("WHO {0} o", host);
  434. }
  435. else
  436. {
  437. msg = string.Format("WHO {0}", host);
  438. }
  439. SendTCPMessage(msg);
  440. }
  441. /// <summary>
  442. /// Sends a Whois command to get info about a user
  443. /// </summary>
  444. /// <param name="nick"></param>
  445. public void SendWhois(string nick)
  446. {
  447. SendTCPMessage(string.Format("WHOIS {0}", nick));
  448. }
  449. public void SendWhois(string nick, string server)
  450. {
  451. SendTCPMessage(string.Format("WHOIS {0} {1}", server, nick));
  452. }
  453. /// <summary>
  454. /// Sends a Whowas command to get the nick history of a user
  455. /// </summary>
  456. /// <param name="nick"></param>
  457. public void SendWhowas(string nick)
  458. {
  459. SendTCPMessage(string.Format("WHOIS {0}", nick));
  460. }
  461. public void SendWhowas(string nick, int entries)
  462. {
  463. SendTCPMessage(string.Format("WHOIS {0} {1}", nick, entries));
  464. }
  465. public void SendWhowas(string nick, int entries, string server)
  466. {
  467. SendTCPMessage(string.Format("WHOIS {0} {1} {2}", nick, entries, server));
  468. }
  469. /// <summary>
  470. /// Sends a Kill command to disconnect a nick
  471. /// </summary>
  472. /// <param name="nick"></param>
  473. /// <param name="comment"></param>
  474. public void SendKill(string nick, string comment)
  475. {
  476. SendTCPMessage(string.Format("KILL {0} {1}", nick, comment));
  477. }
  478. /// <summary>
  479. /// Sends a Ping command to the recipient
  480. /// </summary>
  481. /// <param name="recipient"></param>
  482. public void SendPing(string recipient)
  483. {
  484. SendTCPMessage(string.Format("PING {0}", recipient));
  485. }
  486. /// <summary>
  487. /// Sends a PONG response to respond to a Ping
  488. /// </summary>
  489. /// <param name="sender"></param>
  490. /// <param name="recipient"></param>
  491. public void SendPong()
  492. {
  493. SendTCPMessage("PONG");
  494. }
  495. public void SendPong(string message)
  496. {
  497. SendTCPMessage(string.Format("PONG {0}", message));
  498. }
  499. public void SendPong(string sender, string recipient)
  500. {
  501. SendTCPMessage(string.Format("PONG {0} {1}", sender, recipient));
  502. }
  503. /// <summary>
  504. /// Sends an Away command to unset away status
  505. /// </summary>
  506. public void SendAway()
  507. {
  508. SendTCPMessage("AWAY");
  509. }
  510. /// <summary>
  511. /// Sends an Away comand to set away status with auto-reply message
  512. /// </summary>
  513. /// <param name="message"></param>
  514. public void SendAway(string message)
  515. {
  516. SendTCPMessage(string.Format("AWAY {0}", message));
  517. }
  518. /// <summary>
  519. /// Sends a Rehash command to the server to reload it's configuration file
  520. /// </summary>
  521. public void SendRehash()
  522. {
  523. SendTCPMessage("REHASH");
  524. }
  525. /// <summary>
  526. /// Sends a Restart command to the server to restart
  527. /// </summary>
  528. public void SendRestart()
  529. {
  530. SendTCPMessage("RESTART");
  531. }
  532. /// <summary>
  533. /// Sends a Summon command to summon a nick to the server
  534. /// </summary>
  535. /// <param name="nick"></param>
  536. public void SendSummon()
  537. {
  538. SendTCPMessage("SUMMON");
  539. }
  540. public void SendSummon(string nick)
  541. {
  542. SendTCPMessage(string.Format("SUMMON {0}", nick));
  543. }
  544. public void SendSummon(string nick, string host)
  545. {
  546. SendTCPMessage(string.Format("SUMMON {0} {1}", nick, host));
  547. }
  548. /// <summary>
  549. /// Sends a Users command to get a list of Users from a server
  550. /// </summary>
  551. /// <param name="server"></param>
  552. public void SendUsers(string server)
  553. {
  554. SendTCPMessage(string.Format("USERS {0}", server));
  555. }
  556. /// <summary>
  557. /// Sends a Wallops command which sends a message to all connected ops
  558. /// </summary>
  559. /// <param name="message"></param>
  560. public void SendWallops(string message)
  561. {
  562. SendTCPMessage(string.Format("WALLOPS :{0}", message));
  563. }
  564. /// <summary>
  565. /// Sends an Userhost command to up to 5 nicknames to return information about each nick
  566. /// </summary>
  567. /// <param name="nicks"></param>
  568. public void SendUserhost(List<string> nicks)
  569. {
  570. string message = string.Empty;
  571. foreach (string nick in nicks)
  572. {
  573. message += " " + nick;
  574. }
  575. SendTCPMessage(string.Format("USERHOST {0}", message.Trim()));
  576. }
  577. /// <summary>
  578. /// Sends an IsOn command to get a return if the nicks specified are online
  579. /// </summary>
  580. /// <param name="nicks"></param>
  581. public void SendIson(List<string> nicks)
  582. {
  583. string message = string.Empty;
  584. foreach (string nick in nicks)
  585. {
  586. message += " " + nick;
  587. }
  588. SendTCPMessage(string.Format("ISON {0}", message.Trim()));
  589. }
  590. }
  591. }