File: BuiltinDirectoryPlugin.cpp

package info (click to toggle)
veyon 4.1.7%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,020 kB
  • sloc: cpp: 31,280; ansic: 23,250; makefile: 86; python: 80; sh: 48
file content (701 lines) | stat: -rw-r--r-- 18,756 bytes parent folder | download
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/*
 * BuiltinDirectoryPlugin.cpp - implementation of BuiltinDirectoryPlugin class
 *
 * Copyright (c) 2017-2019 Tobias Junghans <tobydox@veyon.io>
 *
 * This file is part of Veyon - http://veyon.io
 *
 * 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 (see COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include <QFile>

#include "BuiltinDirectoryConfigurationPage.h"
#include "BuiltinDirectory.h"
#include "BuiltinDirectoryPlugin.h"
#include "CommandLineIO.h"
#include "ConfigurationManager.h"
#include "ObjectManager.h"


BuiltinDirectoryPlugin::BuiltinDirectoryPlugin( QObject* parent ) :
	QObject( parent ),
	m_configuration(),
	m_commands( {
{ "help", tr( "Show help for specific command" ) },
{ "add", tr( "Add a room or computer" ) },
{ "clear", tr( "Clear all rooms and computers" ) },
{ "dump", tr( "Dump all or individual rooms and computers" ) },
{ "list", tr( "List all rooms and computers" ) },
{ "remove", tr( "Remove a room or computer" ) },
{ "import", tr( "Import objects from given file" ) },
{ "export", tr( "Export objects to given file" ) },
				} )
{
}



BuiltinDirectoryPlugin::~BuiltinDirectoryPlugin()
{
}



void BuiltinDirectoryPlugin::upgrade( const QVersionNumber& oldVersion )
{
	if( oldVersion < QVersionNumber( 1, 1 ) &&
			m_configuration.localDataNetworkObjects().isEmpty() == false )
	{
		m_configuration.setNetworkObjects( m_configuration.localDataNetworkObjects() );
		m_configuration.setLocalDataNetworkObjects( QJsonArray() );
	}
}



NetworkObjectDirectory *BuiltinDirectoryPlugin::createNetworkObjectDirectory( QObject* parent )
{
	return new BuiltinDirectory( m_configuration, parent );
}



ConfigurationPage *BuiltinDirectoryPlugin::createConfigurationPage()
{
	return new BuiltinDirectoryConfigurationPage( m_configuration );
}



QStringList BuiltinDirectoryPlugin::commands() const
{
	return m_commands.keys();
}



QString BuiltinDirectoryPlugin::commandHelp( const QString& command ) const
{
	return m_commands.value( command );
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_help( const QStringList& arguments )
{
	const auto command = arguments.value( 0 );

	if( command == QStringLiteral("import") )
	{
		CommandLineIO::print( tr("\nUSAGE\n\n%1 import <FILE> [room <ROOM>] [format <FORMAT-STRING-WITH-VARIABLES>] "
								 "[regex <REGULAR-EXPRESSION-WITH-VARIABLES>]\n\n"
								 "Valid variables: %name% %host% %mac% %room%\n\n"
								 "Examples:\n\n"
								 "* Import simple CSV file to a single room:\n\n"
								 "    %1 import computers.csv room \"Room 01\" format \"%name%;%host%;%mac%\"\n\n"
								 "* Import CSV file with room name in first column:\n\n"
								 "    %1 import computers-with-rooms.csv format \"%room%,%name%,%mac%\"\n\n"
								 "* Import text file with with key/value pairs using regular expressions:\n\n"
								 "    %1 import hostlist.txt room \"Room 01\" regex \"^NAME:(%name%:.*)\\s+HOST:(%host%:.*)$\"\n\n"
								 "* Import arbitrarily formatted data:\n\n"
								 "    %1 import data.txt regex '^\"(%room%:[^\"]+)\";\"(%host%:[a-z\\d\\.]+)\".*$'\n").
							  arg( commandLineModuleName() ) );

		return NoResult;
	}
	else if( command == QStringLiteral("export") )
	{
		CommandLineIO::print( tr("\nUSAGE\n\n%1 export <FILE> [room <ROOM>] [format <FORMAT-STRING-WITH-VARIABLES>]\n\n"
								 "Valid variables: %type% %name% %host% %mac% %room%\n\n"
								 "Examples:\n\n"
								 "* Export all objects to a CSV file:\n\n"
								 "    %1 export objects.csv format \"%type%;%name%;%host%;%mac%\"\n\n"
								 "* Export all computers in a room to a CSV file:\n\n"
								 "    %1 export computers.csv room \"Room 01\" format \"%name%;%host%;%mac%\"\n\n").
							  arg( commandLineModuleName() ) );

		return NoResult;
	}
	else if( command == QStringLiteral("add") )
	{
		CommandLineIO::print( tr("\nUSAGE\n\n%1 add <TYPE> <NAME> [<HOST ADDRESS> <MAC ADDRESS> <PARENT>]\n\n"
								 "Adds an object where TYPE can be one of \"%2\" or \"%3\". PARENT can be specified by name or UUID.\n\n"
								 "Examples:\n\n"
								 "* Add a room:\n\n"
								 "    %1 add room \"Room 01\"\n\n"
								 "* Add a computer to room \"Room 01\":\n\n"
								 "    %1 add computer \"Computer 01\" comp01.example.com 11:22:33:44:55:66 \"Room 01\"\n\n").
							  arg( commandLineModuleName(), QStringLiteral("room"), QStringLiteral("computer") ) );

		return NoResult;
	}
	else if( command == QStringLiteral("remove") )
	{
		CommandLineIO::print( tr("\nUSAGE\n\n%1 remove <OBJECT>\n\n"
								 "Removes the specified object from the directory. OBJECT can be specified by name or UUID. "
								 "Removing a room will also remove all computers inside.\n\n"
								 "Examples:\n\n"
								 "* Remove a computer by name:\n\n"
								 "    %1 remove \"Computer 01\"\n\n"
								 "* Remove an object by UUID:\n\n"
								 "    %1 remove 068914fc-0f87-45df-a5b9-099a2a6d9141\n\n").
							  arg( commandLineModuleName(), QStringLiteral("room"), QStringLiteral("computer") ) );

		return NoResult;
	}

	return Unknown;
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_add( const QStringList& arguments )
{
	if( arguments.count() < 2 )
	{
		return NotEnoughArguments;
	}

	NetworkObject object;

	const auto type = arguments[0];
	const auto name = arguments[1];

	if( type == QStringLiteral("room") )
	{
		object = NetworkObject( NetworkObject::Group, name );
	}
	else if( type == QStringLiteral("computer") )
	{
		auto hostAddress = arguments.value( 2 );
		if( hostAddress.isEmpty() )
		{
			hostAddress = name;
		}
		const auto macAddress = arguments.value( 3 );
		const auto parent = findNetworkObject( arguments.value( 4 ) );
		object = NetworkObject( NetworkObject::Host, name, hostAddress, macAddress,
								QString(), NetworkObject::Uid(), parent.isValid() ? parent.uid() : NetworkObject::Uid() );
	}
	else
	{
		CommandLineIO::error( tr("Invalid type specified. Valid values are \"%1\" or \"%2\"." ).
							  arg( QStringLiteral("computer"), QStringLiteral("room") ) );
		return Failed;
	}

	ObjectManager<NetworkObject> objectManager( m_configuration.networkObjects() );
	objectManager.add( object );
	m_configuration.setNetworkObjects( objectManager.objects() );

	return saveConfiguration();
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_clear( const QStringList& arguments )
{
	Q_UNUSED(arguments);

	m_configuration.setNetworkObjects( {} );

	return saveConfiguration();
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_dump( const QStringList& arguments )
{
	CommandLineIO::TableHeader tableHeader( { tr("Object UUID"), tr("Parent UUID"), tr("Type"), tr("Name"), tr("Host address"), tr("MAC address") } );
	CommandLineIO::TableRows tableRows;

	const auto objects = m_configuration.networkObjects();

	tableRows.reserve( objects.size() );

	if( arguments.isEmpty() )
	{
		for( const auto& networkObjectValue : objects )
		{
			tableRows.append( dumpNetworkObject( networkObjectValue.toObject() ) );
		}
	}
	else
	{
		tableRows.append( dumpNetworkObject( findNetworkObject( arguments.first() ) ) );
	}

	CommandLineIO::printTable( CommandLineIO::Table( tableHeader, tableRows ) );

	return NoResult;
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_list( const QStringList& arguments )
{
	if( arguments.isEmpty() )
	{
		listObjects( m_configuration.networkObjects(), NetworkObject::None );
	}
	else
	{
		const auto parents = BuiltinDirectory( m_configuration, this ).queryObjects( NetworkObject::Group, arguments.first() );

		for( const auto& parent : parents )
		{
			listObjects( m_configuration.networkObjects(), parent );
		}
	}

	return NoResult;
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_remove( const QStringList& arguments )
{
	if( arguments.isEmpty() )
	{
		return NotEnoughArguments;
	}

	const auto object = findNetworkObject( arguments.first() );

	if( object.isValid() )
	{
		ObjectManager<NetworkObject> objectManager( m_configuration.networkObjects() );
		objectManager.remove( object, true );
		m_configuration.setNetworkObjects( objectManager.objects() );

		return saveConfiguration();
	}

	CommandLineIO::error( tr( "Specified object not found." ) );

	return Failed;
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_import( const QStringList& arguments )
{
	if( arguments.count() < 3 )
	{
		return NotEnoughArguments;
	}

	const auto inputFileName = arguments.first();
	QFile inputFile( inputFileName );

	if( inputFile.exists() == false )
	{
		CommandLineIO::error( tr( "File \"%1\" does not exist!" ).arg( inputFileName ) );
		return Failed;
	}
	else if( inputFile.open( QFile::ReadOnly | QFile::Text ) == false )
	{
		CommandLineIO::error( tr( "Can't open file \"%1\" for reading!" ).arg( inputFileName ) );
		return Failed;
	}

	QString room;
	QString formatString;
	QString regularExpression;

	for( int i = 1; i < arguments.count(); i += 2 )
	{
		if( i+1 >= arguments.count() )
		{
			return NotEnoughArguments;
		}

		const auto key = arguments[i];
		const auto value = arguments[i+1];
		if( key == QStringLiteral("room") )
		{
			room = value;
		}
		else if( key == QStringLiteral("format") )
		{
			formatString = value;
		}
		else if( key == QStringLiteral("regex") )
		{
			regularExpression = value;
		}
		else
		{
			CommandLineIO::error( tr( "Unknown argument \"%1\"." ).arg( key ) );
			return InvalidArguments;
		}
	}

	if( formatString.isEmpty() == false )
	{
		regularExpression = formatString;

		const auto variables = fileImportVariables();

		for( const auto& var : variables )
		{
			regularExpression.replace( var, QStringLiteral("(%1:[^\\n\\r]*)").arg( var ) );
		}
	}

	if( regularExpression.isEmpty() == false )
	{
		if( importFile( inputFile, regularExpression, room ) )
		{
			return saveConfiguration();
		}

		return Failed;
	}

	CommandLineIO::error( tr("No format string or regular expression specified!") );

	return InvalidArguments;
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::handle_export( const QStringList& arguments )
{
	if( arguments.count() < 3 )
	{
		return NotEnoughArguments;
	}

	const auto outputFileName = arguments.first();
	QFile outputFile( outputFileName );

	if( outputFile.open( QFile::WriteOnly | QFile::Truncate | QFile::Text ) == false )
	{
		CommandLineIO::error( tr( "Can't open file \"%1\" for writing!" ).arg( outputFileName ) );
		return Failed;
	}

	QString room;
	QString formatString;

	for( int i = 1; i < arguments.count(); i += 2 )
	{
		if( i+1 >= arguments.count() )
		{
			return NotEnoughArguments;
		}

		const auto key = arguments[i];
		const auto value = arguments[i+1];
		if( key == QStringLiteral("room") )
		{
			room = value;
		}
		else if( key == QStringLiteral("format") )
		{
			formatString = value;
		}
		else
		{
			CommandLineIO::error( tr( "Unknown argument \"%1\"." ).arg( key ) );
			return InvalidArguments;
		}
	}

	if( formatString.isEmpty() == false )
	{
		if( exportFile( outputFile, formatString, room ) )
		{
			return Successful;
		}

		return Failed;
	}

	CommandLineIO::error( tr("No format string specified!") );

	return InvalidArguments;
}



void BuiltinDirectoryPlugin::listObjects( const QJsonArray& objects, const NetworkObject& parent )
{
	for( const auto& networkObjectValue : objects )
	{
		const NetworkObject networkObject( networkObjectValue.toObject() );

		if( ( parent.type() == NetworkObject::None && networkObject.parentUid().isNull() ) ||
				networkObject.parentUid() == parent.uid() )
		{
			printf( "%s\n", qUtf8Printable( listNetworkObject( networkObject ) ) );
			listObjects( objects, networkObject );
		}
	}
}



QStringList BuiltinDirectoryPlugin::dumpNetworkObject( const NetworkObject& object )
{
	return { VeyonCore::formattedUuid( object.uid() ),
				VeyonCore::formattedUuid( object.parentUid() ),
				networkObjectTypeName( object ),
				object.name(),
				object.hostAddress(),
				object.macAddress() };
}



QString BuiltinDirectoryPlugin::listNetworkObject( const NetworkObject& object )
{
	switch( object.type() )
	{
	case NetworkObject::Group:
		return tr( "Room \"%1\"" ).arg( object.name() );
	case NetworkObject::Host:
		return QChar('\t') +
				tr( "Computer \"%1\" (host address: \"%2\" MAC address: \"%3\")" ).
				arg( object.name() ).
				arg( object.hostAddress() ).
				arg( object.macAddress() );
	default:
		break;
	}

	return tr( "Unclassified object \"%1\" with ID \"%2\"" ).arg( object.name() ).arg( object.uid().toString() );
}



QString BuiltinDirectoryPlugin::networkObjectTypeName( const NetworkObject& object )
{
	switch( object.type() )
	{
	case NetworkObject::None: return tr( "None" );
	case NetworkObject::Group: return tr( "Room" );
	case NetworkObject::Host: return tr( "Computer" );
	case NetworkObject::Root: return tr( "Root");
	default:
		break;
	}

	return tr( "Invalid" );
}



CommandLinePluginInterface::RunResult BuiltinDirectoryPlugin::saveConfiguration()
{
	ConfigurationManager configurationManager;
	if( configurationManager.saveConfiguration() == false )
	{
		CommandLineIO::error( configurationManager.errorString() );
		return Failed;
	}

	return Successful;
}



bool BuiltinDirectoryPlugin::importFile( QFile& inputFile,
										 const QString& regExWithVariables,
										 const QString& room )
{
	int lineCount = 0;
	QMap<QString, QList<NetworkObject> > networkObjects;
	while( inputFile.atEnd() == false )
	{
		++lineCount;

		QString targetRoom = room;

		const auto line = inputFile.readLine();
		const auto networkObject = toNetworkObject( line, regExWithVariables, targetRoom );

		if( networkObject.isValid() )
		{
			networkObjects[targetRoom].append( networkObject );
		}
		else
		{
			CommandLineIO::error( tr( "Error while parsing line %1." ).arg( lineCount ) );
			return false;
		}
	}

	ObjectManager<NetworkObject> objectManager( m_configuration.networkObjects() );

	for( auto it = networkObjects.constBegin(), end = networkObjects.constEnd(); it != end; ++it )
	{
		auto parentRoom = objectManager.findByName( it.key() );
		if( parentRoom.isValid() == false )
		{
			parentRoom = NetworkObject( NetworkObject::Group, it.key() );
			objectManager.add( parentRoom );
		}

		for( const NetworkObject& networkObject : qAsConst(it.value()) )
		{
			objectManager.add( NetworkObject( networkObject.type(),
											  networkObject.name(),
											  networkObject.hostAddress(),
											  networkObject.macAddress(),
											  QString(), NetworkObject::Uid(),
											  parentRoom.uid() ) );
		}
	}

	m_configuration.setNetworkObjects( objectManager.objects() );

	return true;
}



bool BuiltinDirectoryPlugin::exportFile( QFile& outputFile, const QString& formatString, const QString& room )
{
	ObjectManager<NetworkObject> objectManager( m_configuration.networkObjects() );

	const auto networkObjects = objectManager.objects();

	NetworkObject roomObject;
	if( room.isEmpty() == false )
	{
		roomObject = objectManager.findByName( room );
	}

	QStringList lines;
	lines.reserve( networkObjects.count() );

	for( auto it = networkObjects.constBegin(), end = networkObjects.constEnd(); it != end; ++it )
	{
		const NetworkObject networkObject( it->toObject() );

		QString currentRoom = room;

		if( roomObject.isValid() )
		{
			if( networkObject.parentUid() != roomObject.uid() )
			{
				continue;
			}
		}
		else
		{
			currentRoom = objectManager.findByUid( networkObject.parentUid() ).name();
		}

		lines.append( toFormattedString( networkObject, formatString, currentRoom ) );
	}

	// append empty string to generate final newline at end of file
	lines += QString();

	outputFile.write( lines.join( QStringLiteral("\r\n") ).toUtf8() );

	return true;
}



NetworkObject BuiltinDirectoryPlugin::findNetworkObject( const QString& uidOrName ) const
{
	const ObjectManager<NetworkObject> objectManager( m_configuration.networkObjects() );

	if( QUuid( uidOrName ).isNull() )
	{
		return objectManager.findByName( uidOrName );
	}

	return objectManager.findByUid( uidOrName );
}



NetworkObject BuiltinDirectoryPlugin::toNetworkObject( const QString& line, const QString& regExWithVariables, QString& room )
{
	QStringList variables;
	QRegExp varDetectionRX( "\\((%\\w+%):[^)]+\\)" );
	int pos = 0;

	while( ( pos = varDetectionRX.indexIn( regExWithVariables, pos ) ) != -1 )
	{
		variables.append( varDetectionRX.cap(1) );
		pos += varDetectionRX.matchedLength();
	}

	QString rxString = regExWithVariables;
	for( const auto& var : qAsConst(variables) )
	{
		rxString.replace( QStringLiteral("%1:").arg( var ), QString() );
	}

	QRegExp rx( rxString );
	if( rx.indexIn( line ) != -1 )
	{
		const auto roomIndex = variables.indexOf( QStringLiteral("%room%") );
		const auto nameIndex = variables.indexOf( QStringLiteral("%name%") );
		const auto hostIndex = variables.indexOf( QStringLiteral("%host%") );
		const auto macIndex = variables.indexOf( QStringLiteral("%mac%") );

		if( room.isEmpty() && roomIndex != -1 )
		{
			room = rx.cap( 1 + roomIndex ).trimmed();
		}
		auto name = ( nameIndex != -1 ) ? rx.cap( 1 + nameIndex ).trimmed() : QString();
		auto host = ( hostIndex != -1 ) ? rx.cap( 1 + hostIndex ).trimmed() : QString();
		auto mac = ( macIndex != -1 ) ? rx.cap( 1 + macIndex ).trimmed() : QString();

		if( host.isEmpty() )
		{
			host = name;
		}
		else if( name.isEmpty() )
		{
			name = host;
		}
		return NetworkObject( NetworkObject::Host, name, host, mac );
	}

	return NetworkObject::None;
}



QString BuiltinDirectoryPlugin::toFormattedString( const NetworkObject& networkObject,
												   const QString& formatString,
												   const QString& room )
{
	return QString( formatString ).
			replace( QStringLiteral("%room%"), room ).
			replace( QStringLiteral("%name%"), networkObject.name() ).
			replace( QStringLiteral("%host%"), networkObject.hostAddress() ).
			replace( QStringLiteral("%mac%"), networkObject.macAddress() ).
			replace( QStringLiteral("%type%"), networkObjectTypeName( networkObject ) );
}



QStringList BuiltinDirectoryPlugin::fileImportVariables()
{
	return { "%room%", "%name%", "%host%", "%mac%" };
}