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.

Methods.cs 18KB

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