#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  king_phisher/utilities.py
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are
#  met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following disclaimer
#    in the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of the  nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

import os

def server_parse(server, default_port):
	server = server.split(':')
	host = server[0]
	if len(server) == 1:
		return (host, default_port)
	else:
		port = server[1]
		if not port:
			port = default_port
		else:
			port = int(port)
		return (host, port)

def which(program):
	is_exe = lambda fpath: (os.path.isfile(fpath) and os.access(fpath, os.X_OK))
	for path in os.environ["PATH"].split(os.pathsep):
		path = path.strip('"')
		exe_file = os.path.join(path, program)
		if is_exe(exe_file):
			return exe_file
	return None

@@ -1571,18 +1571,1005 @@
 %0Aimport 
-os
+collections%0Aimport functools%0Aimport os%0Aimport time%0A%0Aclass cache(object):%0A%09def __init__(self, timeout):%0A%09%09self.cache_timeout = timeout%0A%09%09self.__cache = %7B%7D%0A%0A%09def __call__(self, *args):%0A%09%09if not hasattr(self, '_target_function'):%0A%09%09%09self._target_function = args%5B0%5D%0A%09%09%09return self%0A%09%09self.clean_cache()%0A%09%09if not isinstance(args, collections.Hashable):%0A%09%09%09return self._target_function(*args)%0A%09%09result, expiration = self.__cache.get(args, (None, 0))%0A%09%09if expiration %3E time.time():%0A%09%09%09return result%0A%09%09result = self._target_function(*args)%0A%09%09self.__cache%5Bargs%5D = (result, time.time() + self.cache_timeout)%0A%09%09return result%0A%0A%09def __repr__(self):%0A%09%09return %22%3Ccached function %7B0%7D%3E%22.format(self._target_function.__name__)%0A%0A%09def clean_cache(self):%0A%09%09now = time.time()%0A%09%09keys_for_removal = %5B%5D%0A%09%09for key, (value, expiration) in self.__cache.items():%0A%09%09%09if expiration %3C now:%0A%09%09%09%09keys_for_removal.append(key)%0A%09%09for key in keys_for_removal:%0A%09%09%09del self.__cache%5Bkey%5D%0A%0A%09def clear_cache(self):%0A%09%09self.__cache = %7B%7D
 %0A%0Adef se

Add a cache decorator.