This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
84 lines (64 loc) · 2.39 KB
/
bot.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
79
80
81
82
83
84
# -*- coding: utf-8 -*-
# try:
# import geventreactor
# geventreactor.install()
# print "gevent reactor found!"
# except ImportError:
# print "no gevent :( (pip install geventreactor)"
# pass
from lib.botplugin import BotPlugin
from lib.ircbot import IRCBot
from lib.botfactory import BotFactory
import logging
logging.basicConfig()
class Bot(object):
def __init__(self, config=False):
self.config = config
self.webresource = None
self.factory = None
from twisted.internet import reactor
self.reactor = reactor
def set_config(self, config):
if not self.config:
self.config = config
def _init_factory(self):
if not self.config:
raise Exception("No config for BotFactory avaiable")
if hasattr(self.config, 'webserver') and self.config.webserver:
from twisted.web.resource import Resource
self.webresource = Resource()
self.factory = BotFactory(
config=self.config, reactor=self.reactor,
webresource=self.webresource
)
def run(self):
if not self.factory:
self._init_factory()
if self.config and self.factory and self.reactor:
if self.config.ssl:
from twisted.internet import ssl
self.reactor.connectSSL(
self.config.server, self.config.port,
self.factory, ssl.ClientContextFactory()
)
else:
self.reactor.connectTCP(
self.config.server, self.config.port, self.factory
)
# Activate webservice
if self.webresource is not None:
from twisted.web.server import Site
site_factory = Site(self.webresource)
self.reactor.listenTCP(
self.config.webserver_port, site_factory
)
# Add sentry error handler
if hasattr(self.config, 'sentry_dsn'):
from twisted.python import log
from raven.handlers.logging import SentryHandler
self.sentry = SentryHandler(self.config.sentry_dsn)
logging.getLogger('twisted').addHandler(self.sentry)
self.observer = log.PythonLoggingObserver()
self.observer.start()
self.reactor.run()
bot = Bot()