<commit_before>#!/usr/bin/env python

"""
$Id$

Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
See the file 'doc/COPYING' for copying permission
"""

optDict = {
            # Format:
            # Family:        { "parameter name":    "parameter datatype" },
            # Or:
            # Family:        { "parameter name":    ("parameter datatype", "category name used for common outputs feature") },
            "Target":        {
                               "direct":            "string",
                               "url":               "string",
                               "list":              "string",
                               "requestFile":       "string",
                               "googleDork":        "string",
                               "configFile":        "string"
                             },

            "Request":       {
                               "method":            "string",
                               "data":              "string",
                               "cookie":            "string",
                               "cookieUrlencode":   "boolean",
                               "dropSetCookie":     "boolean",
                               "agent":             "string",
                               "userAgentsFile":    "string",
                               "referer":           "string",
                               "headers":           "string",
                               "aType":             "string",
                               "aCred":             "string",
                               "aCert":             "string",
                               "proxy":             "string",
                               "pCred":             "string",
                               "ignoreProxy":       "boolean",
                               "delay":             "float",
                               "timeout":           "float",
                               "retries":           "integer",
                               "scope":             "string",
                               "safUrl":            "string",
                               "saFreq":            "integer"
                             },

            "Optimization":  {
                               "optimize":          "boolean",
                               "predictOutput":     "boolean",
                               "keepAlive":         "boolean",
                               "nullConnection":    "boolean",
                               "threads":           "integer"
                             },

            "Injection":     {
                               "testParameter":     "string",
                               "dbms":              "string",
                               "os":                "string",
                               "prefix":            "string",
                               "suffix":            "string",
                               "tamper":            "string"
                             },

            "Detection":     {
                               "level":             "integer",
                               "risk":              "integer",
                               "string":            "string",
                               "regexp":            "string",
                               "eString":           "string",
                               "eRegexp":           "string",
                               "thold":             "float",
                               "textOnly":          "boolean",
                               "longestCommon":     "boolean"
                             },

            "Techniques":    {
                               "timeSec":           "integer",
                               "unionTest":         "boolean",
                               "uCols":             "integer",
                               "uChar":             "string"
                             },

            "Fingerprint":   {
                               "extensiveFp":       "boolean"
                             },

            "Enumeration":   {
                               "getBanner":         ("boolean", "Banners"),
                               "getCurrentUser":    ("boolean", "Users"),
                               "getCurrentDb":      ("boolean", "Databases"),
                               "isDba":             "boolean",
                               "getUsers":          ("boolean", "Users"),
                               "getPasswordHashes": ("boolean", "Passwords"),
                               "getPrivileges":     ("boolean", "Privileges"),
                               "getRoles":          ("boolean", "Roles"),
                               "getDbs":            ("boolean", "Databases"),
                               "getTables":         ("boolean", "Tables"),
                               "getColumns":        ("boolean", "Columns"),
                               "dumpTable":         "boolean",
                               "dumpAll":           "boolean",
                               "search":            "boolean",
                               "db":                "string",
                               "tbl":               "string",
                               "col":               "string",
                               "user":              "string",
                               "excludeSysDbs":     "boolean",
                               "limitStart":        "integer",
                               "limitStop":         "integer",
                               "firstChar":         "integer",
                               "lastChar":          "integer",
                               "query":             "string",
                               "sqlShell":          "boolean",
                             },

            "Brute":         {
                               "commonTables":       "boolean",
                               "commonColumns":      "boolean"
                             },

            "User-defined function": {
                               "udfInject":         "boolean",
                               "shLib":             "string"
                             },

            "File system":   {
                               "rFile":             "string",
                               "wFile":             "string",
                               "dFile":             "string"
                             },

            "Takeover":      {
                               "osCmd":             "string",
                               "osShell":           "boolean",
                               "osPwn":             "boolean",
                               "osSmb":             "boolean",
                               "osBof":             "boolean",
                               "privEsc":           "boolean",
                               "msfPath":           "string",
                               "tmpPath":           "string"
                             },

            "Windows":       {
                               "regRead":           "boolean",
                               "regAdd":            "boolean",
                               "regDel":            "boolean",
                               "regKey":            "string",
                               "regVal":            "string",
                               "regData":           "string",
                               "regType":           "string"
                             },

            "General":       {
                               "xmlFile":           "string",
                               "sessionFile":       "string",
                               "trafficFile":       "string",
                               "flushSession":      "boolean",
                               "forms":             "boolean",
                               "eta":               "boolean",
                               "updateAll":         "boolean",
                               "batch":             "boolean"
                             },

            "Miscellaneous": {
                               "beep":              "boolean",
                               "checkPayload":      "boolean",
                               "cleanup":           "boolean",
                               "googlePage":        "integer",
                               "replicate":         "boolean",
                               "verbose":           "integer"
                             },
          }
<commit_msg>fix for a bug reported by black zero (ValueError: invalid literal for int() with base 10: '1-20')<commit_after>#!/usr/bin/env python

"""
$Id$

Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
See the file 'doc/COPYING' for copying permission
"""

optDict = {
            # Format:
            # Family:        { "parameter name":    "parameter datatype" },
            # Or:
            # Family:        { "parameter name":    ("parameter datatype", "category name used for common outputs feature") },
            "Target":        {
                               "direct":            "string",
                               "url":               "string",
                               "list":              "string",
                               "requestFile":       "string",
                               "googleDork":        "string",
                               "configFile":        "string"
                             },

            "Request":       {
                               "method":            "string",
                               "data":              "string",
                               "cookie":            "string",
                               "cookieUrlencode":   "boolean",
                               "dropSetCookie":     "boolean",
                               "agent":             "string",
                               "userAgentsFile":    "string",
                               "referer":           "string",
                               "headers":           "string",
                               "aType":             "string",
                               "aCred":             "string",
                               "aCert":             "string",
                               "proxy":             "string",
                               "pCred":             "string",
                               "ignoreProxy":       "boolean",
                               "delay":             "float",
                               "timeout":           "float",
                               "retries":           "integer",
                               "scope":             "string",
                               "safUrl":            "string",
                               "saFreq":            "integer"
                             },

            "Optimization":  {
                               "optimize":          "boolean",
                               "predictOutput":     "boolean",
                               "keepAlive":         "boolean",
                               "nullConnection":    "boolean",
                               "threads":           "integer"
                             },

            "Injection":     {
                               "testParameter":     "string",
                               "dbms":              "string",
                               "os":                "string",
                               "prefix":            "string",
                               "suffix":            "string",
                               "tamper":            "string"
                             },

            "Detection":     {
                               "level":             "integer",
                               "risk":              "integer",
                               "string":            "string",
                               "regexp":            "string",
                               "eString":           "string",
                               "eRegexp":           "string",
                               "thold":             "float",
                               "textOnly":          "boolean",
                               "longestCommon":     "boolean"
                             },

            "Techniques":    {
                               "timeSec":           "integer",
                               "unionTest":         "boolean",
                               "uCols":             "string",
                               "uChar":             "string"
                             },

            "Fingerprint":   {
                               "extensiveFp":       "boolean"
                             },

            "Enumeration":   {
                               "getBanner":         ("boolean", "Banners"),
                               "getCurrentUser":    ("boolean", "Users"),
                               "getCurrentDb":      ("boolean", "Databases"),
                               "isDba":             "boolean",
                               "getUsers":          ("boolean", "Users"),
                               "getPasswordHashes": ("boolean", "Passwords"),
                               "getPrivileges":     ("boolean", "Privileges"),
                               "getRoles":          ("boolean", "Roles"),
                               "getDbs":            ("boolean", "Databases"),
                               "getTables":         ("boolean", "Tables"),
                               "getColumns":        ("boolean", "Columns"),
                               "dumpTable":         "boolean",
                               "dumpAll":           "boolean",
                               "search":            "boolean",
                               "db":                "string",
                               "tbl":               "string",
                               "col":               "string",
                               "user":              "string",
                               "excludeSysDbs":     "boolean",
                               "limitStart":        "integer",
                               "limitStop":         "integer",
                               "firstChar":         "integer",
                               "lastChar":          "integer",
                               "query":             "string",
                               "sqlShell":          "boolean",
                             },

            "Brute":         {
                               "commonTables":       "boolean",
                               "commonColumns":      "boolean"
                             },

            "User-defined function": {
                               "udfInject":         "boolean",
                               "shLib":             "string"
                             },

            "File system":   {
                               "rFile":             "string",
                               "wFile":             "string",
                               "dFile":             "string"
                             },

            "Takeover":      {
                               "osCmd":             "string",
                               "osShell":           "boolean",
                               "osPwn":             "boolean",
                               "osSmb":             "boolean",
                               "osBof":             "boolean",
                               "privEsc":           "boolean",
                               "msfPath":           "string",
                               "tmpPath":           "string"
                             },

            "Windows":       {
                               "regRead":           "boolean",
                               "regAdd":            "boolean",
                               "regDel":            "boolean",
                               "regKey":            "string",
                               "regVal":            "string",
                               "regData":           "string",
                               "regType":           "string"
                             },

            "General":       {
                               "xmlFile":           "string",
                               "sessionFile":       "string",
                               "trafficFile":       "string",
                               "flushSession":      "boolean",
                               "forms":             "boolean",
                               "eta":               "boolean",
                               "updateAll":         "boolean",
                               "batch":             "boolean"
                             },

            "Miscellaneous": {
                               "beep":              "boolean",
                               "checkPayload":      "boolean",
                               "cleanup":           "boolean",
                               "googlePage":        "integer",
                               "replicate":         "boolean",
                               "verbose":           "integer"
                             },
          }
<|endoftext|>


<commit_before># Copyright (c) 2012, Braiden Kindt.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
# ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import threading
import logging
import array
import collections
import errno
import Queue

from antagent.antdefs import *

_LOG = logging.getLogger("antagent.ant")
DEFAULT_TIMEOUT = 250

def msg_to_string(msg):
    return array.array("B", msg).tostring().encode("hex")

def generate_checksum(msg):
    return reduce(lambda x, y: x ^ y, msg)

def validate_checksum(msg):
    return generate_checksum(msg) == 0

def tokenize_message(msg):
    while msg:
        assert msg[0] & 0xFE == SYNC
        length = msg[1]
        yield msg[:4 + length]
        msg = msg[4 + length:]

class AntCommand(object):

    def __init__(self, msg_type, *args, **kwds):
        self.msg_type = msg_type
        try:
            self.msg_args = msg_type.msg_tuple(*args, **kwds)
        except AttributeError:
            self.msg_args = args

    def __str__(self):
        return str(self.msg_args)

class AntCore(object):

    def __init__(self, hardware, messages=ALL_MESSAGES):
        self.hardware = hardware
        self.input_msg_by_id = dict((m.msg_id, m) for m in messages if m.msg_dir == DIR_IN)
        #self.hardware.write([0] * 15, DEFAULT_TIMEOUT)

    def pack(self, command):
        if command.msg_type.msg_id == DIR_IN:
            _LOG.warning("Request to pack input message %s.", command)
        try:
            struct = command.msg_type.msg_struct
        except AttributeError:
            _LOG.warning("Attempt to pack unsupported command %s.", command)
            return []
        else:
            msg = [SYNC, struct.size, command.msg_type.msg_id]
            msg.extend(array.array("B", struct.pack(*command.msg_args)))
            msg.append(generate_checksum(msg))
            return msg

    def unpack(self, msg):
        if not validate_checksum(msg):
            raise IOError("Invalid Checksum", msg)
        sync, length, msg_id = msg[:3]
        try:
            msg_type = self.input_msg_by_id[msg_id]
            struct = msg_type.msg_struct
        except (KeyError, AttributeError):
            _LOG.warning("Attempt to unpack unkown message (0x%02x) %s.", msg_id, msg)
            return AntCommand(msg_id)
        else:
            msg_args = struct.unpack(array.array("B", msg[3:-1]).tostring())
            return AntCommand(msg_type, *msg_args)

    def send(self, command, timeout=DEFAULT_TIMEOUT):
        msg = self.pack(command)
        _LOG.debug("SEND: %s", msg_to_string(msg))
        msg.extend([0] * 2)
        self.hardware.write(msg, timeout)

    def recv(self, timeout=DEFAULT_TIMEOUT):
        while True:
            try:
                for msg in tokenize_message(self.hardware.read(timeout)):
                    _LOG.debug("RECV: %s", msg_to_string(msg))
                    yield self.unpack(msg)
            except IOError as (err, msg):
                if err == errno.ETIMEDOUT: raise StopIteration()
                else: raise

class AntWorker(object):

    def __init__(self, ant_core):
        self.ant_core = ant_core
        self.running = False
        self.input_queue = Queue.Queue()
        self.output_queue = Queue.Queue()

    def start(self):
        if not self.running:
            self.running = True
            self.reader_thread = threading.Thread(target=self._reader)
            self.reader_thread.daemon = True
            self.reader_thread.start()
            self.writer_thread = threading.Thread(target=self._writer)
            self.writer_thread.deamon = True
            self.writer_thread.start()

    def stop(self):
        self.running = False
        try:
            self.reader_thread.join(1000)
            self.writer_thread.join(1000)
        except AttributeError:
            pass

    def _writer(self):
        while self.running:
            try:
                cmd = self.input_queue.get(True, 100)
            except Queue.Empty:
                pass
            else:
                self.ant_core.send(cmd)

    def _reader(self):
        cmds = iter(self.ant_core.recv())
        while self.running:
            try:
                for cmd in cmds:
                    pass
            except Exception:
                _LOG.error("Caught execption reading ANT message.", exc_info=True)

# vim: ts=4 sts=4 et
<commit_msg>session stuff<commit_after># Copyright (c) 2012, Braiden Kindt.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#   1. Redistributions of source code must retain the above copyright
#      notice, this list of conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
# ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
# WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

import threading
import logging
import array
import collections
import errno
import Queue
import time

from antagent.antdefs import *

_LOG = logging.getLogger("antagent.ant")

def msg_to_string(msg):
    return array.array("B", msg).tostring().encode("hex")

def generate_checksum(msg):
    return reduce(lambda x, y: x ^ y, msg)

def validate_checksum(msg):
    return generate_checksum(msg) == 0

def tokenize_message(msg):
    while msg:
        assert msg[0] & 0xFE == SYNC
        length = msg[1]
        yield msg[:4 + length]
        msg = msg[4 + length:]

class AntCommand(object):

    def __init__(self, msg_type, *args, **kwds):
        self.msg_type = msg_type
        try:
            self.msg_args = msg_type.msg_tuple(*args, **kwds)
        except AttributeError:
            self.msg_args = args

    def __str__(self):
        return str(self.msg_args)

class AntCore(object):

    def __init__(self, hardware, messages=ALL_MESSAGES):
        self.hardware = hardware
        self.input_msg_by_id = dict((m.msg_id, m) for m in messages if m.msg_dir == DIR_IN)
        # per ant protocol doc, writing 15 zeros
        # should reset internal state of device.
        #self.hardware.write([0] * 15, 100)

    def pack(self, command):
        if command.msg_type.msg_id == DIR_IN:
            _LOG.warning("Request to pack input message %s.", command)
        try:
            struct = command.msg_type.msg_struct
        except AttributeError:
            _LOG.warning("Attempt to pack unsupported command %s.", command)
            return []
        else:
            msg = [SYNC, struct.size, command.msg_type.msg_id]
            msg.extend(array.array("B", struct.pack(*command.msg_args)))
            msg.append(generate_checksum(msg))
            return msg

    def unpack(self, msg):
        if not validate_checksum(msg):
            raise IOError("Invalid Checksum", msg)
        sync, length, msg_id = msg[:3]
        try:
            msg_type = self.input_msg_by_id[msg_id]
            struct = msg_type.msg_struct
        except (KeyError, AttributeError):
            _LOG.warning("Attempt to unpack unkown message (0x%02x) %s.", msg_id, msg)
            return AntCommand(msg_id)
        else:
            msg_args = struct.unpack(array.array("B", msg[3:-1]).tostring())
            return AntCommand(msg_type, *msg_args)

    def send(self, command, timeout=100):
        msg = self.pack(command)
        _LOG.debug("SEND: %s", msg_to_string(msg))
        # ant protocol status \x00\x00 padding is optiontal
        # but nRF24AP2 seems to not occaionally not reply to
        # to short commands when zero padding is excluded.
        msg.extend([0] * 2)
        try:
            self.hardware.write(msg, timeout)
            return True
        except IOError as (err, msg):
            if err == errno.ETIMEOUT: return False
            else: raise

    def recv(self, timeout=500):
        while True:
            try:
                # tokenize message (possibly more than on per read)
                for msg in tokenize_message(self.hardware.read(timeout)):
                    _LOG.debug("RECV: %s", msg_to_string(msg))
                    yield self.unpack(msg)
            except IOError as (err, msg):
                # iteration terminates on timeout
                if err == errno.ETIMEDOUT: raise StopIteration()
                else: raise

class AntSession(object):

    def __init__(self, ant_core, open=True):
        self.ant_core = ant_core
        self.running = False
        self.running_cmd = None
        if open: self.open()

    def sync_exec(self, message_type, *args, **kwds):
        assert not self.running_cmd
        # create a new command. After setting self.running_cmd
        # access to this command from this tread is invalid
        # unless cmd.done event is set.
        cmd = AntCommand(message_type, *args, **kwds)
        cmd.expiration = time.time() + 1
        cmd.done = threading.Event()
        self.running_cmd = cmd
        # continue trying to commit command until session closed or command timeout
        while self.running and not cmd.done.is_set() and not self.ant_core.send(cmd, timeout=100):
            _LOG.warning("Device write timeout. Will keep trying.")
        # continue waiting for command completion until session closed
        while self.running and not cmd.done.is_set():
            cmd.done.wait(1)
        # cmd.done guarenetees a result is availible
        if cmd.done.is_set():
            try:
                return cmd.result
            except AttributeError:
                raise cmd.error
        else:
            raise IOError(errno.EBADF, "Session closed.")

    def open(self):
        if not self.running:
            self.running = True
            self.thread = threading.Thread(target=self.loop)
            self.thread.daemon = True
            self.thread.start()

    def close(self):
        try:
            self.running = False
            self.read_thread.join(1000)
        except AttributeError: pass

    def _handle_command(self, cmd):
        self._handle_timeout()

    def _handle_timeout(self):
        if self.running_cmd and time.time() > self.running_cmd.expiration:
            self.running_cmd.error = IOError(errno.ETIMEDOUT, "No reply to command. %s" %  self.running_cmd)
            self.running_cmd.done.set()
            self.running_cmd = None

    def loop(self):
        try:
            while self.running:
                for cmd in self.ant_core.recv():
                    if not self.running: break
                    self._handle_command(cmd)
                else:
                    if not self.running: break
                    self._handle_timeout()
        except Exception:
            _LOG.error("Caught Exception reading from device.", exc_info=True)
        finally:
            self.running_cmd = None
            self.running = False

# vim: ts=4 sts=4 et
<|endoftext|>


<commit_before>from collections import Counter
from itertools import permutations
from hashlib import md5

wordslist = []
anagram = None
target_md5 = None

with open("./words.txt", "r") as f:
    wordslist = [line.strip("\n") for line in f]

