<commit_before># -*- coding: UTF-8 -*-
#
# Desc:    This file is part of the ecromedos Document Preparation System
# Author:  Tobias Koch <tobias@tobijk.de>
# License: MIT
# URL:     http://www.ecromedos.net
#

import os, re
from lxml import etree
import com.lepture.mistune as mistune
from net.ecromedos.configreader import ECMDSConfigReader
from net.ecromedos.dtdresolver  import ECMDSDTDResolver

class ECMLRendererError(Exception):
    pass

class ECMLRenderer(mistune.Renderer):

    def __init__(self, config):
        mistune.Renderer.__init__(self)
        self.section_level = 0
        self.footnotes_map = {}
        self.config = config
    #end if

    # BLOCK ELEMENTS

    def block_code(self, code, language=None):
        if language == None:
            language = "text"

        return """<listing>%(code)s</listing>""" % {
            "lang": language,
            "code": mistune.escape(code)
        }
    #end function

    def block_quote(self, text):
        return "
%s
" % text
    #end function

    def block_html(self, ecml):
        return ecml
    #end function

    def header(self, text, level, raw=None):
        retval = ""

        diff = level - self.section_level

        if diff > 1:
            msg = "Heading '%s' skips a section level." % text
            raise ECMLRendererError(msg)
        else:
            sign = int(diff > 0) - int(diff < 0)
            diff = sign * diff

            # we close until we reach the new level
            if sign <= 0:
                for i in range(diff+1):
                    retval += "
"
            #end if
        #end if

        retval += '
' % level
        retval += '<title>%s</title>'    % text

        self.section_level = level
        return retval
    #end function

    def hrule(self):
        return ""
    #end function

    def list(self, body, ordered=True):
        if ordered:
            return "
%s
" % body
        else:
            return "
%s
" % body
    #end function

    def list_item(self, text):
        return "
%s
" % text
    #end function

    def paragraph(self, text):
        return "
%s
" % text
    #end function

    def table(self, header, body):
        return """\

                %s

                %s

""" % (header, body)
    #end function

    def table_row(self, content):
        return '
%s
' % content
    #end function

    def table_cell(self, content, **flags):
        align = flags['align']
        if not align:
            return '%s	' % content
        return '%s	' % (align, content)
    #end function

    # INLINE ELEMENTS

    def autolink(self, link, is_email=False):
        text = link = mistune.escape(link)
        if is_email:
            link = "mailto:%s" % link
        return '<link url="%s">%s</link>' % (link, link)
    #end function

    def codespan(self, text):
        return "<tt>%s</tt>" % mistune.escape(text)
    #end function

    def double_emphasis(self, text):
        return "%s" % text
    #end function

    def emphasis(self, text):
        return "%s" % text
    #end function

    def image(self, src, title, text):
        src  = mistune.escape_link(src)
        text = mistune.escape(text, quote=True)

        if title:
            title = mistune.escape(title, quote=True)
            ecml = """\

%s

            """ % (title, src)
        else:
            ecml = """\

            """ % (src,)
        #end if

        return ecml
    #end function

    def linebreak(self):
        return "
"
    #end function

    def newline(self):
        return ""
    #end function

    def footnote_ref(self, key, index):
        return '<footnote-ref idref="%s"/>' % mistune.escape(key)
    #end function

    def footnote_item(self, key, text):
        self.footnotes_map[key] = text
        return ""
    #end function

    def footnotes(self, text):
        return ""
    #end function

    def link(self, link, title, text):
        link = mistune.escape_link(link)
        return '<link url="%s">%s' % (link, text)
    #end function

    def strikethrough(self, text):
        return text
    #end function

    def text(self, text):
        return mistune.escape(text)
    #end function

    def inline_html(self, ecml):
        return ecml
    #end function

#end class

class MarkdownConverterError(Exception):
    pass

class MarkdownConverter(ECMDSDTDResolver, ECMDSConfigReader):

    DOCUMENT_TEMPLATE = """\
<!DOCTYPE %(document_type)s SYSTEM "http://www.ecromedos.net/dtd/3.0/ecromedos.dtd">
<%(document_type)s bcor="%(bcor)s" div="%(div)s" lang="%(lang)s" papersize="%(papersize)s" parskip="%(parskip)s" secnumdepth="%(secnumdepth)s" secsplitdepth="%(secsplitdepth)s">

    %(header)s

    <make-toc depth="%(tocdepth)s" lof="%(have_lof)s" lot="%(have_lot)s" lol="%(have_lol)s"/>

    %(contents)s

</%(document_type)s>
    """

    def __init__(self, options):
        ECMDSConfigReader.__init__(self)
        ECMDSDTDResolver. __init__(self)

        self.readConfig(options)
        self.document_settings = {
            "document_type": "report",
            "bcor": "0cm",
            "div": "16",
            "lang": "en_US",
            "papersize": "a4",
            "parskip": "half",
            "secnumdepth": "2",
            "secsplitdepth": "1",
            "header": "",
            "tocdepth": "5",
            "have_lof": "no",
            "have_lot": "no",
            "have_lol": "no",
            "contents": ""
        }
        self.user_settings = options
    #end function

    def convert(self, string):
        # initial conversion happening here
        renderer  = ECMLRenderer(self.config)
        markdown  = mistune.Markdown(renderer=renderer)
        contents  = markdown(self.parse_preamble(string))
        header    = self.generate_header(self.document_settings)
        footnotes = renderer.footnotes_map

        # close all open sections
        for i in range(renderer.section_level):
            contents += "
"

        self.document_settings["header"] = header
        self.document_settings["contents"] = contents
        self.document_settings["footnotes"] = footnotes

        contents = MarkdownConverter.DOCUMENT_TEMPLATE % self.document_settings

        # parse XML to do post-processing
        parser = etree.XMLParser(
            load_dtd=True,
            remove_blank_text=True
        )
        parser.resolvers.add(self)
        tree = etree.fromstring(contents, parser=parser)

        # fix footnotes, tables, section names...
        tree = self.post_process(tree)

        # return pretty-printed result
        return etree.tostring(tree, pretty_print=True, encoding="unicode")
    #end function

    def parse_preamble(self, string):
        document_settings = {}

        m = re.match(r"\A---+\s*?$.*?^---+\s*?$", string, flags=re.MULTILINE|re.DOTALL)

        if not m:
            return string

        m = m.group(0)

        for line in m.strip("-").splitlines():
            if not line.strip():
                continue

            try:
                k, v = [x.strip() for x in line.split(":", 1)]
                k = k.replace("-", "_")
            except:
                continue

            if k != "author":
                document_settings[k] = v
            else:
                document_settings.setdefault(k, []).append(v)
            #end if
        #end for

        self.document_settings.update(document_settings)
        self.document_settings.update(self.user_settings)
        self.validate_settings(self.document_settings)

        return string[len(m):]
    #end function

    def generate_header(self, settings):
        header_elements = [
            "subject",
            "title",
            "subtitle",
            "author",
            "date",
            "publisher",
            "dedication"
        ]

        header = ""

        for element_name in header_elements:
            if element_name == "title":
                header += "<title>%s</title>\n" % settings.get("title", "")
            elif element_name == "author":
                for author in settings.get("author", []):
                    header += "<author>%s</author>\n" % author
            else:
                element_text = settings.get(element_name, "")
                if element_text:
                    header += "<%s>%s</%s>\n" % \
                            (element_name, element_text , element_name)
                #end if
            #end ifs
        #end for

        return "\n%s" % header
    #end function

    def validate_settings(self, settings):
        pass
    #end function

    def post_process(self, root_node):

        node = root_node

        while node is not None:
            if node.tag == "footnote-ref":
                node = self.__fix_footnote(node)
            elif node.tag == "section":
                node = self.__fix_section(node)
            elif node.tag == "table":
                node = self.__fix_table(node)
            elif node.tag == "thead":
                node = self.__fix_thead(node)
            elif node.tag == "tbody":
                node = self.__fix_tbody(node)
            elif node.tag == "figure":
                node = self.__fix_figure(node)
            elif node.tag == "img":
                node = self.__fix_img(node)
            #end if

            if len(node) != 0:
                node = node[0]
                continue

            while node is not None:
                following_sibling = node.getnext()

                if following_sibling is not None:
                    node = following_sibling
                    break

                node = node.getparent()
            #end while
        #end while

        return root_node
    #end function

    # PRIVATE

    def __fix_footnote(self, ref_node):
        footnotes    = self.document_settings["footnotes"]
        footnote_ref = ref_node.get("idref", None)
        footnote_def = footnotes.get(footnote_ref, None)

        if footnote_def == None:
            raise MarkdownConverterError(
                "Unresolved footnote reference '%s'" % footnote_ref)
        #end if

        try:
            footnote = etree.fromstring(footnote_def)
        except etree.XMLSyntaxError as e:
            raise MarkdownConverterError(
                "Footnote '%s' is not a valid XML fragment." % footnote_ref)
        #end try

        if footnote.tag != "p":
            raise MarkdownConverterError(
                "Footnote '%s' is an invalid block element." % footnote_ref)
        #end if

        footnote.tag  = "footnote"
        footnote.tail = ref_node.tail
        ref_node.getparent().replace(ref_node, footnote)

        return footnote
    #end function

    def __fix_section(self, section_node):
        document_type = self.document_settings["document_type"]

        if document_type == "article":
            section_names = ["section", "subsection", "subsubsection", "minisection"]
        else:
            section_names = ["chapter", "section", "subsection", "subsubsection", "minisection"]
        #end if

        level = int(section_node.attrib["level"]) - 1
        section_node.tag = section_names[level]
        del section_node.attrib["level"]

        return section_node
    #end function

    def __fix_table(self, table_node):
        if table_node.xpath("colgroup"):
            return table_node

        header_cells = table_node.xpath("thead/tr/td")

        width = 100 / len(header_cells)
        colgroup_node = etree.Element("colgroup")

        for cell in header_cells:
            col_node = etree.Element("col")
            col_node.attrib["width"] = str(width) + "%"
            colgroup_node.append(col_node)
        #end for

        table_node.insert(0, colgroup_node)

        return table_node
    #end function

    def __fix_thead(self, thead_node):
        header_row = thead_node.xpath("tr")[0]
        header_row.tag = "th"
        thead_node.getparent().replace(thead_node, header_row)
        return header_row
    #end function

    def __fix_tbody(self, tbody_node):
        table_node = tbody_node.getparent()
        body_rows  = tbody_node.xpath("tr")

        for row in body_rows:
            table_node.append(row)

        table_node.remove(tbody_node)

        return body_rows[0]
    #end function

    def __fix_figure(self, figure_node):
        section_elements = {
            "chapter": 1,
            "section": 1,
            "subsection": 1,
            "subsubsection": 1,
            "minisection": 1,
            "preface": 1,
            "abstract": 1,
            "appendix": 1
        }

        parent      = figure_node.getparent()
        grandparent = parent.getparent()

        if not section_elements.get(grandparent.tag, None):
            raise MarkdownConverterError("The parent or grandparent of image "\
                "'%s' is not a sectioning element." % figure_node.get("alt"))

        if etree.tostring(parent, method="text", encoding="unicode").strip() == "":
            grandparent.replace(parent, figure_node)
        else:
            figure_node.attrib["align"] = "left"
            img_node = figure_node.xpath("img")[0]
            img_node.attrib["print-width"] = "50%"
            img_node.attrib["screen-width"] = "400px"
        #end if

        return figure_node
    #end function

    def __fix_img(self, img_node):
        src = img_node.attrib["src"]

        if os.path.isabs(src) or os.path.isfile(src):
            return img_node
        if not "input_dir" in self.config:
            return img_node

        input_dir = self.config["input_dir"]
        img_node.attrib["src"] = os.path.normpath(os.path.join(input_dir, src))

        return img_node
    #end function

