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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. // Incoming Messages
  137. Combot.IRC.Message.ErrorMessageEvent += (sender, e) => ErrorMessageHandler(sender, e, Combot.ServerConfig.Name);
  138. Combot.IRC.Message.ServerReplyEvent += (sender, e) => ServerReplyHandler(sender, e, Combot.ServerConfig.Name);
  139. Combot.IRC.Message.ChannelMessageReceivedEvent += (sender, e) => ChannelMessageReceivedHandler(sender, e, Combot.ServerConfig.Name);
  140. Combot.IRC.Message.ChannelNoticeReceivedEvent += (sender, e) => ChannelNoticeReceivedHandler(sender, e, Combot.ServerConfig.Name);
  141. Combot.IRC.Message.PrivateMessageReceivedEvent += (sender, e) => PrivateMessageReceivedHandler(sender, e, Combot.ServerConfig.Name);
  142. Combot.IRC.Message.PrivateNoticeReceivedEvent += (sender, e) => PrivateNoticeReceivedHandler(sender, e, Combot.ServerConfig.Name);
  143. Combot.IRC.Message.CTCPMessageReceivedEvent += (sender, e) => CTCPReceivedHandler(sender, e, Combot.ServerConfig.Name);
  144. Combot.IRC.Message.CTCPNoticeReceivedEvent += (sender, e) => CTCPReceivedHandler(sender, e, Combot.ServerConfig.Name);
  145. Combot.IRC.Message.JoinChannelEvent += (sender, e) => JoinEventHandler(sender, e, Combot.ServerConfig.Name);
  146. Combot.IRC.Message.InviteChannelEvent += (sender, e) => InviteEventHandler(sender, e, Combot.ServerConfig.Name);
  147. Combot.IRC.Message.PartChannelEvent += (sender, e) => PartEventHandler(sender, e, Combot.ServerConfig.Name);
  148. Combot.IRC.Message.QuitEvent += (sender, e) => QuitEventHandler(sender, e, Combot.ServerConfig.Name);
  149. Combot.IRC.Message.KickEvent += (sender, e) => KickEventHandler(sender, e, Combot.ServerConfig.Name);
  150. Combot.IRC.Message.TopicChangeEvent += (sender, e) => TopicChangeEventHandler(sender, e, Combot.ServerConfig.Name);
  151. Combot.IRC.Message.ChannelModeChangeEvent += (sender, e) => ChannelModeChangeHandler(sender, e, Combot.ServerConfig.Name);
  152. Combot.IRC.Message.UserModeChangeEvent += (sender, e) => UserModeChangeHandler(sender, e, Combot.ServerConfig.Name);
  153. Combot.IRC.Message.NickChangeEvent += (sender, e) => NickChangeHandler(sender, e, Combot.ServerConfig.Name);
  154. // Outgoing Messages
  155. Combot.IRC.Command.PrivateMessageCommandEvent += (sender, e) => PrivateMessageCommandHandler(sender, e, Combot.ServerConfig.Name);
  156. Combot.IRC.Command.PrivateNoticeCommandEvent += (sender, e) => PrivateNoticeCommandHandler(sender, e, Combot.ServerConfig.Name);
  157. Combot.IRC.ConnectEvent += () => ConnectHandler(Combot.ServerConfig.Name);
  158. Combot.IRC.DisconnectEvent += () => DisconnectHandler(Combot.ServerConfig.Name);
  159. Combot.IRC.TCPErrorEvent += e => TCPErrorHandler(e, Combot.ServerConfig.Name);
  160. SelectedServer = Combot.ServerConfig.Name;
  161. }
  162. Controller.Instance.AutoConnect();
  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 NickChangeHandler(object sender, NickChangeInfo e, string name)
  169. {
  170. string msg = string.Format(" * {0} is now known as {1}", e.OldNick.Nickname, e.NewNick.Nickname);
  171. AddToBuffer(name, null, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  172. }
  173. private void KickEventHandler(object sender, KickInfo e, string name)
  174. {
  175. string msg = string.Format(" * {1} has kicked {2} ({3})", e.Channel, e.Nick.Nickname, e.KickedNick.Nickname, e.Reason);
  176. AddToBuffer(name, e.Channel, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  177. }
  178. private void TopicChangeEventHandler(object sender, TopicChangeInfo e, string name)
  179. {
  180. string msg = string.Format(" * {1} has changed the topic to: {2}.", e.Channel, e.Nick.Nickname, e.Topic);
  181. AddToBuffer(name, e.Channel, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  182. }
  183. private void InviteEventHandler(object sender, InviteChannelInfo e, string name)
  184. {
  185. string msg = string.Format(" * {0} invited {1}", e.Requester.Nickname, e.Recipient.Nickname);
  186. AddToBuffer(name, e.Channel, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  187. }
  188. private void UserModeChangeHandler(object sender, UserModeChangeInfo e, string name)
  189. {
  190. string msg = string.Format(" * {0} sets mode {1}", e.Nick.Nickname, e.Modes.ModesToString());
  191. AddToBuffer(name, null, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  192. }
  193. private void ChannelModeChangeHandler(object sender, ChannelModeChangeInfo e, string name)
  194. {
  195. string msg = string.Format(" * {0} sets mode {1} on {2}.", e.Nick.Nickname, e.Modes.ModesToString(), e.Channel);
  196. AddToBuffer(name, e.Channel, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  197. }
  198. private void CTCPReceivedHandler(object sender, CTCPMessage e, string name)
  199. {
  200. string msg = string.Format("[CTCP] [{0}] {1}: {2}", e.Command, e.Sender.Nickname, e.Arguments);
  201. AddToBuffer(name, e.Location, string.Format("[{0}] {1}", e.TimeStamp.ToString("HH:mm:ss"), msg));
  202. }
  203. private void BotErrorHandler(BotError error, string server)
  204. {
  205. AddToBuffer(server, null, string.Format("[{0}] \u0002{1} Error\u0002: {2}", DateTime.Now.ToString("HH:mm:ss"), error.Type, error.Message));
  206. }
  207. private void TCPErrorHandler(Combot.IRCServices.TCP.TCPError error, string server)
  208. {
  209. AddToBuffer(server, null, string.Format("[{0}] \u0002TCP Error {1}\u0002: {2}", DateTime.Now.ToString("HH:mm:ss"), error.Code, error.Message));
  210. }
  211. private void ServerReplyHandler(object sender, IReply reply, string server)
  212. {
  213. AddToBuffer(server, null, string.Format("[{0}] \u0002*\u0002: {1}", reply.TimeStamp.ToString("HH:mm:ss"), reply.Message));
  214. }
  215. private void ErrorMessageHandler(object sender, ErrorMessage message, string server)
  216. {
  217. AddToBuffer(server, null, string.Format("[{0}] \u0002*\u0002: {1}", message.TimeStamp.ToString("HH:mm:ss"), message.Message));
  218. }
  219. private void ChannelMessageReceivedHandler(object sender, ChannelMessage message, string server)
  220. {
  221. AddToBuffer(server, message.Channel, string.Format("[{0}] \u0002{1}\u0002: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  222. }
  223. private void ChannelNoticeReceivedHandler(object sender, ChannelNotice message, string server)
  224. {
  225. string location = (SelectedServer == server) ? SelectedLocation : null;
  226. AddToBuffer(server, location, string.Format("[{0}] \u0002{1}\u0002 -NOTICE-: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  227. }
  228. private void PrivateMessageReceivedHandler(object sender, PrivateMessage message, string server)
  229. {
  230. string location = message.Sender.Nickname;
  231. if (message.Sender.Nickname.ToLower() == "nickserv" || message.Sender.Nickname.ToLower() == "chanserv")
  232. {
  233. location = null;
  234. }
  235. AddToBuffer(server, location, string.Format("[{0}] \u0002{1}\u0002: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  236. }
  237. private void PrivateNoticeReceivedHandler(object sender, PrivateNotice message, string server)
  238. {
  239. string location = (SelectedServer == server) ? SelectedLocation : null;
  240. if (message.Sender.Nickname.ToLower() == "nickserv" || message.Sender.Nickname.ToLower() == "chanserv")
  241. {
  242. location = null;
  243. }
  244. AddToBuffer(server, location, string.Format("[{0}] \u0002{1}\u0002 -NOTICE-: {2}", message.TimeStamp.ToString("HH:mm:ss"), message.Sender.Nickname, message.Message));
  245. }
  246. private void JoinEventHandler(object sender, JoinChannelInfo info, string server)
  247. {
  248. 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));
  249. }
  250. private void PartEventHandler(object sender, PartChannelInfo info, string server)
  251. {
  252. 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));
  253. }
  254. private void QuitEventHandler(object sender, QuitInfo info, string server)
  255. {
  256. string message = (info.Message == string.Empty) ? info.Nick.Nickname : info.Message;
  257. AddToBuffer(server, null, string.Format("[{0}] \u0002{1}\u0002 has quit: ({2})", info.TimeStamp.ToString("HH:mm:ss"), info.Nick.Nickname, message));
  258. }
  259. private void PrivateMessageCommandHandler(object sender, PrivateMessageCommand message, string server)
  260. {
  261. string curNick = string.Empty;
  262. Bot session = Controller.Instance.GetBot(SelectedServer);
  263. if (session != null)
  264. {
  265. curNick = session.IRC.Nickname;
  266. }
  267. AddToBuffer(server, message.Recipient, string.Format("[{0}] \u0002{1}\u0002: {2}", message.TimeStamp.ToString("HH:mm:ss"), curNick, message.Message));
  268. }
  269. private void PrivateNoticeCommandHandler(object sender, PrivateNoticeCommand message, string server)
  270. {
  271. string curNick = string.Empty;
  272. Bot session = Controller.Instance.GetBot(SelectedServer);
  273. if (session != null)
  274. {
  275. curNick = session.IRC.Nickname;
  276. }
  277. string location = (SelectedServer == server) ? SelectedLocation : null;
  278. AddToBuffer(server, location, string.Format("[{0}] \u0002{1}\u0002 -NOTICE-: {2}", message.TimeStamp.ToString("HH:mm:ss"), curNick, message.Message));
  279. }
  280. private void ConnectHandler(string server)
  281. {
  282. if (server == SelectedServer)
  283. {
  284. Connected = true;
  285. }
  286. AddToBuffer(server, null, "-- Connected --");
  287. }
  288. private void DisconnectHandler(string server)
  289. {
  290. if (server == SelectedServer)
  291. {
  292. Connected = false;
  293. }
  294. AddToBuffer(server, null, "-- Disconnected --");
  295. }
  296. private void ExecuteToggleConnection()
  297. {
  298. if (_Connected)
  299. {
  300. Disconnect(SelectedServer);
  301. }
  302. else
  303. {
  304. Connect(SelectedServer);
  305. }
  306. }
  307. private bool CanToggleConnection()
  308. {
  309. return true;
  310. }
  311. private void ExecuteSubmitText()
  312. {
  313. Bot botInstance = Controller.Instance.GetBot(SelectedServer);
  314. if (botInstance != null && botInstance.Connected)
  315. {
  316. string message = InputBoxText;
  317. if (InputBoxText.StartsWith("/"))
  318. {
  319. MessageType type = MessageType.Query;
  320. if (SelectedLocation.StartsWith("#") || SelectedLocation.StartsWith("&"))
  321. {
  322. type = MessageType.Channel;
  323. }
  324. message = message.Remove(0, 1);
  325. message = string.Join("", botInstance.ServerConfig.CommandPrefix, message);
  326. botInstance.ExecuteCommand(message, SelectedLocation, type);
  327. }
  328. else
  329. {
  330. if (SelectedLocation != " --Server-- ")
  331. {
  332. if (SelectedLocation.StartsWith("#") || SelectedLocation.StartsWith("&"))
  333. {
  334. if (botInstance.IRC.Channels.Exists(chan => chan.Name == SelectedLocation))
  335. {
  336. botInstance.IRC.Command.SendPrivateMessage(SelectedLocation, message);
  337. }
  338. else
  339. {
  340. AddToBuffer(SelectedServer, SelectedLocation, "You are not in this channel.");
  341. }
  342. }
  343. else
  344. {
  345. botInstance.IRC.Command.SendPrivateMessage(SelectedLocation, message);
  346. }
  347. }
  348. }
  349. InputBoxText = string.Empty;
  350. }
  351. }
  352. private bool CanSubmitText()
  353. {
  354. if (Connected)
  355. {
  356. return true;
  357. }
  358. return false;
  359. }
  360. private void ExecuteRemoveLocation()
  361. {
  362. if (SelectedLocation != " --Server-- ")
  363. {
  364. string location = SelectedLocation;
  365. if (location.StartsWith("#") || location.StartsWith("&"))
  366. {
  367. Bot botInstance = Controller.Instance.GetBot(SelectedServer);
  368. if (botInstance.IRC.Channels.Exists(chan => chan.Name == location))
  369. {
  370. botInstance.IRC.Command.SendPart(location);
  371. }
  372. }
  373. if (LocationList.Contains(location))
  374. {
  375. Application.Current.Dispatcher.Invoke((Action)(() => LocationList.Remove(location)));
  376. }
  377. BufferLock.EnterWriteLock();
  378. if (BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == location))
  379. {
  380. BufferList.RemoveAll(buf => buf.Server == SelectedServer && buf.Location == location);
  381. }
  382. BufferLock.ExitWriteLock();
  383. }
  384. }
  385. private bool CanRemoveLocation()
  386. {
  387. return true;
  388. }
  389. private void ExecuteClearLocation()
  390. {
  391. string location = SelectedLocation;
  392. BufferLock.EnterWriteLock();
  393. if (BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == location))
  394. {
  395. BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == location).Buffer.Clear();
  396. }
  397. BufferLock.ExitWriteLock();
  398. CurrentBuffer = string.Empty;
  399. }
  400. private bool CanClearLocation()
  401. {
  402. return true;
  403. }
  404. private void Connect(string server)
  405. {
  406. Controller.Instance.GetBot(server).Connect();
  407. }
  408. private void Disconnect(string server)
  409. {
  410. Controller.Instance.GetBot(server).Disconnect();
  411. }
  412. private void AddToBuffer(string server, string location, string message)
  413. {
  414. if (location == null)
  415. {
  416. location = " --Server-- ";
  417. }
  418. BufferLock.EnterWriteLock();
  419. if (!BufferList.Exists(buf => buf.Server == server && buf.Location == location))
  420. {
  421. BufferInfo newBuffer = new BufferInfo();
  422. newBuffer.Server = server;
  423. newBuffer.Location = location;
  424. BufferList.Add(newBuffer);
  425. }
  426. BufferLock.ExitWriteLock();
  427. if (SelectedServer == server && !LocationList.Contains(location))
  428. {
  429. Application.Current.Dispatcher.Invoke((Action) (() => LocationList.Add(location)));
  430. }
  431. BufferLock.EnterWriteLock();
  432. BufferInfo buffer = BufferList.Find(buf => buf.Server == server && buf.Location == location);
  433. if (buffer.Buffer.Count >= 500)
  434. {
  435. buffer.Buffer.RemoveAt(0);
  436. }
  437. buffer.Buffer.Add(message);
  438. BufferLock.ExitWriteLock();
  439. ChangeBuffer();
  440. }
  441. private void ChangeServer()
  442. {
  443. Application.Current.Dispatcher.Invoke((Action)(() => LocationList.Clear()));
  444. for (int i = 0; i < BufferList.Count; i++)
  445. {
  446. if (BufferList[i].Server == SelectedServer)
  447. {
  448. int index = i;
  449. Application.Current.Dispatcher.Invoke((Action)(() => LocationList.Add(BufferList[index].Location)));
  450. }
  451. }
  452. if (LocationList.Any())
  453. {
  454. SelectedLocation = " --Server-- ";
  455. }
  456. }
  457. private void ChangeBuffer()
  458. {
  459. if (SelectedServer != null && SelectedLocation != null)
  460. {
  461. BufferLock.EnterWriteLock();
  462. if (!BufferList.Exists(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation))
  463. {
  464. BufferInfo newBuffer = new BufferInfo();
  465. newBuffer.Server = SelectedServer;
  466. newBuffer.Location = SelectedLocation;
  467. BufferList.Add(newBuffer);
  468. }
  469. CurrentBuffer = string.Join(Environment.NewLine, BufferList.Find(buf => buf.Server == SelectedServer && buf.Location == SelectedLocation).Buffer);
  470. BufferLock.ExitWriteLock();
  471. Bot session = Controller.Instance.GetBot(SelectedServer);
  472. if (session != null)
  473. {
  474. Connected = session.Connected;
  475. }
  476. }
  477. }
  478. }
  479. }