Advertisement
Guest User

Untitled

a guest
May 9th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. ###
  2. # Copyright (c) 2008, Terence Simpson
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of version 2 of the GNU General Public License as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. ###
  14.  
  15. import supybot.utils as utils
  16. from supybot.commands import *
  17. import supybot.plugins as plugins
  18. import supybot.ircutils as ircutils
  19. import supybot.ircmsgs as ircmsgs
  20. import supybot.callbacks as callbacks
  21. import supybot.ircdb as ircdb
  22. import supybot.conf as conf
  23. import supybot.schedule as schedule
  24. import random, os, sys
  25. from cPickle import Unpickler, Pickler
  26. import lp
  27.  
  28. def checkCapab(msg, irc):
  29. try:
  30. user = ircdb.users.getUser(msg.prefix[:msg.prefix.find('!')])
  31. except:
  32. irc.error(conf.supybot.replies.incorrectAuthentication())
  33. return False
  34. try:
  35. if not user.capabilities.check('Admin'):
  36. irc.error(conf.supybot.replies.noCapability() % 'Admin')
  37. return False
  38. except KeyError:
  39. irc.error(conf.supybot.replies.noCapability() % 'Admin')
  40. return False
  41. return True
  42.  
  43.  
  44. class IRCLogin(callbacks.Plugin):
  45. """Use @login to login, @reloadusers to reload the user list and @updateusers to update the user database from
  46. launchpad"""
  47. threaded = True
  48.  
  49. def updateusers(self, irc, msg, args):
  50. """Takes no arguments
  51.  
  52. Update the user database from Launchpad"""
  53. def writePickle(uf, user2nick, nick2user):
  54. global self
  55. try:
  56. fd = open(uf, 'wb')
  57. except IOError, e:
  58. self.log.error("Could not write to %s (%s)" % (uf, e))
  59. return
  60. except Exception, e:
  61. self.log.error("Unknown error while opening %s for writing:\n%s" % (uf, e))
  62. return
  63. pi = Pickler(fd, 2)
  64. pi.dump((user2nick, nick2user))
  65.  
  66. if not checkCapab(msg, irc):
  67. return
  68. uf = self.registryValue('UserList')
  69. if not uf:
  70. self.log.info('no UserList config set')
  71. irc.error('No UserList config set')
  72. return
  73. if not os.path.exists(uf):
  74. self.log.info('Creating initial userlist')
  75. else:
  76. self.log.info('Updating userlist')
  77. irc.reply('Running...')
  78. user2nick = {}
  79. nick2user = {}
  80. #TODO: Make the team configurable, just needs a config entry
  81. users = lp.getUsers()
  82. for user in users:
  83. lpuser = lp.getIRCNick(user, False)
  84. if not lpuser:
  85. lpuser = [user]
  86. user2nick[user] = lpuser
  87. for nick in lpuser:
  88. nick2user[nick] = user
  89. writePickle(uf, user2nick, nick2user)
  90. self.loadUsers()
  91. irc.reply('updateusers run complete')
  92. updateusers = wrap(updateusers)
  93.  
  94. def loadUsers(self):
  95. uf = self.registryValue('UserList')
  96. if not uf or not os.path.exists(uf) or not os.access(uf, os.R_OK):
  97. self.log.info('Not loading non-existant userlist')
  98. return
  99. fd = open(uf, 'rb')
  100. up = Unpickler(fd)
  101. (user2nick, nick2user) = up.load()
  102. self.knownusers = [i.lower() for i in nick2user.keys()]
  103. allusers = [u.name.lower() for u in ircdb.users.itervalues()]
  104. to_add = [x for x in self.knownusers if x not in allusers]
  105. for a in to_add:
  106. self.log.info("Adding %s" % a)
  107. user = ircdb.users.newUser()
  108. user.name = a
  109. rp = ''
  110. chars = '''1234567890-=~!@#$%^&*()_qwertyuiop[]QWERTYUIOP{}}|asdfghjkl;ASDFGHJKL:zxcvbnm,./ZXCVBNM<>?'''
  111. for i in range(16):
  112. rp += chars[random.randint(1,len(chars))-1]
  113. user.setPassword(rp)
  114. bu = []
  115. for u in nick2user.keys():
  116. try:
  117. user = ircdb.users.getUser(u.lower())
  118. #Add bantracker capability to all users from launchpad team
  119. if not 'bantracker' in user.capabilities:
  120. user.addCapability('bantracker')
  121. except Exception, e:
  122. bu.append("%s (%s)" % (u, e))
  123. pass
  124. if bu:
  125. self.log.info("Could not add users %s" % bu)
  126.  
  127. def reloadusers(self, irc, msg, args):
  128. """Takes no arguments
  129.  
  130. Read the user database and add the users in it"""
  131. if not checkCapab(msg, irc):
  132. return
  133. self.loadUsers()
  134. irc.replySuccess()
  135. reloadusers = wrap(reloadusers)
  136.  
  137. def login(self, irc, msg, args):
  138. """takes no arguments
  139.  
  140. Allows users who are identified to NickServ to login without a password.
  141. """
  142. if not msg.tagged('identified'):
  143. irc.error('You are not identified')
  144. return
  145. try:
  146. user = ircdb.users.getUser(msg.prefix[:msg.prefix.find('!')].lower())
  147. except:
  148. self.loadUsers()
  149. try:
  150. user = ircdb.users.getUser(msg.prefix[:msg.prefix.find('!')].lower())
  151. except:
  152. irc.error(conf.supybot.replies.incorrectAuthentication())
  153. return
  154. user.addAuth(msg.prefix)
  155. ircdb.users.setUser(user, flush=False)
  156. irc.replySuccess()
  157. login = wrap(login)
  158.  
  159. def doPrivmsg(self, irc, msg):
  160. if not conf.supybot.defaultIgnore: # Only do this when defaultIgnore is set
  161. return
  162. if chr(1) in msg.args[1]:
  163. return
  164. to = msg.args[0]
  165. cmd = msg.args[1]
  166. try:
  167. user = ircdb.users.getUser(msg.prefix)
  168. if user.checkHostmask(msg.prefix):
  169. return
  170. except:
  171. pass
  172. if to.lower() == irc.nick.lower():
  173. if cmd != "login":
  174. return
  175. elif msg.args[1][0] in conf.supybot.reply.whenAddressedBy.chars():
  176. cmd = msg.args[1][1:]
  177. if cmd != "login":
  178. return
  179. else:
  180. return
  181. self._callCommand(["login"], irc, msg, [])
  182.  
  183. def do290(self, irc, msg):
  184. assert 'IDENTIFY-MSG' in msg.args[1]
  185. irc.getRealIrc()._Freenode_capabed = True
  186.  
  187. def do376(self, irc, msg):
  188. irc.queueMsg(ircmsgs.IrcMsg('CAPAB IDENTIFY-MSG'))
  189. def do422(self, irc, msg): # MOTD missing
  190. irc.queueMsg(ircmsgs.IrcMsg('CAPAB IDENTIFY-MSG'))
  191.  
  192. def inFilter(self, irc, msg):
  193. if getattr(irc,'_Freenode_capabed',None) and msg.command == 'PRIVMSG':
  194. first = msg.args[1][0]
  195. rest = msg.args[1][1:]
  196. msg.tag('identified', first == '+')
  197. msg = ircmsgs.privmsg(msg.args[0], rest, msg=msg)
  198. assert msg.receivedAt and msg.receivedOn and msg.receivedBy
  199. return msg
  200.  
  201. Class = IRCLogin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement