The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Logging.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.AccessControl;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using Combot.Databases;
  9. using Combot.IRCServices.Messaging;
  10. namespace Combot.Modules.Plugins
  11. {
  12. public class Logging : Module
  13. {
  14. private const string SERVERLOGNAME = "--server--";
  15. private const string LOGFILENAME = "chat";
  16. private const string LOGFILEEXT = ".log";
  17. private static ReaderWriterLockSlim logLock;
  18. public override void Initialize()
  19. {
  20. InitializeTable();
  21. logLock = new ReaderWriterLockSlim();
  22. Bot.IRC.ConnectEvent += AddServer;
  23. Bot.IRC.Message.CTCPMessageReceivedEvent += LogCTCPMessage;
  24. Bot.IRC.Message.CTCPNoticeReceivedEvent += LogCTCPNotice;
  25. Bot.IRC.Message.ChannelMessageReceivedEvent += LogChannelMessage;
  26. Bot.IRC.Message.PrivateMessageReceivedEvent += LogPrivateMessage;
  27. Bot.IRC.Message.ChannelNoticeReceivedEvent += LogChannelNotice;
  28. Bot.IRC.Message.PrivateNoticeReceivedEvent += LogPrivateNotice;
  29. Bot.IRC.Message.JoinChannelEvent += LogChannelJoin;
  30. Bot.IRC.Message.InviteChannelEvent += LogChannelInvite;
  31. Bot.IRC.Message.PartChannelEvent += LogChannelPart;
  32. Bot.IRC.Message.KickEvent += LogChannelKick;
  33. Bot.IRC.Message.QuitEvent += LogQuit;
  34. Bot.IRC.Message.NickChangeEvent += LogNickChange;
  35. }
  36. private void InitializeTable()
  37. {
  38. string sqlPath = Path.Combine(Directory.GetCurrentDirectory(), ConfigPath, "CreateTable.sql");
  39. if (File.Exists(sqlPath))
  40. {
  41. string query = File.ReadAllText(sqlPath);
  42. Bot.Database.Execute(query);
  43. }
  44. }
  45. private void LogChannelMessage(object sender, ChannelMessage message)
  46. {
  47. if (!ChannelBlacklist.Contains(message.Channel)
  48. && !NickBlacklist.Contains(message.Sender.Nickname))
  49. {
  50. AddChannel(message.Channel);
  51. AddNick(message.Sender);
  52. string query = "INSERT INTO `channelmessages` SET " +
  53. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  54. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  55. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  56. "`message` = {5}, " +
  57. "`date_added` = {6}";
  58. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, message.Channel, Bot.ServerConfig.Name, message.Sender.Nickname, message.Message, message.TimeStamp });
  59. }
  60. LogToFile(message.Channel, message.TimeStamp, string.Format("<{0}> {1}", message.Sender.Nickname, message.Message));
  61. }
  62. private void LogPrivateMessage(object sender, PrivateMessage message)
  63. {
  64. if (!NickBlacklist.Contains(message.Sender.Nickname))
  65. {
  66. AddNick(message.Sender);
  67. string query = "INSERT INTO `privatemessages` SET " +
  68. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  69. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
  70. "`message` = {3}, " +
  71. "`date_added` = {4}";
  72. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, message.Sender.Nickname, message.Message, message.TimeStamp });
  73. }
  74. LogToFile(message.Sender.Nickname, message.TimeStamp, message.Message);
  75. }
  76. private void LogChannelNotice(object sender, ChannelNotice notice)
  77. {
  78. LogToFile(notice.Channel, notice.TimeStamp, string.Format("<{0}> {1}", notice.Sender.Nickname, notice.Message));
  79. }
  80. private void LogPrivateNotice(object sender, PrivateNotice notice)
  81. {
  82. LogToFile(SERVERLOGNAME, notice.TimeStamp, string.Format("<{0}> {1}", notice.Sender.Nickname, notice.Message));
  83. }
  84. private void LogCTCPMessage(object sender, CTCPMessage message)
  85. {
  86. LogToFile(SERVERLOGNAME, message.TimeStamp, string.Format("<{0}> CTCP {1} {2}", message.Sender.Nickname, message.Command, message.Arguments));
  87. }
  88. private void LogCTCPNotice(object sender, CTCPMessage notice)
  89. {
  90. LogToFile(SERVERLOGNAME, notice.TimeStamp, string.Format("<{0}> CTCP {1} {2}", notice.Sender.Nickname, notice.Command, notice.Arguments));
  91. }
  92. private void LogChannelJoin(object sender, JoinChannelInfo info)
  93. {
  94. if (!ChannelBlacklist.Contains(info.Channel)
  95. && !NickBlacklist.Contains(info.Nick.Nickname))
  96. {
  97. AddChannel(info.Channel);
  98. AddNick(info.Nick);
  99. string query = "INSERT INTO `channeljoins` SET " +
  100. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  101. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  102. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nicks`.`nickname` = {4}), " +
  103. "`date_added` = {5}";
  104. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, info.Channel, Bot.ServerConfig.Name, info.Nick.Nickname, info.TimeStamp });
  105. }
  106. LogToFile(info.Channel, info.TimeStamp, string.Format("{0} has joined the channel.", info.Nick.Nickname));
  107. }
  108. private void LogChannelInvite(object sender, InviteChannelInfo info)
  109. {
  110. if (!ChannelBlacklist.Contains(info.Channel)
  111. && !NickBlacklist.Contains(info.Requester.Nickname))
  112. {
  113. AddChannel(info.Channel);
  114. AddNick(info.Requester);
  115. AddNick(info.Recipient);
  116. string query = "INSERT INTO `channelinvites` SET " +
  117. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  118. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  119. "`requester_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nicks`.`nickname` = {4}), " +
  120. "`recipient_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {5} && `nicks`.`nickname` = {6}), " +
  121. "`date_invited` = {7}";
  122. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, info.Channel, Bot.ServerConfig.Name, info.Requester.Nickname, Bot.ServerConfig.Name, info.Recipient.Nickname, info.TimeStamp });
  123. }
  124. LogToFile(info.Channel, info.TimeStamp, string.Format("{0} has invited {1} to the channel.", info.Requester.Nickname, info.Recipient.Nickname));
  125. }
  126. private void LogChannelPart(object sender, PartChannelInfo info)
  127. {
  128. if (!ChannelBlacklist.Contains(info.Channel)
  129. && !NickBlacklist.Contains(info.Nick.Nickname))
  130. {
  131. AddChannel(info.Channel);
  132. AddNick(info.Nick);
  133. string query = "INSERT INTO `channelparts` SET " +
  134. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  135. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  136. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  137. "`date_added` = {5}";
  138. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, info.Channel, Bot.ServerConfig.Name, info.Nick.Nickname, info.TimeStamp });
  139. }
  140. LogToFile(info.Channel, info.TimeStamp, string.Format("{0} has left the channel.", info.Nick.Nickname));
  141. }
  142. private void LogChannelKick(object sender, KickInfo info)
  143. {
  144. if (!ChannelBlacklist.Contains(info.Channel)
  145. && !NickBlacklist.Contains(info.KickedNick.Nickname))
  146. {
  147. AddChannel(info.Channel);
  148. AddNick(info.Nick);
  149. AddNick(info.KickedNick);
  150. string query = "INSERT INTO `channelkicks` SET " +
  151. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  152. "`channel_id` = (SELECT `channels`.`id` FROM `channels` INNER JOIN `servers` ON `servers`.`id` = `channels`.`server_id` WHERE `servers`.`name` = {1} && `channels`.`name` = {2}), " +
  153. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {3} && `nickname` = {4}), " +
  154. "`kicked_nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {5} && `nickname` = {6}), " +
  155. "`reason` = {7}, " +
  156. "`date_added` = {8}";
  157. Bot.Database.Execute(query, new object[] { Bot.ServerConfig.Name, Bot.ServerConfig.Name, info.Channel, Bot.ServerConfig.Name, info.Nick.Nickname, Bot.ServerConfig.Name, info.KickedNick.Nickname, info.Reason, info.TimeStamp });
  158. }
  159. LogToFile(info.Channel, info.TimeStamp, string.Format("{0} kicked {1} [{2}]", info.Nick.Nickname, info.KickedNick.Nickname, info.Reason));
  160. }
  161. private void LogQuit(object sender, QuitInfo info)
  162. {
  163. if (!NickBlacklist.Contains(info.Nick.Nickname))
  164. {
  165. AddNick(info.Nick);
  166. string query = "INSERT INTO `quits` SET " +
  167. "`server_id` = (SELECT `id` FROM `servers` WHERE `name` = {0}), " +
  168. "`nick_id` = (SELECT `nicks`.`id` FROM `nicks` INNER JOIN `servers` ON `servers`.`id` = `nicks`.`server_id` WHERE `servers`.`name` = {1} && `nickname` = {2}), " +
  169. "`message` = {3}, " +
  170. "`date_added` = {4}";
  171. Bot.Database.Execute(query, new object[] {Bot.ServerConfig.Name, Bot.ServerConfig.Name, info.Nick.Nickname, info.Message, info.TimeStamp});
  172. }
  173. LogToFile(SERVERLOGNAME, info.TimeStamp, string.Format("{0} has Quit.", info.Nick.Nickname));
  174. }
  175. private void LogNickChange(object sender, NickChangeInfo info)
  176. {
  177. if (!NickBlacklist.Contains(info.OldNick.Nickname) && !NickBlacklist.Contains(info.NewNick.Nickname))
  178. {
  179. AddNick(info.NewNick);
  180. }
  181. LogToFile(SERVERLOGNAME, info.TimeStamp, string.Format("{0} is now known as {1}", info.OldNick.Nickname, info.NewNick.Nickname));
  182. }
  183. private void LogToFile(string location, DateTime date, string log)
  184. {
  185. bool doLog = false;
  186. Boolean.TryParse(GetOptionValue("Log To File").ToString(), out doLog);
  187. if (doLog)
  188. {
  189. logLock.EnterWriteLock();
  190. string pattern = "[^a-zA-Z0-9-_.+#]"; //regex pattern
  191. string parsedLocation = Regex.Replace(location, pattern, "_");
  192. string logDir = Path.Combine(GetOptionValue("Log Path").ToString(), Bot.ServerConfig.Name, parsedLocation);
  193. if (!Directory.Exists(logDir))
  194. Directory.CreateDirectory(logDir);
  195. string logFile = Path.Combine(logDir, LOGFILENAME + LOGFILEEXT);
  196. // Check to see if we need to create a new log
  197. if (File.Exists(logFile))
  198. {
  199. TrimLogFile(logDir);
  200. }
  201. // Write the log to the main log file
  202. StreamWriter logWriter = File.AppendText(logFile);
  203. logWriter.WriteLine(string.Format("[{0}] {1}", date.ToString("G"), log));
  204. logWriter.Close();
  205. logLock.ExitWriteLock();
  206. }
  207. }
  208. private void TrimLogFile(string logDir)
  209. {
  210. string logFile = Path.Combine(logDir, LOGFILENAME + LOGFILEEXT);
  211. int maxSize = 0;
  212. Int32.TryParse(GetOptionValue("Max Log Size").ToString(), out maxSize);
  213. FileInfo file = new FileInfo(logFile);
  214. long fileSize = file.Length;
  215. if (fileSize > maxSize)
  216. {
  217. // The file is too large, we need to increment the file names of the log files
  218. string[] files = Directory.GetFiles(logDir);
  219. for (int i = files.GetUpperBound(0) - 1; i >= 0; i--)
  220. {
  221. string newFileName = LOGFILENAME + "_" + (i + 1) + LOGFILEEXT;
  222. string newFile = Path.Combine(logDir, newFileName);
  223. File.Move(files[i], newFile);
  224. }
  225. }
  226. }
  227. }
  228. }