# Copyright (c) 2013-2014 Molly White
#
# 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 plugins.util import command, get_url
from bs4 import BeautifulSoup


@command("b", "qdb")
def bash(m):
    """Return a quote from bash.org."""
    if len(m.line) > 1 and m.line[1].isnumeric():
        bash_specific(m, m.line[1])
    else:
        bash_rand(m)


def bash_rand(m):
    """Get a random quote from bash.org"""
    resp = get_url(m, "http://bash.org?random")
    if resp:
        soup = BeautifulSoup(resp)
        raw = soup.find(class_="qt")
        meta = soup.find(class_="quote")
        while True:
            if not raw:
                bash_rand(m)
                return
            lines = raw.get_text().splitlines()
            if len(lines) < 5:
                break
            raw = raw.find_next(class_="qt")
            meta = soup.find_next(class_="quote")
        format_quote(m, lines, meta)
    else:
        m.bot.private_message(m.location, "Could not find bash quote.")


def bash_specific(m, number):
    """Get a specific quote from bash.org."""
    resp = get_url(m, "http://bash.org?" + number)
    if resp:
        soup = BeautifulSoup(resp)
        raw = soup.find(class_="qt")
        meta = soup.find(class_="quote")
        lines = raw.get_text().splitlines()
        if len(lines) and not m.is_pm:
            m.bot.private_message(m.location, "This quote is too long to post publicly, "
                                              "but you can view it at http://bash.org?"
                                              "{}.".format(number))
        else:
            format_quote(m, lines, meta, number)
    else:
        m.bot.private_message(m.location, "Could not find bash quote.")


def format_quote(m, raw, meta, number=None):
    """Format the quote with some metadata."""
    score = meta.font.string
    score_str = "\x0304{}\x03".format(score) if "-" in score else "\x0303{}\x03".format(score)
    url = "http://bash.org?" + (number if number else meta.b.string.strip("#"))
    meta_str = "--- {} ({}) {} ".format(meta.b.string, score_str, url)
    m.bot.private_message(m.location, meta_str.ljust(80, '-'))
    for line in raw:
        m.bot.private_message(m.location, line)
    m.bot.private_message(m.location, "-" * 80)

@@ -1459,33 +1459,16 @@
 andom%22)%0A
-    if resp:%0A    
     soup
@@ -1482,36 +1482,32 @@
 tifulSoup(resp)%0A
-    
     raw = soup.f
@@ -1515,32 +1515,44 @@
 nd(class_=%22qt%22)%0A
+    if raw:%0A
         meta = s
@@ -1747,16 +1747,17 @@
 lines) %3C
+=
  5:%0A    
@@ -2121,25 +2121,8 @@
 er)%0A
-    if resp:%0A    
     
@@ -2148,20 +2148,16 @@
 p(resp)%0A
-    
     raw 
@@ -2173,32 +2173,44 @@
 nd(class_=%22qt%22)%0A
+    if raw:%0A
         meta = s
@@ -2270,32 +2270,32 @@
 ().splitlines()%0A
-
         if len(l
@@ -2299,16 +2299,20 @@
 n(lines)
+ %3E 5
  and not

Move 404 check to correct place