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

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