# openioc_10_to_11.py
#
# Copyright 2013 Mandiant Corporation.  
# Licensed under the Apache 2.0 license.  Developed for Mandiant by William 
# Gibb.
#
# Mandiant licenses this file to you under the Apache License, Version
# 2.0 (the "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at:
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.  See the License for the specific language governing
# permissions and limitations under the License.
#
# Allows for the upgrade of OpenIOC 1.0 IOCs to OpenIOC 1.1 format
#

import glob
import logging
import os
import optparse
import sys

# logging config
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s  [%(filename)s:%(funcName)s]')

from lxml import etree as et

from ioc_writer import ioc_api, xmlutils


class IOCParseError(Exception):
    pass

class iocmanager():
    def __init__(self):
        self.iocs = {}
        self.iocs_11 = {}
        self.ioc_xml = {}
        
    def __len__(self):
        return len(self.iocs)
    
    def insert(self, filename):
        '''
        insert(filedir)

        import [all] IOC(s) from a file or directory
        '''
        errors = []
        if os.path.isfile(filename):
            logging.info('loading IOC from: %s' % (filename))
            if not self.parse(filename):
                logging.warning('Failed to prase [%s]' % str(filename))