diff --git a/pyhole/core/api.py b/pyhole/core/api.py index 322d382..c95781c 100644 --- a/pyhole/core/api.py +++ b/pyhole/core/api.py @@ -20,11 +20,13 @@ import time import uuid -import utils -import version +from pyhole.core import queue +from pyhole.core import utils +from pyhole.core import version APP = flask.Flask("pyhole") +QUEUE = queue.MessageQueue() @APP.route("/", methods=["GET"]) @@ -33,6 +35,29 @@ def index(): return version.version_string(), 200 +# BEGIN MESSAGE API # +@APP.route("/messages/send", methods=["POST"]) +def send_message(): + """Send a message.""" + request = flask.request.get_json() + + try: + item = ( + request["network"], + request["source"], + request["target"], + request["message"] + ) + except KeyError: + flask.abort(422) + + # NOTE(jk0): Disable until auth is implemented. + # QUEUE.put(item) + + return "", 200 +# END MESSAGE API # + + # BEGIN PASTE API # PASTE_TEMPLATE = """{% set lines = paste.split('\n') %} diff --git a/pyhole/core/irc/client.py b/pyhole/core/irc/client.py index 8ea4856..f17d084 100644 --- a/pyhole/core/irc/client.py +++ b/pyhole/core/irc/client.py @@ -147,7 +147,7 @@ def on_welcome(self, connection, _event): else: connection.join(channel[0]) - q = queue.FIFOQueue() + q = queue.MessageQueue() q.watch(self) def on_disconnect(self, _connection, _event): diff --git a/pyhole/core/logger.py b/pyhole/core/logger.py index 1b626ba..f271eb3 100644 --- a/pyhole/core/logger.py +++ b/pyhole/core/logger.py @@ -32,14 +32,12 @@ class PyholeFileHandler(logging.handlers.TimedRotatingFileHandler): - # CamelCase because superclass is like that def doRollover(self): result = super(PyholeFileHandler, self).doRollover() self.archive_old_logs() return result def archive_old_logs(self): - # Compress uncompressed logs matcher = "*.log.*[!b][!z][!2]" files = glob.glob(os.path.join(LOG_DIR, matcher)) for file_path in files: @@ -51,11 +49,10 @@ def archive_old_logs(self): compressed_file_path = os.path.join(archive_dir, compressed_filename) - with open(file_path, 'rb') as input: - with bz2.BZ2File(compressed_file_path, - 'wb', + with open(file_path, "rb") as fp: + with bz2.BZ2File(compressed_file_path, "wb", compresslevel=9) as output: - shutil.copyfileobj(input, output) + shutil.copyfileobj(fp, output) os.remove(file_path) diff --git a/pyhole/core/queue.py b/pyhole/core/queue.py index d56f71a..714c1f9 100644 --- a/pyhole/core/queue.py +++ b/pyhole/core/queue.py @@ -22,23 +22,31 @@ QUEUE = multiprocessing.Queue() -class FIFOQueue(object): +class MessageQueue(object): """Global message queue.""" def __init__(self): self.queue = QUEUE def put(self, item): - """Place an item in the queue.""" + """Place an item into the queue.""" self.queue.put_nowait(item) @utils.spawn def watch(self, session): """Watch the queue for incoming messages.""" while True: - network, source, target, message = self.queue.get() + item = self.queue.get() - # NOTE(jk0): Right now there is no way to guarantee that the - # message will get delivered to the right network. - _msg = "New message from %s: %s" % (source, message) - session.reply(target, _msg) + actual_network = session.log.name + intended_network = item[0] + + # NOTE(jk0): If the intended network does not match the actual + # network, place the item back into the queue and until it gets + # picked up by the intended network. + if actual_network != intended_network: + self.queue.put(item) + continue + + _msg = "New message from %s: %s" % (item[1], item[3]) + session.reply(item[2], _msg) diff --git a/pyhole/core/slack/client.py b/pyhole/core/slack/client.py index 1279365..efc5389 100644 --- a/pyhole/core/slack/client.py +++ b/pyhole/core/slack/client.py @@ -63,7 +63,7 @@ def start(self): self.client = slackclient.SlackClient(self.api_token) self.client.rtm_connect() - q = queue.FIFOQueue() + q = queue.MessageQueue() q.watch(self) count = 0 diff --git a/pyhole/core/slack/message.py b/pyhole/core/slack/message.py index 53efe38..fbc4b77 100644 --- a/pyhole/core/slack/message.py +++ b/pyhole/core/slack/message.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Slack Message Class""" +"""Slack Message""" class Message(object): diff --git a/pyhole/core/utils.py b/pyhole/core/utils.py index d791ef4..07b33a3 100644 --- a/pyhole/core/utils.py +++ b/pyhole/core/utils.py @@ -116,7 +116,6 @@ def get_home_directory(): """Return the home directory.""" home_dir = os.getenv("HOME") + "/.pyhole/" make_directory(home_dir) - return home_dir @@ -125,12 +124,11 @@ def get_directory(new_dir): home_dir = get_home_directory() new_dir = os.path.join(home_dir, new_dir, "") make_directory(new_dir) - return new_dir def make_directory(directory): - """Make the specified direectory, if it doesn't exist already.""" + """Make a direectory if it doesn't exist.""" if not os.path.exists(directory): os.makedirs(directory)