The easy to use and full featured Irc Bot everyone is talking about!
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Logging.cs 13KB

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