-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.js
More file actions
executable file
·174 lines (154 loc) · 5.8 KB
/
Copy pathshell.js
File metadata and controls
executable file
·174 lines (154 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env node
(function() {
var Commands = require( 'commands.js' ).Commands,
Config = require( 'config/shell.js' ).Config,
MCat = require( 'message-catalog/shell.js' ).MCat,
Env = require( 'c2fo/env.js' ).Env,
events = require( 'events' ),
emitter = new events.EventEmitter(),
Library = require( MCat.functions ).Library,
Prompt = require( 'prompt.js' ).Prompt,
Connector = require( 'connector.js' ).Connector,
version = '0.0.1';
var prompt = new Prompt( {
debug : false,
default : function () { return MCat.promptPrefix + MCat.promptPoint; },
exitMsg : MCat.exit,
helpBlurb : MCat.helpBlurb,
title : MCat.title,
version : version
} );
var failedHandler = function ( promptContent, failed ) {
try {
if ( failed ) {
console.error( failed );
}
if ( promptContent ) {
prompt.content = function () { return promptContent + MCat.promptPoint; };
}
prompt.show();
}
catch( failed ) {
console.error( MCat.failedPrefix + failed );
return;
}
};
var subPromptHandler = function ( promptContent, msg ) {
try {
if ( msg )
console.log( msg );
if ( promptContent )
prompt.content = function () { return promptContent + MCat.c; };
prompt.show();
}
catch( failed ) {
console.error( MCat.failedPrefix + failed );
return;
}
};
var successHandler = function ( promptContent, msg ) {
try {
if ( msg )
console.log( msg );
if ( promptContent )
prompt.content = function () { return promptContent + MCat.promptPoint; };
prompt.show();
}
catch( failed ) {
console.error( MCat.failedPrefix + failed );
return;
}
};
emitter.on(
'completedCommand', function ( prompt, shellMsg ) {
successHandler( prompt, shellMsg ); } ).on(
'completedServiceCall', function ( prompt, serviceMsg ) {
successHandler( prompt, serviceMsg ); } ).on(
'debug', function ( msg ) {
console.info( MCat.debugPrefix + msg ); }).on(
'failedCommand', function ( failedMsg ) {
process.nextTick( function () { failedHandler( null, MCat.failedPrefix + ( failedMsg ||
MCat.failedCommand ) ); } ); } ).on(
'failedJSONParse', function ( prompt, failedMsg ) {
process.nextTick( function () { failedHandler( prompt, MCat.failedPrefix + ( failedMsg ||
MCat.failedJSONParse )); } ); } ).on(
'failedC2FOfailed', function ( prompt, failedMsg ) {
process.nextTick( function () { failedHandler( prompt, MCat.failedPrefix + ( failedMsg ||
MCat.failedFail ) ); } ); } ).on(
'failedC2FOservice', function ( prompt, failedMsg ) {
process.nextTick( function () { failedHandler( prompt, MCat.failedPrefix + ( failedMsg ||
MCat.failedC2FOservice ) ); } ); } ).on(
'failedC2FOresponse', function ( prompt, failedMsg ) {
process.nextTick( function () { failedHandler( prompt, MCat.failedPrefix + ( failedMsg ||
MCat.failedC2FOresponse ) ); } ); } ).on(
'schedulePrompt', function ( prompt, msg, flag ) {
subPromptHandler( prompt, msg, flag );
}
);
var library = new Library( MCat.agentString + version, successHandler, emitter, Config.pageSize );
prompt.handleCommand = function ( command ) {
// prior to auth provide shell with limited capabilities
Commands( env, emitter,
prompt, command,
null , library
);
};
var env = new Env( version, function ( connectionId, pwd ) {library.auth.signIn( connectionId, pwd );}, successHandler );
prompt.isDebugging = env.context.debug;
var libModules = ['net', 'auth', 'event', 'invoice'];
for ( var m in libModules ) {
library[libModules[m]].isDebugging = env.context.debug;
}
library.auth.access = function ( connectionId, pwd, user, instance, response ) {
if ( response ) {
if ( library.auth.isDebugging )
emitter.emit( 'debug', instance + MCat.s + MCat.sessionGrant );
var connection;
var cachedConnectionHandler = function ( cId, c ) {
connectionId = cId;
connection = c;
var tokens = cId.split( '@' );
connection.user = tokens[0].trim();
connection.instance = tokens[1].trim();
connection.emulating = ( tokens[2] && tokens[2].trim() );
emitter.emit( 'completedCommand', cId );
};
var shell = new Connector( env, prompt, library, cachedConnectionHandler );
shell.prompt.handleCommand = function ( command ) {
// authenticated shell with access to commands
// that wrap library
Commands( env, emitter,
prompt, command,
shell, library,
connectionId, connection
);
};
shell.env.connections[connectionId] = shell.env.connections[connectionId] || {};
var connection = shell.env.connections[connectionId];
connection.id = library.connectionCount++;
connection.instance = instance;
connection.lastAction = MCat.successSignin;
connection.password = pwd;
connection.sessionCookie = response.headers['set-cookie'][0];
connection.user = user;
var payload;
try {
payload = JSON.parse( response.body ).payload;
}
catch( e ) {
emitter.emit( 'failedJSONParse', e );
return;
}
connection.lastActive = new Date( payload.created );
library.auth.getDetails( connectionId, connection, function () {
library.event.getDetails(connectionId, connection, function () {
emitter.emit( 'completedServiceCall', connectionId );
});
});
}
else {
emitter.emit( 'failedCommand', MCat.failedConnect + connectionId );
return;
}
};
}( this ) );