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

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