Example #1
0
 def handle_error(self):
     ''' Handle any uncaptured error in the core. Overrides asyncore's
     handle_error '''
     trace = traceback.format_exc()
     stderr(trace)
     self.debug("core", 'Fatal error in core, please review exception log',
                'always')
     logfile = codecs.open(os.path.join(self.config.logdir,
                                        'exceptions.log'),
                           'a',
                           encoding='utf-8')  # TODO: make not
     # hardcoded
     logfile.write('Fatal error in core, handle_error() was called\n')
     logfile.write('last raw line was %s' % self.raw)
     logfile.write(trace)
     logfile.write('Buffer:\n')
     logfile.write(self.buffer)
     logfile.write('----------------------------------------\n\n')
     logfile.close()
     if self.error_count > 10:
         if (datetime.now() - self.last_error_timestamp).seconds < 5:
             print >> sys.stderr, "Too many errors, can't continue"
             os._exit(1)
     self.last_error_timestamp = datetime.now()
     self.error_count = self.error_count + 1
     # Clearing buffer is important, it will cause willie to lose some lines,
     # But it is vital if the exception happened before found_terminator()
     self.buffer = ''
Example #2
0
    def found_terminator(self):
        line = self.buffer
        if line.endswith('\r'):
            line = line[:-1]
        self.buffer = u''
        self.raw = line
        if line.startswith(':'):
            source, line = line[1:].split(' ', 1)
        else:
            source = None
        
        if ' :' in line:
            argstr, text = line.split(' :', 1)
            args = argstr.split()
            args.append(text)
        else:
            args = line.split()
            text = args[-1]

        if args[0] == 'PING':
            self.write(('PONG', text))
        elif args[0] == 'ERROR':
            self.debug('IRC Server Error', text, 'always')
        elif args[0] == '433':
            stderr('Nickname already in use!')
            self.hasquit = True
            self.handle_close()

        origin = Origin(self, source, args)
        self.dispatch(origin, text, args)
Example #3
0
    def unregister(self, variables):
        """Unregister all willie callables in variables, and their bindings.

        When unloading a module, this ensures that the unloaded modules will
        not get called and that the objects can be garbage collected. Objects
        that have not been registered are ignored.

        Args:
        variables -- A list of callable objects from a willie module.
        """

        def remove_func(func, commands):
            """Remove all traces of func from commands."""
            for func_list in commands.itervalues():
                if func in func_list:
                    func_list.remove(func)

        hostmask = "%s!%s@%s" % (self.nick, self.user, socket.gethostname())
        willie = self.WillieWrapper(self, irc.Origin(self, hostmask, [], {}))
        for obj in variables.itervalues():
            if obj in self.callables:
                self.callables.remove(obj)
                for commands in self.commands.itervalues():
                    remove_func(obj, commands)
            if obj in self.shutdown_methods:
                try:
                    obj(willie)
                except Exception as e:
                    stderr(
                        "Error calling shutdown method for module %s:%s" %
                        (obj.__module__, e)
                    )
                self.shutdown_methods.remove(obj)
Example #4
0
    def found_terminator(self):
        line = self.buffer
        if line.endswith('\r'):
            line = line[:-1]
        self.buffer = u''
        self.raw = line
        if line.startswith(':'):
            source, line = line[1:].split(' ', 1)
        else:
            source = None

        if ' :' in line:
            argstr, text = line.split(' :', 1)
            args = argstr.split()
            args.append(text)
        else:
            args = line.split()
            text = args[-1]

        self.last_ping_time = datetime.now()
        if args[0] == 'PING':
            self.write(('PONG', text))
        elif args[0] == 'ERROR':
            self.debug(__file__, text, 'always')
            if self.hasquit:
                self.close_when_done()
        elif args[0] == '433':
            stderr('Nickname already in use!')
            self.handle_close()

        origin = Origin(self, source, args)
        self.dispatch(origin, text, args)
Example #5
0
 def handle_error(self):
     ''' Handle any uncaptured error in the core. Overrides asyncore's
     handle_error '''
     trace = traceback.format_exc()
     stderr(trace)
     self.debug("core", 'Fatal error in core, please review exception log',
                'always')
     logfile = codecs.open(os.path.join(self.config.logdir, 'exceptions.log'),
                           'a', encoding='utf-8')  # TODO: make not
                                                          # hardcoded
     logfile.write('Fatal error in core, handle_error() was called\n')
     logfile.write('last raw line was %s' % self.raw)
     logfile.write(trace)
     logfile.write('Buffer:\n')
     logfile.write(self.buffer)
     logfile.write('----------------------------------------\n\n')
     logfile.close()
     if self.error_count > 10:
         if (datetime.now() - self.last_error_timestamp).seconds < 5:
             print >> sys.stderr, "Too many errors, can't continue"
             os._exit(1)
     self.last_error_timestamp = datetime.now()
     self.error_count = self.error_count + 1
     if self.config.exit_on_error:
         os._exit(1)
Example #6
0
    def unregister(self, variables):
        """Unregister all willie callables in variables, and their bindings.

        When unloading a module, this ensures that the unloaded modules will
        not get called and that the objects can be garbage collected. Objects
        that have not been registered are ignored.

        Args:
        variables -- A list of callable objects from a willie module.
        """
        def remove_func(func, commands):
            """Remove all traces of func from commands."""
            for func_list in commands.itervalues():
                if func in func_list:
                    func_list.remove(func)

        hostmask = "%s!%s@%s" % (self.nick, self.user, socket.gethostname())
        willie = self.WillieWrapper(self, irc.Origin(self, hostmask, []))
        for obj in variables.itervalues():
            if obj in self.callables:
                self.callables.remove(obj)
                for commands in self.commands.itervalues():
                    remove_func(obj, commands)
            if obj in self.shutdown_methods:
                try:
                    obj(willie)
                except Exception as e:
                    stderr("Error calling shutdown method for module %s:%s" %
                           (obj.__module__, e))
                self.shutdown_methods.remove(obj)
