~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to runbot

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#
3
 
#
4
 
 
5
 
""" start the bot from standalone directory """
6
 
 
7
 
__copyright__ = 'this file is in the public domain'
8
 
 
9
 
import sys, os, socket
10
 
 
11
 
if os.getuid() == 0:
12
 
    print "don't run the bot as root"
13
 
    os._exit(1)
14
 
 
15
 
vi = sys.version_info
16
 
 
17
 
if vi[0] < 2 or (vi[0] == 2 and vi[1] < 4):
18
 
    print "i need at least python version >= 2.4"
19
 
    os._exit(1)
20
 
 
21
 
for i in sys.argv:
22
 
    if os.path.isdir(i):
23
 
        os.chdir(i)
24
 
        print 'changed dir to %s' % i
25
 
        break
26
 
 
27
 
sys.path.insert(0, os.getcwd())
28
 
sys.path.insert(1, os.path.abspath(os.path.join(os.getcwd(), 'myplugs', 'addons')))
29
 
 
30
 
from optparse import OptionParser
31
 
 
32
 
parser = OptionParser(usage='usage: %prog [options]', version='%prog 0.8.1')
33
 
 
34
 
parser.add_option('', '-a', action='store_true', default=False, dest='doascii',
35
 
                  help="Use ASCII as the default encoding instead of UTF-8")
36
 
parser.add_option('', '-b', action='store_true', default=False, dest='dobackup',
37
 
                  help="Do Backup")
38
 
parser.add_option('', '-u', action='store_false', default=True, dest='doumode',
39
 
                  help="Check permissions on the gozerdata folder")
40
 
parser.add_option('', '-r', type='string', default=False, dest='doresume', 
41
 
                  metavar='PATH', 
42
 
                  help="Resume the bot from the folder specified")
43
 
 
44
 
opts, args = parser.parse_args()
45
 
opts.args = args
46
 
 
47
 
if not opts.doascii:
48
 
    reload(sys)
49
 
    sys.setdefaultencoding('utf-8')
50
 
 
51
 
sys.setcheckinterval(50)
52
 
socket.setdefaulttimeout(30)
53
 
 
54
 
from gozerbot.generic import handle_exception
55
 
 
56
 
try:
57
 
    from gozerbot.config import config, writeconfig, writeloadlist
58
 
    from gozerbot.datadir import makedirs
59
 
    from gozerbot.generic import enable_logging
60
 
    makedirs()
61
 
    writeconfig()
62
 
    writeloadlist()
63
 
    config.load()
64
 
    enable_logging()
65
 
except Exception, ex:
66
 
    handle_exception()
67
 
    os._exit(1)
68
 
 
69
 
try:
70
 
    from gozerbot.generic import rlog, handle_exception, checkpermissions
71
 
    from gozerbot.bot import Bot
72
 
    from gozerbot.fleet import fleet
73
 
    from gozerbot.plugins import plugins
74
 
    from gozerbot.eventhandler import mainhandler
75
 
    from gozerbot.users import users
76
 
    from gozerbot.thr import start_new_thread
77
 
    from gozerbot.partyline import partyline
78
 
    from gozerbot.exit import globalshutdown
79
 
except Exception, ex:
80
 
    handle_exception()
81
 
    os._exit(1)
82
 
 
83
 
import time, signal, gc, types
84
 
 
85
 
# enable garbage collection
86
 
gc.enable()
87
 
 
88
 
# provide sigterm support
89
 
def dostop(a, b):
90
 
    """ sig handler """
91
 
    globalshutdown()
92
 
 
93
 
signal.signal(signal.SIGTERM, dostop)
94
 
 
95
 
 
96
 
rlog(10, 'GOZERBOT', 'starting %s' % config['version'])
97
 
rlog(5, 'gozerbot', 'default encoding is %s' % sys.getdefaultencoding())
98
 
 
99
 
if opts.doumode:
100
 
    umask = config['umask']
101
 
    if not umask:
102
 
        checkpermissions('gozerdata', 0700)
103
 
    else:
104
 
        checkpermissions('gozerdata', umask)
105
 
 
106
 
# write pid to pidfile
107
 
k = open('gozerbot.pid','w')
108
 
k.write(str(os.getpid()))
109
 
k.close()
110
 
 
111
 
# see if owner already has a user account if not merge otherwise add
112
 
owner = []
113
 
 
114
 
if config['jabberenable']:
115
 
    if type(config['jabberowner']) != types.ListType:
116
 
        owner.append(config['jabberowner'])
117
 
    else:
118
 
        owner = config['jabberowner']
119
 
 
120
 
if type(config['owneruserhost']) != types.ListType:
121
 
    owner.append(config['owneruserhost'])
122
 
else:
123
 
    owner += config['owneruserhost']
124
 
 
125
 
try:
126
 
    for i in owner:
127
 
        username = users.getname(i)
128
 
        if not username:
129
 
            if not users.merge('owner', i):
130
 
                users.add('owner', [i, ], perms = ['USER', 'OPER'])
131
 
except Exception, ex:
132
 
    print str(ex)
133
 
    os._exit(1)
134
 
 
135
 
# register plugins
136
 
start_new_thread(plugins.regplugins, ())
137
 
 
138
 
# create the bot and connect
139
 
if config['jabberenable']:
140
 
    try:
141
 
        from gozerbot.jabberbot import Jabberbot
142
 
    except ImportError:
143
 
        handle_exception()
144
 
        os._exit(1)
145
 
    bot = Jabberbot('jabbermain', config['jabberowner'])
146
 
    bot.host = config['jabberhost']
147
 
    bot.user = config['jabberuser']
148
 
    bot.password = config['jabberpass']
149
 
    bot.port = 5222
150
 
    # add to fleet
151
 
    fleet.addbot(bot)
152
 
 
153
 
if not config['ircdisable']:
154
 
    bot = Bot('main', config['owneruserhost'])
155
 
    bot.nick = config['nick'] or 'gb1'
156
 
    bot.server = config['server'] or 'localhost'
157
 
    bot.port = config['port'] or 6667
158
 
    bot.password = config['password'] or ""
159
 
    bot.ipv6 = config['ipv6'] or 0
160
 
    bot.ssl = config['ssl'] or 0
161
 
    # add to fleet
162
 
    fleet.addbot(bot)
163
 
 
164
 
if opts.doresume and os.path.isfile(opts.doresume):
165
 
    fleet.resume(opts.doresume)
166
 
    fleet.startok.wait()
167
 
    partyline.resume(opts.doresume)
168
 
    os.unlink(opts.doresume) # bye
169
 
else:
170
 
    fleet.start()
171
 
 
172
 
while 1:
173
 
    try:
174
 
        time.sleep(1)
175
 
        mainhandler.handle_one()
176
 
    except KeyboardInterrupt:
177
 
        globalshutdown()
178
 
        os._exit(0)
179
 
    except Exception, ex:
180
 
        handle_exception()
181
 
        globalshutdown()
182
 
        os._exit(1)