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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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.Threading;
  11. using System.Windows.Documents;
  12. using Combot;
  13. using Combot.IRCServices.Messaging;
  14. using Combot.Configurations;
  15. using Combot.IRCServices;
  16. namespace Interface.ViewModels
  17. {
  18. public class MainViewModel : ViewModelBase
  19. {
  20. public List<Bot> CombotSessions = new List<Bot>();
  21. public Config Config = new Config();
  22. public string ApplicationTitle { get; set; }
  23. private string _CurrentBuffer = string.Empty;
  24. public string CurrentBuffer
  25. {
  26. get { return _CurrentBuffer; }
  27. set
  28. {
  29. _CurrentBuffer = value;
  30. OnPropertyChanged("CurrentBuffer");
  31. }
  32. }
  33. private bool _Connected = false;
  34. public bool Connected
  35. {
  36. get { return _Connected; }
  37. set
  38. {
  39. _Connected = value;
  40. if (_Connected)
  41. {
  42. ToggleConnectionText = "Disconnect";
  43. }
  44. else
  45. {
  46. ToggleConnectionText = "Connect";
  47. }
  48. OnPropertyChanged("Connected");
  49. }
  50. }
  51. private string _ToggleConnectionText = "Connect";
  52. public string ToggleConnectionText
  53. {
  54. get { return _ToggleConnectionText; }
  55. set
  56. {
  57. _ToggleConnectionText = value;
  58. OnPropertyChanged("ToggleConnectionText");
  59. }
  60. }
  61. private string _InputBoxText;
  62. public string InputBoxText
  63. {
  64. get { return _InputBoxText; }
  65. set
  66. {
  67. _InputBoxText = value;
  68. OnPropertyChanged("InputBoxText");
  69. }
  70. }
  71. private string _SelectedServer;
  72. public string SelectedServer
  73. {
  74. get { return _SelectedServer; }
  75. set
  76. {
  77. _SelectedServer = value;
  78. OnPropertyChanged("SelectedServer");
  79. ChangeServer();
  80. ChangeBuffer();
  81. }
  82. }
  83. private string _SelectedLocation;
  84. public string SelectedLocation
  85. {
  86. get { return _SelectedLocation; }
  87. set
  88. {
  89. _SelectedLocation = value;
  90. OnPropertyChanged("SelectedLocation");
  91. ChangeBuffer();
  92. }
  93. }
  94. private ObservableCollection<string> _ServerList;
  95. public ObservableCollection<string> ServerList
  96. {
  97. get { return _ServerList; }
  98. set
  99. {
  100. if (_ServerList != value)
  101. {
  102. _ServerList = value;
  103. OnPropertyChanged("ServerList");
  104. }
  105. }
  106. }
  107. private ObservableCollection<string> _LocationList;
  108. public ObservableCollection<string> LocationList
  109. {
  110. get { return _LocationList; }
  111. set
  112. {
  113. if (_LocationList != value)
  114. {
  115. _LocationList = value;
  116. OnPropertyChanged("LocationList");
  117. }
  118. }
  119. }
  120. public DelegateCommand ToggleConnection { get; private set; }
  121. public DelegateCommand SubmitText { get; set; }
  122. public DelegateCommand RemoveLocation { get; set; }
  123. public DelegateCommand ClearLocation { get; set; }
  124. private List<BufferInfo> BufferList = new List<BufferInfo>();
  125. private ReaderWriterLockSlim BufferLock;
  126. public MainViewModel()
  127. {
  128. ApplicationTitle = "Combot";
  129. Config.LoadServers();
  130. ServerList = new ObservableCollection<string>();
  131. LocationList = new ObservableCollection<string>();
  132. BufferLock = new ReaderWriterLockSlim();
  133. foreach (ServerConfig server in Config.Servers)
  134. {
  135. ServerList.Add(server.Name);
  136. Bot Combot = new Bot(server);
  137. Combot.ErrorEvent += e => BotErrorHandler(e, Combot.ServerConfig.Name);
  138. Combot.IRC.Message.ErrorMessageEvent += (sender, e) => ErrorMessageHandler(sender, e, Combot.ServerConfig.Name);
  139. Combot.IRC.Message.ServerReplyEvent += (sender, e) => ServerReplyHandler(sender, e, Combot.ServerConfig.Name);
  140. Combot.IRC.Message.ChannelMessageReceivedEvent += (sender, e) => ChannelMessageReceivedHandler(sender, e, Combot.ServerConfig.Name);
  141. Combot.IRC.Message.ChannelNoticeReceivedEvent += (sender, e) => ChannelNoticeReceivedHandler(sender, e, Combot.ServerConfig.Name);
  142. Combot.IRC.Message.PrivateMessageReceivedEvent += (sender, e) => PrivateMessageReceivedHandler(sender, e, Combot.ServerConfig.Name);
  143. Combot.IRC.Message.PrivateNoticeReceivedEvent += (sender, e) => PrivateNoticeReceivedHandler(sender, e, Combot.ServerConfig.Name);
  144. Combot.IRC.Message.JoinChannelEvent += (sender, e) => JoinEventHandler(sender, e, Combot.ServerConfig.Name);
  145. Combot.IRC.Message.PartChannelEvent += (sender, e) => PartEventHandler(sender, e, Combot.ServerConfig.Name);
  146. Combot.IRC.ConnectEvent += () => ConnectHandler(Combot.ServerConfig.Name);
  147. Combot.IRC.DisconnectEvent += () => DisconnectHandler(Combot.ServerConfig.Name);
  148. Combot.IRC.TCPErrorEvent += e => TCPErrorHandler(e, Combot.ServerConfig.Name);
  149. CombotSessions.Add(Combot);
  150. SelectedServer = server.Name;
  151. if (server.AutoConnect)
  152. {
  153. Combot.Connect();
  154. }
  155. }
  156. ToggleConnection = new DelegateCommand(ExecuteToggleConnection, CanToggleConnection);
  157. SubmitText = new DelegateCommand(ExecuteSubmitText, CanSubmitText);
  158. RemoveLocation = new DelegateCommand(ExecuteRemoveLocation, CanRemoveLocation);
  159. ClearLocation = new DelegateCommand(ExecuteClearLocation, CanClearLocation);
  160. }
  161. private void BotErrorHandler(BotError error, string server)
  162. {
  163. AddToBuffer(server, null, string.Format("[{0}] \u0002{1} Error\u0002: {2}", DateTime.Now.ToString("HH:mm:ss"), error.Type, error.Message));
  164. }
  165. private void TCPErrorHandler(Combot.IRCServices.TCP.TCPError error, string server)
  166. {
  167. AddToBuffer(server, null, string.Format("[{0}] \u0002TCP Error {1}\u0002: {2}", DateTime.Now.ToString("HH:mm:ss"), error.Code, error.Message));
  168. }
  169. private void ServerReplyHandler(object sender, IReply reply, string server)
  170. {
  171. AddToBuffer(server, null, string.Format("[{0}] \u0002*\u0002: {1}", reply.TimeStamp.ToString("HH:mm:ss"), reply.Message));
  172. }
  173. private void ErrorMessageHandler(object sender, ErrorMessage message, string server)
  174. {
  175. AddToBuffer(server, null, string.Format("[{0}] \u0002*\u0002: {1}", message.TimeStamp.ToString("HH:mm:ss"), message.Message));
  176. }
  177. private void ChannelMessageReceivedHandler(object sender, ChannelMessage message, string server)
  178. {
  179. AddToBuffer(server, message.Channel, string.Format("[{0}] \u0002{1}\u0002: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  180. }
  181. private void ChannelNoticeReceivedHandler(object sender, ChannelNotice message, string server)
  182. {
  183. AddToBuffer(server, message.Channel, string.Format("[{0}] \u0002{1}\u0002 -NOTICE-: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  184. }
  185. private void PrivateMessageReceivedHandler(object sender, PrivateMessage message, string server)
  186. {
  187. string location = message.Sender.Nickname;
  188. if (message.Sender.Nickname.ToLower() == "nickserv" || message.Sender.Nickname.ToLower() == "chanserv")
  189. {
  190. location = null;
  191. }
  192. AddToBuffer(server, location, string.Format("[{0}] \u0002{1}\u0002: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  193. }
  194. private void PrivateNoticeReceivedHandler(object sender, PrivateNotice message, string server)
  195. {
  196. string location = message.Sender.Nickname;
  197. if (message.Sender.Nickname.ToLower() == "nickserv" || message.Sender.Nickname.ToLower() == "chanserv")
  198. {
  199. location = null;
  200. }
  201. AddToBuffer(server, location, string.Format("[{0}] \u0002{1}\u0002 -NOTICE-: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  202. }
  203. private void JoinEventHandler(object sender, JoinChannelInfo info, string server)
  204. {
  205. AddToBuffer(server, info.Channel, string.Format("[{0}] \u0002{1}\u0002 has joined \u0002{2}\u0002.", info.TimeStamp.ToString("HH:mm:ss"), info.Nick.Nickname, info.Channel));
  206. }
  207. private void PartEventHandler(object sender, PartChannelInfo info, string server)
  208. {
  209. AddToBuffer(server, info.Channel, string.Format("[{0}] \u0002{1}\u0002 has left \u0002{2}\u0002.", info.TimeStamp.ToString("HH:mm:ss"), info.Nick.Nickname, info.Channel));
  210. }
  211. private void ConnectHandler(string server)
  212. {
  213. if (server == SelectedServer)
  214. {
  215. Connected = true;
  216. }
  217. AddToBuffer(server, null, "-- Connected --");
  218. }
  219. private void DisconnectHandler(string server)
  220. {
  221. if (server == SelectedServer)
  222. {
  223. Connected = false;
  224. }
  225. AddToBuffer(server, null, "-- Disconnected --");
  226. }
  227. private void ExecuteToggleConnection()
  228. {
  229. if (_Connected)
  230. {
  231. Disconnect(SelectedServer);
  232. }
  233. else
  234. {
  235. Connect(SelectedServer);
  236. }
  237. }
  238. private bool CanToggleConnection()
  239. {
  240. return true;
  241. }
  242. private void ExecuteSubmitText()
  243. {
  244. Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
  245. if (botInstance != null && botInstance.Connected)
  246. {
  247. string message = InputBoxText;
  248. if (InputBoxText.StartsWith("/"))
  249. {
  250. MessageType type = MessageType.Query;
  251. if (SelectedLocation.StartsWith("#") || SelectedLocation.StartsWith("&"))
  252. {
  253. type = MessageType.Channel;
  254. }
  255. message = message.Remove(0, 1);
  256. message = string.Join("", botInstance.ServerConfig.CommandPrefix, message);
  257. botInstance.ExecuteCommand(message, SelectedLocation, type);
  258. }
  259. else
  260. {
  261. if (SelectedLocation != " --Server-- ")
  262. {
  263. if (SelectedLocation.StartsWith("#") || SelectedLocation.StartsWith("&"))
  264. {
  265. if (botInstance.IRC.Channels.Exists(chan => chan.Name == SelectedLocation))
  266. {
  267. botInstance.IRC.SendPrivateMessage(SelectedLocation, message);
  268. }
  269. else
  270. {
  271. AddToBuffer(SelectedServer, SelectedLocation, "You are not in this channel.");
  272. }
  273. }
  274. else
  275. {
  276. botInstance.IRC.SendPrivateMessage(SelectedLocation, message);
  277. }
  278. }
  279. }
  280. InputBoxText = string.Empty;
  281. }
  282. }
  283. private bool CanSubmitText()
  284. {
  285. if (Connected)
  286. {
  287. return true;
  288. }
  289. return false;
  290. }
  291. private void ExecuteRemoveLocation()
  292. {
  293. if (SelectedLocation != " --Server-- ")
  294. {
  295. string location = SelectedLocation;
  296. if (location.StartsWith("#") || location.StartsWith("&"))
  297. {
  298. Bot botInstance = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer);
  299. if (botInstance.IRC.Channels.Exists(chan => chan.Name == location))
  300. {
  301. botInstance.IRC.SendPart(location);
  302. }
  303. }
  304. if (LocationList.Contains(location))
  305. {
  306. App.Current.Dispatcher.Invoke((Action)(() => LocationList.Remove(location)));
  307. }
  308. BufferLock.EnterWriteLock();
  309. if (BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == location))
  310. {
  311. BufferList.RemoveAll(buf => buf.Server == SelectedServer && buf.Location == location);
  312. }
  313. BufferLock.ExitWriteLock();
  314. }
  315. }
  316. private bool CanRemoveLocation()
  317. {
  318. return true;
  319. }
  320. private void ExecuteClearLocation()
  321. {
  322. string location = SelectedLocation;
  323. BufferLock.EnterWriteLock();
  324. if (BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == location))
  325. {
  326. BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == location).Buffer.Clear();
  327. }
  328. BufferLock.ExitWriteLock();
  329. CurrentBuffer = string.Empty;
  330. }
  331. private bool CanClearLocation()
  332. {
  333. return true;
  334. }
  335. private void Connect(string server)
  336. {
  337. CombotSessions.Find(bot => bot.ServerConfig.Name == server).Connect();
  338. }
  339. private void Disconnect(string server)
  340. {
  341. CombotSessions.Find(bot => bot.ServerConfig.Name == server).Disconnect();
  342. }
  343. private void AddToBuffer(string server, string location, string message)
  344. {
  345. if (location == null)
  346. {
  347. location = " --Server-- ";
  348. }
  349. BufferLock.EnterWriteLock();
  350. if (!BufferList.Exists(buf => buf.Server == server && buf.Location == location))
  351. {
  352. BufferInfo newBuffer = new BufferInfo();
  353. newBuffer.Server = server;
  354. newBuffer.Location = location;
  355. BufferList.Add(newBuffer);
  356. }
  357. BufferLock.ExitWriteLock();
  358. if (SelectedServer == server && !LocationList.Contains(location))
  359. {
  360. App.Current.Dispatcher.Invoke((Action) (() => LocationList.Add(location)));
  361. }
  362. BufferLock.EnterWriteLock();
  363. BufferInfo buffer = BufferList.Find(buf => buf.Server == server && buf.Location == location);
  364. if (buffer.Buffer.Count >= 1000)
  365. {
  366. buffer.Buffer.RemoveAt(0);
  367. }
  368. buffer.Buffer.Add(message);
  369. BufferLock.ExitWriteLock();
  370. ChangeBuffer();
  371. }
  372. private void ChangeServer()
  373. {
  374. App.Current.Dispatcher.Invoke((Action)(() => LocationList.Clear()));
  375. for (int i = 0; i < BufferList.Count; i++)
  376. {
  377. if (BufferList[i].Server == SelectedServer)
  378. {
  379. App.Current.Dispatcher.Invoke((Action)(() => LocationList.Add(BufferList[i].Location)));
  380. }
  381. }
  382. if (LocationList.Any())
  383. {
  384. SelectedLocation = " --Server-- ";
  385. }
  386. }
  387. private void ChangeBuffer()
  388. {
  389. if (SelectedServer != null && SelectedLocation != null)
  390. {
  391. BufferLock.EnterWriteLock();
  392. if (!BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation))
  393. {
  394. BufferInfo newBuffer = new BufferInfo();
  395. newBuffer.Server = SelectedServer;
  396. newBuffer.Location = SelectedLocation;
  397. BufferList.Add(newBuffer);
  398. }
  399. CurrentBuffer = string.Join(Environment.NewLine, BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation).Buffer);
  400. BufferLock.ExitWriteLock();
  401. Connected = CombotSessions.Find(bot => bot.ServerConfig.Name == SelectedServer).Connected;
  402. }
  403. }
  404. }
  405. }