Skip to content

Commit df302d8

Browse files
authored
Fix inconsistent parsing of user and subreddit parameters in reddit plugins (CloudBotIRC#229)
1 parent 84b99e9 commit df302d8

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

plugins/reddit.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
from cloudbot import hook
99
from cloudbot.util import timeformat, formatting
1010

11+
sub_re = re.compile(r'^(?:/?r/)?(?P<name>.+)$', re.IGNORECASE)
12+
13+
14+
def get_sub(text):
15+
match = sub_re.match(text)
16+
if match:
17+
return match.group('name')
18+
19+
1120
reddit_re = re.compile(
1221
r"""
1322
https? # Scheme
@@ -89,7 +98,8 @@ def reddit(text, bot, reply):
8998
if text:
9099
# clean and split the input
91100
parts = text.lower().strip().split()
92-
url = base_url.format(parts.pop(0).strip())
101+
sub = get_sub(parts.pop(0).strip())
102+
url = base_url.format(sub)
93103

94104
# find the requested post number (if any)
95105
if parts:

plugins/reddit_info.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from collections import defaultdict
23
from datetime import datetime
34

@@ -10,13 +11,41 @@
1011
from cloudbot.util.pager import paginated_list
1112

1213
search_pages = defaultdict(dict)
14+
user_re = re.compile(r'^(?:/?u(?:ser)?/)?(?P<name>.+)$', re.IGNORECASE)
15+
sub_re = re.compile(r'^(?:/?r/)?(?P<name>.+)$', re.IGNORECASE)
1316

1417
user_url = "http://reddit.com/user/{}/"
1518
subreddit_url = "http://reddit.com/r/{}/"
1619
# This agent should be unique for your cloudbot instance
1720
agent = {"User-Agent": "gonzobot a cloudbot (IRCbot) implementation for snoonet.org by /u/bloodygonzo"}
1821

1922

23+
def test_get_user():
24+
assert get_sub('test') == 'test'
25+
assert get_sub('r/test') == 'test'
26+
assert get_sub('/r/test') == 'test'
27+
28+
29+
def test_get_sub():
30+
assert get_user('test') == 'test'
31+
assert get_user('/u/test') == 'test'
32+
assert get_user('u/test') == 'test'
33+
assert get_user('/user/test') == 'test'
34+
assert get_user('user/test') == 'test'
35+
36+
37+
def get_user(text):
38+
match = user_re.match(text)
39+
if match:
40+
return match.group('name')
41+
42+
43+
def get_sub(text):
44+
match = sub_re.match(text)
45+
if match:
46+
return match.group('name')
47+
48+
2049
def statuscheck(status, item):
2150
"""since we are doing this a lot might as well return something more meaningful"""
2251
if status == 404:
@@ -60,7 +89,7 @@ def moremod(text, chan, conn):
6089
@hook.command("subs", "moderates", singlethread=True)
6190
def moderates(text, chan, conn, reply):
6291
"""<username> - This plugin prints the list of subreddits a user moderates listed in a reddit users profile. Private subreddits will not be listed."""
63-
user = text
92+
user = get_user(text)
6493
r = requests.get(user_url.format(user) + "moderated_subreddits.json", headers=agent)
6594
try:
6695
r.raise_for_status()
@@ -87,7 +116,7 @@ def moderates(text, chan, conn, reply):
87116
@hook.command("karma", "ruser", singlethread=True)
88117
def karma(text, reply):
89118
"""<reddituser> - will return the information about the specified reddit username"""
90-
user = text
119+
user = get_user(text)
91120
url = user_url + "about.json"
92121
r = requests.get(url.format(user), headers=agent)
93122
try:
@@ -133,7 +162,7 @@ def karma(text, reply):
133162
@hook.command("cakeday", singlethread=True)
134163
def cake_day(text, reply):
135164
"""<reddituser> - will return the cakeday for the given reddit username."""
136-
user = text
165+
user = get_user(text)
137166
url = user_url + "about.json"
138167
r = requests.get(url.format(user), headers=agent)
139168

@@ -171,11 +200,7 @@ def time_format(numdays):
171200
@hook.command("submods", "mods", "rmods", singlethread=True)
172201
def submods(text, chan, conn, reply):
173202
"""<subreddit> - prints the moderators of the specified subreddit."""
174-
sub = text
175-
if sub.startswith('/r/'):
176-
sub = sub[3:]
177-
elif sub.startswith('r/'):
178-
sub = sub[2:]
203+
sub = get_sub(text)
179204
url = subreddit_url + "about/moderators.json"
180205
r = requests.get(url.format(sub), headers=agent)
181206

@@ -211,11 +236,7 @@ def submods(text, chan, conn, reply):
211236
@hook.command("subinfo", "subreddit", "sub", "rinfo", singlethread=True)
212237
def subinfo(text, reply):
213238
"""<subreddit> - fetches information about the specified subreddit."""
214-
sub = text
215-
if sub.startswith('/r/'):
216-
sub = sub[3:]
217-
elif sub.startswith('r/'):
218-
sub = sub[2:]
239+
sub = get_sub(text)
219240
url = subreddit_url + "about.json"
220241
r = requests.get(url.format(sub), headers=agent)
221242

0 commit comments

Comments
 (0)