The easy to use and full featured Irc Bot everyone is talking about!
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IRCMethods.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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
  8. {
  9. internal partial class IRCService
  10. {
  11. // ----------------------- //
  12. // Public Mapped Functions //
  13. // ----------------------- //
  14. // ------------------- //
  15. // Internal Functions //
  16. // ------------------- //
  17. /// <summary>
  18. /// Sends a private message to a nick or channel
  19. /// </summary>
  20. /// <param name="nick"></param>
  21. /// <param name="message"></param>
  22. protected void SendPrivMessage(string recipient, string message)
  23. {
  24. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient, message));
  25. }
  26. protected void SendPrivMessage(List<string> recipients, string message)
  27. {
  28. string recipient_list = string.Empty;
  29. foreach (string recipient in recipients)
  30. {
  31. recipient_list += recipient + ",";
  32. }
  33. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", recipient_list.TrimEnd(','), message));
  34. }
  35. /// <summary>
  36. /// Sends a Notice to either a nick or channel
  37. /// </summary>
  38. /// <param name="nick"></param>
  39. /// <param name="message"></param>
  40. protected void SendNotice(string recipient, string message)
  41. {
  42. SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient, message));
  43. }
  44. protected void SendNotice(List<string> recipients, string message)
  45. {
  46. string recipient_list = string.Empty;
  47. foreach (string recipient in recipients)
  48. {
  49. recipient_list += recipient + ",";
  50. }
  51. SendTCPMessage(string.Format("NOTICE {0} :{1}", recipient_list.TrimEnd(','), message));
  52. }
  53. /// <summary>
  54. /// Sends the connection password
  55. /// </summary>
  56. /// <param name="password"></param>
  57. protected void SendPassword(string password)
  58. {
  59. SendTCPMessage(password);
  60. }
  61. /// <summary>
  62. /// Sends a Nick command to set the nickname
  63. /// </summary>
  64. /// <param name="nick"></param>
  65. protected void SendNick(string nick)
  66. {
  67. SendTCPMessage(nick);
  68. }
  69. /// <summary>
  70. /// Sends the User command to set a user
  71. /// </summary>
  72. /// <param name="user"></param>
  73. protected void SendUser(string username, string hostname, string servername, string realname)
  74. {
  75. SendTCPMessage(string.Format("USER {0} {1} {2} :{3}", username, hostname, servername, realname));
  76. }
  77. /// <summary>
  78. /// Sends the Oper command to authorize the client as a newtork Oper
  79. /// </summary>
  80. /// <param name="username"></param>
  81. /// <param name="password"></param>
  82. protected void SendOper(string username, string password)
  83. {
  84. SendTCPMessage(string.Format("OPER {0} {1}", username, password));
  85. }
  86. /// <summary>
  87. /// Sends a Quit command to end the client session
  88. /// </summary>
  89. /// <param name="message"></param>
  90. protected void SendQuit()
  91. {
  92. SendTCPMessage("QUIT");
  93. }
  94. protected void SendQuit(string message)
  95. {
  96. SendTCPMessage(string.Format("QUIT :{0}", message));
  97. }
  98. /// <summary>
  99. /// Sends a Join command to join a channel
  100. /// </summary>
  101. /// <param name="channel"></param>
  102. protected void SendJoin(string channel, string key = "")
  103. {
  104. string message = string.Empty;
  105. message = (key != string.Empty) ? string.Format("{0}; {1}", channel, key) : channel;
  106. SendTCPMessage(string.Format("JOIN {0}", message));
  107. }
  108. protected void SendJoin(List<string> channels, List<string> keys)
  109. {
  110. string message = string.Empty;
  111. string channel_string = string.Empty;
  112. string key_string = string.Empty;
  113. foreach (string channel in channels)
  114. {
  115. channel_string += channel + ",";
  116. }
  117. foreach (string key in keys)
  118. {
  119. if (key != string.Empty)
  120. {
  121. key_string += key + ",";
  122. }
  123. }
  124. channel_string = channel_string.TrimEnd(',');
  125. key_string = key_string.TrimEnd(',');
  126. message = (key_string != string.Empty) ? string.Format("{0}; {1}", channel_string, key_string) : channel_string;
  127. SendTCPMessage(string.Format("JOIN {0}", message));
  128. }
  129. /// <summary>
  130. /// Sends a Part command to leave a channel
  131. /// </summary>
  132. /// <param name="channel"></param>
  133. protected void SendPart(string channel)
  134. {
  135. SendTCPMessage(string.Format("PART {0}", channel));
  136. }
  137. protected void SendPart(List<string> channels)
  138. {
  139. string channel_list = string.Empty;
  140. foreach (string channel in channels)
  141. {
  142. channel_list += channel + ",";
  143. }
  144. SendTCPMessage(string.Format("PART {0}", channel_list.TrimEnd(',')));
  145. }
  146. /// <summary>
  147. /// Sends a Mode command for either a channel mode or user mode
  148. /// </summary>
  149. /// <param name="channel"></param>
  150. /// <param name="mode"></param>
  151. protected void SendMode(string channel, ChannelModeInfo modeInfo)
  152. {
  153. string mode_set = modeInfo.Set ? "+" : "-";
  154. SendTCPMessage(string.Format("MODE {0} {1} {2}", channel, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  155. }
  156. protected void SendMode(string channel, List<ChannelModeInfo> modeInfos)
  157. {
  158. foreach (ChannelModeInfo modeInfo in modeInfos)
  159. {
  160. SendMode(channel, modeInfo);
  161. }
  162. }
  163. protected void SendMode(string nick, UserModeInfo modeInfo)
  164. {
  165. string mode_set = modeInfo.Set ? "+" : "-";
  166. SendTCPMessage(string.Format("MODE {0} {1} {2}", nick, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  167. }
  168. protected void SendMode(string nick, List<UserModeInfo> modeInfos)
  169. {
  170. foreach (UserModeInfo modeInfo in modeInfos)
  171. {
  172. SendMode(nick, modeInfo);
  173. }
  174. }
  175. /// <summary>
  176. /// Sends a Topic command to change the channels topic or view the current one
  177. /// </summary>
  178. /// <param name="channel"></param>
  179. protected void SendTopic(string channel)
  180. {
  181. SendTCPMessage(string.Format("TOPIC {0}", channel));
  182. }
  183. protected void SendTopic(string channel, string topic)
  184. {
  185. SendTCPMessage(string.Format("TOPIC {0} :{1}", channel, topic));
  186. }
  187. /// <summary>
  188. /// Sends a Names command to get a list of visible users
  189. /// </summary>
  190. protected void SendNames()
  191. {
  192. SendTCPMessage("NAMES");
  193. }
  194. protected void SendNames(string channel)
  195. {
  196. SendTCPMessage(string.Format("NAMES {0}", channel));
  197. }
  198. protected void SendNames(List<string> channels)
  199. {
  200. string channel_list = string.Empty;
  201. foreach (string channel in channels)
  202. {
  203. channel_list += channel + ",";
  204. }
  205. SendTCPMessage(string.Format("NAMES {0}", channel_list.TrimEnd(',')));
  206. }
  207. /// <summary>
  208. /// Sends a List command to get the topic of channels
  209. /// </summary>
  210. protected void SendList()
  211. {
  212. SendTCPMessage("LIST");
  213. }
  214. protected void SendList(string channel)
  215. {
  216. SendTCPMessage(string.Format("LIST {0}", channel));
  217. }
  218. protected void SendList(List<string> channels)
  219. {
  220. string channel_list = string.Empty;
  221. foreach (string channel in channels)
  222. {
  223. channel_list += channel + ",";
  224. }
  225. SendTCPMessage(string.Format("LIST {0}", channel_list.TrimEnd(',')));
  226. }
  227. /// <summary>
  228. /// Sends an Invite command that invites the specified nick to the channel
  229. /// </summary>
  230. /// <param name="channel"></param>
  231. /// <param name="nick"></param>
  232. protected void SendInvite(string channel, string nick)
  233. {
  234. SendTCPMessage(string.Format("INVITE {0} {1}", nick, channel));
  235. }
  236. /// <summary>
  237. /// Sends a Kick command to remove a user from a channel
  238. /// </summary>
  239. /// <param name="channel"></param>
  240. /// <param name="nick"></param>
  241. protected void SendKick(string channel, string nick)
  242. {
  243. SendTCPMessage(string.Format("KICK {0} {1}", channel, nick));
  244. }
  245. protected void SendKick(string channel, string nick, string reason)
  246. {
  247. SendTCPMessage(string.Format("KICK {0} {1} :{2}", channel, nick, reason));
  248. }
  249. /// <summary>
  250. /// Sends a Version command to the server to get a Version reply
  251. /// </summary>
  252. /// <param name="server"></param>
  253. protected void SendVersion(string server)
  254. {
  255. SendTCPMessage(string.Format("VERSION {0}", server));
  256. }
  257. /// <summary>
  258. /// Sends a Stats command to view Server information and statistics
  259. /// </summary>
  260. /// <param name="stat"></param>
  261. protected void SendStats(ServerStat stat)
  262. {
  263. SendTCPMessage(string.Format("STATS {0}", stat.ToString()));
  264. }
  265. protected void SendStats(ServerStat stat, string parameter)
  266. {
  267. SendTCPMessage(string.Format("STATS {0} {1}", stat.ToString(), parameter));
  268. }
  269. /// <summary>
  270. /// Sends a Links command to list all servers matching a mask
  271. /// </summary>
  272. /// <param name="mask"></param>
  273. protected void SendLinks(string mask)
  274. {
  275. SendTCPMessage(string.Format("LINKS {0}", mask));
  276. }
  277. protected void SendLinks(string server, string mask)
  278. {
  279. SendTCPMessage(string.Format("LINKS {0} {1}", mask, server));
  280. }
  281. /// <summary>
  282. /// Sends a Time command to query the local server time
  283. /// </summary>
  284. protected void SendTime()
  285. {
  286. SendTCPMessage("TIME");
  287. }
  288. protected void SendTime(string server)
  289. {
  290. SendTCPMessage(string.Format("TIME {0}", server));
  291. }
  292. /// <summary>
  293. /// Senda a Connect command to have the server try to connect to another server
  294. /// </summary>
  295. /// <param name="server"></param>
  296. protected void SendConnect(string server)
  297. {
  298. SendTCPMessage(string.Format("CONNECT {0}", server));
  299. }
  300. protected void SendConnect(string server, string originator, int port)
  301. {
  302. SendTCPMessage(string.Format("CONNECT {0} {1} {2}", originator, port, server));
  303. }
  304. /// <summary>
  305. /// Sends a Trace command to find the route to the target (nick or server)
  306. /// </summary>
  307. /// <param name="target"></param>
  308. protected void SendTrace(string target)
  309. {
  310. SendTCPMessage(string.Format("TRACE {0}", target));
  311. }
  312. }
  313. }