with open("./anagram.txt", "r") as f:
    anagram = f.readline().strip("\n")

with open("./md5.txt", "r") as f:
    target_md5 = [l.strip("\n") for l in f]

anagram_letters = set(anagram)

class PoultryAndAnts:

    def __init__(self, anagram):
        self.anagram_counter = Counter(anagram)
        self.anagram_length = len(anagram)

    def find_match(self, node):
        (words, length, _, children) = node
        # we've reached a leaf node that has the right amount of letters
        if not children and length == self.anagram_length:
            # check for md5 of all permutations with white spaces
            for perm in permutations(words):
                digest = md5(" ".join(perm)).hexdigest()
                if digest in target_md5:
                    print "\n\nWINNER!! - %s \n\n" % list(perm)
        for child in children:
            self.find_match(child)

    def nb_leaf_nodes(self, node, nb_leaves):
        (_, __, ___, children) = node
        if not children:
            return 1
        r = 0
        for c in children:
            r += self.nb_leaf_nodes(c, nb_leaves)
        return nb_leaves + r

    def add_node(self, node, new_word, new_word_counter):
        (words, length, counter, children) = node
        for c in children:
            self.add_node(c, new_word, new_word_counter)
        l = len(new_word)
        new_node_length = length + l
        new_node_counter = new_word_counter + counter
        if self.word_contains(new_node_counter, self.anagram_counter):
            children.append(
                (words + [new_word], new_node_length, new_node_counter, []))

    def word_contains(self, needle, haystack):
        return all(haystack[k] - v >= 0 for k,v in needle.items())

    def find_anagram(self):
        i = 0
        for word in set(wordslist):
            try:
                # If all letters of the word are in the anagram
                word_counter = Counter(word)
                if self.word_contains(word_counter, self.anagram_counter):
                    self.add_node(root, word, word_counter)
                i += 1
                if i % 1000 == 0:
                    print "done: ", i
                    if i % 5000 == 0:
                        print "nb-leaves: %d" % self.nb_leaf_nodes(root, 0)
            except KeyboardInterrupt as e:
                break

        print "%s:%s" % (anagram, self.anagram_length)
        print "nb-leaves: %d" % ants.nb_leaf_nodes(root, 0)

if __name__ == "__main__":
    root = ([], 0, Counter(), [])
    ants = PoultryAndAnts(anagram)
    print "Looking for %s: %s" % (anagram, ants.anagram_counter)
    ants.find_anagram()
<commit_msg>[ANTS] Remove unused global<commit_after>from collections import Counter
from itertools import permutations
from hashlib import md5

wordslist = []
anagram = None
target_md5 = None

with open("./words.txt", "r") as f:
    wordslist = [line.strip("\n") for line in f]

with open("./anagram.txt", "r") as f:
    anagram = f.readline().strip("\n")

with open("./md5.txt", "r") as f:
    target_md5 = [l.strip("\n") for l in f]

class PoultryAndAnts:

    def __init__(self, anagram):
        self.anagram_counter = Counter(anagram)
        self.anagram_length = len(anagram)

    def find_match(self, node):
        (words, length, _, children) = node
        # we've reached a leaf node that has the right amount of letters
        if not children and length == self.anagram_length:
            # check for md5 of all permutations with white spaces
            for perm in permutations(words):
                digest = md5(" ".join(perm)).hexdigest()
                if digest in target_md5:
                    print "\n\nWINNER!! - %s \n\n" % list(perm)
        for child in children:
            self.find_match(child)

    def nb_leaf_nodes(self, node, nb_leaves):
        (_, __, ___, children) = node
        if not children:
            return 1
        r = 0
        for c in children:
            r += self.nb_leaf_nodes(c, nb_leaves)
        return nb_leaves + r

    def add_node(self, node, new_word, new_word_counter):
        (words, length, counter, children) = node
        for c in children:
            self.add_node(c, new_word, new_word_counter)
        l = len(new_word)
        new_node_length = length + l
        new_node_counter = new_word_counter + counter
        if self.word_contains(new_node_counter, self.anagram_counter):
            children.append(
                (words + [new_word], new_node_length, new_node_counter, []))

    def word_contains(self, needle, haystack):
        return all(haystack[k] - v >= 0 for k,v in needle.items())

    def find_anagram(self):
        i = 0
        for word in set(wordslist):
            try:
                # If all letters of the word are in the anagram
                word_counter = Counter(word)
                if self.word_contains(word_counter, self.anagram_counter):
                    self.add_node(root, word, word_counter)
                i += 1
                if i % 1000 == 0:
                    print "done: ", i
                    if i % 5000 == 0:
                        print "nb-leaves: %d" % self.nb_leaf_nodes(root, 0)
            except KeyboardInterrupt as e:
                break

        print "%s:%s" % (anagram, self.anagram_length)
        print "nb-leaves: %d" % ants.nb_leaf_nodes(root, 0)

if __name__ == "__main__":
    root = ([], 0, Counter(), [])
    ants = PoultryAndAnts(anagram)
    print "Looking for %s: %s" % (anagram, ants.anagram_counter)
    ants.find_anagram()
<|endoftext|>


<commit_before>import featuremonkey
import inspect
from functools import wraps

__version__ = '0.2.0'
__author__ = 'Hendrik Speidel <hendrik@schnapptack.de>'
SHORT_HEADER = '''ape - a productive environment

'''

class EnvironmentIncomplete(Exception): pass

class FeatureNotFound(Exception): pass

class TaskNotFound(Exception): pass

class TaskAlreadyRegistered(Exception): pass

class InvalidAccess(Exception): pass

def _get_invalid_accessor(func_name):

    def invalid_accessor(*args, **kws):
        raise InvalidAccess(
            'task "%s" needs to be accessed as "ape.tasks.%s"!' % (
                func_name, func_name
            )
        )

    return invalid_accessor

def get_signature(name, func):
    '''helper to generate a readable signature for a function'''
    args, varargs, keywords, defaults = inspect.getargspec(func)
    defaults = defaults or []
    if varargs is None and keywords is None:
        sig = name + '('
        sigargs = []
        for idx, arg in enumerate(args):
            if idx < len(args) - len(defaults):
                sigargs.append(arg)
            else:
                default = repr(defaults[idx - len(defaults)])
                sigargs.append(arg + '=' + default)
        sig += ', '.join(sigargs) + ')'
        return sig
    elif not args and varargs and not keywords and not defaults:
        return name + '(*' + varargs + ')'
    else:
        raise

class Tasks(object):
    '''
    ape tasks registry
    '''

    def __init__(self):
        #import the module that tasks live in
        from . import _tasks
        self._tasks = _tasks
        self._helper_names = set()

    def register(self, func):
        '''register a task -
        typically used as a decorator to the task function.

        If a task by that name already exists,
        a TaskAlreadyRegistered exception is raised.
        '''
        if hasattr(self._tasks, func.__name__):
            raise TaskAlreadyRegistered(func.__name__)
        setattr(self._tasks, func.__name__, func)
        return _get_invalid_accessor(func.__name__)

    def register_helper(self, func):
        '''a helper is a task that is not directly exposed to
        the command line
        '''
        self._helper_names.add(func.__name__)
        return self.register(func)

    def get_tasks(self):
        '''
        return tasks as list of (name, function) tuples
        '''
        def predicate(item):
            return (inspect.isfunction(item) and
                item.__name__ not in self._helper_names
            )
        return inspect.getmembers(self._tasks, predicate)

    def get_task(self, name, include_helpers=True):
        '''get task identified by name or raise TaskNotFound if there
        is no such task
        '''
        if not include_helpers and name in self._helper_names:
            raise TaskNotFound(name)
        try:
            return getattr(self._tasks, name)
        except AttributeError:
            raise TaskNotFound(name)

    def help(self, taskname=None):
        '''list tasks or provide help for specific task'''
        if not taskname:
            print inspect.getdoc(self._tasks)
            print
            print 'Available tasks:'
            print
            for task in self.get_tasks():
                print '  ' + get_signature(*task)
                help_msg = inspect.getdoc(task[1]) or ''
                help_msg = help_msg.split('\n')[0]
                print '    ' + help_msg
                print
        else:
            try:
                task = self.get_task(taskname)
                print SHORT_HEADER
                print get_signature(task.__name__, task)
                print
                print inspect.getdoc(task)
                print
                print 'defined in: ' + inspect.getfile(task)
                print
            except TaskNotFound:
                print 'Task "%s" not found! Use "ape help" to get usage information.' % taskname

    def __getattr__(self, name):
        '''simple proxy to tasks module
        tasks have to be accessed using only this method,
        e.g. ape.tasks.foo() to call the foo task.
        '''
        return self.get_task(name)

    def superimpose(self, module):
        '''superimpose a task module on registered tasks'''
        featuremonkey.compose(module, self._tasks)
        self._tasks.FEATURE_SELECTION.append(module.__name__)

tasks = Tasks()
<commit_msg>better exception handling in get_signature<commit_after>import featuremonkey
import inspect
from functools import wraps

__version__ = '0.2.0'
__author__ = 'Hendrik Speidel <hendrik@schnapptack.de>'
SHORT_HEADER = '''ape - a productive environment

'''

class EnvironmentIncomplete(Exception): pass

class FeatureNotFound(Exception): pass

class TaskNotFound(Exception): pass

class TaskAlreadyRegistered(Exception): pass

class InvalidAccess(Exception): pass

class InvalidTask(Exception): pass

def _get_invalid_accessor(func_name):

    def invalid_accessor(*args, **kws):
        raise InvalidAccess(
            'task "%s" needs to be accessed as "ape.tasks.%s"!' % (
                func_name, func_name
            )
        )

    return invalid_accessor

def get_signature(name, func):
    '''helper to generate a readable signature for a function'''
    args, varargs, keywords, defaults = inspect.getargspec(func)
    defaults = defaults or []
    if varargs is None and keywords is None:
        sig = name + '('
        sigargs = []
        for idx, arg in enumerate(args):
            if idx < len(args) - len(defaults):
                sigargs.append(arg)
            else:
                default = repr(defaults[idx - len(defaults)])
                sigargs.append(arg + '=' + default)
        sig += ', '.join(sigargs) + ')'
        return sig
    elif not args and varargs and not keywords and not defaults:
        return name + '(*' + varargs + ')'
    else:
        raise InvalidTask('ape tasks may not use **kwargs')

class Tasks(object):
    '''
    ape tasks registry
    '''

    def __init__(self):
        #import the module that tasks live in
        from . import _tasks
        self._tasks = _tasks
        self._helper_names = set()

    def register(self, func):
        '''register a task -
        typically used as a decorator to the task function.

        If a task by that name already exists,
        a TaskAlreadyRegistered exception is raised.
        '''
        if hasattr(self._tasks, func.__name__):
            raise TaskAlreadyRegistered(func.__name__)
        setattr(self._tasks, func.__name__, func)
        return _get_invalid_accessor(func.__name__)

    def register_helper(self, func):
        '''a helper is a task that is not directly exposed to
        the command line
        '''
        self._helper_names.add(func.__name__)
        return self.register(func)

    def get_tasks(self):
        '''
        return tasks as list of (name, function) tuples
        '''
        def predicate(item):
            return (inspect.isfunction(item) and
                item.__name__ not in self._helper_names
            )
        return inspect.getmembers(self._tasks, predicate)

    def get_task(self, name, include_helpers=True):
        '''get task identified by name or raise TaskNotFound if there
        is no such task
        '''
        if not include_helpers and name in self._helper_names:
            raise TaskNotFound(name)
        try:
            return getattr(self._tasks, name)
        except AttributeError:
            raise TaskNotFound(name)

    def help(self, taskname=None):
        '''list tasks or provide help for specific task'''
        if not taskname:
            print inspect.getdoc(self._tasks)
            print
            print 'Available tasks:'
            print
            for task in self.get_tasks():
                print '  ' + get_signature(*task)
                help_msg = inspect.getdoc(task[1]) or ''
                help_msg = help_msg.split('\n')[0]
                print '    ' + help_msg
                print
        else:
            try:
                task = self.get_task(taskname)
                print SHORT_HEADER
                print get_signature(task.__name__, task)
                print
                print inspect.getdoc(task)
                print
                print 'defined in: ' + inspect.getfile(task)
                print
            except TaskNotFound:
                print 'Task "%s" not found! Use "ape help" to get usage information.' % taskname

    def __getattr__(self, name):
        '''simple proxy to tasks module
        tasks have to be accessed using only this method,
        e.g. ape.tasks.foo() to call the foo task.
        '''
        return self.get_task(name)

    def superimpose(self, module):
        '''superimpose a task module on registered tasks'''
        featuremonkey.compose(module, self._tasks)
        self._tasks.FEATURE_SELECTION.append(module.__name__)

tasks = Tasks()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

from flask import Flask, make_response, jsonify
from flask.ext.restful import Api

from exceptions import CellariumException

class CellariumApi(Api):
    def handle_error(self, e):
        if isinstance(e, CellariumException):
            data = {"message": e.description}
            return self.make_response(data, e.code)

        return super(CellariumApi, self).handle_error(e)

app = Flask(__name__)
api = CellariumApi(app)

@app.errorhandler(404)
def general_page_not_found(e):
    response = jsonify({ "message" : e.description })
    return make_response(response, 404)

@app.after_request
def after_request(response):
	# Quick hack while developing
    response.headers.add('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
    response.headers.add('Access-Control-Allow-Origin', 'http://localhost:8000')
    print response.headers
    return response

import views
<commit_msg>Removed CORS-support<commit_after># -*- coding: utf-8 -*-

from flask import Flask, make_response, jsonify
from flask.ext.restful import Api

from exceptions import CellariumException

class CellariumApi(Api):
    def handle_error(self, e):
        if isinstance(e, CellariumException):
            data = {"message": e.description}
            return self.make_response(data, e.code)

        return super(CellariumApi, self).handle_error(e)

app = Flask(__name__)
api = CellariumApi(app)

@app.errorhandler(404)
def general_page_not_found(e):
    response = jsonify({ "message" : e.description })
    return make_response(response, 404)

import views
<|endoftext|>


<commit_before>__author__ = 'Jonathan Burket'

from api.annotations import *
from voluptuous import Required, Length, Schema, Range
from api.common import validate, check, WebException, WebSuccess, WebError

import api.problem
import api.user
import api.common

from api.app import app

game_state_schema = Schema({
    Required('avatar'): check(
        ("Invalid Avatar Value.", [int, Range(min=0, max=3)]),
    ),
    Required('eventid'): check(
        ("Invalid Event ID.", [int, Range(min=0, max=13)]),
    )
}, extra=True)

def get_category_statistics():
    db = api.common.get_conn()
    category_scores = {}
    for p in db.problems.find():
        if p['category'] in category_scores:
            category_scores[p['category']]['total'] += 1
        else:
            category_scores[p['category']] = {'solved': 0, 'total': 1}

    for p in api.problem.get_solved_problems(api.user.get_team()['tid']):
        category_scores[p['category']]['solved'] += 1

    return WebSuccess(data=category_scores)

# The index of the problem corresponds to its etc id
name_map = [None] * 93

# Level 1
name_map[0] = "Tyrannosaurus Hex"

# Level 1 - Floor
name_map[1] = "The Valley of Fear"              # Left Closet
name_map[2] = "RoboPhoto"                       # Right bookshelf ("Locked CD")
name_map[3] = "No Comment"                      # Bottom of the table ("Just a CD")
name_map[4] = "Internet Inspection"             # The computer
name_map[5] = "Caesar"                          # Top of table
name_map[6] = "Common Vulnerability Exercise"   # Bedside table

# Level 2
name_map[7] = "grepfriend"                      # Upper right table ("Documents that need to be cracked")
name_map[8] = "Substitution"                       # Middle upper right ("hard drive is locked")
name_map[9] = "Javascrypt"                      # Officer needs your help
name_map[10] = "ZOR"                    # Officer needs your help
name_map[11] = "Basic ASM"                   # Officer needs your help
name_map[12] = "Spoof Proof"                            # Officer needs help "cracking this"
name_map[13] = "Pickle Jar"                     # Trying to get a log of father's phone calls
name_map[14] = "Easy Overflow"                  # Officer is stuck on a problem

# Level 3-1
name_map[15] = None
name_map[16] = "Toaster Control"
name_map[17] = "Towers of Toast"

# Level 3-2
name_map[18] = "Delicious!"
name_map[19] = "CyborgSecrets"
name_map[20] = "BLendian"
name_map[21] = "Repeated XOR"
name_map[22] = None

#---------------------------------
# Level 4
#---------------------------------

# Binary
name_map[23] = "Write Right"
name_map[24] = "ExecuteMe"
name_map[25] = "Best Shell"
name_map[26] = "ROP 1"
name_map[27] = "Overflow 1"
name_map[28] = "Overflow 2"
name_map[29] = "Nevernote"
name_map[30] = "Guess"
name_map[31] = "Format"
name_map[32] = None

# Crypto
name_map[33] = "RSA Mistakes"
name_map[34] = None
name_map[35] = "Block"
name_map[36] = None
name_map[37] = None
name_map[38] = None
name_map[39] = None
name_map[40] = None
name_map[41] = None
name_map[42] = None

# Forensics
name_map[43] = "supercow"
name_map[44] = None
name_map[45] = None
name_map[46] = None
name_map[47] = None
name_map[48] = None
name_map[49] = None
name_map[50] = None
name_map[51] = None
name_map[52] = None

# Web
name_map[53] = "Injection 1"
name_map[54] = "Injection 2"
name_map[55] = "Injection 3"
name_map[56] = "Injection 4"
name_map[57] = "Massive Fail"
name_map[58] = "Potentially Hidden Password"
name_map[59] = None
name_map[60] = None
name_map[61] = None
name_map[62] = None

# Misc
name_map[63] = None
name_map[64] = None
name_map[65] = None
name_map[66] = None
name_map[67] = None
name_map[68] = None
name_map[69] = None
name_map[70] = None
name_map[71] = None
name_map[72] = None

# Master
name_map[73] = None
name_map[74] = None
name_map[75] = None
name_map[76] = None
name_map[77] = None
name_map[78] = None
name_map[79] = None
name_map[80] = None
name_map[81] = None
name_map[82] = None

# Reversing
name_map[83] = "Obfuscation"
name_map[84] = "Bit Puzzle"
name_map[85] = "Function Address"
name_map[86] = None
name_map[87] = None
name_map[88] = None
name_map[89] = None
name_map[90] = None
name_map[91] = None
name_map[92] = None
1

etcid_map = None
pid_map = None

def gen_maps():
    global etcid_map, pid_map
    # grabs the pid of the problem and creates the mapping from index (etcid) to pid
    etcid_map = {}
    for i, name in enumerate(name_map):
        if name is not None:
            problem = api.problem.get_problem(name=name)
            if problem is not None:
                etcid_map[i+1] = problem['pid']
            else:
                print("WARNING: No problem: %s" % name)
    pid_map = {v : k for k, v in etcid_map.items()}

def get_solved_indices():
    if pid_map is None:
        gen_maps()

    solved_problems = api.problem.get_solved_problems(api.user.get_team()['tid'])
    return WebSuccess(data=sorted([pid_map[p['pid']] for p in solved_problems if p['pid'] in pid_map.keys()]))

def get_game_problem(etcid):
    if etcid_map is None:
        gen_maps()

    useracct = api.user.get_user()
    try:
        pid = etcid_map[int(etcid)]
    except (IndexError, ValueError):
        raise WebException("Invalid Problem")

    p = api.problem.get_problem(pid=pid, tid=useracct['tid'])
    return WebSuccess(data=p)

def get_game_problem_status(etcid):
    if etcid_map is None:
        gen_maps()
    useracct = api.user.get_user()
    try:
        pid = int(etcid)
    except ValueError:
        return WebException("Invalid problem id")
    if 'viewed_problems' in useracct:
        if pid in useracct['viewed_problems']:
            return WebSuccess(data={'viewed': True})
    return WebSuccess(data={'viewed': False})

def get_problems():
    if etcid_map is None:
        gen_maps()
    useracct = api.user.get_user()
    unlocked_problems = [problem for problem in api.problem.get_unlocked_problems(useracct['tid']) if problem['pid'] in pid_map.keys()]
    solved_problems = [p['pid'] for p in api.problem.get_solved_problems(api.user.get_team()['tid'])]
    if 'viewed_problems' not in useracct:
        db = api.common.get_conn()
        db.users.update({'uid': useracct['uid']}, {'$set': {'viewed_problems': []}})
        none_viewed = True
    else:
        none_viewed = False
    for problem in unlocked_problems:
        if none_viewed:
            problem['viewed'] = False
        else:
            problem['viewed'] = pid_map[problem['pid']] in useracct['viewed_problems']
        problem['game_id'] = pid_map[problem['pid']]
        problem['solved'] = problem['pid'] in solved_problems
    return WebSuccess(data=unlocked_problems)

def set_game_problem_status(etcid, viewed):
    if etcid_map is None:
        gen_maps()

    useracct = api.user.get_user()
    uid = useracct['uid']
    db = api.common.get_conn()

    try:
        pid = int(etcid)
    except ValueError:
        return WebException("Invalid problem id")

    if 'viewed_problems' in useracct:
        viewed_problems = useracct['viewed_problems']
        if (pid not in viewed_problems) and viewed:
            viewed_problems += [pid]
            db.users.update({'uid': uid}, {'$set': {'viewed_problems': viewed_problems}})
        elif (pid in viewed_problems) and not viewed:
            viewed_problems.remove(pid)
            db.users.update({'uid': uid}, {'$set': {'viewed_problems': viewed_problems}})

    else:
        db.users.update({'uid': uid}, {'$set': {'viewed_problems': [pid]}})
    return WebSuccess()

def etcid_to_pid(etcid):
    if etcid_map is None:
        gen_maps()

    try:
        pid = etcid_map[int(etcid)]
        return WebSuccess(data=pid)
    except (IndexError, ValueError):
        raise WebException("Invalid Problem")

def get_state():
    useracct = api.user.get_user()
    return WebSuccess(data={
        'level': useracct['level'],
        'avatar': useracct['avatar'],
        'eventid': useracct['eventid']
    })

def update_state(avatar, eventid, level):
    db = api.common.get_conn()
    useracct = api.user.get_user()

    try:
        avatar = int(avatar)
        eventid = int(eventid)
    except ValueError:
        raise WebException("Invalid State Value Type")

    validate(game_state_schema, {
        "avatar": avatar,
        "eventid": eventid
    })

    db.users.update({'uid': useracct['uid']},
                    {'$set': {'avatar': avatar, 'eventid': eventid, 'level': level}})

    return WebSuccess("Update Successful")
<commit_msg>Updates to game problems<commit_after>__author__ = 'Jonathan Burket'

from api.annotations import *
from voluptuous import Required, Length, Schema, Range
from api.common import validate, check, WebException, WebSuccess, WebError

import api.problem
import api.user
import api.common

from api.app import app

game_state_schema = Schema({
    Required('avatar'): check(
        ("Invalid Avatar Value.", [int, Range(min=0, max=3)]),
    ),
    Required('eventid'): check(
        ("Invalid Event ID.", [int, Range(min=0, max=13)]),
    )
}, extra=True)

def get_category_statistics():
    db = api.common.get_conn()
    category_scores = {}
    for p in db.problems.find():
        if p['category'] in category_scores:
            category_scores[p['category']]['total'] += 1
        else:
            category_scores[p['category']] = {'solved': 0, 'total': 1}

    for p in api.problem.get_solved_problems(api.user.get_team()['tid']):
        category_scores[p['category']]['solved'] += 1

    return WebSuccess(data=category_scores)

# The index of the problem corresponds to its etc id
name_map = [None] * 93

# Level 1
name_map[0] = "Tyrannosaurus Hex"

# Level 1 - Floor
name_map[1] = "The Valley of Fear"              # Left Closet
name_map[2] = "RoboPhoto"                       # Right bookshelf ("Locked CD")
name_map[3] = "No Comment"                      # Bottom of the table ("Just a CD")
name_map[4] = "Internet Inspection"             # The computer
name_map[5] = "Caesar"                          # Top of table
name_map[6] = "Common Vulnerability Exercise"   # Bedside table

# Level 2
name_map[7] = "Grep is Still Your Friend"                      # Upper right table ("Documents that need to be cracked")
name_map[8] = "Substitution"                       # Middle upper right ("hard drive is locked")
name_map[9] = "Javascrypt"                      # Officer needs your help
name_map[10] = "ZOR"                    # Officer needs your help
name_map[11] = "Basic ASM"                   # Officer needs your help
name_map[12] = "Spoof Proof"                            # Officer needs help "cracking this"
name_map[13] = "Pickle Jar"                     # Trying to get a log of father's phone calls
name_map[14] = "Easy Overflow"                  # Officer is stuck on a problem

# Level 3-1
name_map[15] = None
name_map[16] = "Toaster Control"
name_map[17] = "Towers of Toast"

# Level 3-2
name_map[18] = "Delicious!"
name_map[19] = "CyborgSecrets"
name_map[20] = "BLendian"
name_map[21] = "Repeated XOR"
name_map[22] = None

#---------------------------------
# Level 4
#---------------------------------

# Binary
name_map[23] = "Write Right"
name_map[24] = "ExecuteMe"
name_map[25] = "Best Shell"
name_map[26] = "ROP 1"
name_map[27] = "Overflow 1"
name_map[28] = "Overflow 2"
name_map[29] = "Nevernote"
name_map[30] = "Guess"
name_map[31] = "Format"
name_map[32] = None

# Crypto
name_map[33] = "RSA Mistakes"
name_map[34] = None
name_map[35] = "Block"
name_map[36] = "Low Entropy"
name_map[37] = "Web Interception"
name_map[38] = "RSA"
name_map[39] = None
name_map[40] = None
name_map[41] = None
name_map[42] = None

# Forensics
name_map[43] = "Supercow"
name_map[44] = "PNG or Not?"
name_map[45] = "Snapcat"
name_map[46] = None
name_map[47] = None
name_map[48] = None
name_map[49] = None
name_map[50] = None
name_map[51] = None
name_map[52] = None

# Web
name_map[53] = "Injection 1"
name_map[54] = "Injection 2"
name_map[55] = "Injection 3"
name_map[56] = "Injection 4"
name_map[57] = "Massive Fail"
name_map[58] = "Potentially Hidden Password"
name_map[59] = None
name_map[60] = None
name_map[61] = None
name_map[62] = None

# Misc
name_map[63] = None
name_map[64] = None
name_map[65] = None
name_map[66] = None
name_map[67] = None
name_map[68] = None
name_map[69] = None
name_map[70] = None
name_map[71] = None
name_map[72] = None

# Master
name_map[73] = "Baleful"
name_map[74] = None
name_map[75] = None
name_map[76] = None
name_map[77] = None
name_map[78] = None
name_map[79] = None
name_map[80] = None
name_map[81] = None
name_map[82] = None

# Reversing
name_map[83] = "Obfuscation"
name_map[84] = "Bit Puzzle"
name_map[85] = "Function Address"
name_map[86] = None
name_map[87] = None
name_map[88] = None
name_map[89] = None
name_map[90] = None
name_map[91] = None
name_map[92] = None
1

etcid_map = None
pid_map = None

def gen_maps():
    global etcid_map, pid_map
    # grabs the pid of the problem and creates the mapping from index (etcid) to pid
    etcid_map = {}
    for i, name in enumerate(name_map):
        if name is not None:
            problem = api.problem.get_problem(name=name)
            if problem['category'] not in set(['Cryptography', 'Reverse Engineering', 'Web Exploitation', 'Binary Exploitation', 'Forensics', 'Miscellaneous', 'Master Challenge']):
                print("WARNING: %s uses invalid category %s" % (problem['name'], problem['category']))
            if problem is not None:
                etcid_map[i+1] = problem['pid']
            else:
                print("WARNING: No problem: %s" % name)
    pid_map = {v : k for k, v in etcid_map.items()}

def get_solved_indices():
    if pid_map is None:
        gen_maps()

    solved_problems = api.problem.get_solved_problems(api.user.get_team()['tid'])
    return WebSuccess(data=sorted([pid_map[p['pid']] for p in solved_problems if p['pid'] in pid_map.keys()]))

def get_game_problem(etcid):
    if etcid_map is None:
        gen_maps()

    useracct = api.user.get_user()
    try:
        pid = etcid_map[int(etcid)]
    except (IndexError, ValueError):
        raise WebException("Invalid Problem")

    p = api.problem.get_problem(pid=pid, tid=useracct['tid'])
    return WebSuccess(data=p)

def get_game_problem_status(etcid):
    if etcid_map is None:
        gen_maps()
    useracct = api.user.get_user()
    try:
        pid = int(etcid)
    except ValueError:
        return WebException("Invalid problem id")
    if 'viewed_problems' in useracct:
        if pid in useracct['viewed_problems']:
            return WebSuccess(data={'viewed': True})
    return WebSuccess(data={'viewed': False})

def get_problems():
    if etcid_map is None:
        gen_maps()
    useracct = api.user.get_user()
    unlocked_problems = [problem for problem in api.problem.get_unlocked_problems(useracct['tid']) if problem['pid'] in pid_map.keys()]
    solved_problems = [p['pid'] for p in api.problem.get_solved_problems(api.user.get_team()['tid'])]
    if 'viewed_problems' not in useracct:
        db = api.common.get_conn()
        db.users.update({'uid': useracct['uid']}, {'$set': {'viewed_problems': []}})
        none_viewed = True
    else:
        none_viewed = False
    for problem in unlocked_problems:
        if none_viewed:
            problem['viewed'] = False
        else:
            problem['viewed'] = pid_map[problem['pid']] in useracct['viewed_problems']
        problem['game_id'] = pid_map[problem['pid']]
        problem['solved'] = problem['pid'] in solved_problems
    return WebSuccess(data=unlocked_problems)

def set_game_problem_status(etcid, viewed):
    if etcid_map is None:
        gen_maps()

    useracct = api.user.get_user()
    uid = useracct['uid']
    db = api.common.get_conn()

    try:
        pid = int(etcid)
    except ValueError:
        return WebException("Invalid problem id")

    if 'viewed_problems' in useracct:
        viewed_problems = useracct['viewed_problems']
        if (pid not in viewed_problems) and viewed:
            viewed_problems += [pid]
            db.users.update({'uid': uid}, {'$set': {'viewed_problems': viewed_problems}})
        elif (pid in viewed_problems) and not viewed:
            viewed_problems.remove(pid)
            db.users.update({'uid': uid}, {'$set': {'viewed_problems': viewed_problems}})

    else:
        db.users.update({'uid': uid}, {'$set': {'viewed_problems': [pid]}})
    return WebSuccess()

def etcid_to_pid(etcid):
    if etcid_map is None:
        gen_maps()

    try:
        pid = etcid_map[int(etcid)]
        return WebSuccess(data=pid)
    except (IndexError, ValueError):
        raise WebException("Invalid Problem")

def get_state():
    useracct = api.user.get_user()
    return WebSuccess(data={
        'level': useracct['level'],
        'avatar': useracct['avatar'],
        'eventid': useracct['eventid']
    })

def update_state(avatar, eventid, level):
    db = api.common.get_conn()
    useracct = api.user.get_user()

    try:
        avatar = int(avatar)
        eventid = int(eventid)
    except ValueError:
        raise WebException("Invalid State Value Type")

    validate(game_state_schema, {
        "avatar": avatar,
        "eventid": eventid
    })

    db.users.update({'uid': useracct['uid']},
                    {'$set': {'avatar': avatar, 'eventid': eventid, 'level': level}})

    return WebSuccess("Update Successful")
<|endoftext|>


<commit_before># vim: tabstop=4 shiftwidth=4 expandtab
"""
This plugin can help save bandwidth for low bandwidth quota sites (how
unfortunate).

This is done by outputing cache friendly HTTP header tags like Last-Modified
and ETag. These values are calculated from the first entry returned by
entryList.
"""
__author__ = "Wari Wahab pyblosxom@wari.per.sg"
__version__ = "$Id$"

from libs import api

def prepare(args):
    request = args["request"]
    data = request.getData()
    config = request.getConfiguration()
    entryList = data["entry_list"]
    renderer = data["renderer"]

    if entryList:
        import os, time
        # Get our first file timestamp for ETag and Last Modified
        # Last-Modified: Wed, 20 Nov 2002 10:08:12 GMT
        # ETag: "2bdc4-7b5-3ddb5f0c"
        lastModed = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(entryList[0]['mtime']))
        if ((os.environ.get('HTTP_IF_NONE_MATCH','') == '"%s"' % entryList[0]['mtime']) or
            (os.environ.get('HTTP_IF_NONE_MATCH','') == '%s' % entryList[0]['mtime']) or
            (os.environ.get('HTTP_IF_MODIFIED_SINCE','') == lastModed)):
            renderer.addHeader(['Status: 304 Not Modified',
                    'ETag: "%s"' % entryList[0]['mtime'],
                    'Last-Modified: %s' % lastModed])
            renderer.needsContentType(None)
            renderer.render()

        from libs import tools
        tools.logRequest(config.get('logfile',''), '304')
        renderer.addHeader(['ETag: "%s"' % entryList[0]['mtime'],
            'Last-Modified: %s' % lastModed])

