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 19KB

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