#end class
<commit_msg>markdown processor allows markdown in header fields<commit_after># -*- coding: UTF-8 -*-
#
# Desc:    This file is part of the ecromedos Document Preparation System
# Author:  Tobias Koch <tobias@tobijk.de>
# License: MIT
# URL:     http://www.ecromedos.net
#

import os, re
from lxml import etree
import com.lepture.mistune as mistune
from net.ecromedos.configreader import ECMDSConfigReader
from net.ecromedos.dtdresolver  import ECMDSDTDResolver

class ECMLRendererError(Exception):
    pass

class ECMLRenderer(mistune.Renderer):

    def __init__(self, config):
        mistune.Renderer.__init__(self)
        self.section_level = 0
        self.footnotes_map = {}
        self.config = config
    #end if

    # BLOCK ELEMENTS

    def block_code(self, code, language=None):
        if language == None:
            language = "text"

        return """<listing>%(code)s</listing>""" % {
            "lang": language,
            "code": mistune.escape(code)
        }
    #end function

    def block_quote(self, text):
        return "
%s
" % text

    def block_html(self, ecml):
        return ecml

    def header(self, text, level, raw=None):
        retval = ""

        diff = level - self.section_level

        if diff > 1:
            msg = "Heading '%s' skips a section level." % text
            raise ECMLRendererError(msg)
        else:
            sign = int(diff > 0) - int(diff < 0)
            diff = sign * diff

            # we close until we reach the new level
            if sign <= 0:
                for i in range(diff+1):
                    retval += "
"
            #end if
        #end if

        retval += '
' % level
        retval += '<title>%s</title>'    % text

        self.section_level = level
        return retval
    #end function

    def hrule(self):
        return ""

    def list(self, body, ordered=True):
        if ordered:
            return "
%s
" % body
        else:
            return "
%s
" % body
    #end function

    def list_item(self, text):
        return "
%s
" % text

    def paragraph(self, text):
        return "
%s
" % text

    def table(self, header, body):
        return """\

                %s

                %s

""" % (header, body)
    #end function

    def table_row(self, content):
        return '
%s
' % content

    def table_cell(self, content, **flags):
        align = flags['align']
        if not align:
            return '%s	' % content
        return '%s	' % (align, content)
    #end function

    # INLINE ELEMENTS

    def autolink(self, link, is_email=False):
        link = mistune.escape(link)
        href = "mailto:%s" % link if is_email else link
        return '<link url="%s">%s</link>' % (href, link)
    #end function

    def codespan(self, text):
        return "<tt>%s</tt>" % mistune.escape(text)

    def double_emphasis(self, text):
        return "%s" % text

    def emphasis(self, text):
        return "%s" % text

    def image(self, src, title, text):
        src  = mistune.escape_link(src)
        text = mistune.escape(text, quote=True)

        if title:
            title = mistune.escape(title, quote=True)
            ecml = """\

%s

            """ % (title, src)
        else:
            ecml = """\

            """ % (src,)
        #end if

        return ecml
    #end function

    def linebreak(self):
        return "
"

    def newline(self):
        return ""

    def footnote_ref(self, key, index):
        return '<footnote-ref idref="%s"/>' % mistune.escape(key)

    def footnote_item(self, key, text):
        self.footnotes_map[key] = text
        return ""
    #end function

    def footnotes(self, text):
        return ""

    def link(self, link, title, text):
        link = mistune.escape_link(link)
        return '<link url="%s">%s' % (link, text)
    #end function

    def strikethrough(self, text):
        return text

    def text(self, text):
        return mistune.escape(text)

    def inline_html(self, ecml):
        return ecml

#end class

class MarkdownConverterError(Exception):
    pass

class MarkdownConverter(ECMDSDTDResolver, ECMDSConfigReader):

    DOCUMENT_TEMPLATE = """\
<!DOCTYPE %(document_type)s SYSTEM "http://www.ecromedos.net/dtd/3.0/ecromedos.dtd">
<%(document_type)s bcor="%(bcor)s" div="%(div)s" lang="%(lang)s" papersize="%(papersize)s" parskip="%(parskip)s" secnumdepth="%(secnumdepth)s" secsplitdepth="%(secsplitdepth)s">

    %(header)s
    %(legal)s

    <make-toc depth="%(tocdepth)s" lof="%(have_lof)s" lot="%(have_lot)s" lol="%(have_lol)s"/>

    %(contents)s

</%(document_type)s>
    """

    def __init__(self, options):
        ECMDSConfigReader.__init__(self)
        ECMDSDTDResolver. __init__(self)

        self.readConfig(options)
        self.document_settings = {
            "document_type": "report",
            "bcor": "0cm",
            "div": "16",
            "lang": "en_US",
            "papersize": "a4",
            "parskip": "half",
            "secnumdepth": "2",
            "secsplitdepth": "1",
            "header": "",
            "tocdepth": "5",
            "have_lof": "no",
            "have_lot": "no",
            "have_lol": "no",
            "contents": "",
            "legal": ""
        }
        self.user_settings = options
    #end function

    def convert(self, string):
        # initial conversion happening here
        renderer  = ECMLRenderer(self.config)
        markdown  = mistune.Markdown(renderer=renderer)
        contents  = markdown(self.parse_preamble(string))
        footnotes = renderer.footnotes_map

        def inline_markdown(s_):
            t_ = etree.fromstring(markdown(s_))

            # Maybe there can be a more elegant solution for this?
            v_ = etree.tostring(t_, pretty_print=True,
                    encoding="unicode")
            v_ = re.sub(r"^\
|\
$", "", v_,
                    flags=re.MULTILINE)

            return v_.strip()
        #end inline function

        for k, v in self.document_settings.items():
            if not v or isinstance(v, str) and not v.strip():
                continue

            if k == "legal":
                self.document_settings["legal"] = \
                    "<legal>" + \
                        markdown(v) + \
                    "</legal>"
            elif k == "author":
                for i in range(len(v)):
                    v[i] = inline_markdown(v[i])
            else:
                v = re.sub(r"\s+", " ", v, flags=re.MULTILINE).strip()
                self.document_settings[k] = inline_markdown(v)
        #end if

        header = self.generate_header(self.document_settings)

        # close all open sections
        for i in range(renderer.section_level):
            contents += "
