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

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