Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0daf72b625 | ||
|
|
7c9225660e | ||
|
|
b79cf48a22 | ||
|
|
fe7eca1b8d | ||
|
|
800cc46574 |
+3
-1
@@ -24,4 +24,6 @@
|
|||||||
"""
|
"""
|
||||||
Scripts for our redundances bot
|
Scripts for our redundances bot
|
||||||
"""
|
"""
|
||||||
from jogobot.jogobot import output # noqa
|
# noqa needed to prevent pyflakes from warning about unused imports
|
||||||
|
from jogobot.jogobot import ( output, sendmail, is_active ) # noqa
|
||||||
|
import jogobot.config as config # noqa
|
||||||
|
|||||||
+249
-115
@@ -3,7 +3,7 @@
|
|||||||
#
|
#
|
||||||
# jogobot.py
|
# jogobot.py
|
||||||
#
|
#
|
||||||
# Copyright 2015 GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
|
# Copyright 2016 GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
|
||||||
#
|
#
|
||||||
# This program is free software; you can redistribute it and/or modify
|
# This program is free software; you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
@@ -32,6 +32,8 @@ import pywikibot
|
|||||||
from pywikibot.bot import(
|
from pywikibot.bot import(
|
||||||
DEBUG, INFO, WARNING, ERROR, CRITICAL, STDOUT, VERBOSE, logoutput )
|
DEBUG, INFO, WARNING, ERROR, CRITICAL, STDOUT, VERBOSE, logoutput )
|
||||||
|
|
||||||
|
import jogobot.config as config
|
||||||
|
|
||||||
|
|
||||||
def output( text, level="INFO", decoder=None, newline=True,
|
def output( text, level="INFO", decoder=None, newline=True,
|
||||||
layer=None, **kwargs ):
|
layer=None, **kwargs ):
|
||||||
@@ -99,114 +101,7 @@ def pywikibot_output( text, decoder=None, newline=True,
|
|||||||
pywikibot.output = pywikibot_output
|
pywikibot.output = pywikibot_output
|
||||||
|
|
||||||
|
|
||||||
class JogoBot:
|
def sendmail( Subject, Body, To=None, CC=None, BCC=None,
|
||||||
"""
|
|
||||||
Basic bot framework
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__( self ):
|
|
||||||
"""
|
|
||||||
Initialise our class
|
|
||||||
"""
|
|
||||||
|
|
||||||
# We need a pywikibot site object
|
|
||||||
self.site = pywikibot.Site()
|
|
||||||
|
|
||||||
# We need the shell working directory
|
|
||||||
self.cwd = os.getcwd()
|
|
||||||
|
|
||||||
def is_active( self ):
|
|
||||||
"""
|
|
||||||
Checks if whole bot or task specified by task_slug is:
|
|
||||||
* not blocked
|
|
||||||
* not disabled by file
|
|
||||||
* not disabled on wiki
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
def is_active_on_wiki( self, task_slug=None ):
|
|
||||||
"""
|
|
||||||
Checks if whole bot or task specified by task_slug is disabled
|
|
||||||
on wiki
|
|
||||||
|
|
||||||
@param task_slug Slug of task to check, None for whole Bot
|
|
||||||
@type str
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Define page for look up
|
|
||||||
if task_slug:
|
|
||||||
page_title = "Benutzer:JogoBot/" + task_slug + "/active.js"
|
|
||||||
else:
|
|
||||||
page_title = "Benutzer:JogoBot/active"
|
|
||||||
|
|
||||||
# Get pywikibot page object
|
|
||||||
page = pywikibot.Page( self.site, page_title )
|
|
||||||
|
|
||||||
# Get page text
|
|
||||||
page_text = page.get()
|
|
||||||
|
|
||||||
if "true" not in page_text.lower():
|
|
||||||
pass
|
|
||||||
# Disabled
|
|
||||||
|
|
||||||
# Return False
|
|
||||||
# Send E-Mail
|
|
||||||
# Create disable-file
|
|
||||||
|
|
||||||
def is_blocked_on_wiki( self ):
|
|
||||||
"""
|
|
||||||
Checks if bot user is blocked on wiki
|
|
||||||
"""
|
|
||||||
if self.site.is_blocked():
|
|
||||||
pass
|
|
||||||
# Blocked
|
|
||||||
|
|
||||||
# Return False
|
|
||||||
# Send E-Mail
|
|
||||||
# Create disable-file
|
|
||||||
|
|
||||||
def is_active_by_file(self, task_slug=None):
|
|
||||||
"""
|
|
||||||
Checks if whole bot or task specified by task_slug is disabled
|
|
||||||
by file
|
|
||||||
|
|
||||||
@param task_slug Slug of task to check, None for whole Bot
|
|
||||||
@type str
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Define filepath
|
|
||||||
if task_slug:
|
|
||||||
disable_file = self.cwd + "/" + task_slug + "/disabled"
|
|
||||||
else:
|
|
||||||
disable_file = self.cwd + "/disabled"
|
|
||||||
|
|
||||||
if os.path.isfile( disable_file ):
|
|
||||||
pass
|
|
||||||
# Disabled
|
|
||||||
|
|
||||||
# Return False
|
|
||||||
# Send E-Mail
|
|
||||||
|
|
||||||
def create_disable_file( self, task_slug=None ):
|
|
||||||
"""
|
|
||||||
Creates disable file for whole bot or task specified by task_slug
|
|
||||||
|
|
||||||
@param task_slug Slug of task to check, None for whole Bot
|
|
||||||
@type str
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Define filepath
|
|
||||||
if task_slug:
|
|
||||||
disable_file = self.cwd + "/" + task_slug + "/disabled"
|
|
||||||
else:
|
|
||||||
disable_file = self.cwd + "/disabled"
|
|
||||||
|
|
||||||
# Try to create file
|
|
||||||
with open(disable_file, 'a'):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def sendmail( Subject, Body, To=None, CC=None, BCC=None,
|
|
||||||
From="JogoBot <tools.jogobot@tools.wmflabs.org>" ):
|
From="JogoBot <tools.jogobot@tools.wmflabs.org>" ):
|
||||||
"""
|
"""
|
||||||
Provides a simple wrapper for exim (MTA) on tool labs
|
Provides a simple wrapper for exim (MTA) on tool labs
|
||||||
@@ -239,15 +134,18 @@ class JogoBot:
|
|||||||
if BCC:
|
if BCC:
|
||||||
msg['BCC'] = BCC
|
msg['BCC'] = BCC
|
||||||
|
|
||||||
msg['Content-Type'] = 'text/plain; charset=utf-8'
|
msg['Content-Type'] = 'text/plain; charset="utf-8"'
|
||||||
|
|
||||||
# Make sure we have a recipient
|
# Make sure we have a recipient
|
||||||
if not( To or CC or BCC):
|
if not( To or CC or BCC):
|
||||||
raise JogoBotMailError( "No recipient was provided!" )
|
raise MailError( "No recipient was provided!" )
|
||||||
|
|
||||||
|
# We have no local MTA so we need to catch errors and write to file instead
|
||||||
|
try:
|
||||||
# Send the message via exim
|
# Send the message via exim
|
||||||
with Popen( ["/usr/sbin/exim", "-odf", "-i", "-t"],
|
with Popen( config.mail_cmd, stdin=PIPE,
|
||||||
stdin=PIPE, universal_newlines=True) as MTA:
|
universal_newlines=True ) as MTA:
|
||||||
|
|
||||||
MTA.communicate(msg.as_string())
|
MTA.communicate(msg.as_string())
|
||||||
|
|
||||||
# Try to get returncode of MTA
|
# Try to get returncode of MTA
|
||||||
@@ -259,12 +157,248 @@ class JogoBot:
|
|||||||
|
|
||||||
# Catch MTA errors
|
# Catch MTA errors
|
||||||
if returncode:
|
if returncode:
|
||||||
raise JogoBotMailError( "/usr/sbin/exim terminated with " +
|
raise MailError( "/usr/sbin/exim terminated with " +
|
||||||
"returncode != 0. Returncode was " +
|
"returncode != 0. Returncode was " +
|
||||||
str( returncode ) )
|
str( returncode ) )
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
|
||||||
class JogoBotMailError( Exception ):
|
# Local fallback
|
||||||
|
with open( config.bot_dir + "/Mail.txt", "a" ) as mail:
|
||||||
|
mail.write( "\n\n" )
|
||||||
|
mail.write( datetime.utcnow().strftime( "%Y-%m-%d %H:%M:%S (UTC)"))
|
||||||
|
mail.write( "\n" + msg.as_string() )
|
||||||
|
|
||||||
|
|
||||||
|
def is_active( task_slug, write=True ):
|
||||||
|
"""
|
||||||
|
Simple wrapper function for our ActiveAPI to use in Tasks
|
||||||
|
"""
|
||||||
|
|
||||||
|
status = StatusAPI()
|
||||||
|
|
||||||
|
# First check if Bot is blocked
|
||||||
|
if write and status.is_blocked():
|
||||||
|
|
||||||
|
status.blocked()
|
||||||
|
|
||||||
|
task_slugs = ( None, task_slug )
|
||||||
|
|
||||||
|
for _task_slug in task_slugs:
|
||||||
|
# Then check if whole Bot is disabled by file
|
||||||
|
if status.is_disabled_by_file(_task_slug):
|
||||||
|
|
||||||
|
# Disabling by file normaly results out of Disabling on wiki
|
||||||
|
if status.is_disabled_on_wiki(_task_slug):
|
||||||
|
|
||||||
|
status.disabled_on_wiki(_task_slug, True)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
status.disabled_by_file(_task_slug)
|
||||||
|
|
||||||
|
# Status disabled is set on wiki but not yet by file
|
||||||
|
elif status.is_disabled_on_wiki(_task_slug):
|
||||||
|
|
||||||
|
status.disabled_on_wiki(_task_slug)
|
||||||
|
|
||||||
|
|
||||||
|
class StatusAPI:
|
||||||
|
"""
|
||||||
|
Provide an API for checking if Jogobot or specific task is blocked,
|
||||||
|
disabled on wiki or by file
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__( self ):
|
||||||
|
"""
|
||||||
|
Initialise our class
|
||||||
|
"""
|
||||||
|
|
||||||
|
# We need a pywikibot site object
|
||||||
|
self.site = pywikibot.Site()
|
||||||
|
|
||||||
|
# We need the shell working directory
|
||||||
|
self.cwd = "/home/joni/GOLDERWEB/Daten/Projekte/" +\
|
||||||
|
"05_Wikimedia/62_BOT/bot"
|
||||||
|
|
||||||
|
def is_disabled_on_wiki( self, task_slug=None ):
|
||||||
|
"""
|
||||||
|
Checks if whole bot or task specified by task_slug is disabled
|
||||||
|
on wiki
|
||||||
|
|
||||||
|
@param task_slug Slug of task to check, None for whole Bot
|
||||||
|
@type str
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Define page for look up
|
||||||
|
if task_slug:
|
||||||
|
page_title = "Benutzer:JogoBot/" + task_slug + "/active"
|
||||||
|
else:
|
||||||
|
page_title = "Benutzer:JogoBot/active"
|
||||||
|
|
||||||
|
# Get pywikibot page object
|
||||||
|
page = pywikibot.Page( self.site, page_title )
|
||||||
|
|
||||||
|
# Make sure page exists
|
||||||
|
if not page.exists():
|
||||||
|
raise DisablingPageError
|
||||||
|
|
||||||
|
# Get page text
|
||||||
|
page_text = page.get()
|
||||||
|
|
||||||
|
if "true" not in page_text.lower():
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_blocked( self ):
|
||||||
|
"""
|
||||||
|
Checks if bot user is blocked on wiki
|
||||||
|
"""
|
||||||
|
if self.site.is_blocked():
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def is_disabled_by_file(self, task_slug=None):
|
||||||
|
"""
|
||||||
|
Checks if whole bot or task specified by task_slug is disabled
|
||||||
|
by file
|
||||||
|
|
||||||
|
@param task_slug Slug of task to check, None for whole Bot
|
||||||
|
@type str
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Define filepath
|
||||||
|
if task_slug:
|
||||||
|
disable_file = self.cwd + "/" + task_slug + "/disabled"
|
||||||
|
else:
|
||||||
|
disable_file = self.cwd + "/disabled"
|
||||||
|
|
||||||
|
if os.path.isfile( disable_file ):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
def create_disable_file( self, task_slug=None ):
|
||||||
|
"""
|
||||||
|
Creates disable file for whole bot or task specified by task_slug
|
||||||
|
|
||||||
|
@param task_slug Slug of task to check, None for whole Bot
|
||||||
|
@type str
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Define filepath
|
||||||
|
if task_slug:
|
||||||
|
disable_file = self.cwd + "/" + task_slug + "/disabled"
|
||||||
|
else:
|
||||||
|
disable_file = self.cwd + "/disabled"
|
||||||
|
|
||||||
|
# Try to create file
|
||||||
|
with open(disable_file, 'a'):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def blocked( self ):
|
||||||
|
"""
|
||||||
|
Handles process if Bot user is blocked
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Mail-Fields
|
||||||
|
subject = "JogoBot: Bot-Account is blocked"
|
||||||
|
body = """Your Bot-Account is blocked on Wiki"""
|
||||||
|
mailto = "jogobot-status@golderweb.de"
|
||||||
|
|
||||||
|
sendmail( subject, body, mailto )
|
||||||
|
|
||||||
|
raise Blocked( body )
|
||||||
|
|
||||||
|
def disabled_by_file( self, task_slug=None ):
|
||||||
|
"""
|
||||||
|
Handles process if Bot/Task is disabled by file
|
||||||
|
"""
|
||||||
|
|
||||||
|
if task_slug:
|
||||||
|
subject = "JogoBot: Task " + task_slug + " is disabled by file!"
|
||||||
|
body = """
|
||||||
|
The Bot-Task with slug %s is disabled by file!
|
||||||
|
""" % task_slug
|
||||||
|
mailto = "jogobot-" + task_slug + "-status@golderweb.de"
|
||||||
|
|
||||||
|
else:
|
||||||
|
subject = "JogoBot: Bot is disabled by file!"
|
||||||
|
body = """The Bot is disabled by file!"""
|
||||||
|
mailto = "jogobot-status@golderweb.de"
|
||||||
|
|
||||||
|
sendmail( subject, body, mailto )
|
||||||
|
|
||||||
|
raise DisabledByFile( body )
|
||||||
|
|
||||||
|
def disabled_on_wiki( self, task_slug=None, file=False ):
|
||||||
|
"""
|
||||||
|
Handles process if Bot/Task is disabled on wiki
|
||||||
|
"""
|
||||||
|
|
||||||
|
if file:
|
||||||
|
suffix = " and also by file!"
|
||||||
|
else:
|
||||||
|
suffix = "!\nA disabled file is going to be created."
|
||||||
|
|
||||||
|
if task_slug:
|
||||||
|
subject = "JogoBot: Task " + task_slug + " is disabled on wiki!"
|
||||||
|
body = """
|
||||||
|
The Bot-Task with slug %s is disabled on wiki%s
|
||||||
|
""" % (task_slug, suffix)
|
||||||
|
mailto = "jogobot-" + task_slug + "-status@golderweb.de"
|
||||||
|
else:
|
||||||
|
subject = "JogoBot: Bot is disabled on wiki!"
|
||||||
|
body = """
|
||||||
|
The Bot is disabled by on wiki%s
|
||||||
|
""" % suffix
|
||||||
|
mailto = "jogobot-status@golderweb.de"
|
||||||
|
|
||||||
|
sendmail( subject, body, mailto )
|
||||||
|
|
||||||
|
self.create_disable_file( task_slug )
|
||||||
|
|
||||||
|
raise DisabledOnWiki( body )
|
||||||
|
|
||||||
|
|
||||||
|
class Disabled( Exception ):
|
||||||
|
"""
|
||||||
|
Handles disabled Bot/Task
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DisablingPageError( Disabled ):
|
||||||
|
"""
|
||||||
|
Handles errors with missing disabling pages
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Blocked( Disabled ):
|
||||||
|
"""
|
||||||
|
Raised if Bot-Account is blocked
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DisabledByFile( Disabled ):
|
||||||
|
"""
|
||||||
|
Raised if Bot/Task is disabled by File
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DisabledOnWiki( Disabled ):
|
||||||
|
"""
|
||||||
|
Raised if Bot/Task is disabled on Wiki
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MailError( Exception ):
|
||||||
"""
|
"""
|
||||||
Handles errors occuring in class JogoBot related to mail actions
|
Handles errors occuring in class JogoBot related to mail actions
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user