def showUser(args):
	username = args[0]

	db = connectToDatabase()

	if db.userExists(username):
		firstname, lastname, email, blocked = db.getUser(username)
		writeln('user........: %s\nfirstname...: %s\nlastname....: %s\nemail.......: %s\nblocked.....: %d' % (username, firstname, lastname, email, blocked))
	else:
		raise Exception('couldn\'t find user: "******"' % username)
def showUsers(args):
	db = connectToDatabase()

	users = db.getUsers()

	if not users is None:
		for username, email, blocked in users:
			if blocked == 0:
				status = 'enabled'
			else:
				status = 'disabled'

			writeln('%s <%s>, %s' % (username, email, status))
	else:
		writeln('user database is empty')
def authenticate(args):
	# get authentication url:
	twitter = createTwitterClient()

	url = twitter.getAuthorizationUrl()
	writeln('Please visit the following id to request a PIN: %s' % url)

	# read pin:
	pin = raw_input('PIN: ').strip()

	# get access key/secret from Twitter:
	key, secret = twitter.getAccessToken(pin)

	# store key/secret in database:
	db = connectToDatabase()
	db.saveOAuthData(key, secret)
def printVersion(args):
	writeln('version %d.%d.%d (%s)' % (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCHLEVEL, VERSION_PHASE))
def printUsage(args=None):
	writeln('USAGE: mail2twitter.py --[command] [arg1] [arg2] ...\n')
	writeln('The following commands are available:')
	writeln('\tTWITTER')
	writeln('\t--authenticate                    grant access to your account to mail2twitter')
	writeln('\t--post                            process queue and post to Twitter\n')
	writeln('\tUSER DATABASE')
	writeln('\t--create-user [username] [firstname] [lastname] [email]   add user to user database')
	writeln('\t--update-user [username] [firstname] [lastname] [email]   update an existing user')
	writeln('\t--enable-user [username]                                  enable user account')
	writeln('\t--disable-user [username]                                 disable user account')
	writeln('\t--show-users                                              print all users from the user database')
	writeln('\t--show-user [username]                                    print details of a user\n')
	writeln('\tQUEUE')
	writeln('\t--show-queue                      print current queue')
	writeln('\t--delete-from-queue [id]          delete item from the queue')
	writeln('\t--clear-queue                     delete all items from the queue')
	writeln('\t--show-history                    print history\n')
	writeln('\tMESSAGES')
	writeln('\t--show-message-queue              print current message queue')
	writeln('\t--delete-from-message-queue [id]  delete item from the message queue')
	writeln('\t--clear-message-queue             delete all items from the message queue')
	writeln('\t--send-messages                   send messages from message queue (SMTP)')
	writeln('\t--show-sent-log                   show sent messages\n')
	writeln('\tPOP3')
	writeln('\t--fetch-mails                     receive mails from specified POP3 account\n')
	writeln('\tGENERAL')
	writeln('\t--help                            show this text\n')
	writeln('\t--version                         show version\n')
def showSentLog(args):
	db = connectToDatabase()

	for username, email, text, sentDate in db.getSentLog():
		writeln('%s to %s<%s>: "%s"' % (time.ctime(sentDate), username, email, text))
def showMessageQueue(args):
	db = connectToDatabase()

	for id, username, email, text, timestamp in db.getMessageQueue():
		writeln('%d. %s to %s<%s>: "%s"' % (id, time.ctime(timestamp), username, email, text))
def showHistory(args):
	db = connectToDatabase()

	for id, username, email, typeId, text, timestamp in db.getHistory():
		writeln('%d. %s %s: "%s", %s<%s>' % (id, time.ctime(timestamp), convertTypeToText(typeId), text, username, email))
	if len(sys.argv) >= 2:
		# check if given action is defined
		if commands.has_key(sys.argv[1]):
			cmd = commands[sys.argv[1]]

	if cmd is None:
		printUsage()
		sys.exit(-1)

	# add default args key:
	if not 'args' in cmd:
		cmd['args'] = None

	# test if number of arguments is correct:
	if not ((len(sys.argv) - 2 == 0 and cmd['args'] is None) or (cmd['args'] is not None and len(cmd['args']) == len(sys.argv) - 2)):
		writeln('invalid number of arguments for action "%s"' % sys.argv[1])
		sys.exit(-1)
	else:
		# validate arguments:
		if cmd['args'] is not None:
			for i in range(len(cmd['args'])):
				try:
					cmd['args'][i].validate(sys.argv[i + 2])
					args.append(sys.argv[i + 2].rstrip('\n'))

				except Exception, e:
					writeln('invalid argument at position %d ("%s"): %s' % (i + 1, sys.argv[i + 2], e))
					sys.exit(-1)

		action = cmd['callback']