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.

Logging.cs 13KB

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