from db import * 
from shared import *
import re
''' NickOP Services '''


def process_nickop_help(self, prefix, command, params):
    
    username = prefix[1:]
    hpath = "./help/nickop/"
    if len(params) == 2:
        hfile = open(hpath + "help.txt", 'r')
        lines = hfile.readlines()
        for line in lines:
            if len(line) > 0:
                self.send_msg_as("nickop","NOTICE", username, line.strip()) 
    if len(params) > 2:
        
        helpfiles = {
            "identify": "identify.txt",
            "register": "register.txt",
            "ghost": "ghost.txt"
        }
        
        topic = params[2].lower().strip()
        valid_file = False
        try:
            hfilesz = hpath + helpfiles[topic]
            valid_file = True
        except:
            valid_file = False
            print("no help file")
         
        if valid_file:
            hfile = open(hfilesz, 'r')
            lines = hfile.readlines()
            for line in lines:
                if len(line.strip()) != 0:
                    self.send_msg_as("nickop","NOTICE", username, line.strip())
        else:
            self.send_msg_as("nickop","NOTICE", username, f"ERROR: No valid help topic for {topic} found")

def process_nickop_ghost(self, prefix, command, params):
    if params[1].lower() == ':ghost' and len(params) == 4:
         
        conn = create_connection("users.db")
        with conn:
            nick = prefix[1:]
            username = params[2]
             
            results = check_nickname_exists(conn, username)
            
            if results == True:
                list_nicks(self)
                password = params[3].rstrip("\r\n")
                print("checking username: {}\n".format(username))
                print("checking password: {}\n".format(password))
                if username and password:
                    regstatus = check_user_password(conn, username, password)
                    if regstatus:
                        
                        nickname = uid_to_nick(self, nick)
                    
                        message = f"GHOST Killed by {nickname}"
                        sendserv = ":00000000B KILL {} :{}\n".format(username, message)
                        print(sendserv)
                        self.sockssl.send(sendserv.encode('ascii')) 
                        
                        self.send_msg_as("nickop", "NOTICE", nick, f"{username} has been KILLED (GHOSTED).\n")

                    else:
                        self.send_msg_as("nickop", "NOTICE", nick, "ERROR: Invalid password specified.\n")
            else:
                self.send_msg_as("nickop", "NOTICE", nick, "ERROR: {} is not a registered nickname\n".format(username))

    elif params[1].lower() == ':ghost' or params[1].lower() == ':ghost\r\n' and len(params) < 3:
        username = uid_to_nick(self, prefix[1:])
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Not enough arguments. Please use /msg NickOP ghost <username> <password>\n")      

def process_nickop_identify(self, prefix, command, params):
    if params[1].lower() == ':identify' and len(params) >= 3:
    
        conn = create_connection("users.db")
        with conn:
         
            username = uid_to_nick(self, prefix[1:])
            
            results = check_nickname_exists(conn, username)
            
            if results == True:
                list_nicks(self)
                password = params[2].rstrip("\<PASSWORD>")
                print("Checking username: {}".format(username))
                print("Checking password: {}".format(password))
                if username and password:
                    regstatus = update_user_identified(conn, username, password)
                    if regstatus:
                        data = ":{} SVS2MODE {} +r\n".format("00000000B",prefix[1:])
                        print(data)
                        self.sockssl.send(data.encode('ascii'))
                        
                        self.send_msg_as("nickop", "NOTICE", username, "You have been successfully identified.\n")

                    else:
                        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Invalid password specified.\n")
            else:
                self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is not a registered nickname\n".format(username))

    elif params[1].lower() == ':identify' or params[1].lower() == ':identify\r\n' and len(params) < 3:
        username = uid_to_nick(self, prefix[1:])
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Not enough arguments. Please use /msg NickOP identify <password>\n")

def process_nickop_register(self, prefix, command, params):
    if params[1].lower() == ':register' and len(params) >= 4:
      
        conn = create_connection("users.db")
        with conn:
            username = uid_to_nick(self, prefix[1:])
            if username:
                results = check_nickname_exists(conn, username)
                if results == False:
                    register_nickname(username, params[2], params[3].strip(), False)
                    
                    self.send_msg_as("nickop", "NOTICE", username, "Successfully registered {}.\n".format(username))
                else:
                    self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is already a registered nickname.\n".format(username))
    
    elif params[1].lower() == ':register\r\n' or params[1].lower() == ':register' and len(params) < 4:
        username = uid_to_nick(self, prefix[1:])
        if username is not None:
            send_notice_as_nickop(self, username, "ERROR: SYNTAX for register: /msg NickOP register <password> <emailaddress>")

