usr bin python encoding utf-8 juick Console utility for posting to jui

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python
# encoding: utf-8
"""juick
Console utility for posting to juick.com
Copyright (c) 2009 Alexey Tcherevatov <darth.lemming@gmail.com>
Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
"""
__author__ = "Alexey Tcherevatov <darth.lemming@gmail.com>"
__date__ = "2009-02-26--2009-03-08"
__version__ = "1.1"
import sys,os,xmpp,ConfigParser,re
from optparse import OptionParser
from getpass import getpass
sys.path.append("xmpp")
sys.path.append("dns")
JUICK_ID = 'juick@juick.com'
def post(user, password, to, text):
jidparams = {'jid': user, 'password': password}
jid=xmpp.protocol.JID(jidparams['jid'])
cl=xmpp.Client(jid.getDomain(),debug=[])
if not cl.connect():
return 'could not connect!'
sys.exit()
if not cl.auth(jid.getNode(),jidparams['password'],resource='console'):
return 'could not authenticate!'
cl.send(xmpp.protocol.Message(to,text))
return 'request sent'
def save_config(filename, section, settings):
config = ConfigParser.RawConfigParser()
config.add_section(section)
for x in settings.keys():
config.set(section, x, settings[x])
configfile = open(filename, 'wb')
config.write(configfile)
configfile.close()
def format_message(s):
matches = re.findall('\*[^\*]*\*', s)
for x in matches:
s = s.replace(x, '\33[1m' + x[1:-1] + '\33[22m')
matches = re.findall('/[^/]*/', s)
for x in matches:
s = s.replace(x, '\33[3m' + x[1:-1] + '\33[23m')
matches = re.findall('_[^_]*_', s)
for x in matches:
s = s.replace(x, '\33[4m' + x[1:-1] + '\33[24m')
return s
def main():
# setting defaults
user = password = text = ''
confirm = True
delete = ''
config_file = os.path.join(os.path.expanduser("~"), '.juick')
# parse command line options
parser = OptionParser(usage="%prog [options] 'message'", version='%prog ' + __version__)
parser.set_defaults(user=user, password=password, confirm=confirm)
parser.add_option('-u', '--user', action='store', dest='user', help='set user login')
parser.add_option('-p', '--password', action='store', dest='password', help='set user password')
parser.add_option('-d', '--delete', action='store', dest='delete', help='-d [last|message number] delete last message or message with given number')
parser.add_option('-S', '--save-config', action='store_true', dest='save', help='save configuration')
parser.add_option('-n', '--no-confirm', action='store_false', dest='confirm', help='disable confirmation on posting')
(options, args) = parser.parse_args()
message = ' '.join(args)
# reading config in case of omitted options
if not options.user or not options.password:
if os.path.exists(config_file):
config = ConfigParser.RawConfigParser()
config.read(config_file)
try:
if not options.user:
options.user = config.get('Preferences', 'user')
if not options.password:
options.password = config.get('Preferences', 'password')
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
pass
except:
sys.exit('wrong configuration file')
else:
pass # TODO: creating configuration
if not options.user:
options.user = raw_input('User: ')
if not options.password:
options.password = getpass("Password: ")
if options.save:
save_config(config_file, 'Preferences', {'user': options.user, 'password': options.password})
if options.delete:
if options.delete == 'last':
if raw_input("Are you sure you want to delete last message? [Y/n]: ") in ('', 'y', 'Y'):
print post(options.user, options.password, JUICK_ID, 'D L')
else:
if raw_input("Are you sure you want to delete message #%s? [Y/n]: " % options.delete) in ('', 'y', 'Y'):
print post(options.user, options.password, JUICK_ID, 'D #' + options.delete)
sys.exit(0)
if options.confirm:
if raw_input("Are you sure you want to post message '%s'? [Y/n]: " % format_message(message)) in ('', 'y', 'Y'):
print post(options.user, options.password, JUICK_ID, message)
else:
print post(options.user, options.password, JUICK_ID, ' '.join(args))
sys.exit(0)
if __name__ == "__main__":
main()