#!/usr/bin/env python3

###############################################################################
# Module Imports
###############################################################################

import sopel
import jarvis

###############################################################################


def setup(bot):
    jarvis.notes.init()
    sopel.bot.Sopel.SopelWrapper.send = send


def send(bot, text, private=False, force=False):
    tr = bot._trigger
    if tr.sender in bot.config.core.channels:
        text = '{}: {}'.format(tr.nick, text)
        jarvis.notes.log_message(bot.config.core.nick, tr.sender, text)

    def say(text, recipient):
        lines = [text[i:i + 400] for i in range(0, len(text), 400)]
        try:
            bot.sending.acquire()
            for line in lines:
                bot.write(('PRIVMSG', recipient), line)
        finally:
            bot.sending.release()

    (say if force else bot.say)(text, tr.nick if private else tr.sender)


###############################################################################


@sopel.module.commands('tell')
def tell(bot, tr):
    """
    Send a message to the user.

    The message will be delivered the next time the user is active in any
    of the channels where the bot is present.
    """
    bot.send(jarvis.notes.store_tell(tr.nick, *tr.group(2).split(maxsplit=1)))


@sopel.module.rule('.*')
@sopel.module.priority('low')
def chat_activity(bot, tr):
    jarvis.notes.log_message(tr.nick, tr.sender, tr.group(0))
    for t in jarvis.notes.get_tells(tr.nick):
        bot.send(t, private=True, force=True)


@sopel.module.commands('showtells', 'showt', 'st')
def showtells(bot, tr):
    """
    Show messages sent to you by other users.

    IRC notice is issued in the channel stating the number of queued messages.
    This notice is visible only to the recipient of the messages.

    The tells themselves are delivered via irc private messages.
    """
    tells = list(jarvis.notes.get_tells(tr.nick))
    if not tells:
        bot.notice(jarvis.lexicon.no_tells(), tr.nick)
    for t in tells:
        bot.send(t, private=True, force=True)


@sopel.module.commands('seen')
def seen(bot, tr):
    """
    Check when the user was last seen.

    Results are channel specific. You must issue the command in the same
    channel where you want to check for the user.
    """
    bot.send(jarvis.notes.user_last_seen(tr.group(2), tr.sender))


@sopel.module.commands('quote', 'q')
def quote(bot, tr):
    channels = bot.config.scp.quotes.split(',')
    if tr.sender not in [i.strip() for i in channels]:
        return
    bot.send(jarvis.notes.quote(tr.group(2), tr.sender))


@sopel.module.commands('rem')
def rem(bot, tr):
    channels = bot.config.scp.quotes.split(',')
    if tr.sender not in [i.strip() for i in channels]:
        return
    bot.send(jarvis.notes.add_rem(tr.group(2), tr.sender))


@sopel.module.rule(r'^\?([\w\d-]+)$')
def get_rem(bot, tr):
    channels = bot.config.scp.quotes.split(',')
    if tr.sender not in [i.strip() for i in channels]:
        return
    bot.send(jarvis.notes.get_rem(tr.group(1), tr.sender))

@@ -2449,120 +2449,180 @@
 )%0A%0A%0A
-@sopel.module.commands('quote', 'q')%0Adef quote(bot, tr):%0A    channels = bot.config.scp.quotes.split(',')
+def channel_quotes_enabled(bot, tr):%0A    sssc = bot.channels.get(bot.config.scp.sssc)%0A    if tr.sender in bot.config.scp.quotes:%0A        return tr.sender
 %0A    
+el
 if 
+sssc and 
 tr.s
@@ -2631,61 +2631,198 @@
 der 
-not in %5Bi.strip() for i in channels%5D:%0A        return%0A
+in sssc.users:%0A        return sssc.name%0A    else:%0A        return%0A%0A%0A@sopel.module.commands('quote', 'q')%0Adef quote(bot, tr):%0A    channel = channel_quotes_enabled(bot, tr)%0A    if channel:%0A    
     
@@ -2858,33 +2858,31 @@
 r.group(2), 
-tr.sender
+channel
 ))%0A%0A%0A@sopel.
@@ -2937,96 +2937,57 @@
 nnel
-s
  = 
-bot.config.scp.quotes.split(',')%0A    if tr.sender not in %5Bi.strip() for i in
+channel_quotes_enabled(bot, tr)%0A    if
  channel
 s%5D:%0A
@@ -2982,35 +2982,22 @@
  channel
-s%5D
 :%0A    
-    return%0A
     bot.
@@ -3031,33 +3031,31 @@
 r.group(2), 
-tr.sender
+channel
 ))%0A%0A%0A@sopel.
@@ -3122,96 +3122,57 @@
 nnel
-s
  = 
-bot.config.scp.quotes.split(',')%0A    if tr.sender not in %5Bi.strip() for i in
+channel_quotes_enabled(bot, tr)%0A    if
  channel
 s%5D:%0A
@@ -3171,27 +3171,14 @@
 nnel
-s%5D
 :%0A    
-    return%0A
     
@@ -3220,20 +3220,18 @@
 oup(1), 
-tr.sender
+channel
 ))%0A

allow quotes via pm-s