def process_nickop_info(self, prefix, command, params):

    if params[1].lower() == ':info' and params[2]:
           
        conn = create_connection("users.db")
        with conn:
            results = check_nickname_exists(conn, params[2].strip())
            if results == True:
                username = uid_to_nick(self, prefix[1:])
                if username:
                    self.send_msg_as("nickop", "NOTICE", username, "The username {} has been registered.\n".format(params[2].strip()))
                    
            else:
                username = uid_to_nick(self, prefix[1:])
                if username:
                    self.send_msg_as("nickop", "NOTICE", username, "The username {} has NOT been registered yet.\n".format(params[2].strip()))

def process_nickop_set(self, prefix, command, params):
 
    username = uid_to_nick(self, prefix[1:])
    if len(params) == 4:
     
        if params[2] == 'password':
           
            conn = create_connection("users.db")
            with conn:
              
                results = check_nickname_exists(conn, username)
                
                if results == True:
                 
                    if check_nickname_identified(conn, username) == True:
                      
                        password = params[3].strip()
                   
                        set_nickname_password(conn, username, password)
                       
                        self.send_msg_as("nickop", "NOTICE", username, "Your password has been updated.\n")
                        
                        
                    else:
                        self.send_msg_as("nickop", "NOTICE", username, "ERROR: You have not identified to services.\n")
                      
                else:
                    self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is not a registered nickname\n".format(username))
        else:
            self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for set is: password <new_password>\n" )
            self.send_msg_as("nickop", "NOTICE", username, "For example. /msg NickOP set password P$<PASSWORD>!\n" )
    
    else:
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for set is: password <new_password>\n" )
        self.send_msg_as("nickop", "NOTICE", username, "For example. /msg NickOP set password P$<PASSWORD>!\n" )

def process_nickop_drop(self, prefix, command, params):
    if len(params) >= 3:
    
        conn = create_connection("users.db")
        with conn:
         
            username = uid_to_nick(self, prefix[1:])
            
            results = check_nickname_exists(conn, username)
            
            if results == True:
                
                password = params[2].rstrip("\r\n")
                print("Checking username: {}".format(username))
                print("Checking password: {}".format(password))
                if username and password:
                    regstatus = update_user_identified(conn, username, password)
 
                    if regstatus:
                        drop_user(conn, username )
                        conn = create_connection("channels.db")
                        with conn:
                            rows = return_channels_owned(conn, username)
                            for row in rows:
                                
                                chan = row[0]
                                drop_channel(conn, chan )
                                sendserv = ":{} PART {}\n".format("00000000A",chan)
                                print(sendserv)
                                self.sockssl.send(sendserv.encode('ascii'))
                            delete_user_from_all_chan_access(conn, username)
                            
                            print("dropped all channels owned by {}".format(username))
                            self.send_msg_as("nickop", "NOTICE", username, "The username {} has been dropped.\n".format(username))
                    else:
                        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Invalid password specified\n")
                else:
                    self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for drop: <password>\n")
            else:
                self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is not a registered nickname\n".format(username))
    else:
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for drop: <password>\n")
                        
def process_nickop_commands(self, prefix, command, params):

    if len(params) >= 2:
        target = params[0]
        cmdp = params[1].strip()

    if command == 'PRIVMSG' and params[0] == '00000000B' and self.spawned_nickop == True:
       
        try:
            lenp = len(cmdp)-1
            cmd = cmdp[-lenp:]
             
            self.dispatchnickoppriv[cmd].__call__(self, prefix, command, params)
        except:
            return
            
           

    if command == 'PRIVMSG' and params[1] == ':\x01VERSION\x01\r\n':
        nick = uid_to_nick(self, prefix[1:])
        self.send_msg_as(params[0], "NOTICE", nick, "\x01VERSION jacks unreal ircd services.\x01\n")
             
from db import * 
from shared import *
import re
''' NickOP Services '''


