(.*?) )|(?:(.*?))')
try:
word = line2[4]
except Exception:
reply("Please enter a word.", chan, nick)
return
word = urllib.quote(word.encode('utf-8'))
bytes = web.get(uri % word)
results = {}
wordkind = None
for kind, sense in r_info.findall(bytes):
kind, sense = trim(kind), trim(sense)
if kind: wordkind = kind
elif sense:
results.setdefault(wordkind, []).append(sense)
result = word.encode('utf-8') + ' - '
for key in sorted(results.keys()):
if results[key]:
result += (key or '') + ' 1. ' + results[key][0]
if len(results[key]) > 1:
result += ', 2. ' + results[key][1]
result += '; '
result = result.rstrip('; ')
if result.endswith('-') and (len(result) < 30):
reply('Sorry, no definition found.', chan, nick)
else: say(result, chan)
return
if command == "ety" or command == "etymology":
etyuri = 'http://etymonline.com/?term=%s'
etysearch = 'http://etymonline.com/?search=%s'
r_definition = re.compile(r'(?ims)
]*>.*?
')
r_tag = re.compile(r'<(?!!)[^>]+>')
r_whitespace = re.compile(r'[\t\r\n ]+')
abbrs = [
'cf', 'lit', 'etc', 'Ger', 'Du', 'Skt', 'Rus', 'Eng', 'Amer.Eng', 'Sp',
'Fr', 'N', 'E', 'S', 'W', 'L', 'Gen', 'J.C', 'dial', 'Gk',
'19c', '18c', '17c', '16c', 'St', 'Capt', 'obs', 'Jan', 'Feb', 'Mar',
'Apr', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'c', 'tr', 'e', 'g'
]
t_sentence = r'^.*?(?')
s = s.replace('<', '<')
s = s.replace('&', '&')
return s
def text(html):
html = r_tag.sub('', html)
html = r_whitespace.sub(' ', html)
return unescape(html).strip()
try:
word = line2[4]
except Exception:
reply("Please enter a word.", chan, nick)
return
def ety(word):
if len(word) > 25:
raise ValueError("Word too long: %s[...]" % word[:10])
word = {'axe': 'ax/axe'}.get(word, word)
bytes = web.get(etyuri % word)
definitions = r_definition.findall(bytes)
if not definitions:
return None
defn = text(definitions[0])
m = r_sentence.match(defn)
if not m:
return None
sentence = m.group(0)
try:
sentence = unicode(sentence, 'iso-8859-1')
sentence = sentence.encode('utf-8')
except: pass
maxlength = 275
if len(sentence) > maxlength:
sentence = sentence[:maxlength]
words = sentence[:-5].split(' ')
words.pop()
sentence = ' '.join(words) + ' [...]'
sentence = '"' + sentence.replace('"', "'") + '"'
return sentence + ' - ' + (etyuri % word)
try:
result = ety(word.encode('utf-8'))
except IOError:
msg = "Can't connect to etymonline.com (%s)" % (etyuri % word)
reply(msg, chan, nick)
return
except AttributeError:
result = None
if result is not None:
reply(result, chan, nick)
else:
uri = etysearch % word
msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri)
reply(msg, chan, nick)
return
if command == "num" or command == "number" or command == "count" or command == "c":
try:
params = string.lower(line2[4])
except Exception:
params = False
if params == "old" or params == "afc" or params == "a":
number = unicode(int(len(re.findall("title=", urllib.urlopen("http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Pending_AfC_submissions&cmlimit=500").read()))) - 2)
reply("There are currently %s pending AfC submissions." % number, chan, nick)
elif params == "redirect" or params == "redir" or params == "redirs" or params == "redirects" or params == "r":
redir_data = urllib.urlopen("http://en.wikipedia.org/w/index.php?title=Wikipedia:Articles_for_creation/Redirects").read()
redirs = (string.count(redir_data, "
") - 1) - (string.count(redir_data, '
'))
reply("There are currently %s open redirect requests." % redirs, chan, nick)
elif params == "files" or params == "ffu" or params == "file" or params == "image" or params == "images" or params == "ifu" or params == "f":
file_data = re.sub("
'))
reply("There are currently %s pending submissions, %s open redirect requests, and %s open file upload requests." % (subs, redirs, files), chan, nick)
return
if command == "nick":
if authy == "owner":
try:
new_nick = line2[4]
except Exception:
reply("Please specify a nick to change to.", chan, nick)
return
s.send("NICK %s\r\n" % new_nick)
else:
reply("You aren't authorized to use that command.", chan, nick)
return
if command == "op" or command == "deop" or command == "voice" or command == "devoice":
if authy == "owner" or authy == "admin":
try:
user = line2[4]
except Exception:
user = nick
say("%s %s %s" % (command, chan, user), "ChanServ")
else:
reply("You aren't authorized to use that command.", chan, nick)
return
if command == "pend" or command == "pending":
say("Pending submissions status page: .", chan)
say("Pending submissions category: .", chan)
return
if command == "sub" or command == "submissions":
try:
number = int(line2[4])
except Exception:
reply("Please enter a number.", chan, nick)
return
do_url = False
try:
if "url" in line2[5:]: do_url = True
except Exception:
pass
url = "http://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:Pending_AfC_submissions&cmlimit=500&cmsort=timestamp"
query = urllib.urlopen(url)
data = query.read()
pages = re.findall("title="(.*?)"", data)
try:
pages.remove("Wikipedia:Articles for creation/Redirects")
except Exception:
pass
try:
pages.remove("Wikipedia:Files for upload")
except Exception:
pass
pages.reverse()
pages = pages[:number]
if not do_url:
s = string.join(pages, "]], [[")
s = "[[%s]]" % s
else:
s = string.join(pages, ">, ,_<", ">, <", s)
report = "\x02First %s pending AfC submissions:\x0F %s" % (number, s)
say(report, chan)
return
if command == "praise" or command == "leonard" or command == "groovedog" or command == "earwig" or command == "macmed" or command == "cubs197" or command == "sparksboy" or command == "tim_song" or command == "tim" or command == "sausage" or command == "mindstormskid" or command == "mcjohn" or command == "fetchcomms" or command == "blurpeace":
bad = False
if command == "leonard":
special = "AfC redirect reviewer"
user = "Leonard^Bloom"
elif command == "groovedog":
special = "heh"
user = "GrooveDog"
elif command == "earwig":
special = "Python programmer"
user = "Earwig"
elif command == "macmed":
special = "CSD tagger"
user = "MacMed"
elif command == "mindstormskid":
special = "Lego fanatic"
user = "MindstormsKid"
elif command == "cubs197":
special = "IRC dude"
user = "Cubs197"
elif command == "sparksboy":
special = "pet owner"
user = "SparksBoy"
elif command == "tim_song" or command == "tim":
special = "JavaScript programmer"
user = "Tim_Song"
elif command == "sausage":
special = "helper"
user = "chzz"
elif command == "mcjohn":
special = "edit summary writer"
user = "McJohn"
elif command == "fetchcomms":
special = "n00b"
user = "Fetchcomms"
elif command == "blurpeace":
special = "Commons admin"
user = "Blurpeace"
else:
say("Only a true fool would use that command, %s." % nick, chan)
# say("The users who you can praise are: Leonard^Bloom, GrooveDog, Earwig, MacMed, Cubs197, SparksBoy, MindstormsKid, Chzz, McJohn, Tim_Song, Fetchcomms, and Blurpeace.", chan)
return
if not bad:
say("\x02%s\x0F is the bestest %s evah!" % (user, special), chan)
if bad:
say("\x02%s\x0F is worstest %s evah!" % (user, special), chan)
return
if command == "trout":
try:
user = line2[4]
user = ' '.join(line2[4:])
except Exception:
reply("Hahahahahahahaha...", chan, nick)
return
normal = unicodedata.normalize('NFKD', unicode(string.lower(user)))
if "itself" in normal:
reply("I'm not that stupid ;)", chan, nick)
return
elif "earwigbot" in normal:
reply("I'm not that stupid ;)", chan, nick)
elif "earwig" not in normal and "ear wig" not in normal:
text = 'slaps %s around a bit with a large trout.' % user
msg = '\x01ACTION %s\x01' % text
say(msg, chan)
else:
reply("I refuse to hurt anything with \"Earwig\" in its name :P", chan, nick)
return
if command == "kill" or command == "destroy" or command == "murder":
reply("Who do you think I am? The Mafia?", chan, nick)
return
if command == "fish":
try:
user = line2[4]
fish = ' '.join(line2[5:])
except Exception:
reply("Hahahahahahahaha...", chan, nick)
return
normal = unicodedata.normalize('NFKD', unicode(string.lower(user)))
if "itself" in normal:
reply("I'm not that stupid ;)", chan, nick)
return
elif "earwigbot" in normal:
reply("I'm not that stupid ;)", chan, nick)
elif "earwig" not in normal and "ear wig" not in normal:
text = 'slaps %s around a bit with a %s.' % (user, fish)
msg = '\x01ACTION %s\x01' % text
say(msg, chan)
else:
reply("I refuse to hurt anything with \"Earwig\" in its name :P", chan, nick)
return
if command == "report":
def find_status(name="", talk=False):
enname = re.sub(" ", "_", name)
if talk == True:
enname = "Wikipedia_talk:Articles_for_creation/%s" % enname
if talk == False:
enname = "Wikipedia:Articles_for_creation/%s" % enname
url = "http://en.wikipedia.org/w/api.php?action=query&titles=%s&prop=revisions&rvprop=content" % enname
query = urllib.urlopen(url)
data = query.read()
status = ""
if "{{AFC submission|D" in data or "{{AFC submission|d" in data:
reason = re.findall("(D|d)\|(.*?)\|", data)
if reason[0][1] != "reason":
status = "Declined, reason is '%s'" % reason[0][1]
if reason[0][1] == "reason":
status = "Declined, reason is a custom reason"
if "{{AFC submission|H" in data or "{{AFC submission|h" in data:
reason = re.findall("(H|h)\|(.*?)\|", data)
if reason[0][1] != "reason":
status = "Held, reason is '%s'" % reason[0][1]
if reason[0][1] == "reason":
status = "Held, reason is a custom reason"
if "{{AFC submission||" in data:
status = "Pending"
if "{{AFC submission|R" in data or "{{AFC submission|r" in data:
status = "Reviewing"
if not status:
exist = exists(name=enname)
if exist == True:
status = "Accepted"
if exist == False:
status = "Not found"
return status
def exists(name=""):
url = "http://en.wikipedia.org/wiki/%s" % name
query = urllib.urlopen(url)
data = query.read()
if "Wikipedia does not have a" in data:
return False
return True
def get_submitter(name="", talk=False):
enname = re.sub(" ", "_", name)
if talk == True:
enname = "Wikipedia_talk:Articles_for_creation/%s" % enname
if talk == False:
enname = "Wikipedia:Articles_for_creation/%s" % enname
url = "http://en.wikipedia.org/w/api.php?action=query&titles=%s&prop=revisions&rvprop=user&rvdir=newer&rvlimit=1" % enname
query = urllib.urlopen(url)
data = query.read()
extract = re.findall("user="(.*?)"", data)
if "anon=" in data:
anon = True
else:
anon = False
try:
return extract[0], anon
except BaseException:
print extract
return "", anon
try:
rawSub = line2[4]
rawSub = ' '.join(line2[4:])
except Exception:
reply("You need to specify a submission name in order to use %s!" % command, chan, nick)
return
talk = False
if "[[" in rawSub and "]]" in rawSub:
name = re.sub("\[\[(.*)\]\]", "\\1", rawSub)
name = re.sub(" ", "_", name)
name = urllib.quote(name, ":/")
name = "http://en.wikipedia.org/wiki/%s" % name
if "talk:" in name:
talk = True
elif "http://" in rawSub:
name = rawSub
if "talk:" in name:
talk = True
elif "en.wikipedia.org" in rawSub:
name = "http://%s" % rawSub
if "talk:" in name:
talk = True
elif "Wikipedia:" in rawSub or "Wikipedia_talk:" in rawSub or "Wikipedia talk:" in rawSub:
name = re.sub(" ", "_", rawSub)
name = urllib.quote(name, ":/")
name = "http://en.wikipedia.org/wiki/%s" % name
if "talk:" in name:
talk = True
else:
url = "http://en.wikipedia.org/wiki/"
pagename = re.sub(" ", "_", rawSub)
pagename = urllib.quote(pagename, ":/")
pagename = "Wikipedia:Articles_for_creation/%s" % pagename
page = urllib.urlopen("%s%s" % (url, pagename))
text = page.read()
name = "http://en.wikipedia.org/wiki/%s" % pagename
if "Wikipedia does not have a" in text:
pagename = re.sub(" ", "_", rawSub)
pagename = urllib.quote(pagename, ":/")
pagename = "Wikipedia_talk:Articles_for_creation/%s" % pagename
page = urllib.urlopen("%s%s" % (url, pagename))
name = "http://en.wikipedia.org/wiki/%s" % pagename
talk = True
unname = re.sub("http://en.wikipedia.org/wiki/Wikipedia:Articles_for_creation/", "", name)
unname = re.sub("http://en.wikipedia.org/wiki/Wikipedia_talk:Articles_for_creation/", "", unname)
unname = re.sub("_", " ", unname)
if "talk" in unname:
talk = True
submitter, anon = get_submitter(name=unname, talk=talk)
status = find_status(name=unname, talk=talk)
if submitter != "":
if anon == True:
submitter_page = "Special:Contributions/%s" % submitter
if anon == False:
unsubmit = re.sub(" ", "_", submitter)
unsubmit = urllib.quote(unsubmit, ":/")
submitter_page = "User:%s" % unsubmit
if status == "Accepted":
submitterm = "Reviewer"
else:
submitterm = "Submitter"
line1 = "\x02AfC submission report for %s:" % unname
line2 = "\x02URL: \x0301\x0F%s" % name
if submitter != "":
line3 = "\x02%s: \x0F\x0302%s (\x0301\x0Fhttp://en.wikipedia.org/wiki/%s)." % (submitterm, submitter, submitter_page)
line4 = "\x02Status: \x0F\x0302%s." % status
say(line1, chan)
time.sleep(0.1)
say(line2, chan)
time.sleep(0.1)
if submitter != "":
say(line3, chan)
time.sleep(0.1)
say(line4, chan)
return
if command == "commands":
if chan.startswith("#"):
reply("Please use that command in a private message.", chan, nick)
return
others2 = get_commandList().values()
others = []
for com in others2:
if com == "copyvio" or com == "number" or com == "pending" or com == "report" or com == "submissions" or com == "access" or com == "help" or com == "join" or com == "linker" or com == "nick" or com == "op" or com == "part" or com == "quiet" or com == "quit" or com == "restart" or com == "voice" or com == "welcome" or com == "fish" or com == "praise" or com == "trout" or com == "notes":
continue
if com in others: continue
others.append(com)
others.sort()
say("\x02AFC commands:\x0F copyvio, number, pending, report, submissions.", chan)
time.sleep(0.1)
say("\x02Bot operation and channel maintaince commands:\x0F access, help, join, linker, nick, op, part, quiet, quit, restart, voice, welcome.", chan)
time.sleep(0.1)
say("\x02Fun commands:\x0F fish, praise, trout, and numerous easter eggs", chan)
time.sleep(0.1)
say("\x02Other commands:\x0F %s" % ', '.join(others), chan)
time.sleep(0.1)
say("The bot maintains a mini-wiki. Type \"!notes help\" for more information.", chan)
time.sleep(0.1)
say("See http://enwp.org/User:The_Earwig/Bots/IRC for details. For help on a specific command, type '!help command'.", chan)
return
if command == "help" or command == "doc" or command == "documentation":
try:
com = line2[4]
except Exception:
reply("Hi, I'm a bot that does work for Articles for Creation. You can find information about me at http://enwp.org/User:The_Earwig/Bots/IRC. Say \"!commands\" to me in a private message for some of my abilities. Earwig is my owner and creator, and you can contact him at http://enwp.org/User_talk:The_Earwig.", chan, nick)
return
say("Sorry, command documentation has not been implemented yet.", chan)
return
if command == "mysql":
if authy != "owner":
reply("You aren't authorized to use this command.", chan, nick)
return
import MySQLdb
try:
strings = line2[4]
strings = ' '.join(line2[4:])
if "db:" in strings:
database = re.findall("db\:(.*?)\s", strings)[0]
else:
database = "enwiki_p"
if "time:" in strings:
times = int(re.findall("time\:(.*?)\s", strings)[0])
else:
times = 60
file = re.findall("file\:(.*?)\s", strings)[0]
sqlquery = re.findall("query\:(.*?)\Z", strings)[0]
except Exception:
reply("You did not specify enough data for the bot to continue.", chan, nick)
return
database2 = database[:-2] + "-p"
db = MySQLdb.connect(db=database, host="%s.rrdb.toolserver.org" % database2, read_default_file="/home/earwig/.my.cnf")
db.query(sqlquery)
r = db.use_result()
data = r.fetch_row(0)
try:
f = codecs.open("/home/earwig/public_html/reports/%s/%s" % (database[:-2], file), 'r')
reply("A file already exists with that name.", chan, nick)
return
except Exception:
pass
f = codecs.open("/home/earwig/public_html/reports/%s/%s" % (database[:-2], file), 'a', 'utf-8')
for line in data:
new_line = []
for l in line:
new_line.append(str(l))
f.write(' '.join(new_line) + "\n")
f.close()
reply("Query completed successfully. See http://toolserver.org/~earwig/reports/%s/%s. I will delete the report in %s seconds." % (database[:-2], file, times), chan, nick)
time.sleep(times)
os.remove("/home/earwig/public_html/reports/%s/%s" % (database[:-2], file))
return
if command == "remind" or command == "reminder":
try:
times = int(line2[4])
content = ' '.join(line2[5:])
except Exception:
reply("Please specify a time and a note in the following format: !remind