The easy to use and full featured Irc Bot everyone is talking about!
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 bool Connected;
  15. public List<Channel> Channels = new List<Channel>();
  16. public Messages Message;
  17. public event Action DisconnectEvent;
  18. private TCPInterface _TCP;
  19. private Thread TCPReader;
  20. private event Action<string> TCPMessageEvent;
  21. public IRC()
  22. {
  23. Connected = false;
  24. _TCP = new TCPInterface();
  25. Message = new Messages(this);
  26. TCPMessageEvent += Message.ParseTCPMessage;
  27. Message.ErrorMessageEvent += HandleErrorMessage;
  28. Message.PingEvent += HandlePing;
  29. }
  30. public bool Connect(IPAddress IP, int port, int readTimeout = 5000, int allowedFailedCount = 0)
  31. {
  32. bool result = false;
  33. if (!_TCP.Connected)
  34. {
  35. result = _TCP.Connect(IP, port, readTimeout, allowedFailedCount);
  36. if (result)
  37. {
  38. TCPReader = new Thread(ReadTCPMessages);
  39. TCPReader.IsBackground = true;
  40. TCPReader.Start();
  41. }
  42. }
  43. return result;
  44. }
  45. public bool Disconnect()
  46. {
  47. bool result = false;
  48. if (_TCP.Connected)
  49. {
  50. _TCP.Disconnect();
  51. }
  52. if (DisconnectEvent != null)
  53. {
  54. DisconnectEvent();
  55. }
  56. return result;
  57. }
  58. public void Login(string serverName, Nick nick)
  59. {
  60. IRCSendNick(nick.Nickname);
  61. IRCSendUser(nick.Nickname, nick.Host, serverName, nick.Realname);
  62. }
  63. private void ReadTCPMessages()
  64. {
  65. while (_TCP.Connected)
  66. {
  67. string response = ReadTCPMessage();
  68. if (TCPMessageEvent != null && response != null && response != string.Empty)
  69. {
  70. TCPMessageEvent(response);
  71. }
  72. Thread.Sleep(10);
  73. }
  74. }
  75. private string ReadTCPMessage()
  76. {
  77. if (_TCP.Connected)
  78. {
  79. return _TCP.Read();
  80. }
  81. return null;
  82. }
  83. private void SendTCPMessage(string message)
  84. {
  85. if (_TCP.Connected)
  86. {
  87. _TCP.Write(message);
  88. }
  89. }
  90. private void HandleErrorMessage(object sender, IRCServices.Messaging.ErrorMessage e)
  91. {
  92. Disconnect();
  93. }
  94. private void HandlePing(object sender, IRCServices.Messaging.PingInfo e)
  95. {
  96. IRCSendPong(e.Message);
  97. }
  98. }
  99. }