Example #7
0
 def handle_error(self):
     ''' Handle any uncaptured error in the core. Overrides asyncore's
     handle_error '''
     trace = traceback.format_exc()
     stderr(trace)
     self.debug(__file__,
                'Fatal error in core, please review exception log',
                'always')
     # TODO: make not hardcoded
     logfile = codecs.open(os.path.join(self.config.logdir,
                                        'exceptions.log'),
                           'a',
                           encoding='utf-8')
     logfile.write('Fatal error in core, handle_error() was called\n')
     logfile.write('last raw line was %s' % self.raw)
     logfile.write(trace)
     logfile.write('Buffer:\n')
     logfile.write(self.buffer)
     logfile.write('----------------------------------------\n\n')
     logfile.close()
     if self.error_count > 10:
         if (datetime.now() - self.last_error_timestamp).seconds < 5:
             print >> sys.stderr, "Too many errors, can't continue"
             os._exit(1)
     self.last_error_timestamp = datetime.now()
     self.error_count = self.error_count + 1
     if self.config.exit_on_error:
         os._exit(1)
Example #8
0
    def found_terminator(self):
        line = self.buffer
        if line.endswith("\r"):
            line = line[:-1]
        self.buffer = u""
        self.raw = line
        if line.startswith(":"):
            source, line = line[1:].split(" ", 1)
        else:
            source = None

        if " :" in line:
            argstr, text = line.split(" :", 1)
            args = argstr.split()
            args.append(text)
        else:
            args = line.split()
            text = args[-1]

        self.last_ping_time = datetime.now()
        if args[0] == "PING":
            self.write(("PONG", text))
        elif args[0] == "ERROR":
            self.debug("IRC Server Error", text, "always")
        elif args[0] == "433":
            stderr("Nickname already in use!")
            self.hasquit = True
            self.handle_close()

        origin = Origin(self, source, args)
        self.dispatch(origin, text, args)
Example #9
0
    def found_terminator(self):
        line = self.buffer
        if line.endswith('\r'):
            line = line[:-1]
        self.buffer = u''
        self.raw = line
        if line.startswith(':'):
            source, line = line[1:].split(' ', 1)
        else:
            source = None

        if ' :' in line:
            argstr, text = line.split(' :', 1)
            args = argstr.split()
            args.append(text)
        else:
            args = line.split()
            text = args[-1]

        self.last_ping_time = datetime.now()
        if args[0] == 'PING':
            self.write(('PONG', text))
        elif args[0] == 'ERROR':
            self.debug(__file__, text, 'always')
            if self.hasquit:
                self.close_when_done()
        elif args[0] == '433':
            stderr('Nickname already in use!')
            self.handle_close()

        origin = Origin(self, source, args)
        self.dispatch(origin, text, args)
Example #10
0
    def handle_close(self):
        self._shutdown()
        stderr('Closed!')

        # This will eventually call asyncore dispatchers close method, which
        # will release the main thread. This should be called last to avoid
        # race conditions.
        asynchat.async_chat.handle_close(self)
Example #11
0
    def handle_close(self):
        self._shutdown()
        stderr('Closed!')

        # This will eventually call asyncore dispatchers close method, which
        # will release the main thread. This should be called last to avoid
        # race conditions.
        asynchat.async_chat.handle_close(self)
Example #12
0
File: irc.py Project: Hawxy/willie
 def _timeout_check(self):
     while True:
         if (datetime.now() - self.last_ping_time).seconds > int(self.config.timeout):
             stderr('Ping timeout reached after %s seconds, closing connection' % self.config.timeout)
             self.handle_close()
             break
         else:
             time.sleep(int(self.config.timeout))
Example #13
0
 def _timeout_check(self):
     while True:
         if (datetime.now() - self.last_ping_time).seconds > int(self.config.timeout):
             stderr('Ping timeout reached after %s seconds, closing connection' % self.config.timeout)
             self.handle_close()
             break
         else:
             time.sleep(int(self.config.timeout))
Example #14
0
    def error(self, origin=None, trigger=None):
        ''' Called internally when a module causes an error '''
        try:
            trace = traceback.format_exc()
            trace = trace.decode('utf-8', errors='xmlcharrefreplace')
            stderr(trace)
            try:
                lines = list(reversed(trace.splitlines()))
                report = [lines[0].strip()]
                for line in lines:
                    line = line.strip()
                    if line.startswith('File "/'):
                        report.append(line[0].lower() + line[1:])
                        break
                else:
                    report.append('source unknown')

                signature = '%s (%s)' % (report[0], report[1])
                # TODO: make not hardcoded
                log_filename = os.path.join(
                    self.config.logdir, 'exceptions.log'
                )
                with codecs.open(
                    log_filename, 'a', encoding='utf-8'
                ) as logfile:
                    logfile.write(u'Signature: %s\n' % signature)
                    if origin:
                        logfile.write(
                            u'from %s at %s:\n' % (
                                origin.sender, str(datetime.now())
                            )
                        )
                    if trigger:
                        logfile.write(
                            u'Message was: <%s> %s\n' % (
                                trigger.nick, trigger.group(0)
                            )
                        )
                    logfile.write(trace)
                    logfile.write(
                        '----------------------------------------\n\n'
                    )
            except Exception as e:
                stderr("Could not save full traceback!")
                self.debug(__file__, "(From: " + origin.sender +
                           "), can't save traceback: " + str(e), 'always')

            if origin:
                self.msg(origin.sender, signature)
        except Exception as e:
            if origin:
                self.msg(origin.sender, "Got an error.")
                self.debug(
                    __file__,
                    "(From: " + origin.sender + ") " + str(e),
                    'always'
                )