"

        self.document_settings["header"] = header
        self.document_settings["contents"] = contents
        self.document_settings["footnotes"] = footnotes

        contents = MarkdownConverter.DOCUMENT_TEMPLATE % self.document_settings

        # parse XML to do post-processing
        parser = etree.XMLParser(
            load_dtd=True,
            remove_blank_text=True
        )
        parser.resolvers.add(self)
        tree = etree.fromstring(contents, parser=parser)

        # fix footnotes, tables, section names...
        tree = self.post_process(tree)

        # return pretty-printed result
        return etree.tostring(tree, pretty_print=True, encoding="unicode")
    #end function

    def parse_preamble(self, string):
        document_settings = {}

        m = re.match(r"\A---+\s*?$.*?^---+\s*?$", string,
                flags=re.MULTILINE|re.DOTALL)

        if not m:
            return string

        m = m.group(0)
        k = ""
        v = ""

        for line in m.strip("-").splitlines():
            if re.match(r"^\S+.*:.*$", line):
                k, v = line.split(":", 1)

                if k != "author":
                    document_settings[k] = v
                else:
                    document_settings.setdefault(k, []).append(v)
            elif k:
                if k != "author":
                    document_settings[k] += line
                else:
                    document_settings[k][-1] += line
            #end if
        #end for

        self.document_settings.update(document_settings)
        self.document_settings.update(self.user_settings)
        self.validate_settings(self.document_settings)

        return string[len(m):]
    #end function

    def generate_header(self, settings):
        header_elements = [
            "subject",
            "title",
            "subtitle",
            "author",
            "date",
            "publisher",
            "dedication"
        ]

        header = ""

        for element_name in header_elements:
            if element_name == "title":
                header += "<title>%s</title>\n" % settings.get("title", "")
            elif element_name == "author":
                for author in settings.get("author", []):
                    header += "<author>%s</author>\n" % author
            else:
                element_text = settings.get(element_name, "")
                if element_text:
                    header += "<%s>%s</%s>\n" % \
                            (element_name, element_text , element_name)
                #end if
            #end ifs
        #end for

        return "\n%s" % header
    #end function

    def validate_settings(self, settings):
        pass

    def post_process(self, root_node):

        node = root_node

        while node is not None:
            if node.tag == "footnote-ref":
                node = self.__fix_footnote(node)
            elif node.tag == "section":
                node = self.__fix_section(node)
            elif node.tag == "table":
                node = self.__fix_table(node)
            elif node.tag == "thead":
                node = self.__fix_thead(node)
            elif node.tag == "tbody":
                node = self.__fix_tbody(node)
            elif node.tag == "figure":
                node = self.__fix_figure(node)
            elif node.tag == "img":
                node = self.__fix_img(node)
            #end if

            if len(node) != 0:
                node = node[0]
                continue

            while node is not None:
                following_sibling = node.getnext()

                if following_sibling is not None:
                    node = following_sibling
                    break

                node = node.getparent()
            #end while
        #end while

        return root_node
    #end function

    # PRIVATE

    def __fix_footnote(self, ref_node):
        footnotes    = self.document_settings["footnotes"]
        footnote_ref = ref_node.get("idref", None)
        footnote_def = footnotes.get(footnote_ref, None)

        if footnote_def == None:
            raise MarkdownConverterError(
                "Unresolved footnote reference '%s'" % footnote_ref)
        #end if

        try:
            footnote = etree.fromstring(footnote_def)
        except etree.XMLSyntaxError as e:
            raise MarkdownConverterError(
                "Footnote '%s' is not a valid XML fragment." % footnote_ref)
        #end try

        if footnote.tag != "p":
            raise MarkdownConverterError(
                "Footnote '%s' is an invalid block element." % footnote_ref)
        #end if

        footnote.tag  = "footnote"
        footnote.tail = ref_node.tail
        ref_node.getparent().replace(ref_node, footnote)

        return footnote
    #end function

    def __fix_section(self, section_node):
        document_type = self.document_settings["document_type"]

        if document_type == "article":
            section_names = [
                "section",
                "subsection",
                "subsubsection",
                "minisection"
            ]
        else:
            section_names = [
                "chapter",
                "section",
                "subsection",
                "subsubsection",
                "minisection"
            ]
        #end if

        level = int(section_node.attrib["level"]) - 1
        section_node.tag = section_names[level]
        del section_node.attrib["level"]

        return section_node
    #end function

    def __fix_table(self, table_node):
        if table_node.xpath("colgroup"):
            return table_node

        header_cells = table_node.xpath("thead/tr/td")

        width = 100 / len(header_cells)
        colgroup_node = etree.Element("colgroup")

        for cell in header_cells:
            col_node = etree.Element("col")
            col_node.attrib["width"] = str(width) + "%"
            colgroup_node.append(col_node)
        #end for

        table_node.insert(0, colgroup_node)

        return table_node
    #end function

    def __fix_thead(self, thead_node):
        header_row = thead_node.xpath("tr")[0]
        header_row.tag = "th"
        thead_node.getparent().replace(thead_node, header_row)
        return header_row
    #end function

    def __fix_tbody(self, tbody_node):
        table_node = tbody_node.getparent()
        body_rows  = tbody_node.xpath("tr")

        for row in body_rows:
            table_node.append(row)

        table_node.remove(tbody_node)

        return body_rows[0]
    #end function

    def __fix_figure(self, figure_node):
        section_elements = {
            "chapter": 1,
            "section": 1,
            "subsection": 1,
            "subsubsection": 1,
            "minisection": 1,
            "preface": 1,
            "abstract": 1,
            "appendix": 1
        }

        parent      = figure_node.getparent()
        grandparent = parent.getparent()

        if not section_elements.get(grandparent.tag, None):
            raise MarkdownConverterError("The parent or grandparent of image "\
                "'%s' is not a sectioning element." % figure_node.get("alt"))

        if etree.tostring(parent, method="text", encoding="unicode")\
                .strip() == "":
            grandparent.replace(parent, figure_node)
        else:
            figure_node.attrib["align"] = "left"
            img_node = figure_node.xpath("img")[0]
            img_node.attrib["print-width"] = "50%"
            img_node.attrib["screen-width"] = "400px"
        #end if

        return figure_node
    #end function

    def __fix_img(self, img_node):
        src = img_node.attrib["src"]

        if os.path.isabs(src) or os.path.isfile(src):
            return img_node
        if not "input_dir" in self.config:
            return img_node

        input_dir = self.config["input_dir"]
        img_node.attrib["src"] = os.path.normpath(os.path.join(input_dir, src))

        return img_node
    #end function

#end class
<|endoftext|>


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

__author__ = 'Jay Shepherd'

import argparse
import re
import sys
from urllib.parse import urlparse
from urllib.request import urlopen

import bs4
import netaddr

def check_args(args=None):
    parser = argparse.ArgumentParser(description="IP Webpage Scraper")
    parser.add_argument('url',
                        type=argparse_url_type,
                        help="URL to scrape, must be FQDN ie https://example.com")
    return parser.parse_args(args)

def argparse_url_type(url_to_check):
    """
    This is a helper function that wires check_args to validate_url
    :param url_to_check: The URL positional argument passed from argparse
    :return: Value if okay, otherwise raises argparse exception
    """
    result = validate_url(url_to_check)
    if result:
        return url_to_check
    else:
        raise argparse.ArgumentTypeError(
            "{} is an invalid URL, must specify fqdn, ex. https://www.example.com".format(url_to_check))

def validate_url(url_arg):
    """
    Takes string input and validates url refers to a valid webpage for passing into other functions
    :param url_arg: URL to check for validation
    :return: Boolean result of validation check
    """
    parse = urlparse(url_arg)
    if parse.scheme not in ['http', 'https']:
        return False
    if '.' not in parse.netloc:
        return False
    return True

def get_webpage_text(url_input):
    """
    Extracts text of webpage and returns
    :param url_input: Fully-qualified webpage url to get contents of
    :return: Text of webpage
    """
    data = bs4.BeautifulSoup(urlopen(url_input), 'html.parser').get_text()
    return data

def validate_ip(ip):
    """
    Validates if passed string is an ipv4 or ipv6 address or network.
    :param ip: IP Address as string
    :return: Boolean Result
    """
    try:
        if '/' in ip:
            netaddr.IPNetwork(ip)
        else:
            netaddr.IPAddress(ip)
        return True
    except netaddr.AddrFormatError:
        return False

def ip_from_string(string):
    """
    Takes a string and extracts all valid IP Addresses as a SET of Strings
    Uses the validate_ip helper function to achieve.
    :param string: Any string of data
    :return: IP Addresses as a netaddr.IPSet or empty netaddr.IPSet if none found
    """
    ip_regex = re.compile('(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?:\/[0-9]{1,2})?')
    potential_ips = ip_regex.findall(string)
    valid_ips = []
    for ip in potential_ips:
        if validate_ip(ip) is True:
            valid_ips.append(ip)
    return netaddr.IPSet(valid_ips)

