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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. // Internal Functions //
  13. // ------------------ //
  14. /// <summary>
  15. /// Sends a private message to a nick or channel
  16. /// </summary>
  17. /// <param name="nick"></param>
  18. /// <param name="message"></param>
  19. protected void SendPrivMessage(Nick nick, string message)
  20. {
  21. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", nick, message));
  22. }
  23. protected void SendPrivMessage(List<Nick> nicks, string message)
  24. {
  25. string nick_list = string.Empty;
  26. foreach (Nick nick in nicks)
  27. {
  28. nick_list += nick.Nickname + ",";
  29. }
  30. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", nick_list.TrimEnd(','), message));
  31. }
  32. protected void SendPrivMessage(Channel channel, string message)
  33. {
  34. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", channel.Name, message));
  35. }
  36. protected void SendPrivMessage(List<Channel> channels, string message)
  37. {
  38. string channel_list = string.Empty;
  39. foreach (Channel channel in channels)
  40. {
  41. channel_list += channel.Name + ",";
  42. }
  43. SendTCPMessage(string.Format("PRIVMSG {0} :{1}", channel_list.TrimEnd(','), message));
  44. }
  45. /// <summary>
  46. /// Sends a Notice to either a nick or channel
  47. /// </summary>
  48. /// <param name="nick"></param>
  49. /// <param name="message"></param>
  50. protected void SendNotice(Nick nick, string message)
  51. {
  52. SendTCPMessage(string.Format("NOTICE {0} :{1}", nick, message));
  53. }
  54. protected void SendNotice(List<Nick> nicks, string message)
  55. {
  56. string nick_list = string.Empty;
  57. foreach (Nick nick in nicks)
  58. {
  59. nick_list += nick.Nickname + ",";
  60. }
  61. SendTCPMessage(string.Format("NOTICE {0} :{1}", nick_list.TrimEnd(','), message));
  62. }
  63. protected void SendNotice(Channel channel, string message)
  64. {
  65. SendTCPMessage(string.Format("NOTICE {0} :{1}", channel.Name, message));
  66. }
  67. protected void SendNotice(List<Channel> channels, string message)
  68. {
  69. string channel_list = string.Empty;
  70. foreach (Channel channel in channels)
  71. {
  72. channel_list += channel.Name + ",";
  73. }
  74. SendTCPMessage(string.Format("NOTICE {0} :{1}", channel_list.TrimEnd(','), message));
  75. }
  76. /// <summary>
  77. /// Sends the connection password
  78. /// </summary>
  79. /// <param name="password"></param>
  80. protected void SendPassword(string password)
  81. {
  82. SendTCPMessage(password);
  83. }
  84. /// <summary>
  85. /// Sends a Nick command to set the nickname
  86. /// </summary>
  87. /// <param name="nick"></param>
  88. protected void SendNick(string nick)
  89. {
  90. SendTCPMessage(nick);
  91. }
  92. /// <summary>
  93. /// Sends the User command to set a user
  94. /// </summary>
  95. /// <param name="user"></param>
  96. protected void SendUser(string username, string hostname, string servername, string realname)
  97. {
  98. SendTCPMessage(string.Format("USER {0} {1} {2} :{3}", username, hostname, servername, realname));
  99. }
  100. /// <summary>
  101. /// Sends the Oper command to authorize the client as a newtork Oper
  102. /// </summary>
  103. /// <param name="username"></param>
  104. /// <param name="password"></param>
  105. protected void SendOper(string username, string password)
  106. {
  107. SendTCPMessage(string.Format("OPER {0} {1}", username, password));
  108. }
  109. /// <summary>
  110. /// Sends a Quit command to end the client session
  111. /// </summary>
  112. /// <param name="message"></param>
  113. protected void SendQuit()
  114. {
  115. SendTCPMessage("QUIT");
  116. }
  117. protected void SendQuit(string message)
  118. {
  119. SendTCPMessage(string.Format("QUIT :{0}", message));
  120. }
  121. /// <summary>
  122. /// Sends a Join command to join a channel
  123. /// </summary>
  124. /// <param name="channel"></param>
  125. protected void SendJoin(Channel channel)
  126. {
  127. string message = string.Empty;
  128. message = (channel.Key != string.Empty) ? string.Format("{0}; {1}", channel.Name, channel.Key) : channel.Name;
  129. SendTCPMessage(string.Format("JOIN {0}", message));
  130. }
  131. protected void SendJoin(List<Channel> channels)
  132. {
  133. string message = string.Empty;
  134. string channel_string = string.Empty;
  135. string key_string = string.Empty;
  136. foreach (Channel channel in channels)
  137. {
  138. channel_string += channel.Name + ",";
  139. if (channel.Key != string.Empty)
  140. {
  141. key_string += channel.Key + ",";
  142. }
  143. }
  144. channel_string = channel_string.TrimEnd(',');
  145. key_string = key_string.TrimEnd(',');
  146. message = (key_string != string.Empty) ? string.Format("{0}; {1}", channel_string, key_string) : channel_string;
  147. SendTCPMessage(string.Format("JOIN {0}", message));
  148. }
  149. /// <summary>
  150. /// Sends a Part command to leave a channel
  151. /// </summary>
  152. /// <param name="channel"></param>
  153. protected void SendPart(Channel channel)
  154. {
  155. SendTCPMessage(string.Format("PART {0}", channel.Name));
  156. }
  157. protected void SendPart(List<Channel> channels)
  158. {
  159. string channel_list = string.Empty;
  160. foreach (Channel channel in channels)
  161. {
  162. channel_list += channel.Name + ",";
  163. }
  164. SendTCPMessage(string.Format("PART {0}", channel_list.TrimEnd(',')));
  165. }
  166. /// <summary>
  167. /// Sends a Mode command for either a channel mode or user mode
  168. /// </summary>
  169. /// <param name="channel"></param>
  170. /// <param name="mode"></param>
  171. protected void SendMode(Channel channel, ChannelModeInfo modeInfo)
  172. {
  173. string mode_set = modeInfo.Set ? "+" : "-";
  174. SendTCPMessage(string.Format("MODE {0} {1} {2}", channel.Name, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  175. }
  176. protected void SendMode(Channel channel, List<ChannelModeInfo> modeInfos)
  177. {
  178. foreach (ChannelModeInfo modeInfo in modeInfos)
  179. {
  180. SendMode(channel, modeInfo);
  181. }
  182. }
  183. protected void SendMode(Nick nick, UserModeInfo modeInfo)
  184. {
  185. string mode_set = modeInfo.Set ? "+" : "-";
  186. SendTCPMessage(string.Format("MODE {0} {1} {2}", nick.Nickname, mode_set + modeInfo.Mode.ToString(), modeInfo.Parameter));
  187. }
  188. protected void SendMode(Nick nick, List<UserModeInfo> modeInfos)
  189. {
  190. foreach (UserModeInfo modeInfo in modeInfos)
  191. {
  192. SendMode(nick, modeInfo);
  193. }
  194. }
  195. /// <summary>
  196. /// Sends a Topic command to change the channels topic or view the current one
  197. /// </summary>
  198. /// <param name="channel"></param>
  199. protected void SendTopic(Channel channel)
  200. {
  201. SendTCPMessage(string.Format("TOPIC {0}", channel.Name));
  202. }
  203. protected void SendTopic(Channel channel, string topic)
  204. {
  205. SendTCPMessage(string.Format("TOPIC {0} :{1}", channel.Name, topic));
  206. }
  207. }
  208. }