def initialize():
    api.prepareChain.register(prepare)
<commit_msg>No point running conditional http when entries have got no mtime (found bug when running Will's plugin_info plugin)<commit_after># vim: tabstop=4 shiftwidth=4 expandtab
"""
This plugin can help save bandwidth for low bandwidth quota sites (how
unfortunate).

This is done by outputing cache friendly HTTP header tags like Last-Modified
and ETag. These values are calculated from the first entry returned by
entryList.
"""
__author__ = "Wari Wahab pyblosxom@wari.per.sg"
__version__ = "$Id$"

from libs import api

def prepare(args):
    request = args["request"]
    data = request.getData()
    config = request.getConfiguration()
    entryList = data["entry_list"]
    renderer = data["renderer"]

    if entryList and entryList[0].has_key('mtime'):
        import os, time
        # Get our first file timestamp for ETag and Last Modified
        # Last-Modified: Wed, 20 Nov 2002 10:08:12 GMT
        # ETag: "2bdc4-7b5-3ddb5f0c"
        lastModed = time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(entryList[0]['mtime']))
        if ((os.environ.get('HTTP_IF_NONE_MATCH','') == '"%s"' % entryList[0]['mtime']) or
            (os.environ.get('HTTP_IF_NONE_MATCH','') == '%s' % entryList[0]['mtime']) or
            (os.environ.get('HTTP_IF_MODIFIED_SINCE','') == lastModed)):
            renderer.addHeader(['Status: 304 Not Modified',
                    'ETag: "%s"' % entryList[0]['mtime'],
                    'Last-Modified: %s' % lastModed])
            renderer.needsContentType(None)
            renderer.render()

        from libs import tools
        tools.logRequest(config.get('logfile',''), '304')
        renderer.addHeader(['ETag: "%s"' % entryList[0]['mtime'],
            'Last-Modified: %s' % lastModed])

def initialize():
    api.prepareChain.register(prepare)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2015,2016 deipi.com LLC and contributors. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
from __future__ import absolute_import

import os
import itertools

try:
    from dfw.core.utils import json
    from dfw.core.utils.datastructures.nested import NestedDict
except ImportError:
    import json
    NestedDict = dict

import msgpack

__all__ = ['Xapiand']

try:
    import requests
except ImportError:
    raise ImportError("Xapiand requires the installation of the requests module.")

class Result(NestedDict):
    def __init__(self, *args, **kwargs):
        super(Result, self).__init__(*args, **kwargs)
        for k in list(self):
            if k[0] == '_':
                setattr(self, k, self.pop(k))

class Results(object):
    def __init__(self, meta, generator):
        for k, v in meta.get('_query', {}).items():
            if k[0] == '_':
                setattr(self, k, v)
        aggregations = NestedDict(meta.get('_aggregations', {}))
        setattr(self, 'aggregations', aggregations)
        self.generator = generator

    def __len__(self):
        return int(getattr(self, 'total_count', 0))

    def __iter__(self):
        return self

    def next(self):
        return Result(next(iter(self.generator)))
    __next__ = next

class DoesNotExist(Exception):
    pass

NA = object()