def ipv6_from_string(string):
    """
    Takes a string and extracts all valid IPv6 Addresses as a SET of Strings
    Uses the validate_ip helper function to achieve.
    :param string: Any string of data
    :return: IPv6 Addresses as a netaddr.IPSet or empty netaddr.IPSet if none found
    """

    ipv6_regex = re.compile('([0-9a-fA-f]{4}:)([0-9a-fA-f]{4}::/)([0-9]{2})')

    potential_ipv6s = re.findall(ipv6_regex, string)
    valid_ipv6s = []

    for ipv6 in potential_ipv6s:
        ipv6 = ipv6[0] + ipv6[75]
        if validate_ip(ipv6) is True:
            valid_ipv6s.append(ipv6)
    return netaddr.IPSet(valid_ipv6s)

def main(url):
    webpage_text = get_webpage_text(url)
    address_list = ip_from_string(webpage_text)
    addressv6_list = ipv6_from_string(webpage_text)
    if address_list:
        print('================\nIPv4 addresses:')
        for cidr in address_list.iter_cidrs():
            print(cidr)
    else:
        print("No ipv4s found when scraping {}".format(url))

    if addressv6_list:
        print('================\nIPv6 addresses:')
        for cidr in addressv6_list.iter_cidrs():
            print(cidr)
    else:
        print("No ipv6s found when scraping {}".format(url))

def entrypoint():
    url = check_args(sys.argv[1:]).url
    main(url)

if __name__ == '__main__':
    entrypoint()
<commit_msg>need to use non capturing group syntax<commit_after>#!/usr/bin/env python3

__author__ = 'Jay Shepherd'

import argparse
import re
import sys
from urllib.parse import urlparse
from urllib.request import urlopen

import bs4
import netaddr

def check_args(args=None):
    parser = argparse.ArgumentParser(description="IP Webpage Scraper")
    parser.add_argument('url',
                        type=argparse_url_type,
                        help="URL to scrape, must be FQDN ie https://example.com")
    return parser.parse_args(args)

def argparse_url_type(url_to_check):
    """
    This is a helper function that wires check_args to validate_url
    :param url_to_check: The URL positional argument passed from argparse
    :return: Value if okay, otherwise raises argparse exception
    """
    result = validate_url(url_to_check)
    if result:
        return url_to_check
    else:
        raise argparse.ArgumentTypeError(
            "{} is an invalid URL, must specify fqdn, ex. https://www.example.com".format(url_to_check))

def validate_url(url_arg):
    """
    Takes string input and validates url refers to a valid webpage for passing into other functions
    :param url_arg: URL to check for validation
    :return: Boolean result of validation check
    """
    parse = urlparse(url_arg)
    if parse.scheme not in ['http', 'https']:
        return False
    if '.' not in parse.netloc:
        return False
    return True

def get_webpage_text(url_input):
    """
    Extracts text of webpage and returns
    :param url_input: Fully-qualified webpage url to get contents of
    :return: Text of webpage
    """
    data = bs4.BeautifulSoup(urlopen(url_input), 'html.parser').get_text()
    return data

def validate_ip(ip):
    """
    Validates if passed string is an ipv4 or ipv6 address or network.
    :param ip: IP Address as string
    :return: Boolean Result
    """
    try:
        if '/' in ip:
            netaddr.IPNetwork(ip)
        else:
            netaddr.IPAddress(ip)
        return True
    except netaddr.AddrFormatError:
        return False

def ip_from_string(string):
    """
    Takes a string and extracts all valid IP Addresses as a SET of Strings
    Uses the validate_ip helper function to achieve.
    :param string: Any string of data
    :return: IP Addresses as a netaddr.IPSet or empty netaddr.IPSet if none found
    """
    ip_regex = re.compile('(?:[0-9]{1,3}\.){3}[0-9]{1,3}(?:\/[0-9]{1,2})?')
    potential_ips = ip_regex.findall(string)
    valid_ips = []
    for ip in potential_ips:
        if validate_ip(ip) is True:
            valid_ips.append(ip)
    return netaddr.IPSet(valid_ips)

def ipv6_from_string(string):
    """
    Takes a string and extracts all valid IPv6 Addresses as a SET of Strings
    Uses the validate_ip helper function to achieve.
    :param string: Any string of data
    :return: IPv6 Addresses as a netaddr.IPSet or empty netaddr.IPSet if none found
    """

    ipv6_regex = re.compile('(?:[0-9a-fA-f]{4}:)(?:[0-9a-fA-f]{4}::/)(?:[0-9]{2})')

    potential_ipv6s = re.findall(ipv6_regex, string)
    valid_ipv6s = []

    for ipv6 in potential_ipv6s:
        ipv6 = ipv6[0] + ipv6[75]
        if validate_ip(ipv6) is True:
            valid_ipv6s.append(ipv6)
    return netaddr.IPSet(valid_ipv6s)

def main(url):
    webpage_text = get_webpage_text(url)
    address_list = ip_from_string(webpage_text)
    addressv6_list = ipv6_from_string(webpage_text)
    if address_list:
        print('================\nIPv4 addresses:')
        for cidr in address_list.iter_cidrs():
            print(cidr)
    else:
        print("No ipv4s found when scraping {}".format(url))

    if addressv6_list:
        print('================\nIPv6 addresses:')
        for cidr in addressv6_list.iter_cidrs():
            print(cidr)
    else:
        print("No ipv6s found when scraping {}".format(url))

def entrypoint():
    url = check_args(sys.argv[1:]).url
    main(url)

if __name__ == '__main__':
    entrypoint()
<|endoftext|>


<commit_before># Copyright 2016 BMC Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
import requests
import json

def event_create_good_response(status_code):
    """
    Determines what status codes represent a good response from an API call.
    """
    return status_code == requests.codes.created

class BaseEvent(object):
    def __init__(self, *args, **kwargs):
        self._created_at = kwargs['created_at'] if 'created_at' in kwargs else None
        self._event_id = kwargs['event_id'] if 'event_id' in kwargs else None
        self._fingerprint_fields = kwargs['fingerprint_fields'] if 'fingerprint_fields' in kwargs else None
        self._id = kwargs['id'] if 'id' in kwargs else None
        self._message = kwargs['message'] if 'message' in kwargs else None
        self._properties = kwargs['properties'] if 'properties' in kwargs else None
        self._source = kwargs['source'] if 'source' in kwargs else None
        self._sender = kwargs['sender'] if 'sender' in kwargs else None
        self._severity = kwargs['severity'] if 'severity' in kwargs else None
        self._status = kwargs['status'] if 'status' in kwargs else None
        self._tags = kwargs['tags'] if 'tags' in kwargs else None
        self._tenant_id = kwargs['tenant_id'] if 'tenant_id' in kwargs else None
        self._title = kwargs['title'] if 'title' in kwargs else None

        self._received_at = kwargs['received_at'] if 'received_at' in kwargs else None

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return "{0}(created_at={1}" \
               ", event_id={2}" \
               ", fingerprint_fields='{3}'" \
               ", id={4}" \
               ", message='{5}'" \
               ", properties={6}" \
               ", {7}" \
               ", sender='{8}'" \
               ", severity='{9}'" \
               ", status='{10}'" \
               ", tags='{11}'" \
               ", tenant_id={12}" \
               ", title={13}" \
               ")".format(
                self.__class__.__name__,
                self._created_at,
                self._event_id,
                self._fingerprint_fields,
                self._id,
                self._message,
                self._properties,
                self._source.__repr__() if self._source is not None else None,
                self._sender,
                self._severity,
                self._status,
                self._tags,
                self._tenant_id,
                self._title)

    @property
    def created_at(self):
        return self._created_at

    @property
    def event_id(self):
        return self._event_id

    @property
    def fingerprint_fields(self):
        return self._fingerprint_fields

    @property
    def id(self):
        return self._id

    @property
    def message(self):
        return self._message

    @property
    def properties(self):
        return self._properties

    @property
    def received_at(self):
        return self._received_at

    @property
    def title(self):
        return self._title

    @property
    def sender(self):
        return self._sender

    @property
    def severity(self):
        return self._severity

    @property
    def source(self):
        return self._source

    @property
    def status(self):
        return self._status

    @property
    def tags(self):
        return self._tags

    @property
    def tenant_id(self):
        return self._tenant_id

class RawEvent(BaseEvent):
    def __init__(self, *args, **kwargs):
        super(RawEvent, self).__init__(*args, **kwargs)

class Event(BaseEvent):
    def __init__(self, *args, **kwargs):
        super(Event, self).__init__(*args, **kwargs)
        self._id = None

    @property
    def id(self):
        return self._id

def serialize_instance(obj):
    d = {}
    print(obj)
    if obj.created_at is not None:
        d['createdAt'] = obj.created_at
    if obj.title is not None:
        d['title'] = obj.title
    if obj.source is not None:
        d['source'] = obj.source.serialize()
    return d

