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

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