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.

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