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.

IRCMethods.cs 11KB

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