class Xapiand(object):

    """
    An object which manages connections to xapiand and acts as a
    go-between for API calls to it
    """

    session = requests.Session()
    session.mount('http://', requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100))
    _methods = dict(
        search=(session.get, True, 'results'),
        facets=(session.get, True, 'facets'),
        stats=(session.get, False, 'result'),
        get=(session.get, False, 'result'),
        delete=(session.delete, False, 'result'),
        head=(session.head, False, 'result'),
        post=(session.post, False, 'result'),
        put=(session.put, False, 'result'),
        patch=(session.patch, False, 'result'),
    )

    def __init__(self, ip='127.0.0.1', port=8880, commit=False, prefix=None):
        if ip and ':' in ip:
            ip, _, port = ip.partition(':')
        self.ip = ip
        self.port = port
        self.commit = commit
        self.prefix = prefix

    def _build_url(self, action_request, index, ip, port, nodename, id, body):
        if ip and ':' in ip:
            ip, _, port = ip.partition(':')
        if not ip:
            ip = self.ip
        if not port:
            port = self.port
        host = '{}:{}'.format(ip, port)

        prefix = '{}/'.format(self.prefix) if self.prefix else ''

        if not isinstance(index, (tuple, list)):
            index = [index]
        index = ','.join(prefix + i for i in index)

        nodename = '@{}'.format(nodename) if nodename else ''

        if id is None and action_request != 'post':
            action_request = '_{}/'.format(action_request)
        elif action_request == 'post':
            action_request = ''
        else:
            action_request = id

        url = 'http://{}/{}{}/{}'.format(host, index, nodename, action_request)

        return url

    def _send_request(self, action_request, index, ip=None, port=None, nodename=None, id=None, body=None, default=NA, **kwargs):
        """
        :arg action_request: Perform index, delete, serch, facets, stats, patch, head actions per request
        :arg query: Query to process on xapiand
        :arg index: index path
        :arg ip: address to connect to xapiand
        :arg port: port to connect to xapiand
        :arg nodename: Node name, if empty is assigned randomly
        :arg id: Document ID
        :arg body: File or dictionary with the body of the request
        """
        method, stream, key = self._methods[action_request]
        url = self._build_url(action_request, index, ip, port, nodename, id, body)

        params = kwargs.pop('params', None)
        if params is not None:
            kwargs['params'] = dict((k.replace('__', '.'), (v and 1 or 0) if isinstance(v, bool) else v) for k, v in params.items())

        stream = kwargs.pop('stream', stream)
        if stream is not None:
            kwargs['stream'] = stream

        headers = kwargs.setdefault('headers', {})
        accept = headers.setdefault('accept', 'application/x-msgpack')
        content_type = headers.setdefault('content-type', accept)
        is_msgpack = 'application/x-msgpack' in content_type
        is_json = 'application/json' in content_type

        if body is not None:
            if isinstance(body, dict):
                if is_msgpack:
                    body = msgpack.packb(body)
                elif is_json:
                    body = json.dumps(body)
            elif os.path.isfile(body):
                body = open(body, 'r')
            res = method(url, body, **kwargs)
        else:
            res = method(url, **kwargs)

        if res.status_code == 404 and action_request in ('patch', 'delete', 'get'):
            if default is NA:
                raise DoesNotExist
            return default
        else:
            res.raise_for_status()

        content_type = res.headers.get('content-type', '')
        is_msgpack = 'application/x-msgpack' in content_type
        is_json = 'application/json' in content_type

        if stream:
            def results(chunks, size=100):
                def fetch():
                    fetch.cache = []
                    fetch.cache.extend(l for l in itertools.islice(chunks, size) if l)
                fetch()
                first = True
                while fetch.cache:
                    for chunk in fetch.cache:
                        if is_msgpack:
                            if first:
                                first = False
                                if ord(chunk[-1]) & 0xf0 == 0x90:
                                    chunk = chunk[:-1] + '\x90'
                                elif chunk[-3] == '\xdc':
                                    chunk = chunk[:-3] + '\x90'
                                elif chunk[-5] == '\xdd':
                                    chunk = chunk[:-5] + '\x90'
                                else:
                                    raise IOError("Unexpected chunk!")
                            yield msgpack.unpackb(chunk)
                        elif is_json:
                            if first:
                                first = False
                                if chunk.rstrip().endswith('['):
                                    chunk += ']}}'
                                else:
                                    raise IOError("Unexpected chunk!")
                            elif chunk.lstrip().startswith(']'):
                                continue
                            else:
                                chunk = chunk.rstrip().rstrip(',')
                            yield json.loads(chunk)
                        else:
                            yield chunk
                    fetch()
            results = results(res.iter_content(chunk_size=None))
            meta = next(results)
        else:
            if is_msgpack:
                content = msgpack.unpackb(res.content)
            elif is_json:
                content = json.loads(res.content)
            else:
                content = res.content
            results = [content]
            meta = {}

        results = Results(meta, results)
        if key == 'result':
            results = results.next()

        for k, v in res.headers.items():
            setattr(results, k.replace('-', '_'), v)

        return results

    def search(self, index, query=None, partial=None, terms=None, offset=None, limit=None, sort=None, facets=None, language=None, pretty=False, volatile=False, kwargs=None, **kw):
        kwargs = kwargs or {}
        kwargs.update(kw)
        kwargs['params'] = dict(
            pretty=pretty,
            volatile=volatile,
        )
        if query is not None:
            kwargs['params']['query'] = query
        if partial is not None:
            kwargs['params']['partial'] = partial
        if terms is not None:
            kwargs['params']['terms'] = terms
        if offset is not None:
            kwargs['params']['offset'] = offset
        if limit is not None:
            kwargs['params']['limit'] = limit
        if sort is not None:
            kwargs['params']['sort'] = sort
        if facets is not None:
            kwargs['params']['facets'] = facets
        if language is not None:
            kwargs['params']['language'] = language
        return self._send_request('search', index, **kwargs)

    def facets(self, index, query=None, partial=None, terms=None, offset=None, limit=None, sort=None, facets=None, language=None, pretty=False, volatile=False, kwargs=None, **kw):
        kwargs = kwargs or {}
        kwargs.update(kw)
        kwargs['params'] = dict(
            pretty=pretty,
            volatile=volatile,
        )
        if query is not None:
            kwargs['params']['query'] = query
        if partial is not None:
            kwargs['params']['partial'] = partial
        if terms is not None:
            kwargs['params']['terms'] = terms
        if offset is not None:
            kwargs['params']['offset'] = offset
        if limit is not None:
            kwargs['params']['limit'] = limit
        if sort is not None:
            kwargs['params']['sort'] = sort
        if facets is not None:
            kwargs['params']['facets'] = facets
        if language is not None:
            kwargs['params']['language'] = language
        return self._send_request('facets', index, **kwargs)

    def stats(self, index, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['params'] = dict(
            pretty=pretty,
        )
        return self._send_request('stats', index, **kwargs)

    def head(self, index, id, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['params'] = dict(
            pretty=pretty,
        )
        return self._send_request('head', index, **kwargs)

    def get(self, index, id, default=NA, pretty=False, volatile=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['params'] = dict(
            pretty=pretty,
            volatile=volatile,
        )
        kwargs['default'] = default
        return self._send_request('get', index, **kwargs)

    def delete(self, index, id, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('delete', index, **kwargs)

    def post(self, index, body, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['body'] = body
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('post', index, **kwargs)

    def put(self, index, body, id, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['body'] = body
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('put', index, **kwargs)
    index = put

    def patch(self, index, id, body, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['body'] = body
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('patch', index, **kwargs)

# TODO: Get settings for these from django.conf.settings:
XAPIAND_SANDBOX_PREFIX = 'sandbox'
XAPIAND_LIVE_PREFIX = 'live'
XAPIAND_HOST = '127.0.0.1'
XAPIAND_PORT = 8880
XAPIAND_COMMIT = False

live = Xapiand(ip=XAPIAND_HOST, port=XAPIAND_PORT, commit=XAPIAND_COMMIT, prefix=XAPIAND_LIVE_PREFIX)
sandbox = Xapiand(ip=XAPIAND_HOST, port=XAPIAND_PORT, commit=XAPIAND_COMMIT, prefix=XAPIAND_SANDBOX_PREFIX)
<commit_msg>Xapiand Python client: remove facets command<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2015,2016 deipi.com LLC and contributors. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
from __future__ import absolute_import

import os
import itertools

try:
    from dfw.core.utils import json
    from dfw.core.utils.datastructures.nested import NestedDict
except ImportError:
    import json
    NestedDict = dict

import msgpack

__all__ = ['Xapiand']

try:
    import requests
except ImportError:
    raise ImportError("Xapiand requires the installation of the requests module.")

class Result(NestedDict):
    def __init__(self, *args, **kwargs):
        super(Result, self).__init__(*args, **kwargs)
        for k in list(self):
            if k[0] == '_':
                setattr(self, k, self.pop(k))

class Results(object):
    def __init__(self, meta, generator):
        for k, v in meta.get('_query', {}).items():
            if k[0] == '_':
                setattr(self, k, v)
        aggregations = NestedDict(meta.get('_aggregations', {}))
        setattr(self, 'aggregations', aggregations)
        self.generator = generator

    def __len__(self):
        return int(getattr(self, 'total_count', 0))

    def __iter__(self):
        return self

    def next(self):
        return Result(next(iter(self.generator)))
    __next__ = next

class DoesNotExist(Exception):
    pass

NA = object()

class Xapiand(object):

    """
    An object which manages connections to xapiand and acts as a
    go-between for API calls to it
    """

    session = requests.Session()
    session.mount('http://', requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100))
    _methods = dict(
        search=(session.get, True, 'results'),
        stats=(session.get, False, 'result'),
        get=(session.get, False, 'result'),
        delete=(session.delete, False, 'result'),
        head=(session.head, False, 'result'),
        post=(session.post, False, 'result'),
        put=(session.put, False, 'result'),
        patch=(session.patch, False, 'result'),
    )

    def __init__(self, ip='127.0.0.1', port=8880, commit=False, prefix=None):
        if ip and ':' in ip:
            ip, _, port = ip.partition(':')
        self.ip = ip
        self.port = port
        self.commit = commit
        self.prefix = prefix

    def _build_url(self, action_request, index, ip, port, nodename, id, body):
        if ip and ':' in ip:
            ip, _, port = ip.partition(':')
        if not ip:
            ip = self.ip
        if not port:
            port = self.port
        host = '{}:{}'.format(ip, port)

        prefix = '{}/'.format(self.prefix) if self.prefix else ''

        if not isinstance(index, (tuple, list)):
            index = [index]
        index = ','.join(prefix + i for i in index)

        nodename = '@{}'.format(nodename) if nodename else ''

        if id is None and action_request != 'post':
            action_request = '_{}/'.format(action_request)
        elif action_request == 'post':
            action_request = ''
        else:
            action_request = id

        url = 'http://{}/{}{}/{}'.format(host, index, nodename, action_request)

        return url

    def _send_request(self, action_request, index, ip=None, port=None, nodename=None, id=None, body=None, default=NA, **kwargs):
        """
        :arg action_request: Perform index, delete, serch, stats, patch, head actions per request
        :arg query: Query to process on xapiand
        :arg index: index path
        :arg ip: address to connect to xapiand
        :arg port: port to connect to xapiand
        :arg nodename: Node name, if empty is assigned randomly
        :arg id: Document ID
        :arg body: File or dictionary with the body of the request
        """
        method, stream, key = self._methods[action_request]
        url = self._build_url(action_request, index, ip, port, nodename, id, body)

        params = kwargs.pop('params', None)
        if params is not None:
            kwargs['params'] = dict((k.replace('__', '.'), (v and 1 or 0) if isinstance(v, bool) else v) for k, v in params.items())

        stream = kwargs.pop('stream', stream)
        if stream is not None:
            kwargs['stream'] = stream

        headers = kwargs.setdefault('headers', {})
        accept = headers.setdefault('accept', 'application/x-msgpack')
        content_type = headers.setdefault('content-type', accept)
        is_msgpack = 'application/x-msgpack' in content_type
        is_json = 'application/json' in content_type

        if body is not None:
            if isinstance(body, dict):
                if is_msgpack:
                    body = msgpack.packb(body)
                elif is_json:
                    body = json.dumps(body)
            elif os.path.isfile(body):
                body = open(body, 'r')
            res = method(url, body, **kwargs)
        else:
            res = method(url, **kwargs)

        if res.status_code == 404 and action_request in ('patch', 'delete', 'get'):
            if default is NA:
                raise DoesNotExist
            return default
        else:
            res.raise_for_status()

        content_type = res.headers.get('content-type', '')
        is_msgpack = 'application/x-msgpack' in content_type
        is_json = 'application/json' in content_type

        if stream:
            def results(chunks, size=100):
                def fetch():
                    fetch.cache = []
                    fetch.cache.extend(l for l in itertools.islice(chunks, size) if l)
                fetch()
                first = True
                while fetch.cache:
                    for chunk in fetch.cache:
                        if is_msgpack:
                            if first:
                                first = False
                                if ord(chunk[-1]) & 0xf0 == 0x90:
                                    chunk = chunk[:-1] + '\x90'
                                elif chunk[-3] == '\xdc':
                                    chunk = chunk[:-3] + '\x90'
                                elif chunk[-5] == '\xdd':
                                    chunk = chunk[:-5] + '\x90'
                                else:
                                    raise IOError("Unexpected chunk!")
                            yield msgpack.unpackb(chunk)
                        elif is_json:
                            if first:
                                first = False
                                if chunk.rstrip().endswith('['):
                                    chunk += ']}}'
                                else:
                                    raise IOError("Unexpected chunk!")
                            elif chunk.lstrip().startswith(']'):
                                continue
                            else:
                                chunk = chunk.rstrip().rstrip(',')
                            yield json.loads(chunk)
                        else:
                            yield chunk
                    fetch()
            results = results(res.iter_content(chunk_size=None))
            meta = next(results)
        else:
            if is_msgpack:
                content = msgpack.unpackb(res.content)
            elif is_json:
                content = json.loads(res.content)
            else:
                content = res.content
            results = [content]
            meta = {}

        results = Results(meta, results)
        if key == 'result':
            results = results.next()

        for k, v in res.headers.items():
            setattr(results, k.replace('-', '_'), v)

        return results

    def search(self, index, query=None, partial=None, terms=None, offset=None, limit=None, sort=None, language=None, pretty=False, volatile=False, kwargs=None, **kw):
        kwargs = kwargs or {}
        kwargs.update(kw)
        kwargs['params'] = dict(
            pretty=pretty,
            volatile=volatile,
        )
        if query is not None:
            kwargs['params']['query'] = query
        if partial is not None:
            kwargs['params']['partial'] = partial
        if terms is not None:
            kwargs['params']['terms'] = terms
        if offset is not None:
            kwargs['params']['offset'] = offset
        if limit is not None:
            kwargs['params']['limit'] = limit
        if sort is not None:
            kwargs['params']['sort'] = sort
        if language is not None:
            kwargs['params']['language'] = language
        return self._send_request('search', index, **kwargs)

    def stats(self, index, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['params'] = dict(
            pretty=pretty,
        )
        return self._send_request('stats', index, **kwargs)

    def head(self, index, id, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['params'] = dict(
            pretty=pretty,
        )
        return self._send_request('head', index, **kwargs)

    def get(self, index, id, default=NA, pretty=False, volatile=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['params'] = dict(
            pretty=pretty,
            volatile=volatile,
        )
        kwargs['default'] = default
        return self._send_request('get', index, **kwargs)

    def delete(self, index, id, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('delete', index, **kwargs)

    def post(self, index, body, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['body'] = body
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('post', index, **kwargs)

    def put(self, index, body, id, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['body'] = body
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('put', index, **kwargs)
    index = put

    def patch(self, index, id, body, commit=None, pretty=False, kwargs=None):
        kwargs = kwargs or {}
        kwargs['id'] = id
        kwargs['body'] = body
        kwargs['params'] = dict(
            commit=self.commit if commit is None else commit,
            pretty=pretty,
        )
        return self._send_request('patch', index, **kwargs)

# TODO: Get settings for these from django.conf.settings:
XAPIAND_SANDBOX_PREFIX = 'sandbox'
XAPIAND_LIVE_PREFIX = 'live'
XAPIAND_HOST = '127.0.0.1'
XAPIAND_PORT = 8880
XAPIAND_COMMIT = False

live = Xapiand(ip=XAPIAND_HOST, port=XAPIAND_PORT, commit=XAPIAND_COMMIT, prefix=XAPIAND_LIVE_PREFIX)
sandbox = Xapiand(ip=XAPIAND_HOST, port=XAPIAND_PORT, commit=XAPIAND_COMMIT, prefix=XAPIAND_SANDBOX_PREFIX)
<|endoftext|>


<commit_before># See readme.md for instructions on running this code.
import logging
import json
import requests
import html2text

class DefineHandler(object):
    '''
    This plugin define a word that the user inputs. It
    looks for messages starting with '@mention-bot'.
    '''

    DEFINITION_API_URL = 'https://owlbot.info/api/v1/dictionary/{}?format=json'
    REQUEST_ERROR_MESSAGE = 'Definition not available.'
    EMPTY_WORD_REQUEST_ERROR_MESSAGE = 'Please enter a word to define.'
    PHRASE_ERROR_MESSAGE = 'Definitions for phrases are not available.'

    def usage(self):
        return '''
            This plugin will allow users to define a word. Users should preface
            messages with @mention-bot.
            '''

    def handle_message(self, message, client, state_handler):
        original_content = message['content'].strip()
        bot_response = self.get_bot_define_response(original_content)

        if message['type'] == 'private':
            client.send_message(dict(
                type='private',
                to=message['sender_email'],
                content=bot_response,
            ))
        else:
            client.send_message(dict(
                type='stream',
                to=message['display_recipient'],
                subject=message['subject'],
                content=bot_response,
            ))

    def get_bot_define_response(self, original_content):
        split_content = original_content.split(' ')
        # If there are more than one word (a phrase)
        if len(split_content) > 1:
            return DefineHandler.PHRASE_ERROR_MESSAGE

        to_define = split_content[0].strip()
        to_define_lower = to_define.lower()

        # No word was entered.
        if not to_define_lower:
            return self.EMPTY_WORD_REQUEST_ERROR_MESSAGE
        else:
            response = '**{}**:\n'.format(to_define)

            try:
                # Use OwlBot API to fetch definition.
                api_result = requests.get(self.DEFINITION_API_URL.format(to_define_lower))
                # Convert API result from string to JSON format.
                definitions = api_result.json()

                # Could not fetch definitions for the given word.
                if not definitions:
                    response += self.REQUEST_ERROR_MESSAGE
                else:  # Definitions available.
                    # Show definitions line by line.
                    for d in definitions:
                        example = d['example'] if d['example'] else '*No example available.*'
                        response += '\n' + '* (**{}**) {}\n  {}'.format(d['type'], d['defenition'], html2text.html2text(example))

            except Exception as e:
                response += self.REQUEST_ERROR_MESSAGE
                logging.exception(e)

            return response

handler_class = DefineHandler
<commit_msg>bots: Switch define bot to use send_reply.<commit_after># See readme.md for instructions on running this code.
import logging
import json
import requests
import html2text

class DefineHandler(object):
    '''
    This plugin define a word that the user inputs. It
    looks for messages starting with '@mention-bot'.
    '''

    DEFINITION_API_URL = 'https://owlbot.info/api/v1/dictionary/{}?format=json'
    REQUEST_ERROR_MESSAGE = 'Definition not available.'
    EMPTY_WORD_REQUEST_ERROR_MESSAGE = 'Please enter a word to define.'
    PHRASE_ERROR_MESSAGE = 'Definitions for phrases are not available.'

    def usage(self):
        return '''
            This plugin will allow users to define a word. Users should preface
            messages with @mention-bot.
            '''

    def handle_message(self, message, client, state_handler):
        original_content = message['content'].strip()
        bot_response = self.get_bot_define_response(original_content)

        client.send_reply(message, bot_response)

    def get_bot_define_response(self, original_content):
        split_content = original_content.split(' ')
        # If there are more than one word (a phrase)
        if len(split_content) > 1:
            return DefineHandler.PHRASE_ERROR_MESSAGE

        to_define = split_content[0].strip()
        to_define_lower = to_define.lower()

        # No word was entered.
        if not to_define_lower:
            return self.EMPTY_WORD_REQUEST_ERROR_MESSAGE
        else:
            response = '**{}**:\n'.format(to_define)

            try:
                # Use OwlBot API to fetch definition.
                api_result = requests.get(self.DEFINITION_API_URL.format(to_define_lower))
                # Convert API result from string to JSON format.
                definitions = api_result.json()

                # Could not fetch definitions for the given word.
                if not definitions:
                    response += self.REQUEST_ERROR_MESSAGE
                else:  # Definitions available.
                    # Show definitions line by line.
                    for d in definitions:
                        example = d['example'] if d['example'] else '*No example available.*'
                        response += '\n' + '* (**{}**) {}\n  {}'.format(d['type'], d['defenition'], html2text.html2text(example))

            except Exception as e:
                response += self.REQUEST_ERROR_MESSAGE
                logging.exception(e)

            return response

handler_class = DefineHandler
<|endoftext|>