def process_nickop_help(self, prefix, command, params):
    
    username = prefix[1:]
    hpath = "./help/nickop/"
    if len(params) == 2:
        hfile = open(hpath + "help.txt", 'r')
        lines = hfile.readlines()
        for line in lines:
            if len(line) > 0:
                self.send_msg_as("nickop","NOTICE", username, line.strip()) 
    if len(params) > 2:
        
        helpfiles = {
            "identify": "identify.txt",
            "register": "register.txt",
            "ghost": "ghost.txt"
        }
        
        topic = params[2].lower().strip()
        valid_file = False
        try:
            hfilesz = hpath + helpfiles[topic]
            valid_file = True
        except:
            valid_file = False
            print("no help file")
         
        if valid_file:
            hfile = open(hfilesz, 'r')
            lines = hfile.readlines()
            for line in lines:
                if len(line.strip()) != 0:
                    self.send_msg_as("nickop","NOTICE", username, line.strip())
        else:
            self.send_msg_as("nickop","NOTICE", username, f"ERROR: No valid help topic for {topic} found")

def process_nickop_ghost(self, prefix, command, params):
    if params[1].lower() == ':ghost' and len(params) == 4:
         
        conn = create_connection("users.db")
        with conn:
            nick = prefix[1:]
            username = params[2]
             
            results = check_nickname_exists(conn, username)
            
            if results == True:
                list_nicks(self)
                password = params[3].rstrip("\r\n")
                print("checking username: {}\n".format(username))
                print("checking password: {}\n".format(password))
                if username and password:
                    regstatus = check_user_password(conn, username, password)
                    if regstatus:
                        
                        nickname = uid_to_nick(self, nick)
                    
                        message = f"GHOST Killed by {nickname}"
                        sendserv = ":00000000B KILL {} :{}\n".format(username, message)
                        print(sendserv)
                        self.sockssl.send(sendserv.encode('ascii')) 
                        
                        self.send_msg_as("nickop", "NOTICE", nick, f"{username} has been KILLED (GHOSTED).\n")

                    else:
                        self.send_msg_as("nickop", "NOTICE", nick, "ERROR: Invalid password specified.\n")
            else:
                self.send_msg_as("nickop", "NOTICE", nick, "ERROR: {} is not a registered nickname\n".format(username))

    elif params[1].lower() == ':ghost' or params[1].lower() == ':ghost\r\n' and len(params) < 3:
        username = uid_to_nick(self, prefix[1:])
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Not enough arguments. Please use /msg NickOP ghost <username> <password>\n")      

def process_nickop_identify(self, prefix, command, params):
    if params[1].lower() == ':identify' and len(params) >= 3:
    
        conn = create_connection("users.db")
        with conn:
         
            username = uid_to_nick(self, prefix[1:])
            
            results = check_nickname_exists(conn, username)
            
            if results == True:
                list_nicks(self)
                password = params[2].rstrip("\<PASSWORD>")
                print("Checking username: {}".format(username))
                print("Checking password: {}".format(password))
                if username and password:
                    regstatus = update_user_identified(conn, username, password)
                    if regstatus:
                        data = ":{} SVS2MODE {} +r\n".format("00000000B",prefix[1:])
                        print(data)
                        self.sockssl.send(data.encode('ascii'))
                        
                        self.send_msg_as("nickop", "NOTICE", username, "You have been successfully identified.\n")

                    else:
                        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Invalid password specified.\n")
            else:
                self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is not a registered nickname\n".format(username))

    elif params[1].lower() == ':identify' or params[1].lower() == ':identify\r\n' and len(params) < 3:
        username = uid_to_nick(self, prefix[1:])
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Not enough arguments. Please use /msg NickOP identify <password>\n")

def process_nickop_register(self, prefix, command, params):
    if params[1].lower() == ':register' and len(params) >= 4:
      
        conn = create_connection("users.db")
        with conn:
            username = uid_to_nick(self, prefix[1:])
            if username:
                results = check_nickname_exists(conn, username)
                if results == False:
                    register_nickname(username, params[2], params[3].strip(), False)
                    
                    self.send_msg_as("nickop", "NOTICE", username, "Successfully registered {}.\n".format(username))
                else:
                    self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is already a registered nickname.\n".format(username))
    
    elif params[1].lower() == ':register\r\n' or params[1].lower() == ':register' and len(params) < 4:
        username = uid_to_nick(self, prefix[1:])
        if username is not None:
            send_notice_as_nickop(self, username, "ERROR: SYNTAX for register: /msg NickOP register <password> <emailaddress>")

