IrcMiRCARTBot.py: adds optional -[46] force IPv[46] flag.

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-06-28 18:18:48 +02:00
parent 85d260835b
commit fd8c9a4267
2 changed files with 25 additions and 15 deletions

View File

@ -38,12 +38,14 @@ class IrcClient:
self.clientSocket.close() self.clientSocket.close()
self.clientSocket = self.clientSocketFile = None; self.clientSocket = self.clientSocketFile = None;
# }}} # }}}
# {{{ connect(self, timeout=None): Connect to server and register w/ optional timeout # {{{ connect(self, preferFamily=socket.AF_INET, timeout=None): Connect to server and register w/ optional timeout
def connect(self, timeout=None): def connect(self, preferFamily=socket.AF_INET, timeout=None):
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) gaiInfo = socket.getaddrinfo(self.serverHname, self.serverPort,
preferFamily, socket.SOCK_STREAM, socket.IPPROTO_TCP)
self.clientSocket = socket.socket(*gaiInfo[0][:3])
self.clientSocket.setblocking(0) self.clientSocket.setblocking(0)
try: try:
self.clientSocket.connect((self.serverHname, int(self.serverPort))) self.clientSocket.connect(gaiInfo[0][4])
except BlockingIOError: except BlockingIOError:
pass pass
if timeout: if timeout:

View File

@ -22,8 +22,9 @@
# SOFTWARE. # SOFTWARE.
# #
from getopt import getopt, GetoptError
import base64 import base64
import os, sys, time import os, socket, sys, time
import json import json
import IrcClient import IrcClient
import requests, urllib.request import requests, urllib.request
@ -206,10 +207,10 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
if (totalSize > pow(2,20)): if (totalSize > pow(2,20)):
raise IrcMiRCARTBot.ContentTooLargeException raise IrcMiRCARTBot.ContentTooLargeException
# }}} # }}}
# {{{ connect(self, timeout=None): Connect to server and (re)initialise w/ optional timeout # {{{ connect(self, preferFamily=0, timeout=None): Connect to server and (re)initialise w/ optional timeout
def connect(self, timeout=None): def connect(self, preferFamily=0, timeout=None):
self._log("Connecting to {}:{}...".format(self.serverHname, self.serverPort)) self._log("Connecting to {}:{}...".format(self.serverHname, self.serverPort))
if super().connect(timeout): if super().connect(preferFamily=preferFamily, timeout=timeout):
self._log("Connected to {}:{}.".format(self.serverHname, self.serverPort)) self._log("Connected to {}:{}.".format(self.serverHname, self.serverPort))
self._log("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos)) self._log("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
self.clientLastMessage = 0; self.clientChannelOps = []; self.clientLastMessage = 0; self.clientChannelOps = [];
@ -255,18 +256,25 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
# #
# Entry point # Entry point
def main(*argv): def main(optdict, *argv):
_IrcMiRCARTBot = IrcMiRCARTBot(*argv[1:]) _IrcMiRCARTBot = IrcMiRCARTBot(*argv)
while True: while True:
if _IrcMiRCARTBot.connect(15): if "-4" in optdict:
preferFamily = socket.AF_INET
elif "-6" in optdict:
preferFamily = socket.AF_INET6
else:
preferFamily = 0
if _IrcMiRCARTBot.connect(preferFamily=preferFamily, timeout=15):
_IrcMiRCARTBot.dispatch() _IrcMiRCARTBot.dispatch()
_IrcMiRCARTBot.close() _IrcMiRCARTBot.close()
time.sleep(15) time.sleep(15)
if __name__ == "__main__": if __name__ == "__main__":
if ((len(sys.argv) - 1) < 1)\ optlist, argv = getopt(sys.argv[1:], "46")
or ((len(sys.argv) - 1) > 4): optdict = dict(optlist)
print("usage: {} " \ if len(argv) < 1 or len(argv) > 4:
print("usage: {} [-4|-6] " \
"<IRC server hostname> " \ "<IRC server hostname> " \
"[<IRC server port; defaults to 6667>] " \ "[<IRC server port; defaults to 6667>] " \
"[<IRC bot nick name; defaults to pngbot>] " \ "[<IRC bot nick name; defaults to pngbot>] " \
@ -274,6 +282,6 @@ if __name__ == "__main__":
"[<IRC bot real name; defaults to pngbot>] " \ "[<IRC bot real name; defaults to pngbot>] " \
"[<IRC bot channel name; defaults to #MiRCART>] ".format(sys.argv[0]), file=sys.stderr) "[<IRC bot channel name; defaults to #MiRCART>] ".format(sys.argv[0]), file=sys.stderr)
else: else:
main(*sys.argv) main(optdict, *argv)
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120