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.9KB

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