Skip to content

Commit

Permalink
Merge branch 'python3.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
dmptrluke committed Jan 24, 2015
2 parents a172b7d + e3dab55 commit e41dd83
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 313 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Changelog
- **1.0.3** - More minor changes to plugins, fixed rate-limiting properly, banished SCP to CloudBotIRC/Plugins, added wildcard support to permissions (note: don't use this yet, it's still not entirely finalized!)
- **1.0.2** - Minor internal changes and fixes, banished minecraft_bukget and worldofwarcraft to CloudBotIRC/Plugins
- **1.0.1** - Fix history.py tracking
- **1.0.0** - Initial stable release
2 changes: 1 addition & 1 deletion cloudbot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
import os

__version__ = "1.0.2"
__version__ = "1.0.3"

__all__ = ["util", "bot", "connection", "config", "permissions", "plugin", "event", "hook", "log_dir"]

Expand Down
4 changes: 4 additions & 0 deletions cloudbot/bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import time
import logging
import collections
import re
import os
import gc
Expand Down Expand Up @@ -61,6 +62,9 @@ def __init__(self, loop=asyncio.get_event_loop()):
# for plugins
self.logger = logger

# for plugins to abuse
self.memory = collections.defaultdict()

# declare and create data folder
self.data_dir = os.path.abspath('data')
if not os.path.exists(self.data_dir):
Expand Down
4 changes: 4 additions & 0 deletions cloudbot/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
import collections

from cloudbot.permissions import PermissionManager

Expand Down Expand Up @@ -48,6 +49,9 @@ def __init__(self, bot, name, nick, *, channels=None, config=None):
# create permissions manager
self.permissions = PermissionManager(self)

# for plugins to abuse
self.memory = collections.defaultdict()

def describe_server(self):
raise NotImplementedError

Expand Down
2 changes: 1 addition & 1 deletion cloudbot/clients/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def send(self, line):
# make sure we are connected before sending
if not self._connected:
yield from self._connected_future
line = line.splitlines()[0][:500] + "\r\n"
line = line[:510] + "\r\n"
data = line.encode("utf-8", "replace")
self._transport.write(data)

Expand Down
20 changes: 9 additions & 11 deletions cloudbot/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,15 @@ def has_perm_mask(self, user_mask, perm, notice=True):
if fnmatch(user_mask.lower(), backdoor.lower()):
return True

if not perm.lower() in self.perm_users:
# no one has access
return False

allowed_users = self.perm_users[perm.lower()]

for allowed_mask in allowed_users:
if fnmatch(user_mask.lower(), allowed_mask):
if notice:
logger.info("[{}|permissions] Allowed user {} access to {}".format(self.name, user_mask, perm))
return True
perm = perm.lower()

for user_perm, allowed_users in self.perm_users.items():
if fnmatch(perm, user_perm):
for allowed_mask in allowed_users:
if fnmatch(allowed_mask, user_mask.lower()):
if notice:
logger.info("[{}|permissions] Allowed user {} access to {}".format(self.name, user_mask, perm))
return True

return False

Expand Down
2 changes: 1 addition & 1 deletion cloudbot/util/cleverbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,6 @@ def quote(s, safe='/'):
safe_map[c] = (c in safe) and c or ('%%%02X' % i)
try:
res = list(map(safe_map.__getitem__, s))
except:
except Exception:
return ''
return ''.join(res)
4 changes: 2 additions & 2 deletions cloudbot/util/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,13 @@ def dict_format(args, formats):
m = f.format(**args)
# Insert match and number of matched values (max matched values if already in dict)
matches[m] = max([matches.get(m, 0), len(re.findall(r'(\{.*?\})', f))])
except:
except Exception:
continue

# Return most complete match, ranked by values matched and then my match length or None
try:
return max(matches.items(), key=lambda x: (x[1], len(x[0])))[0]
except:
except Exception:
return None


Expand Down
33 changes: 11 additions & 22 deletions plugins/core_sieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,37 @@
from cloudbot import hook
from cloudbot.util.tokenbucket import TokenBucket

inited = []

# when STRICT is enabled, every time a user gets ratelimted it wipes
# their tokens so they have to wait at least X seconds to regen

ready = False
buckets = {}

logger = logging.getLogger("cloudbot")


def task_clear(loop):
for uid, _bucket in buckets:
global buckets
for uid, _bucket in buckets.copy().items():
if (time() - _bucket.timestamp) > 600:
del buckets[uid]
loop.call_later(600, task_clear, loop)
loop.call_later(10, task_clear, loop)