Example #15
0
    def error(self, origin=None, trigger=None):
        ''' Called internally when a module causes an error '''
        try:
            trace = traceback.format_exc()
            trace = trace.decode('utf-8', errors='xmlcharrefreplace')
            stderr(trace)
            try:
                lines = list(reversed(trace.splitlines()))
                report = [lines[0].strip()]
                for line in lines:
                    line = line.strip()
                    if line.startswith('File "/'):
                        report.append(line[0].lower() + line[1:])
                        break
                else:
                    report.append('source unknown')

                signature = '%s (%s)' % (report[0], report[1])
                # TODO: make not hardcoded
                log_filename = os.path.join(
                    self.config.logdir, 'exceptions.log'
                )
                with codecs.open(
                    log_filename, 'a', encoding='utf-8'
                ) as logfile:
                    logfile.write(u'Signature: %s\n' % signature)
                    if origin:
                        logfile.write(
                            u'from %s at %s:\n' % (
                                origin.sender, str(datetime.now())
                            )
                        )
                    if trigger:
                        logfile.write(
                            u'Message was: <%s> %s\n' % (
                                trigger.nick, trigger.group(0)
                            )
                        )
                    logfile.write(trace)
                    logfile.write(
                        '----------------------------------------\n\n'
                    )
            except Exception as e:
                stderr("Could not save full traceback!")
                self.debug(__file__, "(From: " + origin.sender +
                           "), can't save traceback: " + str(e), 'always')

            if origin:
                self.msg(origin.sender, signature)
        except Exception as e:
            if origin:
                self.msg(origin.sender, "Got an error.")
                self.debug(
                    __file__,
                    "(From: " + origin.sender + ") " + str(e),
                    'always'
                )
Example #16
0
    def error(self, origin, trigger):
        ''' Called internally when a module causes an error '''
        try:
            trace = traceback.format_exc()
            try:
                trace = trace.decode('utf-8')
            except:
                pass  # Can't do much about it
            stderr(trace)
            try:
                lines = list(reversed(trace.splitlines()))
                report = [lines[0].strip()]
                for line in lines:
                    line = line.strip()
                    if line.startswith('File "/'):
                        report.append(line[0].lower() + line[1:])
                        break
                else:
                    report.append('source unknown')

                signature = '%s (%s)' % (report[0], report[1])
                logfile = codecs.open(os.path.join(self.config.logdir,
                                                   'exceptions.log'),
                                      'a', encoding='utf-8')  # TODO: make not
                                                              # hardcoded
                logfile.write(u'Signature: %s\n' % signature)
                logfile.write(u'from %s at %s:\n' % (origin.sender,
                                                     str(datetime.now())))
                logfile.write(u'Message was: <%s> %s\n' % (trigger.nick,
                                                           trigger.group(0)))
                try:
                    logfile.write(trace.encode('utf-8'))
                except:
                    logfile.write(trace)
                logfile.write('----------------------------------------\n\n')
                logfile.close()
            except Exception as e:
                stderr("Could not save full traceback!")
                self.debug("core: error reporting", "(From: " + origin.sender +
                           "), can't save traceback: " + str(e), 'always')

            if getattr(self.config, 'errors_to_sender', 'True') == 'True':
                self.msg(origin.sender, signature)
        except Exception as e:
            if getattr(self.config, 'errors_to_sender', 'True') == 'True':
                self.msg(origin.sender, "Got an error.")
            self.debug("core: error reporting", "(From: " + origin.sender +
                       ") " + str(e), 'always')
Example #17
0
 def initiate_connect(self, host, port):
     stderr('Connecting to %s:%s...' % (host, port))
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     if self.config.core.bind_host is not None:
         self.socket.bind((self.config.core.bind_host, 0))
     if self.config.core.use_ssl and has_ssl:
         self.send = self._ssl_send
         self.recv = self._ssl_recv
     elif not has_ssl and self.config.core.use_ssl:
         stderr('SSL is not avilable on your system, attempting connection '
                'without it')
     self.connect((host, port))
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         print 'KeyboardInterrupt'
         self.quit('KeyboardInterrupt')
Example #18
0
 def initiate_connect(self, host, port):
     stderr('Connecting to %s:%s...' % (host, port))
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     if self.config.core.bind_host is not None:
         self.socket.bind((self.config.core.bind_host, 0))
     if self.config.core.use_ssl and has_ssl:
         self.send = self._ssl_send
         self.recv = self._ssl_recv
     elif not has_ssl and self.config.core.use_ssl:
         stderr('SSL is not avilable on your system, attempting connection '
                'without it')
     self.connect((host, port))
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         print 'KeyboardInterrupt'
         self.quit('KeyboardInterrupt')
Example #19
0
 def initiate_connect(self, host, port):
     stderr('Connecting to %s:%s...' % (host, port))
     source_address = ((self.config.core.bind_host, 0)
                       if self.config.core.bind_address else None)
     self.set_socket(socket.create_connection((host, port),
         source_address=source_address))
     if self.config.core.use_ssl and has_ssl:
         self.send = self._ssl_send
         self.recv = self._ssl_recv
     elif not has_ssl and self.config.core.use_ssl:
         stderr('SSL is not avilable on your system, attempting connection '
                'without it')
     self.connect((host, port))
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         print 'KeyboardInterrupt'
         self.quit('KeyboardInterrupt')
Example #20
0
 def initiate_connect(self, host, port):
     stderr('Connecting to %s:%s...' % (host, port))
     source_address = ((self.config.core.bind_host, 0)
                       if self.config.core.bind_address else None)
     self.set_socket(socket.create_connection((host, port),
         source_address=source_address))
     if self.config.core.use_ssl and has_ssl:
         self.send = self._ssl_send
         self.recv = self._ssl_recv
     elif not has_ssl and self.config.core.use_ssl:
         stderr('SSL is not avilable on your system, attempting connection '
                'without it')
     self.connect((host, port))
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         print 'KeyboardInterrupt'
         self.quit('KeyboardInterrupt')
Example #21
0
 def initiate_connect(self, host, port):
     if self.verbose:
         message = "Connecting to %s:%s..." % (host, port)
         stderr(message)
     self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
     if self.config.core.bind_host is not None:
         self.socket.bind((self.config.core.bind_host, 0))
     if self.config.core.use_ssl and has_ssl:
         self.send = self._ssl_send
         self.recv = self._ssl_recv
     elif not has_ssl and self.config.core.use_ssl:
         stderr("SSL is not avilable on your system, attempting connection " "without it")
     self.connect((host, port))
     try:
         asyncore.loop()
     except KeyboardInterrupt:
         print "KeyboardInterrupt"
         self.quit("KeyboardInterrupt")
Example #22
0
    def found_terminator(self):
        line = self.buffer
        if line.endswith('\r'):
            line = line[:-1]
        self.buffer = u''
        self.raw = line

        # Break off IRCv3 message tags, if present
        tags = {}
        if line.startswith('@'):
            tagstring, line = line.split(' ', 1)
            for tag in tagstring[1:].split(';'):
                tag = tag.split('=', 1)
                if len(tag) > 1:
                    tags[tag[0]] = tag[1]
                else:
                    tags[tag[0]] = None

        if line.startswith(':'):
            source, line = line[1:].split(' ', 1)
        else:
            source = None

        if ' :' in line:
            argstr, text = line.split(' :', 1)
            args = argstr.split()
            args.append(text)
        else:
            args = line.split()
            text = args[-1]

        self.last_ping_time = datetime.now()
        if args[0] == 'PING':
            self.write(('PONG', text))
        elif args[0] == 'ERROR':
            self.debug(__file__, text, 'always')
            if self.hasquit:
                self.close_when_done()
        elif args[0] == '433':
            stderr('Nickname already in use!')
            self.handle_close()

        origin = Origin(self, source, args, tags)
        self.dispatch(origin, text, args)
Example #23
0
    def found_terminator(self):
        line = self.buffer
        if line.endswith('\r'):
            line = line[:-1]
        self.buffer = u''
        self.raw = line

        # Break off IRCv3 message tags, if present
        tags = {}
        if line.startswith('@'):
            tagstring, line = line.split(' ', 1)
            for tag in tagstring[1:].split(';'):
                tag = tag.split('=', 1)
                if len(tag) > 1:
                    tags[tag[0]] = tag[1]
                else:
                    tags[tag[0]] = None

        if line.startswith(':'):
            source, line = line[1:].split(' ', 1)
        else:
            source = None

        if ' :' in line:
            argstr, text = line.split(' :', 1)
            args = argstr.split(' ')
            args.append(text)
        else:
            args = line.split(' ')
            text = args[-1]

        self.last_ping_time = datetime.now()
        if args[0] == 'PING':
            self.write(('PONG', text))
        elif args[0] == 'ERROR':
            self.debug(__file__, text, 'always')
            if self.hasquit:
                self.close_when_done()
        elif args[0] == '433':
            stderr('Nickname already in use!')
            self.handle_close()

        origin = Origin(self, source, args, tags)
        self.dispatch(origin, text, args)
Example #24
0
def run(config):
    if config.core.delay is not None:
        delay = config.core.delay
    else:
        delay = 20

    def signal_handler(sig, frame):
        if sig == signal.SIGUSR1 or sig == signal.SIGTERM:
            stderr('Got quit signal, shutting down.')
            p.quit('Closing')

    while True:
        try:
            p = bot.Sopel(config)
            if hasattr(signal, 'SIGUSR1'):
                signal.signal(signal.SIGUSR1, signal_handler)
            if hasattr(signal, 'SIGTERM'):
                signal.signal(signal.SIGTERM, signal_handler)
            p.run(config.core.host, int(config.core.port))
        except KeyboardInterrupt:
            break
        except Exception, e:
            trace = traceback.format_exc()
            try:
                stderr(trace)
            except:
                pass
            logfile = open(os.path.join(config.logdir, 'exceptions.log'), 'a')
            logfile.write('Critical exception in core')
            logfile.write(trace)
            logfile.write('----------------------------------------\n\n')
            logfile.close()
            os.unlink(config.pid_file_path)
            os._exit(1)

        if not isinstance(delay, int):
            break
        if p.hasquit or config.exit_on_error:
            break
        stderr('Warning: Disconnected. Reconnecting in %s seconds...' % delay)
        time.sleep(delay)
Example #25
0
File: bot.py Project: MarkTr/willie
    def setup(self):
        stderr("\nWelcome to Willie. Loading modules...\n\n")
        self.callables = set()

        filenames = self.config.enumerate_modules()
        # Coretasks is special. No custom user coretasks.
        this_dir = os.path.dirname(os.path.abspath(__file__))
        filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py')

        modules = []
        error_count = 0
        for name, filename in filenames.iteritems():
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                stderr("Error loading %s: %s (in bot.py)" % (name, e))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    stderr("Error in %s setup procedure: %s (in bot.py)"
                           % (name, e))
Example #26
0
    def setup(self):
        stderr("\nWelcome to Willie. Loading modules...\n\n")
        self.callables = set()
        self.shutdown_methods = set()

        filenames = self.config.enumerate_modules()
        # Coretasks is special. No custom user coretasks.
        this_dir = os.path.dirname(os.path.abspath(__file__))
        filenames["coretasks"] = os.path.join(this_dir, "coretasks.py")

        modules = []
        error_count = 0
        for name, filename in filenames.iteritems():
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                filename, lineno = tools.get_raising_file_and_line()
                rel_path = os.path.relpath(filename, os.path.dirname(__file__))
                raising_stmt = "%s:%d" % (rel_path, lineno)
                stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
            else:
                try:
                    if hasattr(module, "setup"):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(filename, os.path.dirname(__file__))
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr("Error in %s setup procedure: %s (%s)" % (name, e, raising_stmt))
Example #27
0
File: bot.py Project: Airead/daeria
    def setup(self):
        stderr("\nWelcome to Willie. Loading modules...\n\n")
        self.variables = {}

        filenames = enumerate_modules(self.config)
        filenames.append(os.path.join(this_dir, 'coretasks.py'))
        self.enumerate_modules = enumerate_modules

        modules = []
        excluded_modules = getattr(self.config, 'exclude', [])
        error_count = 0
        for filename in filenames:
            name = os.path.basename(filename)[:-3]
            if name in excluded_modules:
                continue
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                stderr("Error loading %s: %s (in bot.py)" % (name, e))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    stderr("Error in %s setup procedure: %s (in bot.py)"
                           % (name, e))
Example #28
0
    def setup(self):
        stderr("\nWelcome to Willie. Loading modules...\n\n")
        self.callables = set()

        filenames = self.config.enumerate_modules()
        # Coretasks is special. No custom user coretasks.
        this_dir = os.path.dirname(os.path.abspath(__file__))
        filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py')

        modules = []
        error_count = 0
        for name, filename in filenames.iteritems():
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                stderr("Error loading %s: %s (in bot.py)" % (name, e))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    stderr("Error in %s setup procedure: %s (in bot.py)" %
                           (name, e))
Example #29
0
    def setup(self):
        stderr("\nWelcome to Willie. Loading modules...\n\n")
        self.callables = set()
        self.shutdown_methods = set()

        filenames = self.config.enumerate_modules()
        # Coretasks is special. No custom user coretasks.
        this_dir = os.path.dirname(os.path.abspath(__file__))
        filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py')

        modules = []
        error_count = 0
        for name, filename in filenames.iteritems():
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                filename, lineno = tools.get_raising_file_and_line()
                rel_path = os.path.relpath(filename, os.path.dirname(__file__))
                raising_stmt = "%s:%d" % (rel_path, lineno)
                stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(filename,
                                               os.path.dirname(__file__))
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr("Error in %s setup procedure: %s (%s)" %
                           (name, e, raising_stmt))
Example #30
0
def run(config):
    if config.core.delay is not None:
        delay = config.core.delay
    else:
        delay = 20

    def signal_handler(sig, frame):
        if sig == signal.SIGUSR1 or sig == signal.SIGTERM:
            stderr('Got quit signal, shutting down.')
            p.quit('Closing')
    while True:
        try:
            p = bot.Skrizz(config)
            if hasattr(signal, 'SIGUSR1'):
                signal.signal(signal.SIGUSR1, signal_handler)
            if hasattr(signal, 'SIGTERM'):
                signal.signal(signal.SIGTERM, signal_handler)
            p.run(config.core.host, int(config.core.port))
        except KeyboardInterrupt:
            break
        except Exception, e:
            trace = traceback.format_exc()
            try:
                stderr(trace)
            except:
                pass
            logfile = open(os.path.join(config.logdir, 'exceptions.log'), 'a')
            logfile.write('Critical exception in core')
            logfile.write(trace)
            logfile.write('----------------------------------------\n\n')
            logfile.close()
            os.unlink(config.pid_file_path)
            os._exit(1)

        if not isinstance(delay, int):
            break
        if p.hasquit or config.exit_on_error:
            break
        stderr('Warning: Disconnected. Reconnecting in %s seconds...' % delay)
        time.sleep(delay)
Example #31
0
    def _shutdown(self):
        stderr("Calling shutdown for %d modules." % (len(self.shutdown_methods),))

        hostmask = "%s!%s@%s" % (self.nick, self.user, socket.gethostname())
        willie = self.WillieWrapper(self, irc.Origin(self, hostmask, [], {}))
        for shutdown_method in self.shutdown_methods:
            try:
                stderr("calling %s.%s" % (shutdown_method.__module__, shutdown_method.__name__))
                shutdown_method(willie)
            except Exception as e:
                stderr("Error calling shutdown method for module %s:%s" % (shutdown_method.__module__, e))
Example #32
0
 def log_raw(self, line, prefix):
     """ Log raw line to the raw log """
     if not self.config.core.log_raw:
         return
     if not self.config.core.logdir:
         self.config.core.logdir = os.path.join(self.config.dotdir, "logs")
     if not os.path.isdir(self.config.core.logdir):
         try:
             os.mkdir(self.config.core.logdir)
         except Exception, e:
             stderr("There was a problem creating the logs directory.")
             stderr(e.__class__, str(e))
             stderr("Please fix this and then run Willie again.")
             os._exit(1)
Example #33
0
 def log_raw(self, line, prefix):
     ''' Log raw line to the raw log '''
     if not self.config.core.log_raw:
         return
     if not self.config.core.logdir:
         self.config.core.logdir = os.path.join(self.config.dotdir, 'logs')
     if not os.path.isdir(self.config.core.logdir):
         try:
             os.mkdir(self.config.core.logdir)
         except Exception, e:
             stderr('There was a problem creating the logs directory.')
             stderr('%s %s' % (str(e.__class__), str(e)))
             stderr('Please fix this and then run Willie again.')
             os._exit(1)
Example #34
0
 def log_raw(self, line):
     ''' Log raw line to the raw log '''
     if not self.config.core.log_raw:
         return
     if not self.config.core.logdir:
         self.config.core.logdir = os.path.join(self.config.dotdir,
                                                'logs')
     if not os.path.isdir(self.config.core.logdir):
         try:
             os.mkdir(self.config.core.logdir)
         except Exception, e:
             stderr('There was a problem creating the logs directory.')
             stderr(e.__class__, str(e))
             stderr('Please fix this and then run Willie again.')
             os._exit(1)
Example #35
0
    def _shutdown(self):
        stderr('Calling shutdown for %d modules.' %
               (len(self.shutdown_methods), ))

        hostmask = "%s!%s@%s" % (self.nick, self.user, socket.gethostname())
        willie = self.WillieWrapper(self, irc.Origin(self, hostmask, []))
        for shutdown_method in self.shutdown_methods:
            try:
                stderr("calling %s.%s" % (
                    shutdown_method.__module__,
                    shutdown_method.__name__,
                ))
                shutdown_method(willie)
            except Exception as e:
                stderr("Error calling shutdown method for module %s:%s" %
                       (shutdown_method.__module__, e))
Example #36
0
    def setup(self):
        stderr(
            u"\nXeniBot será ejecutado en la red y canales indicados. Leyendo módulos...\n\n"
        )
        self.callables = set()
        self.shutdown_methods = set()

        filenames = self.config.enumerate_modules()
        # Coretasks is special. No custom user coretasks.
        this_dir = os.path.dirname(os.path.abspath(__file__))
        filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py')

        modules = []
        error_count = 0
        for name, filename in filenames.iteritems():
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                filename, lineno = tools.get_raising_file_and_line()
                rel_path = os.path.relpath(filename, os.path.dirname(__file__))
                raising_stmt = "%s:%d" % (rel_path, lineno)
                stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(filename,
                                               os.path.dirname(__file__))
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr(u"Error in the configuration protocol %s: %s (%s)" %
                           (name, e, raising_stmt))
Example #37
0
    def setup(self):
        stderr(u"\nBenvingut al Willie. Carregant mòduls...\n\n")
        self.callables = set()
        self.shutdown_methods = set()

        filenames = self.config.enumerate_modules()
        # Coretasks is special. No custom user coretasks.
        this_dir = os.path.dirname(os.path.abspath(__file__))
        filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py')

        modules = []
        error_count = 0
        for name, filename in filenames.iteritems():
            try:
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                filename, lineno = tools.get_raising_file_and_line()
                rel_path = os.path.relpath(filename, os.path.dirname(__file__))
                raising_stmt = "%s:%d" % (rel_path, lineno)
                stderr("Error carregant %s: %s (%s)" % (name, e, raising_stmt))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(
                        filename, os.path.dirname(__file__)
                    )
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr(u"Error al procediment de configuració %s: %s (%s)"
                           % (name, e, raising_stmt))
Example #38
0
 def handle_connect(self):
     if self.config.core.use_ssl and has_ssl:
         if not self.config.core.verify_ssl:
             self.ssl = ssl.wrap_socket(self.socket,
                                        do_handshake_on_connect=False,
                                        suppress_ragged_eofs=True)
         else:
             verification = verify_ssl_cn(self.config.host,
                                          int(self.config.port))
             if verification is 'NoCertFound':
                 stderr('Can\'t get server certificate, SSL might be '
                        'disabled on the server.')
                 os.unlink(self.config.pid_file_path)
                 os._exit(1)
             elif verification is not None:
                 stderr('\nSSL Cret information: %s' % verification[1])
                 if verification[0] is False:
                     stderr("Invalid cretficate, CN mismatch!")
                     os.unlink(self.config.pid_file_path)
                     os._exit(1)
             else:
                 stderr('WARNING! certficate information and CN validation '
                        'are not avilable. Is pyOpenSSL installed?')
                 stderr('Trying to connect anyway:')
             self.ssl = ssl.wrap_socket(self.socket,
                                        do_handshake_on_connect=False,
                                        suppress_ragged_eofs=True,
                                        cert_reqs=ssl.CERT_REQUIRED,
                                        ca_certs=self.ca_certs)
         stderr('\nSSL Handshake intiated...')
         error_count = 0
         while True:
             try:
                 self.ssl.do_handshake()
                 break
             except ssl.SSLError, err:
                 if err.args[0] == ssl.SSL_ERROR_WANT_READ:
                     select.select([self.ssl], [], [])
                 elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
                     select.select([], [self.ssl], [])
                 elif err.args[0] == 1:
                     stderr('SSL Handshake failed with error: %s' %
                            err.args[1])
                     os._exit(1)
                 else:
                     error_count = error_count + 1
                     if error_count > 5:
                         stderr('SSL Handshake failed (%d failed attempts)'
                                % error_count)
                         os._exit(1)
                     raise
             except Exception as e:
                 print >> sys.stderr, ('SSL Handshake failed with error: %s'
                                       % e)
                 os._exit(1)
Example #39
0
 def run(self, host, port=6667):
     try:
         self.initiate_connect(host, port)
     except socket.error, e:
         stderr('Connection error: %s' % e.strerror)
         self.hasquit = True
Example #40
0
File: irc.py Project: Hawxy/willie
 def handle_close(self):
     self.close()
     stderr('Closed!')