def event_create_handle_results(api_result, context=None):
    # Only process if we get HTTP result of 200
    result = None
    if api_result.status_code == requests.codes.created and len(api_result.text) > 0:
        result = json.loads(api_result.text)
    return result

def event_get_handle_results(api_result, context=None):
    logging.debug("event_get_handle_results")
    events = None
    # Only process if we get HTTP result of 200
    if api_result.status_code == requests.codes.ok:
        results = json.loads(api_result.text)
        events = []
        for event in results['results']:
            events.append(Event(fingerprint_fiels=event['fingerprintFields'],
                                first_seen_at=event['firstSeenAt'],
                                id=event['id'],
                                last_seen_at=event['lastSeenAt'],
                                properties=event['properties'],
                                severity=event['severity'],
                                status=event['status'],
                                times_seen=event['timesSeen'],
                                title=event['title']))

    return events
<commit_msg>Remove print statement<commit_after># Copyright 2016 BMC Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import logging
import requests
import json

def event_create_good_response(status_code):
    """
    Determines what status codes represent a good response from an API call.
    """
    return status_code == requests.codes.created

class BaseEvent(object):
    def __init__(self, *args, **kwargs):
        self._created_at = kwargs['created_at'] if 'created_at' in kwargs else None
        self._event_id = kwargs['event_id'] if 'event_id' in kwargs else None
        self._fingerprint_fields = kwargs['fingerprint_fields'] if 'fingerprint_fields' in kwargs else None
        self._id = kwargs['id'] if 'id' in kwargs else None
        self._message = kwargs['message'] if 'message' in kwargs else None
        self._properties = kwargs['properties'] if 'properties' in kwargs else None
        self._source = kwargs['source'] if 'source' in kwargs else None
        self._sender = kwargs['sender'] if 'sender' in kwargs else None
        self._severity = kwargs['severity'] if 'severity' in kwargs else None
        self._status = kwargs['status'] if 'status' in kwargs else None
        self._tags = kwargs['tags'] if 'tags' in kwargs else None
        self._tenant_id = kwargs['tenant_id'] if 'tenant_id' in kwargs else None
        self._title = kwargs['title'] if 'title' in kwargs else None

        self._received_at = kwargs['received_at'] if 'received_at' in kwargs else None

    def __str__(self):
        return self.__repr__()

    def __repr__(self):
        return "{0}(created_at={1}" \
               ", event_id={2}" \
               ", fingerprint_fields='{3}'" \
               ", id={4}" \
               ", message='{5}'" \
               ", properties={6}" \
               ", {7}" \
               ", sender='{8}'" \
               ", severity='{9}'" \
               ", status='{10}'" \
               ", tags='{11}'" \
               ", tenant_id={12}" \
               ", title={13}" \
               ")".format(
                self.__class__.__name__,
                self._created_at,
                self._event_id,
                self._fingerprint_fields,
                self._id,
                self._message,
                self._properties,
                self._source.__repr__() if self._source is not None else None,
                self._sender,
                self._severity,
                self._status,
                self._tags,
                self._tenant_id,
                self._title)

    @property
    def created_at(self):
        return self._created_at

    @property
    def event_id(self):
        return self._event_id

    @property
    def fingerprint_fields(self):
        return self._fingerprint_fields

    @property
    def id(self):
        return self._id

    @property
    def message(self):
        return self._message

    @property
    def properties(self):
        return self._properties

    @property
    def received_at(self):
        return self._received_at

    @property
    def title(self):
        return self._title

    @property
    def sender(self):
        return self._sender

    @property
    def severity(self):
        return self._severity

    @property
    def source(self):
        return self._source

    @property
    def status(self):
        return self._status

    @property
    def tags(self):
        return self._tags

    @property
    def tenant_id(self):
        return self._tenant_id

class RawEvent(BaseEvent):
    def __init__(self, *args, **kwargs):
        super(RawEvent, self).__init__(*args, **kwargs)

class Event(BaseEvent):
    def __init__(self, *args, **kwargs):
        super(Event, self).__init__(*args, **kwargs)
        self._id = None

    @property
    def id(self):
        return self._id

def serialize_instance(obj):
    d = {}
    if obj.created_at is not None:
        d['createdAt'] = obj.created_at
    if obj.title is not None:
        d['title'] = obj.title
    if obj.source is not None:
        d['source'] = obj.source.serialize()
    return d

def event_create_handle_results(api_result, context=None):
    # Only process if we get HTTP result of 200
    result = None
    if api_result.status_code == requests.codes.created and len(api_result.text) > 0:
        result = json.loads(api_result.text)
    return result

def event_get_handle_results(api_result, context=None):
    logging.debug("event_get_handle_results")
    events = None
    # Only process if we get HTTP result of 200
    if api_result.status_code == requests.codes.ok:
        results = json.loads(api_result.text)
        events = []
        for event in results['results']:
            events.append(Event(fingerprint_fiels=event['fingerprintFields'],
                                first_seen_at=event['firstSeenAt'],
                                id=event['id'],
                                last_seen_at=event['lastSeenAt'],
                                properties=event['properties'],
                                severity=event['severity'],
                                status=event['status'],
                                times_seen=event['timesSeen'],
                                title=event['title']))

    return events
<|endoftext|>


<commit_before>import logging
import os
import pickle
from datetime import datetime

from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import boto3

from src import settings

logger = logging.getLogger()

SCOPES = [
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/calendar.readonly",
    "https://www.googleapis.com/auth/drive.metadata.readonly",
]

def get_credentials():
    if not settings.DEBUG and not os.path.isfile(settings.GOOGLE_API_CLIENT_SECRET_PATH):
        _download_google_api_auth_files()
    credentials = None
    if os.path.exists(settings.GOOGLE_API_CREDENTIAL_PATH):
        with open(settings.GOOGLE_API_CREDENTIAL_PATH, "rb") as token:
            credentials = pickle.load(token)
    if not credentials:
        if credentials and credentials.expired and credentials.refresh_token:
            credentials.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                settings.GOOGLE_API_CLIENT_SECRET_PATH, SCOPES
            )
            credentials = flow.run_local_server()
        with open(settings.GOOGLE_API_CREDENTIAL_PATH, "wb") as token:
            pickle.dump(credentials, token)
    return credentials

def _download_google_api_auth_files():
    try:
        if not os.path.isdir("/tmp/config"):
            os.makedirs("/tmp/config")
        s3 = boto3.resource("s3")
        bucket = s3.Bucket(settings.S3_BUCKET_NAME)
        bucket.download_file("config/client_secret.json", settings.GOOGLE_API_CLIENT_SECRET_PATH)
        logger.info("Download S3 config/client_secret.json")
        bucket.download_file("config/client_secret.json", settings.GOOGLE_API_CREDENTIAL_PATH)
        logger.info("Download S3 config/credential.pickle")
    except Exception as e:
        # TODO: Error handling
        logger.info(e)

def get_service(name, version):
    """
    サービスを取得する
    """
    credentials = get_credentials()
    # ref https://github.com/googleapis/google-api-python-client/issues/299
    service = build(name, version, credentials=credentials, cache_discovery=False)
    return service

def main():
    service = get_service("calendar", "v3")

    now = datetime.utcnow().isoformat() + "Z"  # 'Z' indicates UTC time
    print("直近の5件のイベントを表示")
    eventsResult = (
        service.events()
        .list(
            calendarId="primary",
            timeMin=now,
            maxResults=5,
            singleEvents=True,
            orderBy="startTime",
        )
        .execute()
    )
    events = eventsResult.get("items", [])

    if not events:
        print("No upcoming events found.")
    for event in events:
        start = event["start"].get("dateTime", event["start"].get("date"))
        print(start, event["summary"])

if __name__ == "__main__":
    main()
<commit_msg>fixed<commit_after>import logging
import os
import pickle
from datetime import datetime

from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import boto3

from src import settings

logger = logging.getLogger()

SCOPES = [
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/calendar.readonly",
    "https://www.googleapis.com/auth/drive.metadata.readonly",
]

def get_credentials():
    if not settings.DEBUG and not os.path.isfile(settings.GOOGLE_API_CLIENT_SECRET_PATH):
        _download_google_api_auth_files()
    credentials = None
    if os.path.exists(settings.GOOGLE_API_CREDENTIAL_PATH):
        with open(settings.GOOGLE_API_CREDENTIAL_PATH, "rb") as token:
            credentials = pickle.load(token)
    if not credentials:
        if credentials and credentials.expired and credentials.refresh_token:
            credentials.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                settings.GOOGLE_API_CLIENT_SECRET_PATH, SCOPES
            )
            credentials = flow.run_local_server()
        with open(settings.GOOGLE_API_CREDENTIAL_PATH, "wb") as token:
            pickle.dump(credentials, token)
    return credentials

def _download_google_api_auth_files():
    try:
        if not os.path.isdir("/tmp/config"):
            os.makedirs("/tmp/config")
        s3 = boto3.resource("s3")
        bucket = s3.Bucket(settings.S3_BUCKET_NAME)
        bucket.download_file("config/client_secret.json", settings.GOOGLE_API_CLIENT_SECRET_PATH)
        logger.info("Download S3 config/client_secret.json")
        bucket.download_file("config/credential.pickle", settings.GOOGLE_API_CREDENTIAL_PATH)
        logger.info("Download S3 config/credential.pickle")
    except Exception as e:
        # TODO: Error handling
        logger.info(e)

