<commit_before>//
//  IRCHandler.swift
//  GamingStreamsTVApp
//
//  Created by <NAME> on 2015-09-19.
//  Copyright © 2015 Rivus Media Inc. All rights reserved.
//

import Foundation

protocol IRCHandlerDelegate
{
    func respond(target: IRCHandlerBase , prefix: String? , destination: String? , message: String?, metadata: String?)
}

class IRCHandlerBase: NSObject, NSStreamDelegate
{
    var host: String
    var port: Int
    var useSSL: Bool
    var genericCredentials: ( String? , [String] , String , Bool , String )?
    
    var IRCBuffer = [UInt8](count: 65536, repeatedValue: 0)
    var commandHandlers = [String : IRCHandlerDelegate]()
    
    var currentNick: Int
    
    var inputStream: NSInputStream?
    var outputStream: NSOutputStream?
    
    init(host: String, port: Int, useSSL: Bool) {
        self.host = host
        self.port = port
        self.useSSL = useSSL
        self.currentNick = 0
        super.init()
    }
    
    
    func send(command: String , destination: String?, message: String?) {
        var fullCommand : String = command
        
        if(destination != nil) {
            fullCommand += " "
            fullCommand += destination!
        }
        
        if(message != nil){
            fullCommand += " :"
            fullCommand += message!
        }
        
        fullCommand += "\r\n"
        
        var buffer = [UInt8](fullCommand.utf8)
        outputStream!.write(&buffer, maxLength: fullCommand.utf8.count)
    }
    
    func doLoop(){
        if inputStream!.hasBytesAvailable
        {
            var inputStringLines : [String] = []
            while inputStream!.hasBytesAvailable
            {
                let bytesRead = inputStream!.read(&IRCBuffer, maxLength: IRCBuffer.count)
                
                if(bytesRead > 0){
                    let string = NSString(bytes: &IRCBuffer, length: IRCBuffer.count, encoding: NSUTF8StringEncoding)
                    let lines = string?.componentsSeparatedByString("\r\n")
                    inputStringLines.appendContentsOf(lines!)
                }
                
                IRCBuffer = [UInt8](count: IRCBuffer.count, repeatedValue: 0)
            }
            //Remove that fucking crap
            inputStringLines.removeAtIndex(inputStringLines.count-1)
            
            for string in inputStringLines
            {
                var metadata : String?
                var prefix: String?
                var command: String = ""
                var destination : String?
                var message : String?
                
                let msgPattern = "(.+\\s)?:([a-z0-9.@!_]+)?\\s([a-zA-Z0-9]+)?\\s(\\#*[a-z0-9]+)\\s:?(.+)?"
                
                var regex : NSRegularExpression?
                do {
                    //First we check for any type of command except anormal ones like PING
                    regex = try NSRegularExpression(pattern: msgPattern, options: .AllowCommentsAndWhitespace)
                    if let match = regex?.firstMatchInString(string, options: NSMatchingOptions.WithTransparentBounds, range: NSRange(location: 0, length: string.characters.count)) {

                        if(match.numberOfRanges == 6){
                            let region = (string as NSString)
                            
                            let metadataRange = match.rangeAtIndex(1)
                            if metadataRange.length > 0 && (metadataRange.location + metadataRange.length) <= region.length {
                                metadata = region.substringWithRange(metadataRange)
                            }
                            
                            let prefixRange = match.rangeAtIndex(2)<commit_msg>Closes #4 Check array count before removing last object<commit_after>//
//  IRCHandler.swift
//  GamingStreamsTVApp
//
//  Created by <NAME> on 2015-09-19.
//  Copyright © 2015 Rivus Media Inc. All rights reserved.
//

import Foundation

protocol IRCHandlerDelegate
{
    func respond(target: IRCHandlerBase , prefix: String? , destination: String? , message: String?, metadata: String?)
}

class IRCHandlerBase: NSObject, NSStreamDelegate
{
    var host: String
    var port: Int
    var useSSL: Bool
    var genericCredentials: ( String? , [String] , String , Bool , String )?
    
    var IRCBuffer = [UInt8](count: 65536, repeatedValue: 0)
    var commandHandlers = [String : IRCHandlerDelegate]()
    
    var currentNick: Int
    
    var inputStream: NSInputStream?
    var outputStream: NSOutputStream?
    
    init(host: String, port: Int, useSSL: Bool) {
        self.host = host
        self.port = port
        self.useSSL = useSSL
        self.currentNick = 0
        super.init()
    }
    
    
    func send(command: String , destination: String?, message: String?) {
        var fullCommand : String = command
        
        if(destination != nil) {
            fullCommand += " "
            fullCommand += destination!
        }
        
        if(message != nil){
            fullCommand += " :"
            fullCommand += message!
        }
        
        fullCommand += "\r\n"
        
        var buffer = [UInt8](fullCommand.utf8)
        outputStream!.write(&buffer, maxLength: fullCommand.utf8.count)
    }
    
    func doLoop(){
        if inputStream!.hasBytesAvailable
        {
            var inputStringLines : [String] = []
            while inputStream!.hasBytesAvailable
            {
                let bytesRead = inputStream!.read(&IRCBuffer, maxLength: IRCBuffer.count)
                
                if(bytesRead > 0){
                    let string = NSString(bytes: &IRCBuffer, length: IRCBuffer.count, encoding: NSUTF8StringEncoding)
                    let lines = string?.componentsSeparatedByString("\r\n")
                    inputStringLines.appendContentsOf(lines!)
                }
                
                IRCBuffer = [UInt8](count: IRCBuffer.count, repeatedValue: 0)
            }
            //Remove that fucking crap
            if(inputStringLines.count > 0){
                inputStringLines.removeAtIndex(inputStringLines.count-1)
            }
            
            
            for string in inputStringLines
            {
                var metadata : String?
                var prefix: String?
                var command: String = ""
                var destination : String?
                var message : String?
                
                let msgPattern = "(.+\\s)?:([a-z0-9.@!_]+)?\\s([a-zA-Z0-9]+)?\\s(\\#*[a-z0-9]+)\\s:?(.+)?"
                
                var regex : NSRegularExpression?
                do {
                    //First we check for any type of command except anormal ones like PING
                    regex = try NSRegularExpression(pattern: msgPattern, options: .AllowCommentsAndWhitespace)
                    if let match = regex?.firstMatchInString(string, options: NSMatchingOptions.WithTransparentBounds, range: NSRange(location: 0, length: string.characters.count)) {

                        if(match.numberOfRanges == 6){
                            let region = (string as NSString)
                            
                            let metadataRange = match.rangeAtIndex(1)
                            if metadataRange.length > 0 && (metadataRange.location + metadataRange.length) <= region.length {
                                metadata = region.substringWithRange(metadataRange)
                            }
                            
                            let prefixRange = match.rangeAtIndex(2)
{'fraction_non_alphanumeric': 0.09639984175128577, 'fraction_numerical': 0.01054991428194646, 'mean_word_length': 1.5804695474651242, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '801984', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1902, 'n_words': 469}