Advertisement
Guest User

imap

a guest
Mar 18th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. # Copyright (C) 2012 Johnny Vestergaard <jkv@unixcluster.dk>
  2. #
  3. # This program is free software: you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation, either version 3 of the License, or
  6. # (at your option) any later version.
  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. # You should have received a copy of the GNU General Public License
  14. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15.  
  16. import logging
  17. import socket
  18.  
  19. from heralding.capabilities.handlerbase import HandlerBase
  20.  
  21. logger = logging.getLogger(__name__)
  22.  
  23.  
  24. class Imap(HandlerBase):
  25.     max_tries = 10
  26.  
  27.     def __init__(self, options, ):
  28.         super(Imap, self).__init__(options)
  29.         Imap.max_tries = int(self.options['protocol_specific_data']['max_attempts'])
  30.  
  31.     def execute_capability(self, address, socket, session):
  32.         self._handle_session(session, socket)
  33.  
  34.     def _handle_session(self, session, gsocket):
  35.         fileobj = gsocket.makefile()
  36.  
  37.         self.send_message(session, gsocket, "* OK IMAP Server Ready")
  38.  
  39.         state = "Not Authenticated"
  40.         while state != '' and session.connected:
  41.             try:
  42.                 raw_msg = fileobj.readline()
  43.             except socket.error:
  44.                 session.end_session()
  45.                 break
  46.  
  47.             session.activity()
  48.             cmd_msg = raw_msg.rstrip().split(' ', 2)
  49.             if len(cmd_msg) == 0:
  50.                 continue
  51.             elif len(cmd_msg) == 1:
  52.                 tag = cmd_msg[0]
  53.                 self.send_message(session, gsocket, "* BAD invalid command")
  54.                 continue
  55.             elif len(cmd_msg) == 2:
  56.                 tag = cmd_msg[0]
  57.                 cmd = cmd_msg[1]
  58.                 msg = ''
  59.             else:
  60.                 tag = cmd_msg[0]
  61.                 cmd = cmd_msg[1]
  62.                 msg = cmd_msg[2]
  63.  
  64.             cmd = cmd.lower()
  65.             if cmd not in ['login', 'logout', 'noop', 'capability']:
  66.                 self.send_message(session, gsocket, "* BAD invalid command")
  67.             else:
  68.                 func_to_call = getattr(self, 'cmd_{0}'.format(cmd), None)
  69.                 return_value = func_to_call(session, gsocket, tag, msg)
  70.                 if state == 'Not Authenticated' or cmd == 'logout':
  71.                     state = return_value
  72.  
  73.         session.end_session()
  74.  
  75.     def cmd_login(self, session, gsocket, tag, msg):
  76.         user_cred= msg.split(' ', 1)
  77.         if len(user_cred) == 1:
  78.             user = user_cred[0].strip('\"')
  79.             password = ''
  80.         else:
  81.             user = user_cred[0].strip('\"')
  82.             password = user_cred[1].strip('\"')
  83.         session.add_auth_attempt('plaintext', username=user, password=password)
  84.         self.send_message(session, gsocket, tag + ' NO Authentication failed')
  85.         return 'Not Authenticated'
  86.  
  87.     def cmd_noop(self, session, gsocket, tag, msg):
  88.         self.send_message(session, gsocket, tag + ' OK NOOP COMPLETED')
  89.  
  90.     def cmd_logout(self, session, gsocket, tag, msg):
  91.         self.send_message(session, gsocket, "* BYE IMAP Server logging out")
  92.         self.send_message(session, gsocket, tag + ' OK LOGOUT COMPLETED')
  93.         return ''
  94.  
  95.     def cmd_capability(self, session, gsocket, tag, msg):
  96.         self.send_message(session, gsocket, '* CAPABILITY IMAP AUTH=LOGIN')
  97.         self.send_message(session, gsocket, tag + ' OK CAPABILITY COMPLETED')
  98.         return 'Not Authenticated'
  99.  
  100.     def send_message(self, session, gsocket, msg):
  101.         try:
  102.             gsocket.sendall(msg + "\n")
  103.         except socket.error, (value, exceptionMessage):
  104.             session.end_session()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement