Commits

Jason R. Coombs  committed 84242a9

Extracted _handle_incoming

  • Participants
  • Parent commits da15ec0

Comments (0)

Files changed (1)

File irc/server.py

         self.topic = topic
         self.clients = set()
 
+class ClientDisconnect(BaseException):
+    pass
+
 class IRCClient(_py2_compat.socketserver.BaseRequestHandler):
     """
     IRC client connect and command handling. Client connection is handled by
     def handle(self):
         log.info('Client connected: %s', self.client_ident())
 
-        while self._handle_one():
-            pass
-
-        self.request.close()
+        try:
+            while self._handle_one():
+                pass
+        except ClientDisconnect:
+            self.request.close()
 
     def _handle_one(self):
         """
-        Handle one read/write cycle. Return True unless the client has
-        disconnected.
+        Handle one read/write cycle.
         """
-        buf = ''
         ready_to_read, ready_to_write, in_error = select.select(
             [self.request], [], [], 0.1)
 
 
         # See if the client has any commands for us.
         if len(ready_to_read) == 1 and ready_to_read[0] == self.request:
-            data = self.request.recv(1024)
+            self._handle_incoming()
 
-            if not data:
-                return False
-            elif len(data) > 0:
-                # There is data. Process it and turn it into line-oriented
-                #  input.
-                buf += str(data)
+    def _handle_incoming(self):
+        buf = ''
+        data = self.request.recv(1024)
 
-                while buf.find("\n") != -1:
-                    line, buf = buf.split("\n", 1)
-                    line = line.rstrip()
+        if not data:
+            raise ClientDisconnect()
+        elif len(data) > 0:
+            # There is data. Process it and turn it into line-oriented
+            #  input.
+            buf += str(data)
 
-                    response = ''
-                    try:
-                        log.debug('from %s: %s' % (self.client_ident(),
-                            line))
-                        if ' ' in line:
-                            command, params = line.split(' ', 1)
-                        else:
-                            command = line
-                            params = ''
-                        handler = getattr(self,
-                            'handle_%s' % (command.lower()), None)
-                        if not handler:
-                            log.info('No handler for command: %s. '
-                                'Full line: %s' % (command, line))
-                            raise IRCError(events.codes['unknowncommand'],
-                                '%s :Unknown command' % (command))
-                        response = handler(params)
-                    except AttributeError as e:
-                        raise e
-                        log.error('%s' % (e))
-                    except IRCError as e:
-                        response = ':%s %s %s' % (self.server.servername,
-                            e.code, e.value)
-                        log.error('%s' % (response))
-                    except Exception as e:
-                        response = ':%s ERROR %s' % (self.server.servername,
-                            repr(e))
-                        log.error('%s' % (response))
-                        raise
+            while buf.find("\n") != -1:
+                line, buf = buf.split("\n", 1)
+                line = line.rstrip()
 
-                    if response:
-                        log.debug('to %s: %s' % (self.client_ident(),
-                            response))
-                        self.request.send(response + '\r\n')
-        return True
+                response = ''
+                try:
+                    log.debug('from %s: %s' % (self.client_ident(),
+                        line))
+                    if ' ' in line:
+                        command, params = line.split(' ', 1)
+                    else:
+                        command = line
+                        params = ''
+                    handler = getattr(self,
+                        'handle_%s' % (command.lower()), None)
+                    if not handler:
+                        log.info('No handler for command: %s. '
+                            'Full line: %s' % (command, line))
+                        raise IRCError(events.codes['unknowncommand'],
+                            '%s :Unknown command' % (command))
+                    response = handler(params)
+                except AttributeError as e:
+                    raise e
+                    log.error('%s' % (e))
+                except IRCError as e:
+                    response = ':%s %s %s' % (self.server.servername,
+                        e.code, e.value)
+                    log.error('%s' % (response))
+                except Exception as e:
+                    response = ':%s ERROR %s' % (self.server.servername,
+                        repr(e))
+                    log.error('%s' % (response))
+                    raise
+
+                if response:
+                    log.debug('to %s: %s' % (self.client_ident(),
+                        response))
+                    self.request.send(response + '\r\n')
 
     def handle_nick(self, params):
         """