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.

IRC.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Net;
  8. using Combot.IRCServices.Messaging;
  9. using Combot.IRCServices.TCP;
  10. namespace Combot.IRCServices
  11. {
  12. public partial class IRC
  13. {
  14. public List<Channel> Channels = new List<Channel>();
  15. public Messages Message;
  16. public event Action DisconnectEvent;
  17. public event Action<TCPError> TCPErrorEvent;
  18. public string Nickname { get; set; }
  19. public Dictionary<string, PrivaledgeMode> PrivaledgeMapping = new Dictionary<string, PrivaledgeMode>() { { "+", PrivaledgeMode.v }, { "%", PrivaledgeMode.h }, { "@", PrivaledgeMode.o }, { "&", PrivaledgeMode.a }, { "~", PrivaledgeMode.q } };
  20. private TCPInterface _TCP;
  21. private Thread TCPReader;
  22. private event Action<string> TCPMessageEvent;
  23. private ReaderWriterLockSlim ChannelRWLock;
  24. public IRC()
  25. {
  26. _TCP = new TCPInterface();
  27. Message = new Messages(this);
  28. Nickname = string.Empty;
  29. ChannelRWLock = new ReaderWriterLockSlim();
  30. TCPMessageEvent += Message.ParseTCPMessage;
  31. _TCP.TCPConnectionEvent += HandleTCPConnection;
  32. _TCP.TCPErrorEvent += HandleTCPError;
  33. Message.ErrorMessageEvent += HandleErrorMessage;
  34. Message.PingEvent += HandlePing;
  35. Message.ServerReplyEvent += HandleReply;
  36. Message.ChannelModeChangeEvent += HandleChannelModeChange;
  37. Message.UserModeChangeEvent += HandleUserModeChange;
  38. Message.NickChangeEvent += HandleNickChange;
  39. Message.JoinChannelEvent += HandleJoin;
  40. Message.PartChannelEvent += HandlePart;
  41. Message.KickEvent += HandleKick;
  42. Message.QuitEvent += HandleQuit;
  43. }
  44. public bool Connect(IPAddress IP, int port, int readTimeout = 5000, int allowedFailedCount = 0)
  45. {
  46. bool result = false;
  47. if (!_TCP.Connected)
  48. {
  49. result = _TCP.Connect(IP, port, readTimeout, allowedFailedCount);
  50. if (result)
  51. {
  52. TCPReader = new Thread(ReadTCPMessages);
  53. TCPReader.IsBackground = true;
  54. TCPReader.Start();
  55. }
  56. }
  57. return result;
  58. }
  59. public bool Disconnect()
  60. {
  61. bool result = false;
  62. if (_TCP.Connected)
  63. {
  64. _TCP.Disconnect();
  65. }
  66. if (DisconnectEvent != null)
  67. {
  68. DisconnectEvent();
  69. }
  70. return result;
  71. }
  72. public void Login(string serverName, Nick nick)
  73. {
  74. Nickname = nick.Nickname;
  75. IRCSendNick(nick.Nickname);
  76. IRCSendUser(nick.Nickname, nick.Host, serverName, nick.Realname);
  77. }
  78. private void ReadTCPMessages()
  79. {
  80. while (_TCP.Connected)
  81. {
  82. string response = ReadTCPMessage();
  83. if (TCPMessageEvent != null && response != null && response != string.Empty)
  84. {
  85. TCPMessageEvent(response);
  86. }
  87. Thread.Sleep(10);
  88. }
  89. }
  90. private string ReadTCPMessage()
  91. {
  92. if (_TCP.Connected)
  93. {
  94. return _TCP.Read();
  95. }
  96. return null;
  97. }
  98. private void SendTCPMessage(string message)
  99. {
  100. if (_TCP.Connected)
  101. {
  102. _TCP.Write(message);
  103. }
  104. }
  105. private void HandleTCPConnection(int e)
  106. {
  107. if (DisconnectEvent != null)
  108. {
  109. DisconnectEvent();
  110. }
  111. }
  112. private void HandleTCPError(TCPError e)
  113. {
  114. if (TCPErrorEvent != null)
  115. {
  116. TCPErrorEvent(e);
  117. }
  118. }
  119. private void HandleErrorMessage(object sender, ErrorMessage e)
  120. {
  121. Disconnect();
  122. }
  123. private void HandlePing(object sender, PingInfo e)
  124. {
  125. IRCSendPong(e.Message);
  126. }
  127. private void HandleReply(object sender, IReply e)
  128. {
  129. if (e.GetType() == typeof(ServerReplyMessage))
  130. {
  131. ServerReplyMessage msg = (ServerReplyMessage)e;
  132. switch (msg.ReplyCode)
  133. {
  134. case IRCReplyCode.RPL_WHOREPLY:
  135. ChannelRWLock.EnterWriteLock();
  136. string[] msgSplit = msg.Message.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  137. string target = msgSplit[0];
  138. if (target.StartsWith("&") || target.StartsWith("#"))
  139. {
  140. if (msgSplit.GetUpperBound(0) >= 7)
  141. {
  142. string nickname = msgSplit[4];
  143. string realname = msgSplit[7];
  144. string username = msgSplit[1];
  145. string host = msgSplit[2];
  146. string modeString = msgSplit[5];
  147. Channel channel = Channels.Find(chan => chan.Name == target);
  148. if (channel != null)
  149. {
  150. Nick nick = channel.GetNick(nickname);
  151. bool nickFound = true;
  152. if (nick == null)
  153. {
  154. nickFound = false;
  155. nick = new Nick();
  156. }
  157. nick.Host = host;
  158. nick.Realname = realname;
  159. nick.Username = username;
  160. nick.Modes = new List<UserMode>();
  161. nick.Privaledges = new List<PrivaledgeMode>();
  162. char[] modeArr = modeString.ToCharArray();
  163. for (int i = 1; i <= modeArr.GetUpperBound(0); i++)
  164. {
  165. if (PrivaledgeMapping.ContainsKey(modeArr[i].ToString()))
  166. {
  167. nick.Privaledges.Add(PrivaledgeMapping[modeArr[i].ToString()]);
  168. }
  169. else if (modeArr[i].ToString() == "*")
  170. {
  171. nick.Modes.Add(UserMode.o);
  172. }
  173. else
  174. {
  175. nick.Modes.Add((UserMode)Enum.Parse(typeof(UserMode), modeArr[i].ToString()));
  176. }
  177. }
  178. if (!nickFound)
  179. {
  180. channel.AddNick(nick);
  181. }
  182. }
  183. }
  184. }
  185. ChannelRWLock.ExitWriteLock();
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. else
  192. {
  193. ServerErrorMessage msg = (ServerErrorMessage)e;
  194. }
  195. }
  196. private void HandleChannelModeChange(object sender, ChannelModeChangeInfo e)
  197. {
  198. ChannelRWLock.EnterWriteLock();
  199. Channel channel = Channels.Find(chan => chan.Name == e.Channel);
  200. if (channel != null)
  201. {
  202. foreach (ChannelModeInfo mode in e.Modes)
  203. {
  204. switch (mode.Mode)
  205. {
  206. case ChannelMode.v:
  207. case ChannelMode.h:
  208. case ChannelMode.o:
  209. case ChannelMode.a:
  210. case ChannelMode.q:
  211. Nick changedNick = channel.GetNick(mode.Parameter);
  212. if (mode.Set)
  213. {
  214. changedNick.AddPrivaledge((PrivaledgeMode)Enum.Parse(typeof(PrivaledgeMode), mode.Mode.ToString()));
  215. }
  216. else
  217. {
  218. changedNick.RemovePrivaledge((PrivaledgeMode)Enum.Parse(typeof(PrivaledgeMode), mode.Mode.ToString()));
  219. }
  220. break;
  221. case ChannelMode.b:
  222. if (mode.Set)
  223. {
  224. channel.AddBan(mode.Parameter);
  225. }
  226. else
  227. {
  228. channel.RemoveBan(mode.Parameter);
  229. }
  230. break;
  231. case ChannelMode.k:
  232. if (mode.Set)
  233. {
  234. channel.AddMode(mode.Mode);
  235. channel.Key = mode.Parameter;
  236. }
  237. else
  238. {
  239. channel.RemoveMode(mode.Mode);
  240. channel.Key = string.Empty;
  241. }
  242. break;
  243. default:
  244. if (mode.Set)
  245. {
  246. channel.AddMode(mode.Mode);
  247. }
  248. else
  249. {
  250. channel.RemoveMode(mode.Mode);
  251. }
  252. break;
  253. }
  254. }
  255. }
  256. ChannelRWLock.ExitWriteLock();
  257. }
  258. private void HandleUserModeChange(object sender, UserModeChangeInfo e)
  259. {
  260. ChannelRWLock.EnterWriteLock();
  261. for (int i = 0; i < Channels.Count; i++)
  262. {
  263. Nick changedNick = Channels[i].GetNick(e.Nick.Nickname);
  264. if (changedNick != null)
  265. {
  266. foreach (UserModeInfo mode in e.Modes)
  267. {
  268. if (mode.Set)
  269. {
  270. changedNick.AddMode(mode.Mode);
  271. }
  272. else
  273. {
  274. changedNick.RemoveMode(mode.Mode);
  275. }
  276. }
  277. }
  278. }
  279. ChannelRWLock.ExitWriteLock();
  280. }
  281. private void HandleNickChange(object sender, NickChangeInfo e)
  282. {
  283. ChannelRWLock.EnterWriteLock();
  284. for (int i = 0; i < Channels.Count; i++)
  285. {
  286. Nick newNick = Channels[i].GetNick(e.OldNick.Nickname);
  287. if (newNick != null)
  288. {
  289. newNick.Nickname = e.NewNick.Nickname;
  290. }
  291. }
  292. ChannelRWLock.ExitWriteLock();
  293. }
  294. private void HandleJoin(object sender, JoinChannelInfo e)
  295. {
  296. ChannelRWLock.EnterWriteLock();
  297. Channel channel = Channels.Find(chan => chan.Name == e.Channel);
  298. if (channel != null)
  299. {
  300. channel.AddNick(e.Nick);
  301. }
  302. else
  303. {
  304. Channel newChannel = new Channel();
  305. newChannel.Name = e.Channel;
  306. if (e.Nick.Nickname == Nickname)
  307. {
  308. newChannel.Joined = true;
  309. }
  310. newChannel.Nicks.Add(e.Nick);
  311. Channels.Add(newChannel);
  312. IRCSendWho(newChannel.Name);
  313. }
  314. ChannelRWLock.ExitWriteLock();
  315. }
  316. private void HandlePart(object sender, PartChannelInfo e)
  317. {
  318. ChannelRWLock.EnterWriteLock();
  319. Channel channel = Channels.Find(chan => chan.Name == e.Channel);
  320. if (channel != null)
  321. {
  322. channel.RemoveNick(e.Nick.Nickname);
  323. }
  324. ChannelRWLock.ExitWriteLock();
  325. }
  326. private void HandleKick(object sender, KickInfo e)
  327. {
  328. ChannelRWLock.EnterWriteLock();
  329. Channel channel = Channels.Find(chan => chan.Name == e.Channel);
  330. if (channel != null)
  331. {
  332. channel.RemoveNick(e.Nick.Nickname);
  333. }
  334. ChannelRWLock.ExitWriteLock();
  335. }
  336. private void HandleQuit(object sender, QuitInfo e)
  337. {
  338. ChannelRWLock.EnterWriteLock();
  339. for (int i = 0; i < Channels.Count; i++)
  340. {
  341. Channels[i].RemoveNick(e.Nick.Nickname);
  342. }
  343. ChannelRWLock.ExitWriteLock();
  344. }
  345. }
  346. }