def get_service(name, version):
    """
    サービスを取得する
    """
    credentials = get_credentials()
    # ref https://github.com/googleapis/google-api-python-client/issues/299
    service = build(name, version, credentials=credentials, cache_discovery=False)
    return service

def main():
    service = get_service("calendar", "v3")

    now = datetime.utcnow().isoformat() + "Z"  # 'Z' indicates UTC time
    print("直近の5件のイベントを表示")
    eventsResult = (
        service.events()
        .list(
            calendarId="primary",
            timeMin=now,
            maxResults=5,
            singleEvents=True,
            orderBy="startTime",
        )
        .execute()
    )
    events = eventsResult.get("items", [])

    if not events:
        print("No upcoming events found.")
    for event in events:
        start = event["start"].get("dateTime", event["start"].get("date"))
        print(start, event["summary"])

if __name__ == "__main__":
    main()
<|endoftext|>


<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
"""

import urllib2

from lib.core.data import conf
from lib.core.data import logger
from lib.core.common import getUnicode
from lib.core.common import logHTTPTraffic
from lib.core.exception import sqlmapConnectionException
from lib.core.threads import getCurrentThreadData

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    # maximum number of redirections to any single URL
    # this is needed because of the state that cookies introduce
    max_repeats = 4

    # maximum total number of redirections (regardless of URL) before
    # assuming we're in a loop
    max_redirections = 10

    def common_http_redirect(self, result, headers, code, content, msg):
        threadData = getCurrentThreadData()
        threadData.lastRedirectMsg = (threadData.lastRequestUID, content)

        responseMsg     = "HTTP response "
        responseMsg += "[#%d] (%d %s):\n" % (threadData.lastRequestUID, code, getUnicode(msg))
        if headers:
            logHeaders = "\n".join(["%s: %s" % (key.capitalize() if isinstance(key, basestring) else key, getUnicode(value)) for (key, value) in headers.items()])
        else:
            logHeaders = ""

        if conf.verbose <= 5:
            responseMsg += getUnicode(logHeaders)
        elif conf.verbose > 5:
            responseMsg += "%s\n%s\n" % (logHeaders, content)

        logger.log(7, responseMsg)

        if "location" in headers:
            result.redurl = headers.getheaders("location")[0].split("?")[0]
        elif "uri" in headers:
            result.redurl = headers.getheaders("uri")[0].split("?")[0]

        if "set-cookie" in headers:
            result.setcookie = headers["set-cookie"].split("; path")[0]

        result.redcode = code

        return result

    def http_error_301(self, req, fp, code, msg, headers):
        self.infinite_loop_check(req)

        content = None
        try:
            content = fp.read()
        except Exception, msg:
            dbgMsg  = "there was a problem while retrieving "
            dbgMsg += "redirect response content (%s)" % msg
            logger.debug(dbgMsg)

        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
        return self.common_http_redirect(result, headers, code, content, msg)

    def http_error_302(self, req, fp, code, msg, headers):
        self.infinite_loop_check(req)

        content = None
        try:
            content = fp.read()
        except Exception, msg:
            dbgMsg  = "there was a problem while retrieving "
            dbgMsg += "redirect response content (%s)" % msg
            logger.debug(dbgMsg)

        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
        return self.common_http_redirect(result, headers, code, content, msg)

    def infinite_loop_check(self, req):
        if hasattr(req, 'redirect_dict') and (req.redirect_dict.get(req.get_full_url(), 0) >= self.max_repeats or len(req.redirect_dict) >= self.max_redirections):
            errMsg  = "infinite redirect loop detected (%s). " % ", ".join(item for item in req.redirect_dict.keys())
            errMsg += "please check all provided parameters and/or provide missing ones."
            raise sqlmapConnectionException, errMsg
<commit_msg>minor cosmetics<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
"""

import urllib2

from lib.core.data import conf
from lib.core.data import logger
from lib.core.common import getUnicode
from lib.core.common import logHTTPTraffic
from lib.core.exception import sqlmapConnectionException
from lib.core.threads import getCurrentThreadData

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    # maximum number of redirections to any single URL
    # this is needed because of the state that cookies introduce
    max_repeats = 4

    # maximum total number of redirections (regardless of URL) before
    # assuming we're in a loop
    max_redirections = 10

    def common_http_redirect(self, result, headers, code, content, msg):
        threadData = getCurrentThreadData()
        threadData.lastRedirectMsg = (threadData.lastRequestUID, content)

        responseMsg     = "HTTP response "
        responseMsg += "[#%d] (%d %s):\n" % (threadData.lastRequestUID, code, getUnicode(msg))

        if headers:
            logHeaders = "\n".join(["%s: %s" % (key.capitalize() if isinstance(key, basestring) else key, getUnicode(value)) for (key, value) in headers.items()])
        else:
            logHeaders = ""

        if conf.verbose <= 5:
            responseMsg += getUnicode(logHeaders)
        elif conf.verbose > 5:
            responseMsg += "%s\n%s\n" % (logHeaders, content)

        logger.log(7, responseMsg)

        if "location" in headers:
            result.redurl = headers.getheaders("location")[0].split("?")[0]
        elif "uri" in headers:
            result.redurl = headers.getheaders("uri")[0].split("?")[0]

        if "set-cookie" in headers:
            result.setcookie = headers["set-cookie"].split("; path")[0]

        result.redcode = code

        return result

    def http_error_301(self, req, fp, code, msg, headers):
        self.infinite_loop_check(req)

        content = None
        try:
            content = fp.read()
        except Exception, msg:
            dbgMsg  = "there was a problem while retrieving "
            dbgMsg += "redirect response content (%s)" % msg
            logger.debug(dbgMsg)

        result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers)
        return self.common_http_redirect(result, headers, code, content, msg)

    def http_error_302(self, req, fp, code, msg, headers):
        self.infinite_loop_check(req)

        content = None
        try:
            content = fp.read()
        except Exception, msg:
            dbgMsg  = "there was a problem while retrieving "
            dbgMsg += "redirect response content (%s)" % msg
            logger.debug(dbgMsg)

        result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
        return self.common_http_redirect(result, headers, code, content, msg)

    def infinite_loop_check(self, req):
        if hasattr(req, 'redirect_dict') and (req.redirect_dict.get(req.get_full_url(), 0) >= self.max_repeats or len(req.redirect_dict) >= self.max_redirections):
            errMsg  = "infinite redirect loop detected (%s). " % ", ".join(item for item in req.redirect_dict.keys())
            errMsg += "please check all provided parameters and/or provide missing ones."
            raise sqlmapConnectionException, errMsg
<|endoftext|>


<commit_before>import re
import socket
import time

class IRCConnection(object):
    _sock = None
    _callbacks = None

    chanmsg_re = re.compile(':(?P<nick>.*?)!~\S+\s+?PRIVMSG\s+#(?P<channel>[-\w]+)\s+:(?P<message>[^\n\r]+)')
    privmsg_re = re.compile(':(?P<nick>.*?)!~\S+\s+?PRIVMSG\s+[^#][^:]+:(?P<message>[^\n\r]+)')

    def __init__(self, host, port, nick):
        self.host = host
        self.port = port
        self.nick = nick
        self._callbacks = {
            'PRIVMSG': [],
            'CHANMSG': [],
        }

    def connect(self):
        self._sock = socket.create_connection((self.host, self.port)).makefile()

    def disconnect(self):
        self.send('QUIT')
        self._sock.close()

    def send(self, data):
        self._sock.write('%s\r\n' % data)
        self._sock.flush()

    def authenticate(self):
        self.send('NICK %s' % self.nick)
        self.send('USER %s %s bla :%s' % (self.nick, self.host, self.nick))

    def join(self, channel):
        channel = channel.lstrip('#')
        self.send('JOIN #%s' % channel)

    def part(self, channel):
        channel = channel.lstrip('#')
        self.send('PART #%s' % channel)

    def register_callback(self, event, func):
        self._callbacks[event].append(func)

    def unregister_callback(self, event, func):
        self._callbacks[event].remove(func)

    def enter_event_loop(self):
        while not self._sock.closed:
            line = self._sock.readline()
            line = line.rstrip()
            if line.startswith('PING'):
                self.send('PONG %s' % line.split()[1])
            else:
                chan_match = self.chanmsg_re.match(line)
                if chan_match:
                    for event in self._callbacks['CHANMSG']:
                        event(**chan_match.groupdict())

                priv_match = self.privmsg_re.match(line)
                if priv_match:
                    for event in self._callbacks['PRIVMSG']:
                        event(**priv_match.groupdict())