@asyncio.coroutine
@hook.irc_raw('004')
def init_tasks(loop, conn):
global inited
if conn.name in inited:
global ready
if ready:
# tasks already started
return

logger.info("[{}|sieve] Bot is starting ratelimiter cleanup task.".format(conn.name))
loop.call_later(600, task_clear, loop)
inited.append(conn.name)
loop.call_later(10, task_clear, loop)
ready = True


@asyncio.coroutine
@hook.sieve
def sieve_suite(bot, event, _hook):
"""
this function stands between your users and the commands they want to use. it decides if they can or not
:type bot: cloudbot.bot.CloudBot
:type event: cloudbot.event.Event
:type _hook: cloudbot.plugin.Hook
"""
global buckets

conn = event.conn
# check ignore bots
if event.irc_command == 'PRIVMSG' and event.nick.endswith('bot') and _hook.ignore_bots:
Expand Down Expand Up @@ -83,8 +75,7 @@ def sieve_suite(bot, event, _hook):

# check command spam tokens
if _hook.type == "command":
# right now ratelimiting is per-channel, but this can be changed
uid = (event.chan, event.nick.lower())
uid = "!".join([conn.name, event.chan, event.nick]).lower()

tokens = conn.config.get('ratelimit', {}).get('tokens', 17.5)
restore_rate = conn.config.get('ratelimit', {}).get('restore_rate', 2.5)
Expand All @@ -110,5 +101,3 @@ def sieve_suite(bot, event, _hook):
return None

return event


1 change: 0 additions & 1 deletion plugins/core_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import asyncio
import logging
import re
import functools
from collections import deque

from cloudbot import hook
Expand Down
99 changes: 0 additions & 99 deletions plugins/dice.py

This file was deleted.

2 changes: 1 addition & 1 deletion plugins/geoip.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def update_db():
else:
try:
return geoip2.database.Reader(PATH)
except:
except geoip2.errors.GeoIP2Error:
# issue loading, geo
fetch_db()
return geoip2.database.Reader(PATH)
Expand Down
2 changes: 1 addition & 1 deletion plugins/ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def ignore_sieve(bot, event, _hook):
return event

# don't block an event that could be unignoring
if _hook.type == "command" and event.triggered_command == "unignore":
if _hook.type == "command" and event.triggered_command in ("unignore", "global_unignore"):
return event

if event.mask is None:
Expand Down
7 changes: 3 additions & 4 deletions plugins/imgur.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random

from imgurpython import ImgurClient
from contextlib import suppress

from cloudbot import hook
from cloudbot.util import web
Expand Down Expand Up @@ -85,11 +86,9 @@ def imgur(text):
title = item.title

# if it's an imgur meme, add the meme name
try:
# if not, AttributeError will trigger and code will carry on
with suppress(AttributeError):
title = "\x02{}\x02 - {}".format(item.meme_metadata["meme_name"].lower(), title)
except:
# this is a super un-important thing, so if it fails we don't care, carry on
pass

# if the item has a tag, show that
if item.section:
Expand Down
19 changes: 10 additions & 9 deletions plugins/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def password(text, notice):

if length > 50:
notice("Maximum length is 50 characters.")
return

# add alpha characters
if "alpha" in text or "letter" in text:
Expand Down Expand Up @@ -65,18 +66,18 @@ def password(text, notice):
@hook.command("wpass", "wordpass", "wordpassword", autohelp=False)
def word_password(text, notice):
"""[length] - generates an easy to remember password with [length] (default 4) commonly used words"""
if text:
try:
length = int(text)
except ValueError:
notice("Invalid input '{}'".format(text))
return
else:
length = 4
try:
length = int(text)
except ValueError:
length = 3

if length > 10:
notice("Maximum length is 50 characters.")
return

words = []
# generate password
for x in range(length):
words.append(gen.choice(common_words))

notice("Your password is '{}'. Feel free to remove the spaces when using it.".format(" ".join(words)))

7 changes: 5 additions & 2 deletions plugins/quote.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

from cloudbot import hook
from cloudbot.util import botvars

from sqlalchemy import select
from sqlalchemy import Table, Column, String, PrimaryKeyConstraint
from sqlalchemy.types import REAL
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError


qtable = Table(
'quote',
Expand Down Expand Up @@ -40,7 +43,7 @@ def add_quote(db, chan, target, sender, message):
)
db.execute(query)
db.commit()
except:
except IntegrityError:
return "Message already stored, doing nothing."
return "Quote added."

Expand Down
Loading

0 comments on commit e41dd83

Please sign in to comment.