The easy to use and full featured Irc Bot everyone is talking about!
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using Combot.IRCServices;
  11. using Combot.Configurations;
  12. using Combot.Databases;
  13. using Combot.IRCServices.Messaging;
  14. using Combot.Modules;
  15. namespace Combot
  16. {
  17. public class Bot
  18. {
  19. public event Action<CommandMessage> CommandReceivedEvent;
  20. public event Action<BotError> ErrorEvent;
  21. public ServerConfig ServerConfig;
  22. public IRC IRC;
  23. public Database Database;
  24. public List<Module> Modules;
  25. public bool Connected = false;
  26. public bool LoggedIn = false;
  27. public DateTime ConnectionTime;
  28. public DateTime LoadTime;
  29. public Dictionary<PrivilegeMode, AccessType> PrivilegeModeMapping = new Dictionary<PrivilegeMode, AccessType>() { { PrivilegeMode.v, AccessType.Voice }, { PrivilegeMode.h, AccessType.HalfOperator }, { PrivilegeMode.o, AccessType.Operator }, { PrivilegeMode.a, AccessType.SuperOperator }, { PrivilegeMode.q, AccessType.Founder } };
  30. public Dictionary<ChannelMode, AccessType> ChannelModeMapping = new Dictionary<ChannelMode, AccessType>() { { ChannelMode.v, AccessType.Voice }, { ChannelMode.h, AccessType.HalfOperator }, { ChannelMode.o, AccessType.Operator }, { ChannelMode.a, AccessType.SuperOperator }, { ChannelMode.q, AccessType.Founder } };
  31. private bool GhostSent;
  32. private int CurNickChoice;
  33. private int RetryCount;
  34. private bool RetryAllowed;
  35. public Bot(ServerConfig serverConfig)
  36. {
  37. Modules = new List<Module>();
  38. GhostSent = false;
  39. CurNickChoice = 0;
  40. RetryCount = 0;
  41. ServerConfig = serverConfig;
  42. LoadTime = DateTime.Now;
  43. ConnectionTime = DateTime.Now;
  44. IRC = new IRC(serverConfig.MaxMessageLength, serverConfig.MessageSendDelay);
  45. IRC.ConnectEvent += HandleConnectEvent;
  46. IRC.DisconnectEvent += HandleDisconnectEvent;
  47. IRC.Message.ServerReplyEvent += HandleReplyEvent;
  48. IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessageReceivedEvent;
  49. IRC.Message.PrivateMessageReceivedEvent += HandlePrivateMessageReceivedEvent;
  50. IRC.Message.PrivateNoticeReceivedEvent += HandlePrivateNoticeReceivedEvent;
  51. IRC.Message.JoinChannelEvent += HandleJoinEvent;
  52. IRC.Message.KickEvent += HandleKickEvent;
  53. IRC.Message.ChannelModeChangeEvent += HandleChannelModeChangeEvent;
  54. Database = new Database(serverConfig.Database);
  55. LoadModules();
  56. }
  57. /// <summary>
  58. /// Trys to connect to one of the IPs of the given hostname. If the connection was successful, it will login the nick.
  59. /// </summary>
  60. public void Connect()
  61. {
  62. ConnectionTime = DateTime.Now;
  63. GhostSent = false;
  64. CurNickChoice = 0;
  65. RetryAllowed = ServerConfig.Reconnect;
  66. bool serverConnected = false;
  67. int i = 0;
  68. do
  69. {
  70. if (ServerConfig.Hosts.Count > i)
  71. {
  72. try
  73. {
  74. IPAddress[] ipList = Dns.GetHostAddresses(ServerConfig.Hosts[i].Host);
  75. foreach (IPAddress ip in ipList)
  76. {
  77. serverConnected = IRC.Connect(ip, ServerConfig.Hosts[i].Port);
  78. if (serverConnected)
  79. {
  80. break;
  81. }
  82. }
  83. i++;
  84. }
  85. catch (SocketException ex)
  86. {
  87. break;
  88. }
  89. }
  90. else
  91. {
  92. break;
  93. }
  94. }
  95. while (!serverConnected);
  96. if (serverConnected)
  97. {
  98. if (CurNickChoice < ServerConfig.Nicknames.Count)
  99. {
  100. IRC.Login(ServerConfig.Name, new Nick()
  101. {
  102. Nickname = ServerConfig.Nicknames[CurNickChoice],
  103. Host = Dns.GetHostName(),
  104. Realname = ServerConfig.Realname,
  105. Username = ServerConfig.Username
  106. });
  107. }
  108. else
  109. {
  110. Disconnect();
  111. }
  112. }
  113. else
  114. {
  115. Reconnect();
  116. }
  117. }
  118. /// <summary>
  119. /// Disconnects from the current server.
  120. /// </summary>
  121. public void Disconnect()
  122. {
  123. RetryAllowed = false;
  124. RetryCount = 0;
  125. IRC.Disconnect();
  126. Connected = false;
  127. LoggedIn = false;
  128. }
  129. private void Reconnect()
  130. {
  131. if (RetryAllowed)
  132. {
  133. if (ErrorEvent != null)
  134. {
  135. ErrorEvent(new BotError() { Message = string.Format("Retrying connection in {0} seconds.", (int)Math.Pow(2, RetryCount)), Type = ErrorType.IRC });
  136. }
  137. Task.Run(() =>
  138. {
  139. Thread.Sleep(1000 * (int)Math.Pow(2, RetryCount));
  140. RetryCount++;
  141. Connect();
  142. });
  143. }
  144. }
  145. private void HandleConnectEvent()
  146. {
  147. Connected = true;
  148. RetryCount = 0;
  149. }
  150. private void HandleDisconnectEvent()
  151. {
  152. Connected = false;
  153. Reconnect();
  154. }
  155. public void LoadModules()
  156. {
  157. // Get all config files from Module directory
  158. string[] moduleLocations = Directory.GetDirectories(ServerConfig.ModuleLocation);
  159. foreach (string location in moduleLocations)
  160. {
  161. LoadModule(location);
  162. }
  163. }
  164. public bool LoadModule(string module)
  165. {
  166. Module newModule = new Module();
  167. newModule.ConfigPath = module;
  168. newModule.LoadConfig();
  169. if (newModule.Enabled && !Modules.Exists(mod => mod.ClassName == newModule.ClassName))
  170. {
  171. if (File.Exists(string.Format(@"{0}\{1}.dll", module, newModule.Name)))
  172. {
  173. Module loadedModule = newModule.CreateInstance(this);
  174. if (loadedModule.Loaded)
  175. {
  176. Modules.Add(loadedModule);
  177. return true;
  178. }
  179. }
  180. }
  181. return false;
  182. }
  183. public void UnloadModules()
  184. {
  185. List<Module> moduleList = Modules;
  186. for (int i = 0; i < moduleList.Count; i++)
  187. {
  188. UnloadModule(moduleList[i].Name);
  189. }
  190. }
  191. public bool UnloadModule(string moduleName)
  192. {
  193. Module module = Modules.Find(mod => mod.Name.ToLower() == moduleName.ToLower());
  194. if (module != null)
  195. {
  196. module.Loaded = false;
  197. Modules.Remove(module);
  198. return true;
  199. }
  200. return false;
  201. }
  202. public bool CheckChannelAccess(string channel, string nickname, AccessType access)
  203. {
  204. Channel foundChannel = IRC.Channels.Find(chan => chan.Name == channel);
  205. if (foundChannel != null)
  206. {
  207. Nick foundNick = foundChannel.Nicks.Find(nick => nick.Nickname == nickname);
  208. if (foundNick != null)
  209. {
  210. for (int i = 0; i < foundNick.Privileges.Count; i++)
  211. {
  212. switch (PrivilegeModeMapping[foundNick.Privileges[i]])
  213. {
  214. case AccessType.User:
  215. if (access == AccessType.User)
  216. {
  217. return true;
  218. }
  219. break;
  220. case AccessType.Voice:
  221. if (access == AccessType.User || access == AccessType.Voice)
  222. {
  223. return true;
  224. }
  225. break;
  226. case AccessType.HalfOperator:
  227. if (access == AccessType.User || access == AccessType.Voice || access == AccessType.HalfOperator)
  228. {
  229. return true;
  230. }
  231. break;
  232. case AccessType.Operator:
  233. if (access == AccessType.User || access == AccessType.Voice || access == AccessType.HalfOperator || access == AccessType.Operator)
  234. {
  235. return true;
  236. }
  237. break;
  238. case AccessType.SuperOperator:
  239. if (access == AccessType.User || access == AccessType.Voice || access == AccessType.HalfOperator || access == AccessType.Operator || access == AccessType.SuperOperator)
  240. {
  241. return true;
  242. }
  243. break;
  244. case AccessType.Founder:
  245. if (access == AccessType.User || access == AccessType.Voice || access == AccessType.HalfOperator || access == AccessType.Operator || access == AccessType.SuperOperator || access == AccessType.Founder)
  246. {
  247. return true;
  248. }
  249. break;
  250. case AccessType.Owner:
  251. return true;
  252. break;
  253. }
  254. }
  255. }
  256. }
  257. return false;
  258. }
  259. public bool CheckChannelAccess(string channel, string nickname, List<AccessType> access)
  260. {
  261. bool hasAccess = false;
  262. for (int i = 0; i < access.Count; i++)
  263. {
  264. hasAccess = CheckChannelAccess(channel, nickname, access[i]);
  265. if (hasAccess)
  266. {
  267. break;
  268. }
  269. }
  270. return hasAccess;
  271. }
  272. public void ExecuteCommand(string message, string location, MessageType type)
  273. {
  274. ParseCommandMessage(DateTime.Now, message, new Nick { Nickname = IRC.Nickname }, location, type);
  275. }
  276. private void HandleJoinEvent(object sender, JoinChannelInfo info)
  277. {
  278. if (info.Nick.Nickname == IRC.Nickname)
  279. {
  280. if (!ServerConfig.Channels.Exists(chan => chan.Name == info.Channel))
  281. {
  282. ChannelConfig chanConfig = new ChannelConfig();
  283. chanConfig.Name = info.Channel;
  284. ServerConfig.Channels.Add(chanConfig);
  285. ServerConfig.Save();
  286. }
  287. }
  288. }
  289. private void HandleKickEvent(object sender, KickInfo info)
  290. {
  291. if (info.KickedNick.Nickname == IRC.Nickname)
  292. {
  293. ServerConfig.Channels.RemoveAll(chan => chan.Name == info.Channel);
  294. ServerConfig.Save();
  295. }
  296. }
  297. private void HandleChannelModeChangeEvent(object sender, ChannelModeChangeInfo e)
  298. {
  299. ChannelConfig channel = ServerConfig.Channels.Find(chan => chan.Name == e.Channel);
  300. if (channel != null)
  301. {
  302. foreach (ChannelModeInfo mode in e.Modes)
  303. {
  304. switch (mode.Mode)
  305. {
  306. case ChannelMode.k:
  307. if (mode.Set)
  308. {
  309. channel.Key = mode.Parameter;
  310. }
  311. else
  312. {
  313. channel.Key = string.Empty;
  314. }
  315. ServerConfig.Save();
  316. break;
  317. }
  318. }
  319. }
  320. }
  321. private void HandleReplyEvent(object sender, IReply e)
  322. {
  323. if (e.GetType() == typeof(ServerReplyMessage))
  324. {
  325. ServerReplyMessage reply = (ServerReplyMessage)e;
  326. switch (reply.ReplyCode)
  327. {
  328. case IRCReplyCode.RPL_WELCOME:
  329. // If the reply is Welcome, that means we are fully connected to the server.
  330. LoggedIn = true;
  331. if (!GhostSent && IRC.Nickname != ServerConfig.Nicknames[CurNickChoice])
  332. {
  333. IRC.SendPrivateMessage("NickServ", string.Format("GHOST {0} {1}", ServerConfig.Nicknames[CurNickChoice], ServerConfig.Password));
  334. Thread.Sleep(1000);
  335. IRC.SendNick(ServerConfig.Nicknames[CurNickChoice]);
  336. GhostSent = true;
  337. }
  338. // Identify to NickServ if need be
  339. IRC.SendPrivateMessage("NickServ", string.Format("IDENTIFY {0}", ServerConfig.Password));
  340. // Join all required channels
  341. // Delay joining channels for configured time
  342. Thread.Sleep(ServerConfig.JoinDelay);
  343. foreach (ChannelConfig channel in ServerConfig.Channels)
  344. {
  345. IRC.SendJoin(channel.Name, channel.Key);
  346. }
  347. break;
  348. }
  349. }
  350. else if (e.GetType() == typeof(ServerErrorMessage))
  351. {
  352. ServerErrorMessage error = (ServerErrorMessage) e;
  353. switch (error.ErrorCode)
  354. {
  355. case IRCErrorCode.ERR_NOTREGISTERED:
  356. if (ServerConfig.AutoRegister && ServerConfig.Password != string.Empty && ServerConfig.Email != string.Empty)
  357. {
  358. IRC.SendPrivateMessage("NickServ", string.Format("REGISTER {0} {1}", ServerConfig.Password, ServerConfig.Email));
  359. }
  360. break;
  361. case IRCErrorCode.ERR_NICKNAMEINUSE:
  362. if (LoggedIn == false)
  363. {
  364. string nick = string.Empty;
  365. if (IRC.Nickname == ServerConfig.Nicknames[CurNickChoice] && ServerConfig.Nicknames.Count > CurNickChoice + 1)
  366. {
  367. CurNickChoice++;
  368. nick = ServerConfig.Nicknames[CurNickChoice];
  369. }
  370. else
  371. {
  372. Random rand = new Random();
  373. nick = string.Format("{0}_{1}", ServerConfig.Nicknames.First(), rand.Next(100000).ToString());
  374. }
  375. IRC.Login(ServerConfig.Name, new Nick()
  376. {
  377. Nickname = nick,
  378. Host = Dns.GetHostName(),
  379. Realname = ServerConfig.Realname,
  380. Username = ServerConfig.Username
  381. });
  382. }
  383. break;
  384. }
  385. }
  386. }
  387. private void HandleChannelMessageReceivedEvent(object sender, ChannelMessage e)
  388. {
  389. // The message was a command
  390. if (e.Message.StartsWith(ServerConfig.CommandPrefix))
  391. {
  392. if (!ServerConfig.ChannelBlacklist.Contains(e.Channel)
  393. && !ServerConfig.NickBlacklist.Contains(e.Sender.Nickname)
  394. )
  395. {
  396. ParseCommandMessage(e.TimeStamp, e.Message, e.Sender, e.Channel, MessageType.Channel);
  397. }
  398. }
  399. }
  400. private void HandlePrivateMessageReceivedEvent(object sender, PrivateMessage e)
  401. {
  402. // The message was a command
  403. if (e.Message.StartsWith(ServerConfig.CommandPrefix))
  404. {
  405. if (!ServerConfig.NickBlacklist.Contains(e.Sender.Nickname))
  406. {
  407. ParseCommandMessage(e.TimeStamp, e.Message, e.Sender, e.Sender.Nickname, MessageType.Query);
  408. }
  409. }
  410. }
  411. private void HandlePrivateNoticeReceivedEvent(object sender, PrivateNotice e)
  412. {
  413. // The notice was a command
  414. if (e.Message.StartsWith(ServerConfig.CommandPrefix))
  415. {
  416. if (!ServerConfig.NickBlacklist.Contains(e.Sender.Nickname))
  417. {
  418. ParseCommandMessage(e.TimeStamp, e.Message, e.Sender, e.Sender.Nickname, MessageType.Notice);
  419. }
  420. }
  421. }
  422. private void ParseCommandMessage(DateTime timestamp, string message, Nick sender, string location, MessageType messageType)
  423. {
  424. // Extract command and arguments
  425. string[] msgArgs = message.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
  426. string command = msgArgs[0].Remove(0, ServerConfig.CommandPrefix.Length);
  427. List<string> argsOnly = msgArgs.ToList();
  428. argsOnly.RemoveAt(0);
  429. // Find the module that contains the command
  430. Module module = Modules.Find(mod => mod.Commands.Exists(c => c.Triggers.Contains(command)) && mod.Loaded);
  431. if (module != null)
  432. {
  433. // Find the command
  434. Command cmd = module.Commands.Find(c => c.Triggers.Contains(command));
  435. if (cmd != null)
  436. {
  437. CommandMessage newCommand = new CommandMessage();
  438. newCommand.Nick.Copy(sender);
  439. bool nickFound = false;
  440. IRC.Channels.ForEach(channel => channel.Nicks.ForEach(nick =>
  441. {
  442. if (nick.Nickname == newCommand.Nick.Nickname)
  443. {
  444. nickFound = true;
  445. newCommand.Nick.AddModes(nick.Modes);
  446. newCommand.Nick.AddPrivileges(nick.Privileges);
  447. }
  448. }));
  449. // Nickname has not been found, so need to run a query for nick's modes
  450. if (!nickFound)
  451. {
  452. string whoStyle = string.Format(@"[^\s]+\s[^\s]+\s[^\s]+\s[^\s]+\s({0})\s(?<Modes>[^\s]+)\s:[\d]\s(.+)", newCommand.Nick.Nickname);
  453. Regex whoRegex = new Regex(whoStyle);
  454. IRC.SendWho(newCommand.Nick.Nickname);
  455. ServerReplyMessage whoReply = IRC.Message.GetServerReply(IRCReplyCode.RPL_WHOREPLY, whoStyle);
  456. if (whoReply.ReplyCode != 0)
  457. {
  458. Match whoMatch = whoRegex.Match(whoReply.Message);
  459. List<UserModeInfo> modeInfoList = IRC.ParseUserModeString(whoMatch.Groups["Modes"].ToString());
  460. modeInfoList.ForEach(info =>
  461. {
  462. if (info.Set)
  463. {
  464. newCommand.Nick.AddMode(info.Mode);
  465. }
  466. });
  467. }
  468. }
  469. newCommand.TimeStamp = timestamp;
  470. newCommand.Location = location;
  471. newCommand.MessageType = messageType;
  472. newCommand.Command = command;
  473. // Check arguments against required arguments
  474. List<string> usedArgs = new List<string>();
  475. if (argsOnly.Any())
  476. {
  477. usedArgs.AddRange(argsOnly.First().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList());
  478. }
  479. List<CommandArgument> validArguments = cmd.GetValidArguments(usedArgs, messageType);
  480. if (argsOnly.Count > 0)
  481. {
  482. string[] argSplit = argsOnly.First().Split(new[] { ' ' }, validArguments.Count, StringSplitOptions.RemoveEmptyEntries);
  483. for (int i = 0; i < validArguments.Count && i <= argSplit.GetUpperBound(0); i++)
  484. {
  485. newCommand.Arguments.Add(validArguments[i].Name, argSplit[i]);
  486. }
  487. }
  488. bool invalidArgs = false;
  489. for (int i = 0; i < newCommand.Arguments.Count; i++)
  490. {
  491. if (validArguments[i].AllowedValues.Count > 0)
  492. {
  493. // Check to see if any of the arguments are invalid
  494. string argVal = newCommand.Arguments[validArguments[i].Name];
  495. if (!validArguments[i].AllowedValues.Exists(val => val.ToLower() == argVal.ToLower()))
  496. {
  497. invalidArgs = true;
  498. string argHelp = string.Format(" \u0002{0}\u0002", string.Join(" ", validArguments.Select(arg =>
  499. {
  500. string argString = string.Empty;
  501. if (arg.DependentArguments.Count > 0)
  502. {
  503. argString = "(";
  504. }
  505. if (arg.Required)
  506. {
  507. argString += "\u001F" + arg.Name + "\u001F";
  508. }
  509. else
  510. {
  511. argString += "[\u001F" + arg.Name + "\u001F]";
  512. }
  513. if (arg.DependentArguments.Count > 0)
  514. {
  515. argString += string.Format("\u0002:When {0}\u0002)", string.Join(" or ", arg.DependentArguments.Select(dep => { return string.Format("\u0002\u001F{0}\u001F\u0002=\u0002{1}\u0002", dep.Name, string.Join(",", dep.Values)); })));
  516. }
  517. return argString;
  518. })));
  519. string invalidMessage = string.Format("Invalid value for \u0002{0}\u0002 in \u0002{1}{2}\u0002{3}. Valid options are \u0002{4}\u0002.", validArguments[i].Name, ServerConfig.CommandPrefix, command, argHelp, string.Join(", ", validArguments[i].AllowedValues));
  520. module.SendResponse(messageType, location, sender.Nickname, invalidMessage);
  521. break;
  522. }
  523. }
  524. }
  525. if (validArguments.FindAll(arg => arg.Required).Count <= newCommand.Arguments.Count)
  526. {
  527. if (!invalidArgs)
  528. {
  529. if (CommandReceivedEvent != null)
  530. {
  531. CommandReceivedEvent(newCommand);
  532. }
  533. }
  534. }
  535. else
  536. {
  537. string argHelp = string.Format(" \u0002{0}\u0002", string.Join(" ", validArguments.Select(arg =>
  538. {
  539. string argString = string.Empty;
  540. if (arg.DependentArguments.Count > 0)
  541. {
  542. argString = "(";
  543. }
  544. if (arg.Required)
  545. {
  546. argString += "\u001F" + arg.Name + "\u001F";
  547. }
  548. else
  549. {
  550. argString += "[\u001F" + arg.Name + "\u001F]";
  551. }
  552. if (arg.DependentArguments.Count > 0)
  553. {
  554. argString += string.Format("\u0002:When {0}\u0002)", string.Join(" or ", arg.DependentArguments.Select(dep => { return string.Format("\u0002\u001F{0}\u001F\u0002=\u0002{1}\u0002", dep.Name, string.Join(",", dep.Values)); })));
  555. }
  556. return argString;
  557. })));
  558. string missingArgument = string.Format("Missing a required argument for \u0002{0}{1}\u0002{2}. The required arguments are \u0002{3}\u0002.", ServerConfig.CommandPrefix, command, argHelp, string.Join(", ", validArguments.Where(arg => arg.Required).Select(arg => arg.Name)));
  559. module.SendResponse(messageType, location, sender.Nickname, missingArgument);
  560. }
  561. }
  562. }
  563. }
  564. }
  565. }