forked from kofany/psotnic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-create.cpp
648 lines (607 loc) · 16.7 KB
/
config-create.cpp
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/***************************************************************************
* Copyright (C) 2008 by Stefan Valouch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "prots.h"
#include "global-var.h"
/*! Reads a string from a terminal and sets a pstrings value to it.
* \overload
* \param prompt The question used to ask for the value.
* \param var The pstring to set.
* \param defaultValue The default to use as pstring does not have that.
* \return true if a value was set, even if it is \e defaultValue. false if not set (i.e. if no
* default and nothing entered.
* \author Stefan Valouch <[email protected]>
*/
bool readUserInput( const char *prompt, pstring<> &var, const char *defaultValue )
{
string buf;
do
{
if(strlen(defaultValue))
{
printPrompt( "%s [%s]: ", prompt, defaultValue );
}
else
{
printPrompt( "%s: ", prompt );
}
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() >= 1)
{
var = buf.c_str();
return true;
}
else if (strlen(defaultValue))
{
var = defaultValue;
return true;
}
else
{
continue;
}
}
while( true );
}
/*! Reads a boolean value from a terminal and sets an entities value to that value.
* \overload
* \param prompt The question used to ask for the value.
* \param entity The entity.
* \param force Wether or not to repeat the question till valid data is entered.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entBool &entity, bool force )
{
string buf;
options::event *e;
do
{
printPrompt( "%s (ON or OFF) [%s]: ", prompt, (entity.defaultValue ? "ON" : "OFF") );
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() >= 1)
{
e = entity.setValue(entity.name, buf.c_str());
if(e->ok)
{
break;
}
else if(!force)
{
entity.setValue(entity.name, itoa(entity.defaultValue));
break;
}
else
{
printError( (const char*)e->reason );
}
}
}
while( force );
//return false;
}
/*! Reads an integer value from stdin and sets an entities value to it.
* \overload
* \param prompt The question used to ask for the value.
* \param entity The entity.
* \param force Wether or not to repeat the question till valid data is entered.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entInt &entity, bool force )
{
string buf;
options::event *e;
do
{
if( entity.defaultValue )
{
printPrompt( "%s (%d to %d) [%d]: ", prompt, entity.min, entity.max, entity.defaultValue );
}
else
{
printPrompt( "%s (%d to %d): ", prompt, entity.min, entity.max );
}
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() < 1)
{
if(force)
{
printError( "You have to enter something here" );
continue;
}
else
{
entity.setValue(entity.name, itoa(entity.defaultValue));
break;
}
}
e = entity.setValue(entity.name, buf.c_str());
if(e->ok)
{
break;
}
else
{
printError( (const char*)e->reason );
}
}
while ( true );
}
/*! Read a host address from stdin and set an entities value to it.
* \overload
* \param prompt The question to ask for the value.
* \param entity The entits.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entHost &entity, bool allowEmpty )
{
string buf;
do
{
if(strcmp((const char*)entity.ip, "0.0.0.0"))
{
printPrompt( "%s [%s]: ", prompt, (const char*)entity.ip );
}
else
{
printPrompt( "%s: ", prompt );
}
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if( allowEmpty && buf.empty() )
{
break;
}
options::event *e = entity.setValue(entity.name, buf.c_str());
if(!e->ok)
{
printError( (const char*)e->reason );
}
else
{
break;
}
}
while( true );
}
/*! Reads an IP Port Passwort Handle combination from stdin and sets an entities value to it.
* \todo Implement checks!!!
* \overload
* \param prompt The question used to ask for the value.
* \param entity The entity.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entHub &entity )
{
string buf;
options::event *e;
entHost host = entHost( "tmp" );
entInt port = entInt( "tmp", 1, 65535, 0 );
entMD5Hash pass = entMD5Hash( "tmp" );
entWord handle = entWord( "tmp" );
printMessage( "%s: ", prompt );
do
{
readUserInput( "\tHost", host );
readUserInput( "\tPort", port );
readUserInput( "\tPassword", pass );
readUserInput( "\tHandle", handle );
char buf[MAX_LEN];
snprintf( buf, MAX_LEN, "%s %s %s %s", (const char*)host.ip, port.getValue(), pass.getHash(), (const char*)handle.string );
e = entity.setValue( entity.name, buf );
if( !e->ok )
{
printError( (const char*)e->reason );
}
else
{
break;
}
}
while( true );
}
/*! Reads a md5 string from stdin.
* \overload
* \param prompt The question used to ask for the value.
* \param entity The entity.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entMD5Hash &entity )
{
string buf;
options::event *e;
do
{
printPrompt( "%s: ", prompt );
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() < 8) // password length okay?
{
printError( "Passwords should have a length of at least 8 characters!" );
}
else // okay, throw it in
{
e = entity.setValue(entity.name, buf.c_str());
if(e->ok)
{
break;
}
else
{
printError( (const char*)e->reason );
}
}
}
while( true );
}
/*! Asks a user for information to build an entServer object.
* \overload
* \param prompt The question used to ask for the value.
* \param entity The entity.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entServer &entity )
{
string buf;
options::event *e;
entHost host = entHost( "tmp" );
entInt port = entInt( "tmp", 1, 65535, 0 );
entWord pass = entWord( "tmp" );
printMessage( "%s: ", prompt );
do
{
readUserInput( "\tHost address", host );
readUserInput( "\tPort number", port );
readUserInput( "\tPassword", pass );
e = entity.set( entity.name, (const char*)host.ip, port.getValue(), (const char*)pass.string );
if( !e->ok )
{
printError( (const char*)e->reason );
}
else
{
break;
}
}
while( true );
}
/*! Reads a string value from stdin and sets an entities value to it.
* If the \e entity has no default value, the user is forced to enter something.
* \overload
* \param prompt The question used to ask for the value.
* \param entity The entity.
* \author Stefan Valouch <[email protected]>
*/
void readUserInput( const char *prompt, entString &entity )
{
string buf;
options::event *e;
do
{
if(entity.defaultString)
{
printPrompt( "%s (%d to %d chars) [%s]: ", prompt, entity.min, entity.max,
(const char*)entity.defaultString );
}
else
{
printPrompt( "%s (%d to %d chars): ", prompt, entity.min, entity.max );
}
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() < 1)
{
if(entity.defaultString)
{
entity.setValue(entity.name, entity.defaultString);
break;
}
continue;
}
e = entity.setValue(entity.name, buf.c_str());
if(e->ok)
{
break;
}
else
{
printError( (const char*)e->reason );
}
}
while( true );
}
/*! Asks a multiple choice question and returns the answer-id.
* \note Be sure that \e defChoice is whithin the range of \e choices.
* \param prompt The question to ask.
* \param choices A descriptive text to make the decision easier.
* \param defChoice The ID of the default value.
* \return The selected ID or the default if nothing (valid) was entered.
* \author Stefan Valouch <[email protected]>
*/
int readUserMC( const char *prompt, const char *choices[], size_t len, unsigned int defChoice )
{
string buf;
printMessage( "%s:", prompt );
for( unsigned int i = 0; i < len; i++ )
{
if(i == defChoice)
{
printItem( "\t[%d]\t%s", i, choices[i] );
}
else
{
printItem( "\t %d \t%s", i, choices[i] );
}
}
do
{
printPrompt( "Your choice: " );
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() < 1)
{
return defChoice;
}
else
{
unsigned int inp = atoi(buf.c_str());
if( inp > len-1 )
{
printError( "Please choose a valid value!" );
continue;
}
for( unsigned int i = 0; i < sizeof(choices); i++ )
{
if(i == inp)
return inp;
}
return defChoice;
}
}
while( true );
}
/*! Asks the user a simple Yes/No question.
* \param prompt The question.
* \param defaultValue The default value.
* \return true if the user entered yes, false if no.
* \author Stefan Valouch <[email protected]>
*/
bool readUserYesNo( const char *prompt, bool defaultValue )
{
string buf;
printPrompt( "%s (Y or N) [%s]: ", prompt, (defaultValue ? "Y" : "N") );
getline(cin, buf);
if(buf.size() < 1)
{
return defaultValue;
}
if(buf == "Y" || buf == "y" || buf == "Yes" || buf == "yes" || buf == "1")
{
return true;
}
return false;
}
/*! Interactive generator for config files.
* This function asks the user for the data and generates the initial config without needing to have
* the unencrypted config file laying around waiting for encryption.
* \author Stefan Valouch <[email protected]>
*/
void createInitialConfig()
{
printMessage( "Bot now runs in config creation mode" );
printMessage( "The options are explained at http://psotnic.wiki.sourceforge.net/Config+File" );
printMessage( "Mandatory options for all bot types:" );
readUserInput( "Which nick should the bot use?", config.nick );
readUserInput( "What should be used as the bots realname?", config.realname );
readUserInput( "Which characters should get appended to the nick if it is already taken?", config.nickappend );
readUserInput( "Nick to use when the normal nick is already taken:", config.altnick );
readUserInput( "Enter the bots IPv4 address, use 0.0.0.0 for any", config.myipv4 );
const char *choices[3] = {
"Main",
"Slave",
"Leaf"
};
int bottype = readUserMC( "What type of bot do you want?", choices, 3, 0 );
switch( bottype )
{
case 0:
printMessage( "Configuration for bot type: MAIN" );
config.bottype = BOT_MAIN;
readUserInput( "What port should the bot listen on for connections from owners and slaves?", config.listenport );
#ifdef HAVE_SSL
readUserInput( "What port should the bot listen on for SSL encrypted connections from owners and slaves?", config.ssl_listenport );
#endif
readUserInput( "Please enter a password for connecting to partyline.", config.ownerpass );
break;
case 1:
printMessage( "Configuration for bot type: SLAVE" );
config.bottype = BOT_SLAVE;
readUserInput( "What port should the bot listen on for connections from leafs?", config.listenport );
#ifdef HAVE_SSL
readUserInput( "What port should the bot listen on for SSL encrypted connections from leafs?", config.ssl_listenport );
#endif
readUserInput( "Please enter the connection options for connecting to the main bot.", config.hub );
break;
case 2:
printMessage( "Configuration for bot type: LEAF" );
config.bottype = BOT_LEAF;
readUserInput( "Connection options to connect to slave?", config.hub );
break;
default:
printError( "ERROR: unknown bot type: " + bottype );
exit(1);
break;
}
string buf;
options::event *e;
int n;
// we don't make a function for that entMult stuff, its easier this way!
if( config.bottype == BOT_LEAF )
{
if( readUserYesNo( "Do you want to add alternative hubs?", false ) )
{
printMessage( "Valid lines are {}" );
for( n = 0; n < MAX_ALTS; )
{
printPrompt( "\tAltHub %d: ", n );
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if( buf.size() < 1 )
{
break;
}
e = config.alt[n].setValue( config.alt[n].name, buf.c_str() );
if( !e->ok )
{
printError( (const char*)e->reason );
continue;
}
n++;
}
printMessage( "Okay, added %d alternative hubs.", n );
}
}
if( readUserYesNo( "Do you want to add some servers?", true ) )
{
printMessage( "Valid lines are: \"1.2.3.4 6667\" or \"1.2.3.4 6667 password\"" );
for( n = 0; n < MAX_SERVERS; )
{
printPrompt( "\tServer %d: ", n );
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if( buf.size() < 1 )
{
break;
}
e = config.server[n].setValue( config.server[n].name, buf.c_str() );
if( !e->ok )
{
printError( (const char*)e->reason );
continue;
}
n++;
}
printMessage( "Okay, added %d servers.", n );
}
if( readUserYesNo( "Do you want to add some modules?", false ) )
{
for( n = 0; n < MAX_MODULES; )
{
printPrompt( "\tModule %d: ", n );
getline(cin, buf);
if(!cin.good())
{
cout << endl;
exit(1);
}
if(buf.size() < 1)
{
break;
}
e = config.module_load[n].setValue(config.module_load[n].name, buf.c_str());
if(!e->ok)
{
printError( (const char*)e->reason );
continue;
}
n++;
}
printMessage( "Okay, added %d modules.", n );
}
if( readUserYesNo( "Do you want to set up the very basic and optional stuff?", false ) )
{
printMessage( "Detailed configuration" );
readUserInput( "IPv4 or IPv6 address (default: determined by OS", config.vhost, true );
readUserInput( "What is the bots username?", config.ident );
config.handle.defaultString = config.nick.string;
readUserInput( "What is the bots partyline handle?", config.handle );
// vhost
// logfile TODO: implement {partially done in branch}
//readUserInput( "Path where psotnics logfiles should be stored to", config.logfile );
//readUserInput( "Loglevel for psotnics actions", config.loglevel );
config.userlist_file.defaultString = config.nick.string + ".ul";
readUserInput( "Where should the bot place its userlist?", config.userlist_file );
readUserInput( "Which ctcp version do you want? 0 = none, 1 = psotnic, 2 = irssi, 3 = epic, 4 = lice, 5 = bitchx, 6 = dzony loker, 7 = luzik, 8 = mirc 6.14", config.ctcptype );
readUserInput( "Disallow forking?", config.dontfork );
readUserInput( "Keepnick?", config.keepnick );
readUserInput( "Kick reason used for most kicks", config.kickreason );
readUserInput( "Kick reason used when someone overrides the channel limit", config.limitreason );
readUserInput( "Kick reason for keepout setting", config.keepoutreason );
readUserInput( "Part reason", config.partreason );
readUserInput( "Quit reason", config.quitreason );
readUserInput( "Cycle reason", config.cyclereason );
// bnc
// bouncer
readUserInput( "Botnetword, has to be equal on all bots in the same botnet", config.botnetword );
#ifdef HAVE_ADNS
readUserInput( "How many resolve threads should be used?", config.resolve_threads );
readUserInput( "Domain Time-To-Live?", config.domain_ttl );
#endif
readUserInput( "Check shit on nick change?", config.check_shit_on_nick_change );
}
string defaultcfgfile = config.nick.getValue() + string(".cfg");
readUserInput( "Where should the config file be saved?", config.file, defaultcfgfile.c_str()); // TODO: use confdir as default path once make-install is merged
bool decryptedSave = readUserYesNo( "Should the config file be saved decrypted?", false );
e = config.save( decryptedSave );
if (!e->ok)
{
printError( (const char*)e->reason );
exit(1);
}
printMessage( (const char*)e->reason );
exit(0);
}