From Fedora Project Wiki
"""
Copyright (c) 2013, Patrick Uiterwijk <puiterwijk@fedoraproject.org>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""


notification_ignore = 'You are on my ignore-list. I will not be notified of your private messages!'
notification_welcome_to_pm = 'Okay, you are free to chat now, please say hello'
notification_require_confirmation = 'Are you sure you want to PM me? Say "Yes, I am sure" to verify. Please note that I will not receive your messages until you do so'

def is_confirmation(message):
    """This function should return whether or not this message is a confirmation
    
    @var message is the message received as a string
    """
    return 'yes' in message



############################################################
### You do not need to modify below this line ##############
################ Author: Patrick Uiterwijk #################
############################################################
import xchat

__module_name__ = 'verify-pm'
__module_version__ = '1.0'
__module_description__ = 'Make anyone verify they really want PM after warning them'

def set_pref(key, value):
    xchat.set_pluginpref('verify-pm-%s' % key, value)

def get_pref(key, default):
    got = xchat.get_pluginpref('verify-pm-%s' % key)
    if got:
        return got.split(',')
    return default

allowed = get_pref('always-accept', [])
ignored = get_pref('always-ignore', [])
sent = {}

def get_nickname(snd):
    return snd[1:].split('!')[0]   

def on_privmsg(word, word_eol, userdata):
    #xchat.prnt('recpm. word: %s' % word)
    sender = get_nickname(word[0])
    if sender in ignored:
        xchat.command("quote notice %s :%s" % (sender, notification_ignore))
        return xchat.EAT_ALL
    if sender in allowed:
        return xchat.EAT_NONE
    if is_confirmation(word_eol[3][2:]):
        sent[sender].append(word_eol[3][2:])
        currentctx = xchat.get_context()
        allowed.append(sender)
        xchat.command("query %s" % sender)
        privmsgctx = xchat.find_context(channel=sender)
        privmsgctx.command("quote notice %s :%s" % (sender, notification_welcome_to_pm))
        privmsgctx.prnt('This user sent you private messages. He/she sent this until now:')
        for msg in sent[sender]:
            privmsgctx.prnt(msg)
        currentctx.set()
        del sent[sender]
        return xchat.EAT_ALL
    if not sender in sent.keys():
        sent[sender] = []
    sent[sender].append(word_eol[3][2:])    # The 2: to strip :+
    xchat.command("quote notice %s :%s" % (sender, notification_require_confirmation))
    return xchat.EAT_ALL
xchat.hook_server('PRIVMSG', on_privmsg)

def command_accept(word, word_eol, userdata):
    if len(word) == 1:
        xchat.get_context().prnt('The current always-accept list: %s' % get_pref('always-accept', []))
        return xchat.EAT_ALL
    other = word[1]
    currentlist = get_pref('always-accept', [])
    if other in get_pref('always-ignore', []) and not (len(word) > 2 and word[2] == 'delete'):
        xchat.get_context().prnt('%s is in the ignore list. That entry will override the accept list' % other)
    if other in currentlist and not (len(word) > 2 and word[2] == 'delete'):
        xchat.get_context().prnt('%s is already in your accept list' % other)
        return xchat.EAT_ALL
    if len(word) > 2 and word[2] == 'delete':
        currentlist.remove(other)
        allowed.remove(other)
        xchat.get_context().prnt('%s has been deleted from your accept list' % other)
    else:
        currentlist.append(other)
        allowed.append(other)
        xchat.get_context().prnt('%s has been added to your accept list' % other)
    set_pref('always-accept', ','.join(currentlist))
    return xchat.EAT_ALL
xchat.hook_command('pm-accept', command_accept)

def command_ignore(word, word_eol, userdata):
    if len(word) == 1:
        xchat.get_context().prnt('The current always-ignore list: %s' % get_pref('always-ignore', []))
        return xchat.EAT_ALL
    other = word[1]
    currentlist = get_pref('always-ignore', [])
    if other in get_pref('always-accept', []) and not (len(word) > 2 and word[2] == 'delete'):
        xchat.get_context().prnt('%s is in the always-accept list. Ignore list will override the accept list' % other)
    if other in currentlist and not (len(word) > 2 and word[2] == 'delete'):
        xchat.get_cntext().prnt('%s is already on your ignore list' % other)
        return xchat.EAT_ALL 
    if len(word) > 2 and word[2] == 'delete':
        currentlist.remove(other)
        ignored.remove(other)
        xchat.get_context().prnt('%s has been deleted from your ignore list' % other)
    else:
        currentlist.append(other)
        ignored.append(other)
        xchat.get_context().prnt('%s has been added to your ignore list' % other)
    set_pref('always-ignore', ','.join(currentlist))
    return xchat.EAT_ALL
xchat.hook_command('pm-ignore', command_ignore)