Example #41
0
 def signal_handler(sig, frame):
     if sig == signal.SIGUSR1 or sig == signal.SIGTERM:
         stderr('Got quit signal, shutting down.')
         p.quit('Closing')
Example #42
0
                            stderr('SSL Handshake failed (%d failed attempts)'
                                   % error_count)
                            os._exit(1)
                        raise
                except Exception as e:
                    print >> sys.stderr, ('SSL Handshake failed with error: %s'
                                          % e)
                    os._exit(1)
            self.set_socket(self.ssl)

        if self.config.core.server_password is not None:
            self.write(('PASS', self.config.core.server_password))
        self.write(('NICK', self.nick))
        self.write(('USER', self.user, '+iw', self.nick), self.name)

        stderr('Connected.')
        self.last_ping_time = datetime.now()
        timeout_check_thread = threading.Thread(target=self._timeout_check)
        timeout_check_thread.start()
        ping_thread = threading.Thread(target=self._send_ping)
        ping_thread.start()

    def _timeout_check(self):
        while True:
            if (datetime.now() - self.last_ping_time).seconds > int(self.config.timeout):
                stderr('Ping timeout reached after %s seconds, closing connection' % self.config.timeout)
                self.handle_close()
                break
            else:
                time.sleep(int(self.config.timeout))
Example #43
0
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(
                        filename, os.path.dirname(__file__)
                    )
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr(u"Error in the configuration protocol %s: %s (%s)"
                           % (name, e, raising_stmt))

        if modules:
            stderr(u'\n\n%d registered modules,' % (len(modules) - 1))
            stderr(u'%d unloaded modules\n\n' % error_count)
        else:
            stderr(u"Warning: I couldn't find any modules!")

        self.bind_commands()

    @staticmethod
    def is_callable(obj):
        """Return true if object is a sopel callable.
        Object must be both be callable and have hashable. Furthermore, it must
        have either "commands", "rule" or "interval" as attributes to mark it
        as a sopel callable.
        """
        if not callable(obj):
            # Check is to help distinguish between sopel callables and objects
Example #44
0
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(
                        filename, os.path.dirname(__file__)
                    )
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr(u"Error in the configuration protocol %s: %s (%s)"
                           % (name, e, raising_stmt))

        if modules:
            stderr(u'\n\n%d registered modules,' % (len(modules) - 1))
            stderr(u'%d unloaded modules\n\n' % error_count)
        else:
            stderr(u"Warning: I couldn't find any modules!")

        self.bind_commands()

    @staticmethod
    def is_callable(obj):
        """Return true if object is a willie callable.

        Object must be both be callable and have hashable. Furthermore, it must
        have either "commands", "rule" or "interval" as attributes to mark it
        as a willie callable.
        """
        if not callable(obj):
Example #45
0
 def handle_connect(self):
     if self.config.core.use_ssl and has_ssl:
         if not self.config.core.verify_ssl:
             self.ssl = ssl.wrap_socket(self.socket,
                                        do_handshake_on_connect=False,
                                        suppress_ragged_eofs=True)
         else:
             verification = verify_ssl_cn(self.config.host,
                                          int(self.config.port))
             if verification is 'NoCertFound':
                 stderr('Can\'t get server certificate, SSL might be '
                        'disabled on the server.')
                 os.unlink(self.config.pid_file_path)
                 os._exit(1)
             elif verification is not None:
                 stderr('\nSSL Cert information: %s' % verification[1])
                 if verification[0] is False:
                     stderr("Invalid certficate, CN mismatch!")
                     os.unlink(self.config.pid_file_path)
                     os._exit(1)
             else:
                 stderr('WARNING! certficate information and CN validation '
                        'are not avilable. Is pyOpenSSL installed?')
                 stderr('Trying to connect anyway:')
             self.ssl = ssl.wrap_socket(self.socket,
                                        do_handshake_on_connect=False,
                                        suppress_ragged_eofs=True,
                                        cert_reqs=ssl.CERT_REQUIRED,
                                        ca_certs=self.ca_certs)
         stderr('\nSSL Handshake intiated...')
         error_count = 0
         while True:
             try:
                 self.ssl.do_handshake()
                 break
             except ssl.SSLError, err:
                 if err.args[0] == ssl.SSL_ERROR_WANT_READ:
                     select.select([self.ssl], [], [])
                 elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
                     select.select([], [self.ssl], [])
                 elif err.args[0] == 1:
                     stderr('SSL Handshake failed with error: %s' %
                            err.args[1])
                     os._exit(1)
                 else:
                     error_count = error_count + 1
                     if error_count > 5:
                         stderr(
                             'SSL Handshake failed (%d failed attempts)' %
                             error_count)
                         os._exit(1)
                     raise
             except Exception as e:
                 print >> sys.stderr, (
                     'SSL Handshake failed with error: %s' % e)
                 os._exit(1)
Example #46
0
 def handle_close(self):
     self.close()
     stderr('Closed!')
Example #47
0
 def handle_close(self):
     self.close()
     stderr("Closed!")
Example #48
0
 def run(self, host, port=6667):
     try:
         self.initiate_connect(host, port)
     except socket.error, e:
         stderr('Connection error: %s' % e.strerror)
         self.hasquit = True
