###
# Copyright (c) 2012-2013, spline
# All rights reserved.
#

###
# my libs
try:  # use cElementTree but revert back to regular ET
    import xml.etree.cElementTree as ElementTree
except ImportError:
    import xml.etree.ElementTree as ElementTree
# supybot libs
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
from supybot.i18n import PluginInternationalization, internationalizeDocstring

_ = PluginInternationalization('UrbanDictionary')

class Bible(callbacks.Plugin):
    threaded = True

    # http://www.4-14.org.uk/xml-bible-web-service-api
    def bible(self, irc, msg, args, optlist, optpassage):
        """[--version akjv|asv|douayrheims|kjv|web|ylt] <passage>
        Returns text from specified Bible transation matching search parameters.
        Ex: Job 3:14, Acts 3:17-4:2, Amos 7; Psa 119:4-16, Acts 15:1-5, 10, 15
        By default, will consult KJV version. Use --version to utilize a different translation.
        """

        # set default version
        version = 'kjv'
        # check optlist (getopts)
        validVersions = {'akjv':'American King James Version',
                         'asv':'American Standard Version',
                         'douayrheims':'Douay-Rheims',
                         'kjv':'King James Version',
                         'web':'World English Bible',
                         'ylt':'Youngs Literal Translation' }

        if optlist:
            for (key, value) in optlist:
                if key == 'version':
                    if value.lower() not in validVersions:
                        irc.reply("ERROR: Invalid version. Version must be one of: {0}".format(validVersions.keys()))
                        return
                    else:
                        version = value.lower()
        # build and fetch url.
        url = 'http://api.preachingcentral.com/bible.php?passage='
        url += utils.web.urlquote(optpassage) + '&version=%s' % version
        try:
            u = utils.web.getUrl(url)
        except utils.web.Error as e:
            self.log.error("ERROR opening {0} message: {1}".format(url, e))
            irc.reply("ERROR: could not open {0} message: {1}".format(url, e))
            return
        # now try to process XML.
        try:
            document = ElementTree.fromstring(u)
        except Exception, e:
            irc.reply("ERROR: Failed to parse XML. Check logs.")
            self.log.error("ERROR: {0} Could not parse Bible XML. {1}".format(e, u))
            return
        # first check for when syntax is broke. They don't give a proper error message.
        # Error return: ParseError: mismatched tag: line 1909, column 2
        if document.find('range/result') is None or document.tag != 'bible':
            irc.reply("ERROR: Failed to load/parse the verse or page. Check your syntax.")
            return
        # if you do give an invalid passage, it will spit out an error in XML.
        if document.find('range/error') is not None:
            irc.reply("ERROR: '{0}' when searching for: {1}".format(document.find('range/error').text, optpassage))
            return
        # prepare output.
        for node in document.findall('range/item'):
            bookname = node.find('bookname')
            chapter = node.find('chapter')
            verse = node.find('verse')
            text = node.find('text')
            irc.reply("[{0}] {1} {2}:{3} :: {4}".format(ircutils.mircColor(version.upper(), 'red'), ircutils.bold(bookname.text),\
                ircutils.bold(chapter.text), ircutils.bold(verse.text), text.text))

    bible = wrap(bible, [getopts({'version':('text')}), ('text')])

Class = Bible

@@ -3069,20 +3069,8 @@
 or')
- is not None
 :%0A  
@@ -3227,16 +3227,60 @@
  output.
+ limit to 5 lines before stopping for flood.
 %0A       
@@ -3286,17 +3286,18 @@
   fo
-r node in
+unditems =
  doc
@@ -3323,16 +3323,95 @@
 e/item')
+%0A        # now iterate over these.%0A        for i, node in enumerate(founditems)
 :%0A      
@@ -3568,16 +3568,42 @@
 'text')%0A
+            if i %3C 5:%0A    
         
@@ -3697,123 +3697,526 @@
 d'),
- ircutils.bold(bookname.text),%5C%0A                ircutils.bold(chapter.text), ircutils.bold(verse.text), text.text))
+%5C%0A                                                        ircutils.bold(bookname.text),%5C%0A                                                        ircutils.bold(chapter.text),%5C%0A                                                        ircutils.bold(verse.text),%5C%0A                                                        text.text))%0A            else:%0A                irc.reply(%22ERROR: I have more than 5 matching passages from your query (%7B0%7D left). Please be more specific.%22.format(len(founditems)-5))%0A                break
 %0A%0A  

Add in flood protection and clean up some things.