Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Copyright (C) 2012 Johnny Vestergaard <jkv@unixcluster.dk>
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- import logging
- import socket
- from heralding.capabilities.handlerbase import HandlerBase
- logger = logging.getLogger(__name__)
- class Imap(HandlerBase):
- max_tries = 10
- def __init__(self, options, ):
- super(Imap, self).__init__(options)
- Imap.max_tries = int(self.options['protocol_specific_data']['max_attempts'])
- def execute_capability(self, address, socket, session):
- self._handle_session(session, socket)
- def _handle_session(self, session, gsocket):
- fileobj = gsocket.makefile()
- self.send_message(session, gsocket, "* OK IMAP Server Ready")
- state = "Not Authenticated"
- while state != '' and session.connected:
- try:
- raw_msg = fileobj.readline()
- except socket.error:
- session.end_session()
- break
- session.activity()
- cmd_msg = raw_msg.rstrip().split(' ', 2)
- if len(cmd_msg) == 0:
- continue
- elif len(cmd_msg) == 1:
- tag = cmd_msg[0]
- self.send_message(session, gsocket, "* BAD invalid command")
- continue
- elif len(cmd_msg) == 2:
- tag = cmd_msg[0]
- cmd = cmd_msg[1]
- msg = ''
- else:
- tag = cmd_msg[0]
- cmd = cmd_msg[1]
- msg = cmd_msg[2]
- cmd = cmd.lower()
- if cmd not in ['login', 'logout', 'noop', 'capability']:
- self.send_message(session, gsocket, "* BAD invalid command")
- else:
- func_to_call = getattr(self, 'cmd_{0}'.format(cmd), None)
- return_value = func_to_call(session, gsocket, tag, msg)
- if state == 'Not Authenticated' or cmd == 'logout':
- state = return_value
- session.end_session()
- def cmd_login(self, session, gsocket, tag, msg):
- user_cred= msg.split(' ', 1)
- if len(user_cred) == 1:
- user = user_cred[0].strip('\"')
- password = ''
- else:
- user = user_cred[0].strip('\"')
- password = user_cred[1].strip('\"')
- session.add_auth_attempt('plaintext', username=user, password=password)
- self.send_message(session, gsocket, tag + ' NO Authentication failed')
- return 'Not Authenticated'
- def cmd_noop(self, session, gsocket, tag, msg):
- self.send_message(session, gsocket, tag + ' OK NOOP COMPLETED')
- def cmd_logout(self, session, gsocket, tag, msg):
- self.send_message(session, gsocket, "* BYE IMAP Server logging out")
- self.send_message(session, gsocket, tag + ' OK LOGOUT COMPLETED')
- return ''
- def cmd_capability(self, session, gsocket, tag, msg):
- self.send_message(session, gsocket, '* CAPABILITY IMAP AUTH=LOGIN')
- self.send_message(session, gsocket, tag + ' OK CAPABILITY COMPLETED')
- return 'Not Authenticated'
- def send_message(self, session, gsocket, msg):
- try:
- gsocket.sendall(msg + "\n")
- except socket.error, (value, exceptionMessage):
- session.end_session()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement