<commit_before>//
//  ViewController.swift
//  LockMac
//
//  Created by <NAME> on 30/01/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import NMSSH

class ViewController: UIViewController {
    
    // Constants
    <#ERROR#>// FILL IN WITH YOUR OWN VALUES HERE
    let kUsername = "USERNAME"
    let kPassword = "PASSWORD"
    let kHost = "HOST"
    
    // Properties
    var isAccessGranted: Bool = false
    var session: NMSSHSession?
    
    // Outlets
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var sleepButton: UIButton!
    
    // MARK: - NMSSH connection - establishes a connection with your Mac
    
    private func connect() {
        // create the NMSSHSession
        session = NMSSHSession()
        session = NMSSHSession.connect(toHost: kHost, withUsername: kUsername)
        if let session = session {
            if session.isConnected {
                // authenticate by password
                session.authenticate(byPassword: <PASSWORD>)
                if session.isAuthorized {
                    self.isAccessGranted = true
                } else {
                    self.isAccessGranted = false
                }
            }
        }
    }
    
    // MARK: - Sleep method implemented using SSH command
    
    public func signOut() {
        connect()
        
        // ssh command which puts a Mac to sleep
        let sshCommand = "osascript -e 'tell application \"System Events\" to sleep'"
        // timeout defined in seconds
        let timeout: NSNumber = 30
        
        var error: NSError?
        guard let session = session else {
            // session is nil
            print("No session has been initialized.")
            return
        }
        
        if isAccessGranted {
            let response = session.channel.execute(sshCommand, error: &error, timeout: timeout)
            
            if let response = response {
                // if we have a response, it will be printed
                print(response)
            } else if error != nil {
                // if we have an error, the localizedDescription will be printed
                print(error?.localizedDescription ?? "No error message was provided.")
            }
            
            // either way we have to close the connection
            session.disconnect()
        } else if !isAccessGranted {
            // access is not granted
            print("Check your credentials, something is worng.")
        }
    }
    
    // MARK: - Login method implemented using SSH command
    
    public func login() {
        connect()
        
        // wake up your Mac's screen
        wakeScreen()
        
        // Virtual Keyboard ANSI Standard US keyboard
        // for letters t, e, s, t, 1, 2, 3, 4 - ordered exactly as are written here
        let keyCodes = [17, 14, 1, 17, 18, 19, 20, 21]
        
        // timeout defined in seconds
        let timeout: NSNumber = 30
        
        var error: NSError?
        guard let session = session else {
            // session is nil
            print("No session has been initialized.")
            return
        }
        
        if isAccessGranted {
            for index in 0..<keyCodes.count {
                // ssh command for keystroke
                let sshCommand = "osascript -e 'tell application \"System Events\" to key code \(keyCodes[index])'"
                let response = session.channel.execute(sshCommand, error: &error, timeout: timeout)
                
                if let response = response {<commit_msg>user-defined error - additional setup is required<commit_after>//
//  ViewController.swift
//  LockMac
//
//  Created by <NAME> on 30/01/2017.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import NMSSH

class ViewController: UIViewController {
    
    // Constants
    <#ERROR#>// FILL IN WITH YOUR OWN VALUES HERE
    let kUsername = "USERNAME"
    let kPassword = "PASSWORD"
    let kHost = "HOST"
    
    // Properties
    var isAccessGranted: Bool = false
    var session: NMSSHSession?
    
    // Outlets
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var sleepButton: UIButton!
    
    // MARK: - NMSSH connection - establishes a connection with your Mac
    
    private func connect() {
        // create the NMSSHSession
        session = NMSSHSession()
        session = NMSSHSession.connect(toHost: kHost, withUsername: kUsername)
        if let session = session {
            if session.isConnected {
                // authenticate by password
                session.authenticate(byPassword: <PASSWORD>)
                if session.isAuthorized {
                    self.isAccessGranted = true
                } else {
                    self.isAccessGranted = false
                }
            }
        }
    }
    
    // MARK: - Sleep method implemented using SSH command
    
    public func signOut() {
        connect()
        
        // ssh command which puts a Mac to sleep
        let sshCommand = "osascript -e 'tell application \"System Events\" to sleep'"
        // timeout defined in seconds
        let timeout: NSNumber = 30
        
        var error: NSError?
        guard let session = session else {
            // session is nil
            print("No session has been initialized.")
            return
        }
        
        if isAccessGranted {
            let response = session.channel.execute(sshCommand, error: &error, timeout: timeout)
            
            if let response = response {
                // if we have a response, it will be printed
                print(response)
            } else if error != nil {
                // if we have an error, the localizedDescription will be printed
                print(error?.localizedDescription ?? "No error message was provided.")
            }
            
            // either way we have to close the connection
            session.disconnect()
        } else if !isAccessGranted {
            // access is not granted
            print("Check your credentials, something is worng.")
        }
    }
    
    // MARK: - Login method implemented using SSH command
    
    public func login() {
        connect()
        
        // wake up your Mac's screen
        wakeScreen()
        
        <#ERROR#>// IF YOU HAVE A DIFFERENT PASSWORD, YOU NEED TO BUILD A BIGGER ARRAY HERE
        
        // Virtual Keyboard ANSI Standard US keyboard
        // for letters t, e, s, t, 1, 2, 3, 4 - ordered exactly as are written here
        let keyCodes = [17, 14, 1, 17, 18, 19, 20, 21]
        
        // timeout defined in seconds
        let timeout: NSNumber = 30
        
        var error: NSError?
        guard let session = session else {
            // session is nil
            print("No session has been initialized.")
            return
        }
        
        if isAccessGranted {
            for index in 0..<keyCodes.count {
                // ssh command for keystroke
                let sshCommand = "osascript -e 'tell application \"System Events\" to key code \(keyCodes[index])'"
                let response = session.channel.execute(sshCommand, error: &error, timeout: timeout)
                
                if let response = response {
{'fraction_non_alphanumeric': 0.07846090647679035, 'fraction_numerical': 0.009858962070382034, 'mean_word_length': 1.5334720776968436, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2105996', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1812, 'n_words': 670}