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.

MainViewModel.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. using Combot;
  8. using Combot.IRCServices.Messaging;
  9. using Combot.Configurations;
  10. using Combot.Modules;
  11. namespace Interface.ViewModels
  12. {
  13. public class MainViewModel : ViewModelBase
  14. {
  15. public List<Bot> CombotSessions = new List<Bot>();
  16. public Config Config = new Config();
  17. public string ApplicationTitle { get; set; }
  18. private string _CurrentBuffer = string.Empty;
  19. public string CurrentBuffer { get { return _CurrentBuffer; } set { _CurrentBuffer = value; OnPropertyChanged("CurrentBuffer"); } }
  20. private bool _Connected = false;
  21. public bool Connected { get { return _Connected; } set { _Connected = value; if (_Connected) { ToggleConnectionText = "Disconnect"; } else { ToggleConnectionText = "Connect"; } OnPropertyChanged("Connected"); } }
  22. private string _ToggleConnectionText = "Connect";
  23. public string ToggleConnectionText { get { return _ToggleConnectionText; } set { _ToggleConnectionText = value; OnPropertyChanged("ToggleConnectionText"); } }
  24. public DelegateCommand ToggleConnection { get; private set; }
  25. public MainViewModel()
  26. {
  27. ApplicationTitle = "Combot";
  28. Config.LoadServers();
  29. foreach (ServerConfig server in Config.Servers)
  30. {
  31. Bot Combot = new Bot(server);
  32. /*
  33. Combot.IRC.Message.ErrorMessageEvent += ErrorMessageHandler;
  34. Combot.IRC.Message.ServerReplyEvent += ServerReplyHandler;
  35. Combot.IRC.Message.ChannelMessageReceivedEvent += ChannelMessageReceivedHandler;
  36. Combot.IRC.Message.ChannelNoticeReceivedEvent += ChannelNoticeReceivedHandler;
  37. Combot.IRC.Message.PrivateMessageReceivedEvent += PrivateMessageReceivedHandler;
  38. Combot.IRC.Message.PrivateNoticeReceivedEvent += PrivateNoticeReceivedHandler;
  39. */
  40. Combot.IRC.Message.RawMessageEvent += RawMessageHandler;
  41. Combot.IRC.ConnectEvent += ConnectHandler;
  42. Combot.IRC.DisconnectEvent += DisconnectHandler;
  43. Combot.IRC.TCPErrorEvent += TCPErrorHandler;
  44. CombotSessions.Add(Combot);
  45. }
  46. ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection);
  47. }
  48. private void RawMessageHandler(object sender, string message)
  49. {
  50. CurrentBuffer += message + Environment.NewLine;
  51. }
  52. private void TCPErrorHandler(Combot.IRCServices.TCP.TCPError error)
  53. {
  54. CurrentBuffer += string.Format("[{0}] {1}", error.Code.ToString(), error.Message) + Environment.NewLine;
  55. }
  56. private void ServerReplyHandler(object sender, IReply reply)
  57. {
  58. CurrentBuffer += reply.Message + Environment.NewLine;
  59. }
  60. private void ErrorMessageHandler(object sender, ErrorMessage message)
  61. {
  62. CurrentBuffer += message.Message + Environment.NewLine;
  63. }
  64. private void ChannelMessageReceivedHandler(object sender, ChannelMessage message)
  65. {
  66. CurrentBuffer += message.Message + Environment.NewLine;
  67. }
  68. private void ChannelNoticeReceivedHandler(object sender, ChannelNotice message)
  69. {
  70. CurrentBuffer += message.Message + Environment.NewLine;
  71. }
  72. private void PrivateMessageReceivedHandler(object sender, PrivateMessage message)
  73. {
  74. CurrentBuffer += message.Message + Environment.NewLine;
  75. }
  76. private void PrivateNoticeReceivedHandler(object sender, PrivateNotice message)
  77. {
  78. CurrentBuffer += message.Message + Environment.NewLine;
  79. }
  80. private void ConnectHandler()
  81. {
  82. Connected = true;
  83. }
  84. private void DisconnectHandler()
  85. {
  86. Connected = false;
  87. }
  88. private void ExecuteToggleConnection()
  89. {
  90. if (_Connected)
  91. {
  92. Disconnect();
  93. }
  94. else
  95. {
  96. Connect();
  97. }
  98. }
  99. private bool CanToggleConnection()
  100. {
  101. return true;
  102. }
  103. private void Connect()
  104. {
  105. CombotSessions.ForEach(Combot => Combot.Connect());
  106. }
  107. private void Disconnect()
  108. {
  109. CombotSessions.ForEach(Combot => Combot.Disconnect());
  110. }
  111. }
  112. }