Example #49
0
                    print >> sys.stderr, (
                        'SSL Handshake failed with error: %s' % e)
                    os._exit(1)
            self.set_socket(self.ssl)

        # Request list of server capabilities. IRCv3 servers will respond with
        # CAP * LS (which we handle in coretasks). v2 servers will respond with
        # 421 Unknown command, which we'll ignore
        self.write(('CAP', 'LS'))

        if self.config.core.server_password is not None:
            self.write(('PASS', self.config.core.server_password))
        self.write(('NICK', self.nick))
        self.write(('USER', self.user, '+iw', self.nick), self.name)

        stderr('Connected.')
        self.last_ping_time = datetime.now()
        timeout_check_thread = threading.Thread(target=self._timeout_check)
        timeout_check_thread.start()
        ping_thread = threading.Thread(target=self._send_ping)
        ping_thread.start()

    def _timeout_check(self):
        while True:
            if (datetime.now() - self.last_ping_time).seconds > int(
                    self.config.timeout):
                stderr('Ping timeout reached after %s seconds,' +
                       ' closing connection' % self.config.timeout)
                self.handle_close()
                break
            else:
Example #50
0
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(filename,
                                               os.path.dirname(__file__))
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr(u"Error in the configuration protocol %s: %s (%s)" %
                           (name, e, raising_stmt))

        if modules:
            stderr(u'\n\n%d módulos existentes,' % (len(modules) - 1))
            stderr(u'%d módulos no cargados.\n\n' % error_count)
        else:
            stderr(u"Warning: I couldn't find any modules!")

        self.bind_commands()

    @staticmethod
    def is_callable(obj):
        """Return true if object is a sopel callable.
        Object must be both be callable and have hashable. Furthermore, it must
        have either "commands", "rule" or "interval" as attributes to mark it
        as a sopel callable.
        """
        if not callable(obj):
            # Check is to help distinguish between sopel callables and objects
Example #51
0
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    filename, lineno = tools.get_raising_file_and_line()
                    rel_path = os.path.relpath(filename,
                                               os.path.dirname(__file__))
                    raising_stmt = "%s:%d" % (rel_path, lineno)
                    stderr("Error in %s setup procedure: %s (%s)" %
                           (name, e, raising_stmt))

        if modules:
            stderr('\n\nRegistered %d modules,' % (len(modules) - 1))
            stderr('%d modules failed to load\n\n' % error_count)
        else:
            stderr("Warning: Couldn't find any modules")

        self.bind_commands()

    @staticmethod
    def is_callable(obj):
        """Return true if object is a willie callable.

        Object must be both be callable and have hashable. Furthermore, it must
        have either "commands", "rule" or "interval" as attributes to mark it
        as a willie callable.
        """
        if not callable(obj):
Example #52
0
                    print >> sys.stderr, (
                        'SSL Handshake failed with error: %s' % e)
                    os._exit(1)
            self.set_socket(self.ssl)

        # Request list of server capabilities. IRCv3 servers will respond with
        # CAP * LS (which we handle in coretasks). v2 servers will respond with
        # 421 Unknown command, which we'll ignore
        self.write(('CAP', 'LS'))

        if self.config.core.server_password is not None:
            self.write(('PASS', self.config.core.server_password))
        self.write(('NICK', self.nick))
        self.write(('USER', self.user, '+iw', self.nick), self.name)

        stderr('¡Conectado!.')
        self.last_ping_time = datetime.now()
        timeout_check_thread = threading.Thread(target=self._timeout_check)
        timeout_check_thread.start()
        ping_thread = threading.Thread(target=self._send_ping)
        ping_thread.start()

    def _timeout_check(self):
        while True:
            if (datetime.now() - self.last_ping_time).seconds > int(
                    self.config.timeout):
                stderr('Ping timeout reached after %s seconds,' +
                       ' closing connection' % self.config.timeout)
                self.handle_close()
                break
            else:
Example #53
0
File: bot.py Project: Airead/daeria
            except Exception, e:
                error_count = error_count + 1
                stderr("Error loading %s: %s (in bot.py)" % (name, e))
            else:
                try:
                    if hasattr(module, 'setup'):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    stderr("Error in %s setup procedure: %s (in bot.py)"
                           % (name, e))

        if modules:
            stderr('\n\nRegistered %d modules,' % (len(modules) - 1))
            stderr('%d modules failed to load\n\n' % error_count)
        else:
            stderr("Warning: Couldn't find any modules")

        self.bind_commands()

    def register(self, variables):
        """
        With the ``__dict__`` attribute from a Willie module, update or add the
        trigger commands and rules to allow the function to be triggered.
        """
        # This is used by reload.py, hence it being methodised
        for name, obj in variables.iteritems():
            if hasattr(obj, 'commands') or hasattr(obj, 'rule'):
                self.variables[name] = obj
Example #54
0
 def signal_handler(sig, frame):
     if sig == signal.SIGUSR1 or sig == signal.SIGTERM:
         stderr('Got quit signal, shutting down.')
         p.quit('Closing')
Example #55
0
                module = imp.load_source(name, filename)
            except Exception, e:
                error_count = error_count + 1
                stderr("Error loading %s: %s (in bot.py)" % (name, e))
            else:
                try:
                    if hasattr(module, "setup"):
                        module.setup(self)
                    self.register(vars(module))
                    modules.append(name)
                except Exception, e:
                    error_count = error_count + 1
                    stderr("Error in %s setup procedure: %s (in bot.py)" % (name, e))

        if modules:
            stderr("\n\nRegistered %d modules," % len(modules))
            stderr("%d modules failed to load\n\n" % error_count)
        else:
            stderr("Warning: Couldn't find any modules")

        self.bind_commands()

    def register(self, variables):
        """
        With the ``__dict__`` attribute from a Willie module, update or add the
        trigger commands and rules to allow the function to be triggered.
        """
        # This is used by reload.py, hence it being methodised
        for name, obj in variables.iteritems():
            if hasattr(obj, "commands") or hasattr(obj, "rule"):
                self.variables[name] = obj