-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsub.py
executable file
·78 lines (67 loc) · 1.81 KB
/
gsub.py
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
#!/usr/bin/env python
# A simple gsub client
# Does not respond to AKD requests
import sys, getopt, time, string
from pygale import pygale, prettypuff, gale_env, version
def usage(arg):
print 'gsub.py version %s' % version.VERSION
print "Usage: %s location [location ...]" % arg
def print_msg(msg):
date = time.strftime('%m-%d %H:%M:%S ',
time.localtime(time.time()))
msg = date + string.strip(msg)
print msg
def recvpuffs(sub, quiet):
conn = pygale.GaleClient()
conn.set_onconnect(lambda host, c=conn, s=sub, q=quiet:
on_connect(host, c, s, q))
host = conn.connect()
if host is None:
print 'Unable to connect to a gale server'
return
conn.set_puff_callback(prettypuff.show)
# loop forever
conn.next()
def on_connect(host, conn, sub, quiet):
print_msg('Connected to %s' % host)
if not quiet:
sender = gale_env.get('GALE_FROM', 'Unknown sender')
pygale.notify_in('in.present', conn, fullname=sender)
pygale.notify_out('out.disconnected', conn, fullname=sender)
# Subscribe to a list of locations
# Add in our location if not there
myid = pygale.gale_user()
if myid not in sub:
sub.append(myid)
bad, good = conn.sub_to(sub)
if bad:
for b in bad:
print_msg('Unable to find location %s' % b)
if not good:
print_msg('No locations found')
sys.exit(-1)
else:
for g in good:
print_msg('Subscribed to %s' % g)
if __name__ == '__main__':
opts, args = getopt.getopt(sys.argv[1:], 'd:av')
quiet = 0
for opt, val in opts:
if opt == '-d':
pygale.DEBUG = int(val)
elif opt == '-a':
quiet = 1
elif opt == '-v':
usage(sys.argv[0])
sys.exit()
if len(args) < 1:
usage(sys.argv[0])
sys.exit()
pygale.init()
sys.exitfunc = pygale.shutdown
pygale.set_error_handler(print_msg)
pygale.set_update_handler(print_msg)
try:
recvpuffs(args, quiet)
except KeyboardInterrupt:
sys.exit(0)