The easy to use and full featured Irc Bot everyone is talking about!
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MainViewModel.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Net;
  9. using System.Runtime.Remoting.Channels;
  10. using System.Windows.Documents;
  11. using Combot;
  12. using Combot.IRCServices.Messaging;
  13. using Combot.Configurations;
  14. namespace Interface.ViewModels
  15. {
  16. public class MainViewModel : ViewModelBase
  17. {
  18. public List<Bot> CombotSessions = new List<Bot>();
  19. public Config Config = new Config();
  20. public string ApplicationTitle { get; set; }
  21. private string _CurrentBuffer = string.Empty;
  22. public string CurrentBuffer
  23. {
  24. get { return _CurrentBuffer; }
  25. set
  26. {
  27. _CurrentBuffer = value;
  28. OnPropertyChanged("CurrentBuffer");
  29. }
  30. }
  31. private bool _Connected = false;
  32. public bool Connected
  33. {
  34. get { return _Connected; }
  35. set
  36. {
  37. _Connected = value;
  38. if (_Connected)
  39. {
  40. ToggleConnectionText = "Disconnect";
  41. }
  42. else
  43. {
  44. ToggleConnectionText = "Connect";
  45. }
  46. OnPropertyChanged("Connected");
  47. }
  48. }
  49. private string _ToggleConnectionText = "Connect";
  50. public string ToggleConnectionText
  51. {
  52. get { return _ToggleConnectionText; }
  53. set
  54. {
  55. _ToggleConnectionText = value;
  56. OnPropertyChanged("ToggleConnectionText");
  57. }
  58. }
  59. private string _InputBoxText;
  60. public string InputBoxText
  61. {
  62. get { return _InputBoxText; }
  63. set
  64. {
  65. _InputBoxText = value;
  66. OnPropertyChanged("InputBoxText");
  67. }
  68. }
  69. private string _SelectedServer;
  70. public string SelectedServer
  71. {
  72. get { return _SelectedServer; }
  73. set
  74. {
  75. _SelectedServer = value;
  76. OnPropertyChanged("SelectedServer");
  77. ChangeServer();
  78. ChangeBuffer();
  79. }
  80. }
  81. private string _SelectedLocation;
  82. public string SelectedLocation
  83. {
  84. get { return _SelectedLocation; }
  85. set
  86. {
  87. _SelectedLocation = value;
  88. OnPropertyChanged("SelectedLocation");
  89. ChangeBuffer();
  90. }
  91. }
  92. private ObservableCollection<string> _ServerList;
  93. public ObservableCollection<string> ServerList
  94. {
  95. get { return _ServerList; }
  96. set
  97. {
  98. if (_ServerList != value)
  99. {
  100. _ServerList = value;
  101. OnPropertyChanged("ServerList");
  102. }
  103. }
  104. }
  105. private ObservableCollection<string> _LocationList;
  106. public ObservableCollection<string> LocationList
  107. {
  108. get { return _LocationList; }
  109. set
  110. {
  111. if (_LocationList != value)
  112. {
  113. _LocationList = value;
  114. OnPropertyChanged("LocationList");
  115. }
  116. }
  117. }
  118. public DelegateCommand ToggleConnection { get; private set; }
  119. public DelegateCommand SubmitText { get; set; }
  120. private List<BufferInfo> BufferList = new List<BufferInfo>();
  121. public MainViewModel()
  122. {
  123. ApplicationTitle = "Combot";
  124. Config.LoadServers();
  125. ServerList = new ObservableCollection<string>();
  126. LocationList = new ObservableCollection<string>();
  127. foreach (ServerConfig server in Config.Servers)
  128. {
  129. ServerList.Add(server.Name);
  130. Bot Combot = new Bot(server);
  131. Combot.IRC.Message.ErrorMessageEvent += (sender, e) => ErrorMessageHandler(sender, e, server.Name);
  132. Combot.IRC.Message.ServerReplyEvent += (sender, e) => ServerReplyHandler(sender, e, server.Name);
  133. Combot.IRC.Message.ChannelMessageReceivedEvent += (sender, e) => ChannelMessageReceivedHandler(sender, e, server.Name);
  134. Combot.IRC.Message.ChannelNoticeReceivedEvent += (sender, e) => ChannelNoticeReceivedHandler(sender, e, server.Name);
  135. Combot.IRC.Message.PrivateMessageReceivedEvent += (sender, e) => PrivateMessageReceivedHandler(sender, e, server.Name);
  136. Combot.IRC.Message.PrivateNoticeReceivedEvent += (sender, e) => PrivateNoticeReceivedHandler(sender, e, server.Name);
  137. //Combot.IRC.Message.RawMessageEvent += RawMessageHandler;
  138. Combot.IRC.Message.JoinChannelEvent += (sender, e) => JoinEventHandler(sender, e, server.Name);
  139. Combot.IRC.ConnectEvent += () => ConnectHandler(server.Name);
  140. Combot.IRC.DisconnectEvent += () => DisconnectHandler(server.Name);
  141. Combot.IRC.TCPErrorEvent += e => TCPErrorHandler(e, server.Name);
  142. CombotSessions.Add(Combot);
  143. SelectedServer = server.Name;
  144. if (server.AutoConnect)
  145. {
  146. Combot.Connect();
  147. }
  148. }
  149. ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection);
  150. SubmitText = new DelegateCommand(ExecuteSubmitText, CanSubmitText);
  151. }
  152. private void JoinEventHandler(object sender, JoinChannelInfo info, string server)
  153. {
  154. AddToBuffer(server, info.Channel, string.Format("{0} has joined {1}.", info.Nick.Nickname, info.Channel));
  155. }
  156. private void RawMessageHandler(object sender, string message)
  157. {
  158. //CurrentBuffer += message + Environment.NewLine;
  159. }
  160. private void TCPErrorHandler(Combot.IRCServices.TCP.TCPError error, string server)
  161. {
  162. AddToBuffer(server, null, string.Format("[{0}] {1}", error.Code.ToString(), error.Message));
  163. }
  164. private void ServerReplyHandler(object sender, IReply reply, string server)
  165. {
  166. AddToBuffer(server, null, reply.Message);
  167. }
  168. private void ErrorMessageHandler(object sender, ErrorMessage message, string server)
  169. {
  170. AddToBuffer(server, null, message.Message);
  171. }
  172. private void ChannelMessageReceivedHandler(object sender, ChannelMessage message, string server)
  173. {
  174. AddToBuffer(server, message.Channel, message.Message);
  175. }
  176. private void ChannelNoticeReceivedHandler(object sender, ChannelNotice message, string server)
  177. {
  178. AddToBuffer(server, message.Channel, message.Message);
  179. }
  180. private void PrivateMessageReceivedHandler(object sender, PrivateMessage message, string server)
  181. {
  182. AddToBuffer(server, message.Sender.Nickname, message.Message);
  183. }
  184. private void PrivateNoticeReceivedHandler(object sender, PrivateNotice message, string server)
  185. {
  186. AddToBuffer(server, message.Sender.Nickname, message.Message);
  187. }
  188. private void ConnectHandler(string server)
  189. {
  190. if (server == SelectedServer)
  191. {
  192. Connected = true;
  193. }
  194. }
  195. private void DisconnectHandler(string server)
  196. {
  197. if (server == SelectedServer)
  198. {
  199. Connected = false;
  200. }
  201. }
  202. private void ExecuteToggleConnection()
  203. {
  204. if (_Connected)
  205. {
  206. Disconnect(SelectedServer);
  207. }
  208. else
  209. {
  210. Connect(SelectedServer);
  211. }
  212. }
  213. private bool CanToggleConnection()
  214. {
  215. return true;
  216. }
  217. private void ExecuteSubmitText()
  218. {
  219. if (SelectedLocation != " --Server-- ")
  220. {
  221. Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
  222. if (botInstance != null && botInstance.Connected)
  223. {
  224. string message = InputBoxText;
  225. if (InputBoxText.StartsWith("/"))
  226. {
  227. MessageType type = MessageType.Query;
  228. if (SelectedLocation.StartsWith("#") || SelectedLocation.StartsWith("&"))
  229. {
  230. type = MessageType.Channel;
  231. }
  232. message = message.Remove(0, 1);
  233. message = string.Join("", botInstance.ServerConfig.CommandPrefix, message);
  234. botInstance.ExecuteCommand(message, SelectedLocation, type);
  235. }
  236. else
  237. {
  238. botInstance.IRC.SendPrivateMessage(SelectedLocation, message);
  239. }
  240. InputBoxText = string.Empty;
  241. }
  242. }
  243. }
  244. private bool CanSubmitText()
  245. {
  246. return true;
  247. }
  248. private void Connect(string server)
  249. {
  250. CombotSessions.Find(bot => bot.ServerConfig.Name == server).Connect();
  251. }
  252. private void Disconnect(string server)
  253. {
  254. CombotSessions.Find(bot => bot.ServerConfig.Name == server).Disconnect();
  255. }
  256. private void AddToBuffer(string server, string location, string message)
  257. {
  258. if (location == null)
  259. {
  260. location = " --Server-- ";
  261. }
  262. if (!BufferList.Exists(buf => buf.Server == server && buf.Location == location))
  263. {
  264. BufferInfo newBuffer = new BufferInfo();
  265. newBuffer.Server = server;
  266. newBuffer.Location = location;
  267. BufferList.Add(newBuffer);
  268. }
  269. if (SelectedServer == server && !LocationList.Contains(location))
  270. {
  271. App.Current.Dispatcher.Invoke((Action) (() => LocationList.Add(location)));
  272. }
  273. BufferInfo buffer = BufferList.Find(buf => buf.Server == server && buf.Location == location);
  274. if (buffer.Buffer.Count >= 1000)
  275. {
  276. buffer.Buffer.RemoveAt(0);
  277. }
  278. buffer.Buffer.Add(message);
  279. ChangeBuffer();
  280. }
  281. private void ChangeServer()
  282. {
  283. App.Current.Dispatcher.Invoke((Action)(() => LocationList.Clear()));
  284. for (int i = 0; i < BufferList.Count; i++)
  285. {
  286. if (BufferList[i].Server == SelectedServer)
  287. {
  288. App.Current.Dispatcher.Invoke((Action)(() => LocationList.Add(BufferList[i].Location)));
  289. }
  290. }
  291. if (LocationList.Any())
  292. {
  293. SelectedLocation = LocationList.First();
  294. }
  295. }
  296. private void ChangeBuffer()
  297. {
  298. if (SelectedServer != null && SelectedLocation != null)
  299. {
  300. if (!BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation))
  301. {
  302. BufferInfo newBuffer = new BufferInfo();
  303. newBuffer.Server = SelectedServer;
  304. newBuffer.Location = SelectedLocation;
  305. BufferList.Add(newBuffer);
  306. }
  307. CurrentBuffer = string.Join(Environment.NewLine, BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation).Buffer);
  308. Connected = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer).Connected;
  309. }
  310. }
  311. }
  312. }