def process_nickop_info(self, prefix, command, params):

    if params[1].lower() == ':info' and params[2]:
           
        conn = create_connection("users.db")
        with conn:
            results = check_nickname_exists(conn, params[2].strip())
            if results == True:
                username = uid_to_nick(self, prefix[1:])
                if username:
                    self.send_msg_as("nickop", "NOTICE", username, "The username {} has been registered.\n".format(params[2].strip()))
                    
            else:
                username = uid_to_nick(self, prefix[1:])
                if username:
                    self.send_msg_as("nickop", "NOTICE", username, "The username {} has NOT been registered yet.\n".format(params[2].strip()))

def process_nickop_set(self, prefix, command, params):
 
    username = uid_to_nick(self, prefix[1:])
    if len(params) == 4:
     
        if params[2] == 'password':
           
            conn = create_connection("users.db")
            with conn:
              
                results = check_nickname_exists(conn, username)
                
                if results == True:
                 
                    if check_nickname_identified(conn, username) == True:
                      
                        password = params[3].strip()
                   
                        set_nickname_password(conn, username, password)
                       
                        self.send_msg_as("nickop", "NOTICE", username, "Your password has been updated.\n")
                        
                        
                    else:
                        self.send_msg_as("nickop", "NOTICE", username, "ERROR: You have not identified to services.\n")
                      
                else:
                    self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is not a registered nickname\n".format(username))
        else:
            self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for set is: password <new_password>\n" )
            self.send_msg_as("nickop", "NOTICE", username, "For example. /msg NickOP set password P$<PASSWORD>!\n" )
    
    else:
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for set is: password <new_password>\n" )
        self.send_msg_as("nickop", "NOTICE", username, "For example. /msg NickOP set password P$<PASSWORD>!\n" )

def process_nickop_drop(self, prefix, command, params):
    if len(params) >= 3:
    
        conn = create_connection("users.db")
        with conn:
         
            username = uid_to_nick(self, prefix[1:])
            
            results = check_nickname_exists(conn, username)
            
            if results == True:
                
                password = params[2].rstrip("\r\n")
                print("Checking username: {}".format(username))
                print("Checking password: {}".format(password))
                if username and password:
                    regstatus = update_user_identified(conn, username, password)
 
                    if regstatus:
                        drop_user(conn, username )
                        conn = create_connection("channels.db")
                        with conn:
                            rows = return_channels_owned(conn, username)
                            for row in rows:
                                
                                chan = row[0]
                                drop_channel(conn, chan )
                                sendserv = ":{} PART {}\n".format("00000000A",chan)
                                print(sendserv)
                                self.sockssl.send(sendserv.encode('ascii'))
                            delete_user_from_all_chan_access(conn, username)
                            
                            print("dropped all channels owned by {}".format(username))
                            self.send_msg_as("nickop", "NOTICE", username, "The username {} has been dropped.\n".format(username))
                    else:
                        self.send_msg_as("nickop", "NOTICE", username, "ERROR: Invalid password specified\n")
                else:
                    self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for drop: <password>\n")
            else:
                self.send_msg_as("nickop", "NOTICE", username, "ERROR: {} is not a registered nickname\n".format(username))
    else:
        self.send_msg_as("nickop", "NOTICE", username, "ERROR: SYNTAX for drop: <password>\n")
                        
def process_nickop_commands(self, prefix, command, params):

    if len(params) >= 2:
        target = params[0]
        cmdp = params[1].strip()

    if command == 'PRIVMSG' and params[0] == '00000000B' and self.spawned_nickop == True:
       
        try:
            lenp = len(cmdp)-1
            cmd = cmdp[-lenp:]
             
            self.dispatchnickoppriv[cmd].__call__(self, prefix, command, params)
        except:
            return
            
           

    if command == 'PRIVMSG' and params[1] == ':\x01VERSION\x01\r\n':
        nick = uid_to_nick(self, prefix[1:])
        self.send_msg_as(params[0], "NOTICE", nick, "\x01VERSION jacks unreal ircd services.\x01\n")
             
NickOP Services