Skip to content

Commit

Permalink
core updates (made IPv6 optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
myano committed Oct 21, 2014
1 parent 0e95a90 commit 624211d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
17 changes: 13 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ def decode(bytes):

class Jenni(irc.Bot):
def __init__(self, config):
lc_pm = None
if hasattr(config, "logchan_pm"): lc_pm = config.logchan_pm
else: lc_pm = None
logging = False
if hasattr(config, "logging"): logging = config.logging
else: logging = False
args = (config.nick, config.name, config.channels, config.password, lc_pm, logging)
ipv6 = False
if hasattr(config, 'ipv6'): ipv6 = config.ipv6
args = (config.nick, config.name, config.channels, config.password, lc_pm, logging, ipv6)
## next, try putting a try/except around the following line
irc.Bot.__init__(self, *args)
self.config = config
self.doc = {}
self.stats = {}
self.times = {}
self.excludes = {}
if hasattr(config, 'excludes'):
self.excludes = config.excludes
self.setup()
Expand Down Expand Up @@ -221,14 +224,20 @@ def __new__(cls, text, origin, bytes, match, event, args):

def call(self, func, origin, jenni, input):
nick = (input.nick).lower()

## rate limiting
if nick in self.times:
if func in self.times[nick]:
if not input.admin:
## admins are not rate limited
if time.time() - self.times[nick][func] < func.rate:
self.times[nick][func] = time.time()
return
else: self.times[nick] = dict()
else:
self.times[nick] = dict()

self.times[nick][func] = time.time()

try:
if hasattr(self, 'excludes'):
if input.sender in self.excludes:
Expand Down
40 changes: 26 additions & 14 deletions irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def log_raw(line):
f.close()

class Bot(asynchat.async_chat):
def __init__(self, nick, name, channels, password=None, logchan_pm=None, logging=False):
def __init__(self, nick, name, channels, password=None, logchan_pm=None, logging=False, ipv6=False):
asynchat.async_chat.__init__(self)
self.set_terminator('\n')
self.buffer = ''
Expand All @@ -82,6 +82,7 @@ def __init__(self, nick, name, channels, password=None, logchan_pm=None, logging
self.stack_log = list()
self.logchan_pm = logchan_pm
self.logging = logging
self.ipv6 = ipv6

import threading
self.sending = threading.RLock()
Expand Down Expand Up @@ -151,20 +152,24 @@ def initiate_connect(self, host, port):
self.send = self._ssl_send
self.recv = self._ssl_recv

for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.create_socket(af,socktype)
except socket.error as msg:
continue
try:
self.connect(sa)
except socket.error as msg:
self.close()
continue
break
if self.ipv6:
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.create_socket(af,socktype)
except socket.error as msg:
continue
try:
self.connect(sa)
except socket.error as msg:
self.close()
continue
break
else:
raise Exception("No connectivity")
else:
raise Exception("No connectivity")
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))

try: asyncore.loop()
except KeyboardInterrupt:
Expand All @@ -186,6 +191,8 @@ def handle_connect(self):
select.select([], [self.ssl], [])
else:
raise
except:
continue
self.set_socket(self.ssl)

if self.verbose:
Expand Down Expand Up @@ -250,16 +257,21 @@ def collect_incoming_data(self, data):

def found_terminator(self):
line = self.buffer

if line.endswith('\r'):
line = line[:-1]

if line:
if self.logchan_pm:
## if logging to logging channel is enabled
## send stuff in PM to logging channel
dlist = line.split()
if len(dlist) >= 3:
if "#" not in dlist[2] and dlist[1].strip() not in IRC_CODES:
self.msg(self.logchan_pm, line, True)
if self.logging:
## if logging (to log file) is enabled
## send stuff to the log file
log_raw(line)

self.buffer = ''
Expand Down
2 changes: 1 addition & 1 deletion jenni
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def main(argv=None):
module.prefix = r'\.'

if not hasattr(module, 'name'):
module.name = 'jenni yanosbot, https://github.com/myano/jenni'
module.name = 'jenni yanosbot, http://git.io/jenni'

if not hasattr(module, 'port'):
module.port = 6667
Expand Down
6 changes: 4 additions & 2 deletions modules/sasl.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ def irc_cap (jenni, input):


def irc_authenticated (jenni, input):
auth = "\0".join((jenni.nick, jenni.nick, jenni.password))
auth = base64.b64encode(auth)
auth = False
if hasattr(jenni.config, 'nick') and hasattr(jenni.config, 'password'):
auth = "\0".join((jenni.config.nick, jenni.config.nick, jenni.config.password))
auth = base64.b64encode(auth)

if not auth:
jenni.write(('AUTHENTICATE', '+'))
Expand Down

0 comments on commit 624211d

Please sign in to comment.