This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
forked from Wolvan/PicartoChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
262 lines (236 loc) · 9.46 KB
/
app.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env node
'use strict';
var commander = require("commander");
var storage = require("node-persist");
var EventEmitter = require("events");
var NiceList = require('./modules/nicelist');
var PluginLoader = require('./modules/pluginloader_old');
var ChannelManager = require('./modules/channelmanager');
var ConsoleChannel = require('./modules/consolechannel');
var NiceCLI = require('./modules/nicecli');
var EventTypes = require('./modules/eventtypes');
var BotEvent = require('./modules/botevent');
var MessageType = require('./modules/messagetypes');
var CommandMessage = require('./modules/commandmessage');
var API = function () {
this.version = "2.0.0";
this.events = new EventEmitter();
this.channels = new ChannelManager(this);
this.events.setMaxListeners(0);
this.sharedStorage = storage.create({dir: process.cwd() + "/storage/shared_storage"});
this.sharedStorage.initSync();
this.mainAppStorage = storage.create({dir: process.cwd() + "/storage/main_app"});
this.mainAppStorage.initSync();
this.pluginLoader = new PluginLoader(this, storage.create({dir: process.cwd() + "/storage/plugin_loader"}));
this.chatProviders = new NiceList();
this.cliparameters = new NiceList();
};
API.prototype.registerChatProvider = function (id, provider) {
this.chatProviders.Set(id, provider);
};
API.prototype.degisterChatProvider = function (id) {
this.chatProviders.RemoveById(id);
};
API.prototype.registerCliParameters = function (id, parameter) {
this.cliparameters.Set(id, parameter);
};
API.prototype.degisterCliParameters = function (id) {
this.cliparameters.RemoveById(id);
};
var api = new API();
api.events.on(EventTypes.CONNECT, function (event) {
console.log("Connected to " + event.source.channelName);
});
api.events.on(EventTypes.DISCONNECT, function (event) {
console.log("Disconnected from " + event.source.channelName);
});
api.events.on(EventTypes.EXCEPTION, function (event) {
if (event.data.channel)
event.data.channel.sendMessage("(╯°□°)╯︵ uoᴉʇdǝɔxƎ");
console.log(event);
});
api.events.on(EventTypes.CONSOLECOMMAND, function (event) {
if (event.data.command === "quit" || event.data.command === "exit" || event.data.command === "stop") {
cli.moveStdOut = false;
process.stdout.write('\n');
process.exit();
}
});
api.registerCliParameters('url', {
name: "Url",
description: "An url",
flag: "-u"
});
// Load all Plugins in the ./plugins directory
var quiet_loading = true;
api.pluginLoader.listPlugins().forEach(function (item) {
var plugin_state = api.pluginLoader.getInitialPluginState(item);
if (!plugin_state) {
plugin_state = "running";
}
if (plugin_state === "running" || plugin_state === "loaded") {
api.pluginLoader.loadPlugin(item, quiet_loading);
}
if (plugin_state === "running") {
api.pluginLoader.startPlugin(item, quiet_loading);
}
});
console.log("Loaded plugins:", api.pluginLoader.getLoadedPlugins().join(", "));
console.log("Started plugins:", api.pluginLoader.getStartedPlugins().join(", "));
var globalConfig = {};
var connectConfigs = [];
var parseGlobals = true;
var currentConnectConfig = false;
var readReturn = false;
var readBuffer = "";
var args = process.argv.slice(2);
for (var i = 0; i < args.length; ++i) {
var arg = args[i];
console.log(arg);
if (arg.indexOf('+') === 0 && process.env[arg.slice(1)] && process.env[arg.slice(1)].indexOf("-") !== 0) {
arg = process.env[arg.slice(1)];
}
if (arg.indexOf("-") !== 0) {
if (readReturn) {
readBuffer += " " + arg;
}
} else {
if (readReturn) {
readReturn.call(null, readBuffer.slice(1));
var readReturn = false;
var readBuffer = "";
}
var globalFlag = false;
if (parseGlobals) {
api.cliparameters.ForEach(function (par, parId) {
if (arg === par.flag || arg === "--" + parId) {
globalFlag = parId;
}
});
}
if (globalFlag) {
readReturn = function (val) {
globalConfig[globalFlag] = val;
};
} else {
if (currentConnectConfig !== false) {
var options = currentConnectConfig.provider.ConnectOptions;
var option = false;
for (var opt in options) {
if (arg === options[opt].flag || arg === "--" + opt) {
option = opt;
break;
}
}
if (option !== false) {
readReturn = function (val) {
currentConnectConfig[option] = val;
};
} else {
currentConnectConfig = false;
}
}
if (currentConnectConfig === false) {
var channel = api.chatProviders.Where(function (provider, id) {
return arg === "--" + id;
}).First();
if (channel) {
currentConnectConfig = {
provider: channel
};
connectConfigs.push(currentConnectConfig);
} else {
console.error("Unknown chat provider or option:" + arg);
}
}
}
}
}
if (readReturn) {
readReturn.call(null, readBuffer.slice(1));
var readReturn = false;
var readBuffer = "";
}
// Load config
var config = require("./config.json") || {};
if (config.channels && config.channels.length) {
config.channels.forEach(function (channel) {
if (channel.enabled !== false) {
var provider = api.chatProviders.Get(channel.provider);
if(provider) {
channel.provider = provider;
connectConfigs.push(channel);
}
}
});
}
console.log(globalConfig);
console.log(connectConfigs);
for (var i = 0; i < connectConfigs.length; ++i) {
var con = connectConfigs[i];
api.channels.Add(new con.provider.Channel(api, con));
}
console.log();
var CONSOLE_CHANNEL = new ConsoleChannel(api, process.stdin, process.stdout);
var cli = new NiceCLI(process.stdin, process.stdout);
cli.events.on(EventTypes.CONSOLECOMMAND, function (command) {
var ccEvt = new BotEvent(EventTypes.CONSOLECOMMAND, CONSOLE_CHANNEL.stdinUser.channel, new CommandMessage(CONSOLE_CHANNEL.stdinUser.channel, new Date(), CONSOLE_CHANNEL.stdinUser, command, Math.random(), MessageType.GENERIC));
ccEvt.cmdHelp = new NiceList();
api.events.emit("consoleCommand", ccEvt);
if (!ccEvt.claimed) {
console.log("Command '" + ccEvt.data.command + "' could not be found!");
//showConsoleHelp(ccEvt.cmdHelp);
}
});
cli.events.on(EventTypes.COMMANDCOMPLETION, function (completionData) {
var ccEvt = new BotEvent(EventTypes.COMMANDCOMPLETION, CONSOLE_CHANNEL.stdinUser.channel, new CommandMessage(CONSOLE_CHANNEL.stdinUser.channel, new Date(), CONSOLE_CHANNEL.stdinUser, completionData.input, Math.random(), MessageType.GENERIC));
ccEvt.data.completionList = completionData.suggestions;
api.events.emit(EventTypes.COMMANDCOMPLETION, ccEvt);
});
function askForChannel(returnFunc) {
cli.prompt("Channel: ", returnFunc);
}
function askForAccount(returnFunc) {
cli.prompt("Account: ", returnFunc);
}
function askForPasswordFor(accountName, returnFunc) {
cli.prompt("Password for '" + accountName + "': ", returnFunc, true);
}
api.events.on(EventTypes.CONSOLECOMMAND, function (event) {
var command = event.data;
if ((command.command === 'pluginloader' || command.command === 'pl') && event.claim()) {
var subcommand = command.parameters.shift();
switch (subcommand) {
case 'load':
api.pluginLoader.loadPlugin(event.data.parameters.join(' '), false);
break;
case 'start':
api.pluginLoader.startPlugin(event.data.parameters.join(' '), false);
break;
case 'stop':
api.pluginLoader.stopPlugin(event.data.parameters.join(' '), false);
break;
case 'unload':
api.pluginLoader.unloadPlugin(event.data.parameters.join(' '), false);
break;
case 'reload':
api.pluginLoader.stopPlugin(event.data.parameters.join(' '), false);
api.pluginLoader.unloadPlugin(event.data.parameters.join(' '), false);
api.pluginLoader.loadPlugin(event.data.parameters.join(' '), false);
api.pluginLoader.startPlugin(event.data.parameters.join(' '), false);
break;
}
} else if (command.command === 'connect' && event.claim()) {
if (command.parameters.length > 0) {
var channel = command.parameters[0];
var acctIndex = command.parameters.indexOf("-a");
var account = (acctIndex !== -1 && command.parameters.length > acctIndex) ? command.parameters[acctIndex + 1] : false;
var passIndex = command.parameters.indexOf("-p");
var password = (passIndex !== -1 && command.parameters.length > passIndex) ? command.parameters[passIndex + 1] : false;
} else {
command.reply("Usage: connect <Channel> -a <BotAccount> -p <BotAccountPassword>");
}
} else if (command.command === 'reconnect' && event.claim()) {
} else if (command.command === 'disconnect' && event.claim()) {
}
});