class Dispatcher(object):
    def bind(self, conn):
        self.irc = conn

        self.irc.register_callback('CHANMSG', self.on_channel_message)
        self.irc.register_callback('PRIVMSG', self.on_private_message)

    def unbind(self):
        self.irc.unregister_callback('CHANMSG', self.on_channel_message)
        self.irc.unregister_callback('PRIVMSG', self.on_private_message)

    def get_patterns(self):
        """
        A tuple of regex -> callback where the argument signature of callback:

        def some_callback(sender, message, channel, is_ping, reply):
            do some shit
        """
        raise NotImplementedError

    def on_channel_message(self, nick, channel, message):
        is_ping = False
        if re.match('^%s[:,\s]' % self.irc.nick, message):
            message = re.sub('%s[^\s]*?\s' % self.irc.nick, '', message)
            is_ping = True
        for (pattern, callback) in self.get_patterns():
            if re.search(pattern, message):
                def curried_reply(message):
                    return self.send(message, channel=channel, nick=nick)
                result = callback(nick, message, channel, is_ping, curried_reply)
                if result:
                    curried_reply(result)

    def on_private_message(self, nick, message):
        self.on_channel_message(nick, None, message)

    def send(self, message, channel=None, nick=None):
        if channel:
            self.irc.send('PRIVMSG #%s :%s' % (channel.lstrip('#'), message))
        elif nick:
            self.irc.send('PRIVMSG %s :%s' % (nick, message))

class RateLimitedDispatcher(Dispatcher):
    rate_limit = (3, .75) # every 3 events, wait .75

    def __init__(self):
        self.num_sent = 0
        self.last_send = 0

    def send(self, *args, **kwargs):
        if self.num_sent == self.rate_limit[0]:
            self.num_sent = 0
            if time.time() - self.last_send <= self.rate_limit[1]:
                self.delay(self.rate_limit[1] - (time.time() - self.last_send))
            self.last_send = time.time()
        self.num_sent += 1
        return super(RateLimitedDispatcher, self).send(*args, **kwargs)

    def delay(self, timeout):
        time.sleep(timeout)

class IRCBot(object):
    def __init__(self, host, port, nick, channels, dispatchers):
        self.conn = IRCConnection(host, port, nick)
        self.conn.connect()
        self.conn.authenticate()

        for channel in channels:
            self.conn.join(channel)

        for dispatcher in dispatchers:
            dispatcher.bind(self.conn)

    def run_forever(self):
        try:
            self.conn.enter_event_loop()
        except KeyboardInterrupt:
            self.conn.disconnect()
<commit_msg>Simplifying the API, dispatchers folded into bots<commit_after>import re
import socket
import time

class IRCConnection(object):
    _sock = None
    _callbacks = None

    chanmsg_re = re.compile(':(?P<nick>.*?)!~\S+\s+?PRIVMSG\s+#(?P<channel>[-\w]+)\s+:(?P<message>[^\n\r]+)')
    privmsg_re = re.compile(':(?P<nick>.*?)!~\S+\s+?PRIVMSG\s+[^#][^:]+:(?P<message>[^\n\r]+)')

    def __init__(self, host, port, nick):
        self.host = host
        self.port = port
        self.nick = nick
        self._callbacks = {
            'PRIVMSG': [],
            'CHANMSG': [],
        }

    def connect(self):
        self._sock = socket.create_connection((self.host, self.port)).makefile()

    def disconnect(self):
        self.send('QUIT')
        self._sock.close()

    def send(self, data):
        self._sock.write('%s\r\n' % data)
        self._sock.flush()

    def authenticate(self):
        self.send('NICK %s' % self.nick)
        self.send('USER %s %s bla :%s' % (self.nick, self.host, self.nick))

    def join(self, channel):
        channel = channel.lstrip('#')
        self.send('JOIN #%s' % channel)

    def part(self, channel):
        channel = channel.lstrip('#')
        self.send('PART #%s' % channel)

    def register_callback(self, event, func):
        self._callbacks[event].append(func)

    def unregister_callback(self, event, func):
        self._callbacks[event].remove(func)

    def enter_event_loop(self):
        while not self._sock.closed:
            line = self._sock.readline()
            line = line.rstrip()
            if line.startswith('PING'):
                self.send('PONG %s' % line.split()[1])
            else:
                chan_match = self.chanmsg_re.match(line)
                if chan_match:
                    for event in self._callbacks['CHANMSG']:
                        event(**chan_match.groupdict())

                priv_match = self.privmsg_re.match(line)
                if priv_match:
                    for event in self._callbacks['PRIVMSG']:
                        event(**priv_match.groupdict())

class IRCBot(object):
    def __init__(self, host, port, nick, channels):
        self.conn = IRCConnection(host, port, nick)
        self.bind(self.conn)

        self.channels = channels

    def run_forever(self):
        self.conn.connect()
        self.conn.authenticate()

        for channel in self.channels:
            self.conn.join(channel)

        try:
            self.conn.enter_event_loop()
        except KeyboardInterrupt:
            self.conn.disconnect()

    def bind(self, conn):
        self.irc = conn

        self.irc.register_callback('CHANMSG', self.on_channel_message)
        self.irc.register_callback('PRIVMSG', self.on_private_message)

    def unbind(self):
        self.irc.unregister_callback('CHANMSG', self.on_channel_message)
        self.irc.unregister_callback('PRIVMSG', self.on_private_message)

    def get_patterns(self):
        """
        A tuple of regex -> callback where the argument signature of callback:

        def some_callback(sender, message, channel, is_ping, reply):
            do some shit
        """
        raise NotImplementedError

    def on_channel_message(self, nick, channel, message):
        is_ping = False
        if re.match('^%s[:,\s]' % self.irc.nick, message):
            message = re.sub('%s[^\s]*?\s' % self.irc.nick, '', message)
            is_ping = True
        for (pattern, callback) in self.get_patterns():
            if re.search(pattern, message):
                def curried_reply(message):
                    return self.send(message, channel=channel, nick=nick)
                result = callback(nick, message, channel, is_ping, curried_reply)
                if result:
                    curried_reply(result)

    def on_private_message(self, nick, message):
        self.on_channel_message(nick, None, message)

    def send(self, message, channel=None, nick=None):
        if channel:
            self.irc.send('PRIVMSG #%s :%s' % (channel.lstrip('#'), message))
        elif nick:
            self.irc.send('PRIVMSG %s :%s' % (nick, message))

class RateLimitedIRCBot(IRCBot):
    rate_limit = (3, .75) # every 3 events, wait .75

    def __init__(self, *args, **kwargs):
        self.num_sent = 0
        self.last_send = 0

        super(RateLimitedIRCBot, self).__init__(*args, **kwargs)

    def send(self, *args, **kwargs):
        if self.num_sent == self.rate_limit[0]:
            self.num_sent = 0
            if time.time() - self.last_send <= self.rate_limit[1]:
                self.delay(self.rate_limit[1] - (time.time() - self.last_send))
            self.last_send = time.time()
        self.num_sent += 1
        return super(RateLimitedIRCBot, self).send(*args, **kwargs)

    def delay(self, timeout):
        time.sleep(timeout)
<|endoftext|>


<commit_before>"""
Django settings for jamjar project.

Generated by 'django-admin startproject' using Django 1.8.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# Application definition
DEPENDENCY_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',
    'django.contrib.sessions',
    'django.contrib.messages',

    'django_extensions',

    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',

    'corsheaders',
    'django_nose'
]

PROJECT_APPS = [
    'jamjar.videos',
    'jamjar.users',
    'jamjar.authentication',
    'jamjar.tasks',
    'jamjar.concerts',
    'jamjar.venues'
]

INSTALLED_APPS = DEPENDENCY_APPS + PROJECT_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'jamjar.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'jamjar.wsgi.application'

AUTH_USER_MODEL = 'users.User'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

CORS_ORIGIN_ALLOW_ALL = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

VIDEOS_PATH = '/opt/code/masonjar/videos'

# for celery (job queue)
REDIS_QUEUE = 'redis://localhost:6379/0'

CELERY_IMPORTS = ("jamjar.tasks",)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
    '--with-coverage',
    '--cover-package={:}'.format(",".join(PROJECT_APPS)),
    '--cover-html'
]

JAMJAR_ENV = os.environ['JAMJAR_ENV']
<commit_msg>update packages to test<commit_after>"""
Django settings for jamjar project.

Generated by 'django-admin startproject' using Django 1.8.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# Application definition
DEPENDENCY_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',
    'django.contrib.sessions',
    'django.contrib.messages',

    'django_extensions',

    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',

    'corsheaders',
    'django_nose'
]

PROJECT_APPS = [
    'jamjar.videos',
    'jamjar.users',
    'jamjar.authentication',
    'jamjar.tasks',
    'jamjar.concerts',
    'jamjar.venues'
]

INSTALLED_APPS = DEPENDENCY_APPS + PROJECT_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'jamjar.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'jamjar.wsgi.application'

AUTH_USER_MODEL = 'users.User'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
    )
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

CORS_ORIGIN_ALLOW_ALL = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

VIDEOS_PATH = '/opt/code/masonjar/videos'

# for celery (job queue)
REDIS_QUEUE = 'redis://localhost:6379/0'

CELERY_IMPORTS = ("jamjar.tasks",)

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

NOSE_ARGS = [
    '--with-coverage',
    '--cover-package=.',
    '--cover-html'
]

JAMJAR_ENV = os.environ['JAMJAR_ENV']
<|endoftext|>


<commit_before># Import some necessary libraries.
import socket
import string
import commands
import time

# Some basic variables used to configure the bot
server = "irc.rizon.net"  # Server
channel = "#omgatestchannel"  # Channel
botnick = "cybits1"  # bot's nick

def ping():  # This is our first function! It will respond to server Pings.
    ircsock.send("PONG :ping\n")

def sendmsg(recipient, msg):  # This is the send message function, it simply sends messages to the channel.
    ircsock.send("PRIVMSG " + recipient + " :" + msg + "\n")

def joinchan(chan):  # This function is used to join channels.
    ircsock.send("JOIN " + chan + "\n")

ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))  # connect to the server using the port 6667
ircsock.send("USER " + botnick + " " + botnick + " " + botnick + " :some stuff\n")  # user authentication
ircsock.send("NICK " + botnick + "\n")  # here we actually assign the nick to the bot
joinchan(channel)  # Join the channel using the functions we previously defined

while 1:
    ircmsg = ircsock.recv(1024)  # receive data from the server
  # ircmsg = ircmsg.strip('\n\r')  # removing any unnecessary linebreaks.
    print(ircmsg)  # Here we print what's coming from the server

    # if ircmsg.find(botnick) in ircmsg and "PRIVMSG ") in ircmsg and "rizon") == -1:
    # # If we can find "cybits" it will call the function hello()
    #     hello(ircmsg.split(":")[1].split('!')[0])
    #     continue

    if " :.lit" in ircmsg and channel in ircmsg:  # If we can find ".lit" it will call the function sentence()
        sendmsg(channel, commands.sentence())
        continue

    if " :.feel" in ircmsg and channel in ircmsg:  # If we can find ".feel" it will call the function feel()
        array = commands.feel()
        user = ircmsg.split(":")[1].split('!')[0]
        feelguy = commands.breaklines(array[1])
        sendmsg(channel, array[0])
        for lines in range(0, len(feelguy)):
            sendmsg(user, feelguy[lines])
            time.sleep(1)
        continue

    if " :.interject" in ircmsg and channel in ircmsg:  # If we can find ".interject" it will call the function
        sendmsg(channel, commands.interjection())       # interjection()
        continue

    # lircmsg = string.lower(ircmsg)
    # if "linux" in ircmsg and "gnu" not in ircmsg and "linuz" not in ircmsg and "source" not in ircmsg \
    #         and "kernel" not in ircmsg and channel in ircmsg:  # mods are asleep, post interjects
    #     user = ircmsg.split(":")[1].split('!')[0]
    #     msg, msg1 = commands.autointerject()
    #     interjection = commands.breaklines(msg1)
    #     sendmsg(channel, msg)

    #for lines in range(0, len(interjection)):
    #     sendmsg(user, interjection[lines])
    #
    #     continue

    if " :.implying" in ircmsg and channel in ircmsg:  # If we can find ".implying" it will call the function implying()
        sendmsg(channel, commands.implying())
        continue

    if " :.memearrows" in ircmsg and channel in ircmsg:  # If we can find ".memearrows" it will call the function
        sendmsg(channel, commands.memearrows())          # memearrows()
        continue

    if " :.shitpost" in ircmsg and channel in ircmsg:  # If we can find ".shitpost" it will call the function
        sendmsg(channel, commands.shitpost())          # shitpost()
        continue

    if " :.SHITPOST" in ircmsg and channel in ircmsg:        # If we can find ".SHITPOST" it will call the function
        sendmsg(channel, string.upper(commands.shitpost()))  # shitpost(), but in caps
        continue

    if " :.int" in ircmsg and channel in ircmsg:  # If we can find ".int" it will call the function intensifies()
        sendmsg(channel, commands.intensifies(ircmsg.split(":")[2].split('!')[0]))
        continue

    if " :.cybhelp" in ircmsg and channel in ircmsg:  # If we can find ".cybhelp" it will call the function help()
        user = ircmsg.split(":")[1].split('!')[0]
        array = commands.halp(user)
        sendmsg(channel, array[0])
        sendmsg(user, array[1])
        continue
    if " :.git" in ircmsg and channel in ircmsg:
        sendmsg(channel, commands.git())
        continue

    if "PING :" in ircmsg:  # respond to pings
        ping()
<commit_msg>[FUNCTIONALITY INTENSIFIES]<commit_after># Import some necessary libraries.
import socket
import string
import commands
import time

# Some basic variables used to configure the bot
server = "irc.rizon.net"  # Server
channel = "#omgatestchannel"  # Channel
botnick = "cybits1"  # bot's nick

def ping():  # This is our first function! It will respond to server Pings.
    ircsock.send("PONG :ping\n")

def sendmsg(recipient, msg):  # This is the send message function, it simply sends messages to the channel.
    ircsock.send("PRIVMSG " + recipient + " :" + msg + "\n")

def joinchan(chan):  # This function is used to join channels.
    ircsock.send("JOIN " + chan + "\n")

ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ircsock.connect((server, 6667))  # connect to the server using the port 6667
ircsock.send("USER " + botnick + " " + botnick + " " + botnick + " :some stuff\n")  # user authentication
ircsock.send("NICK " + botnick + "\n")  # here we actually assign the nick to the bot
joinchan(channel)  # Join the channel using the functions we previously defined

while 1:
    ircmsg = ircsock.recv(1024)  # receive data from the server
  # ircmsg = ircmsg.strip('\n\r')  # removing any unnecessary linebreaks.
    print(ircmsg)  # Here we print what's coming from the server

    # if ircmsg.find(botnick) in ircmsg and "PRIVMSG ") in ircmsg and "rizon") == -1:
    # # If we can find "cybits" it will call the function hello()
    #     hello(ircmsg.split(":")[1].split('!')[0])
    #     continue

    if " :.lit" in ircmsg and channel in ircmsg:  # If we can find ".lit" it will call the function sentence()
        sendmsg(channel, commands.sentence())
        continue

    if " :.feel" in ircmsg and channel in ircmsg:  # If we can find ".feel" it will call the function feel()
        array = commands.feel()
        user = ircmsg.split(":")[1].split('!')[0]
        feelguy = commands.breaklines(array[1])
        sendmsg(channel, array[0])
        for lines in range(0, len(feelguy)):
            sendmsg(user, feelguy[lines])
            time.sleep(1)
        continue

    if " :.interject" in ircmsg and channel in ircmsg:  # If we can find ".interject" it will call the function
        sendmsg(channel, commands.interjection())       # interjection()
        continue

    # lircmsg = string.lower(ircmsg)
    # if "linux" in ircmsg and "gnu" not in ircmsg and "linuz" not in ircmsg and "source" not in ircmsg \
    #         and "kernel" not in ircmsg and channel in ircmsg:  # mods are asleep, post interjects
    #     user = ircmsg.split(":")[1].split('!')[0]
    #     msg, msg1 = commands.autointerject()
    #     interjection = commands.breaklines(msg1)
    #     sendmsg(channel, msg)

    #for lines in range(0, len(interjection)):
    #     sendmsg(user, interjection[lines])
    #
    #     continue

    if " :.implying" in ircmsg and channel in ircmsg:  # If we can find ".implying" it will call the function implying()
        sendmsg(channel, commands.implying())
        continue

    if " :.memearrows" in ircmsg and channel in ircmsg:  # If we can find ".memearrows" it will call the function
        sendmsg(channel, commands.memearrows())          # memearrows()
        continue

    if " :.shitpost" in ircmsg and channel in ircmsg:  # If we can find ".shitpost" it will call the function
        sendmsg(channel, commands.shitpost())          # shitpost()
        continue

    if " :.SHITPOST" in ircmsg and channel in ircmsg:        # If we can find ".SHITPOST" it will call the function
        sendmsg(channel, string.upper(commands.shitpost()))  # shitpost(), but in caps
        continue

    if " :.int" in ircmsg and channel in ircmsg:  # If we can find ".int" it will call the function intensifies()
        sendmsg(channel, commands.intensifies(ircmsg.split(":")[2].split('!')[0]))
        continue

    if " :.INT" in ircmsg and channel in ircmsg:
        sendmsg(channel, string.upper(commands.intensifies(ircmsg.split(":")[2].split('!')[0])))
        continue

    if " :.cybhelp" in ircmsg and channel in ircmsg:  # If we can find ".cybhelp" it will call the function help()
        user = ircmsg.split(":")[1].split('!')[0]
        array = commands.halp(user)
        sendmsg(channel, array[0])
        sendmsg(user, array[1])
        continue
    if " :.git" in ircmsg and channel in ircmsg:
        sendmsg(channel, commands.git())
        continue

    if "PING :" in ircmsg:  # respond to pings
        ping()
<|endoftext|>