The easy to use and full featured Irc Bot everyone is talking about!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SendCommand.cs 22KB

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