home · contact · privacy
Some refactoring and variable renaming.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 17 Jan 2016 21:33:31 +0000 (22:33 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 17 Jan 2016 21:33:31 +0000 (22:33 +0100)
plomlombot.py

index bea14223adea1ef355512111f804bc92338d9561..1e904fa79f1eb9a1ac9b68216b01ba2eabd77206 100644 (file)
@@ -6,20 +6,20 @@ import re
 import urllib.request
 import html
 
 import urllib.request
 import html
 
-servernet = "irc.freenode.net"
-port = 6667
-servername = ""
-timeout = 240
-username = "plomlombot"
-nickname = username
-channel = "#zrolaps"
+SERVERNET = "irc.freenode.net"
+PORT = 6667
+TIMEOUT = 240
+USERNAME = "plomlombot"
+NICKNAME = USERNAME
+CHANNEL = "#zrolaps-test"
 
 class ExceptionForRestart(Exception):
     pass
 
 class IO:
 
 
 class ExceptionForRestart(Exception):
     pass
 
 class IO:
 
-    def __init__(self, servernet, port):
+    def __init__(self, servernet, port, timeout):
+        self.timeout = timeout
         self.socket = socket.socket()
         self.socket.connect((servernet, port))
         self.socket.setblocking(0)
         self.socket = socket.socket()
         self.socket.connect((servernet, port))
         self.socket.setblocking(0)
@@ -29,11 +29,11 @@ class IO:
         self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
 
     def _pingtest(self, send_ping=True):
         self.servername = self.recv_line(send_ping=False).split(" ")[0][1:]
 
     def _pingtest(self, send_ping=True):
-        if self.last_pong + timeout < time.time():
+        if self.last_pong + self.timeout < time.time():
             print("SERVER NOT ANSWERING")
             raise ExceptionForRestart
         if send_ping:
             print("SERVER NOT ANSWERING")
             raise ExceptionForRestart
         if send_ping:
-            self.send_line("PING " + nickname + " " + self.servername)
+            self.send_line("PING " + self.servername)
 
     def send_line(self, msg):
         msg = msg.replace("\r", " ")
 
     def send_line(self, msg):
         msg = msg.replace("\r", " ")
@@ -56,7 +56,7 @@ class IO:
         if len(self.line_buffer) > 0:
             return self.line_buffer.pop(0)
         while True:
         if len(self.line_buffer) > 0:
             return self.line_buffer.pop(0)
         while True:
-            ready = select.select([self.socket], [], [], int(timeout / 2))
+            ready = select.select([self.socket], [], [], int(self.timeout / 2))
             if not ready[0]:
                 self._pingtest(send_ping)
                 return None
             if not ready[0]:
                 self._pingtest(send_ping)
                 return None
@@ -79,12 +79,12 @@ class IO:
             line)
         return line
 
             line)
         return line
 
-def init_connection():
-    print("CONNECTING TO " + servernet)
-    io = IO(servernet, port)
-    io.send_line("NICK " + nickname)
-    io.send_line("USER " + username + " 0 * : ")
-    io.send_line("JOIN " + channel)
+def init_session():
+    print("CONNECTING TO " + SERVERNET)
+    io = IO(SERVERNET, PORT, TIMEOUT)
+    io.send_line("NICK " + NICKNAME)
+    io.send_line("USER " + USERNAME + " 0 * : ")
+    io.send_line("JOIN " + CHANNEL)
     return io
 
 def lineparser_loop():
     return io
 
 def lineparser_loop():
@@ -128,7 +128,7 @@ def lineparser_loop():
             if rune != ":":
                 receiver += rune
         target = sender
             if rune != ":":
                 receiver += rune
         target = sender
-        if receiver != nickname:
+        if receiver != NICKNAME:
             target = receiver
         msg = str.join(" ", tokens[3:])[1:]
         url_check(msg)
             target = receiver
         msg = str.join(" ", tokens[3:])[1:]
         url_check(msg)
@@ -146,7 +146,7 @@ def lineparser_loop():
 
 while 1:
     try:
 
 while 1:
     try:
-        io = init_connection()
+        io = init_session()
         lineparser_loop()
     except ExceptionForRestart:
         io.socket.close()
         lineparser_loop()
     except ExceptionForRestart:
         io.socket.close()