{
  "name": "rai",
  "description": "Request-Answer-Interface for generating text based command servers (SMTP, POP etc)",
  "version": "0.1.9",
  "author": {
    "name": "Andris Reinman"
  },
  "maintainers": [
    {
      "name": "andris",
      "email": "andris@node.ee"
    }
  ],
  "repository": {
    "type": "git",
    "url": "http://github.com/andris9/rai.git"
  },
  "scripts": {
    "test": "nodeunit test"
  },
  "main": "./lib/rai",
  "licenses": [
    {
      "type": "MIT",
      "url": "http://github.com/andris9/rai/blob/master/LICENSE"
    }
  ],
  "devDependencies": {
    "nodeunit": "*"
  },
  "engines": [
    "node >=0.4.0"
  ],
  "keywords": [
    "servers",
    "text-based"
  ],
  "readme": "# RAI - Request-Answer-Interface\n\n**rai** is a node.js module to easily generate text based command line servers.\nWhen a client sends something to the server, the first word of the line is\ntreated as a command and the rest of the line as binary payload.\n\n[![Build Status](https://secure.travis-ci.org/andris9/rai.png)](http://travis-ci.org/andris9/rai)\n\nIn addition to line based commands, there's also a data mode, to transmit\neverygting received. And there's also an option to switch to TLS mode for\nsecure connections.\n\nThis way it is trivial to create SMTP, POP3 or similar servers.\n\n## Installation\n\n    npm install rai\n    \n## Usage\n\n### Simple server\n\n    var RAIServer = require(\"rai\").RAIServer;\n    \n    // create a RAIServer on port 1234\n    var server = new RAIServer();\n    server.listen(1234);\n    \n    // Start listening for client connections\n    server.on(\"connect\", function(client){\n    \n        // Greet the client\n        client.send(\"Hello!\");\n        \n        // Wait for a command\n        client.on(\"command\", function(command, payload){\n        \n            if(command == \"STATUS\"){\n                client.send(\"Status is OK!\");\n            }else if(command == \"QUIT\"){\n                client.send(\"Goodbye\");\n                client.end();\n            }else{\n                client.send(\"Unknown command\");\n            }\n        \n        });\n    \n    });\n\nServer only emits `'connect'` and `'error'` events, while the client \nobjects emit `'timeout'`, `'error'` and `'end'` in addition to data \nrelated events.\n\n### Starting a server\n\nServer can be started with `new RAIServer([options])` where options is an optional\nparameters object with the following properties:\n\n  * **debug** - if set to true print traffic to console\n  * **disconnectOnTimeout** - if set to true close the connection on disconnect\n  * **secureConnection** - if set to true close the connection on disconnect\n  * **credentials** - credentials for secureConnection and STARTTLS\n  * **timeout** - timeout in milliseconds for disconnecting the client, defaults to 0 (no timeout)\n  \nOnce the server has been set up, it can start listening for client connections\nwith `server.listen(port[, hostname][, callback])`. Callback function gets an error\nobject as a parameter if the listening failed.\n\n    var server = new RAIServer();\n    server.listen(25); // start listening for port 25 on \"localhost\"\n\n### Closing server\n\nServer can be closed with `server.end([callback])` where callback is run when\nthe server is finally closed.\n\n### Sending data\n\nData can be sent with `client.send(data)` where `data` is either a String or\na Buffer. `\"\\r\\n\"` is automatically appended to the data.\n\n    client.send(\"Greetings!\");\n\n### Forcing connection close\n\nConnections can be ended with `client.end()`\n\n    if(command == \"QUIT\"){\n        client.send(\"Good bye!\");\n        client.end();\n    }\n\n### TLS mode\n\nTLS can be switched on with `client.startTLS([credentials][, callback])` and the status can\nbe listened with `'tls'` (emitted when secure connection is established)\n\n`credentials` is an object with strings of pem encoded `key`, `cert` and optionally an\narray `ca`. If `credentials` is not supplied, an autogenerated value is used.\n\n    if(command == \"STARTTLS\"){\n        client.startTLS();\n    }\n    \n    client.on(\"tls\", function(){\n        console.log(\"Switched to secure connection\");\n    });\n\nIf `callback` is not set `'tls'` will be emitted on connection upgrade.\n\n### Data mode\n\nData mode can be turned on with `client.startDataMode([endSequence])` and incoming\nchunks can be received with `'data'`. The end of data mode can be detected by\n`'ready'`.\n\n`endSequence` is a String for matching the end (entire line) of the data stream.\nBy default it's `\".\"` which is suitable for SMTP and POP3.\n\n    if(command == \"DATA\"){\n        client.send(\"End data with <CR><LF>.<CR><LF>\");\n        client.startDataMode();\n    }\n\n    client.on(\"data\", function(chunk){\n        console.log(\"Data from client:\", chunk);\n    });\n    \n    client.on(\"ready\", function(){\n        client.send(\"Data received\");\n    });\n\n## Testing\n\nThere is a possibility to set up a mockup client which sends a batch of commands\none by one to the server and returns the last response and an array of all responses(except the TLS negotiation).\n\n    var runClientMockup = require(\"rai\").runClientMockup;\n    \n    var cmds = [\"EHLO FOOBAR\", \"STARTTLS\", \"QUIT\"];\n    runClientMockup(25, \"mail.hot.ee\", cmds, function(lastResponse, allResponses){\n        console.log(\"Final:\", lastResponse.toString(\"utf-8\").trim());\n        console.log(\"All:\", allResponses.map(function(e){\n            return e.toString(\"utf-8\").trim()\n        }).join(', '));\n    });\n\n`runClientMockup` has he following parameters in the following order:\n\n  * **port** - Port number\n  * **host** - Hostname to connect to\n  * **commands** - Command list (an array) to be sent to server\n  * **callback** - Callback function to run on completion\n  * **debug** - if set to true log all input/output\n\nResponse from the callback function is a Buffer and contains the\nlast data received from the server and an array of Buffers with all data received from the server.\n\n## License\n\n**MIT**",
  "readmeFilename": "README.md",
  "bugs": {
    "url": "https://github.com/andris9/rai/issues"
  },
  "_id": "rai@0.1.9",
  "dist": {
    "shasum": "400795602478beae550fb1fba9b2b1c721602664"
  },
  "_from": "rai@~0.1",
  "_resolved": "https://registry.npmjs.org/rai/-/rai-0.1.9.tgz"
}
