PageRenderTime 71ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/ChannelLogger/plugin.py

https://bitbucket.org/Freso/supybot-code
Python | 267 lines | 205 code | 25 blank | 37 comment | 54 complexity | 9b4a0645b94a07f7a545b2bd7a625ec2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. ###
  2. # Copyright (c) 2002-2004, Jeremiah Fincher
  3. # Copyright (c) 2009-2010, James McCoy
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. #
  9. # * Redistributions of source code must retain the above copyright notice,
  10. # this list of conditions, and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright notice,
  12. # this list of conditions, and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # * Neither the name of the author of this software nor the name of
  15. # contributors to this software may be used to endorse or promote products
  16. # derived from this software without specific prior written consent.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. ###
  30. import os
  31. import time
  32. from cStringIO import StringIO
  33. import supybot.conf as conf
  34. import supybot.world as world
  35. import supybot.irclib as irclib
  36. import supybot.ircmsgs as ircmsgs
  37. import supybot.ircutils as ircutils
  38. import supybot.registry as registry
  39. import supybot.callbacks as callbacks
  40. class FakeLog(object):
  41. def flush(self):
  42. return
  43. def close(self):
  44. return
  45. def write(self, s):
  46. return
  47. class ChannelLogger(callbacks.Plugin):
  48. noIgnore = True
  49. def __init__(self, irc):
  50. self.__parent = super(ChannelLogger, self)
  51. self.__parent.__init__(irc)
  52. self.lastMsgs = {}
  53. self.lastStates = {}
  54. self.logs = {}
  55. self.flusher = self.flush
  56. world.flushers.append(self.flusher)
  57. def die(self):
  58. for log in self._logs():
  59. log.close()
  60. world.flushers = [x for x in world.flushers if x is not self.flusher]
  61. def __call__(self, irc, msg):
  62. try:
  63. # I don't know why I put this in, but it doesn't work, because it
  64. # doesn't call doNick or doQuit.
  65. # if msg.args and irc.isChannel(msg.args[0]):
  66. self.__parent.__call__(irc, msg)
  67. if irc in self.lastMsgs:
  68. if irc not in self.lastStates:
  69. self.lastStates[irc] = irc.state.copy()
  70. self.lastStates[irc].addMsg(irc, self.lastMsgs[irc])
  71. finally:
  72. # We must make sure this always gets updated.
  73. self.lastMsgs[irc] = msg
  74. def reset(self):
  75. for log in self._logs():
  76. log.close()
  77. self.logs.clear()
  78. self.lastMsgs.clear()
  79. self.lastStates.clear()
  80. def _logs(self):
  81. for logs in self.logs.itervalues():
  82. for log in logs.itervalues():
  83. yield log
  84. def flush(self):
  85. self.checkLogNames()
  86. for log in self._logs():
  87. try:
  88. log.flush()
  89. except ValueError, e:
  90. if e.args[0] != 'I/O operation on a closed file':
  91. self.log.exception('Odd exception:')
  92. def logNameTimestamp(self, channel):
  93. format = self.registryValue('filenameTimestamp', channel)
  94. return time.strftime(format)
  95. def getLogName(self, channel):
  96. if self.registryValue('rotateLogs', channel):
  97. return '%s.%s.log' % (channel, self.logNameTimestamp(channel))
  98. else:
  99. return '%s.log' % channel
  100. def getLogDir(self, irc, channel):
  101. logDir = conf.supybot.directories.log.dirize(self.name())
  102. if self.registryValue('directories'):
  103. if self.registryValue('directories.network'):
  104. logDir = os.path.join(logDir, irc.network)
  105. if self.registryValue('directories.channel'):
  106. logDir = os.path.join(logDir, channel)
  107. if self.registryValue('directories.timestamp'):
  108. format = self.registryValue('directories.timestamp.format')
  109. timeDir =time.strftime(format)
  110. logDir = os.path.join(logDir, timeDir)
  111. if not os.path.exists(logDir):
  112. os.makedirs(logDir)
  113. return logDir
  114. def checkLogNames(self):
  115. for (irc, logs) in self.logs.items():
  116. for (channel, log) in logs.items():
  117. if self.registryValue('rotateLogs', channel):
  118. name = self.getLogName(channel)
  119. if name != log.name:
  120. log.close()
  121. del logs[channel]
  122. def getLog(self, irc, channel):
  123. self.checkLogNames()
  124. try:
  125. logs = self.logs[irc]
  126. except KeyError:
  127. logs = ircutils.IrcDict()
  128. self.logs[irc] = logs
  129. if channel in logs:
  130. return logs[channel]
  131. else:
  132. try:
  133. name = self.getLogName(channel)
  134. logDir = self.getLogDir(irc, channel)
  135. log = file(os.path.join(logDir, name), 'a')
  136. logs[channel] = log
  137. return log
  138. except IOError:
  139. self.log.exception('Error opening log:')
  140. return FakeLog()
  141. def timestamp(self, log):
  142. format = conf.supybot.log.timestampFormat()
  143. if format:
  144. log.write(time.strftime(format))
  145. log.write(' ')
  146. def normalizeChannel(self, irc, channel):
  147. return ircutils.toLower(channel)
  148. def doLog(self, irc, channel, s, *args):
  149. if not self.registryValue('enable', channel):
  150. return
  151. s = format(s, *args)
  152. channel = self.normalizeChannel(irc, channel)
  153. log = self.getLog(irc, channel)
  154. if self.registryValue('timestamp', channel):
  155. self.timestamp(log)
  156. if self.registryValue('stripFormatting', channel):
  157. s = ircutils.stripFormatting(s)
  158. log.write(s)
  159. if self.registryValue('flushImmediately'):
  160. log.flush()
  161. def doPrivmsg(self, irc, msg):
  162. (recipients, text) = msg.args
  163. for channel in recipients.split(','):
  164. if irc.isChannel(channel):
  165. noLogPrefix = self.registryValue('noLogPrefix', channel)
  166. if noLogPrefix and text.startswith(noLogPrefix):
  167. text = '-= THIS MESSAGE NOT LOGGED =-'
  168. nick = msg.nick or irc.nick
  169. if ircmsgs.isAction(msg):
  170. self.doLog(irc, channel,
  171. '* %s %s\n', nick, ircmsgs.unAction(msg))
  172. else:
  173. self.doLog(irc, channel, '<%s> %s\n', nick, text)
  174. def doNotice(self, irc, msg):
  175. (recipients, text) = msg.args
  176. for channel in recipients.split(','):
  177. if irc.isChannel(channel):
  178. self.doLog(irc, channel, '-%s- %s\n', msg.nick, text)
  179. def doNick(self, irc, msg):
  180. oldNick = msg.nick
  181. newNick = msg.args[0]
  182. for (channel, c) in irc.state.channels.iteritems():
  183. if newNick in c.users:
  184. self.doLog(irc, channel,
  185. '*** %s is now known as %s\n', oldNick, newNick)
  186. def doJoin(self, irc, msg):
  187. for channel in msg.args[0].split(','):
  188. self.doLog(irc, channel,
  189. '*** %s <%s> has joined %s\n',
  190. msg.nick, msg.prefix, channel)
  191. def doKick(self, irc, msg):
  192. if len(msg.args) == 3:
  193. (channel, target, kickmsg) = msg.args
  194. else:
  195. (channel, target) = msg.args
  196. kickmsg = ''
  197. if kickmsg:
  198. self.doLog(irc, channel,
  199. '*** %s was kicked by %s (%s)\n',
  200. target, msg.nick, kickmsg)
  201. else:
  202. self.doLog(irc, channel,
  203. '*** %s was kicked by %s\n', target, msg.nick)
  204. def doPart(self, irc, msg):
  205. for channel in msg.args[0].split(','):
  206. self.doLog(irc, channel,
  207. '*** %s <%s> has left %s\n',
  208. msg.nick, msg.prefix, channel)
  209. def doMode(self, irc, msg):
  210. channel = msg.args[0]
  211. if irc.isChannel(channel) and msg.args[1:]:
  212. self.doLog(irc, channel,
  213. '*** %s sets mode: %s %s\n',
  214. msg.nick or msg.prefix, msg.args[1],
  215. ' '.join(msg.args[2:]))
  216. def doTopic(self, irc, msg):
  217. if len(msg.args) == 1:
  218. return # It's an empty TOPIC just to get the current topic.
  219. channel = msg.args[0]
  220. self.doLog(irc, channel,
  221. '*** %s changes topic to "%s"\n', msg.nick, msg.args[1])
  222. def doQuit(self, irc, msg):
  223. if not isinstance(irc, irclib.Irc):
  224. irc = irc.getRealIrc()
  225. for (channel, chan) in self.lastStates[irc].channels.iteritems():
  226. if msg.nick in chan.users:
  227. self.doLog(irc, channel,
  228. '*** %s <%s> has quit IRC\n',
  229. msg.nick, msg.prefix)
  230. def outFilter(self, irc, msg):
  231. # Gotta catch my own messages *somehow* :)
  232. # Let's try this little trick...
  233. if msg.command in ('PRIVMSG', 'NOTICE'):
  234. # Other messages should be sent back to us.
  235. m = ircmsgs.IrcMsg(msg=msg, prefix=irc.prefix)
  236. self(irc, m)
  237. return msg
  238. Class = ChannelLogger
  239. # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: