#!/usr/bin/python
# -*- coding: UTF-8 -*-
# vim: expandtab sw=4 ts=4 sts=4:
'''
Stores message on IMAP.
'''
__author__ = 'Michal Čihař'
__email__ = 'michal@cihar.com'
__license__ = '''
Copyright (c) 2003 - 2014 Michal Čihař

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 IMAPUtils
import sys

class StoreIMAP(IMAPUtils.IMAPUtil):
    '''
    Class for creating IMAP folders.
    '''

    def store(self, folder):
        '''
        Stores message in folder.
        '''
        res = self._imap.select(folder)
        if res[0] != 'OK':
            sys.stderr.write("select: %s\n" % str(res))
            sys.exit(2)

        res = self._imap.append(sys.argv[1], 
                None,
                None, 
                sys.stdin.read())
        if res[0] != 'OK':
            sys.stderr.write("append: %s\n" % str(res))
            sys.exit(3)

    def run(self, folder):
        '''
        Executes all actions.
        '''
        self.login()
        self.store(folder)

if __name__ == '__main__':
    try:
        StoreIMAP().run(sys.argv[1])
    except IndexError:
        print 'Usage: store-image folder'
