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

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