1 // Copyright 2009 The Archiveopteryx Developers <info@aox.org>
2 
3 #ifndef AOXCOMMAND_H
4 #define AOXCOMMAND_H
5 
6 #include "event.h"
7 #include "ustring.h"
8 
9 #include <string.h> // strcmp
10 
11 
12 class EStringList;
13 
14 
15 class AoxCommand
16     : public EventHandler
17 {
18 public:
19     AoxCommand( EStringList * );
20 
21     bool done() const;
22     int status() const;
23 
24     static AoxCommand * create( EStringList * );
25 
26 protected:
27     EString next();
28     EStringList * args();
29     class Address * nextAsAddress();
30     void setopt( char );
31     uint opt( char );
32     void parseOptions();
33     void end();
34     void database( bool = false );
35     void error( const EString & );
36     void finish( int = 0 );
37     UString sqlPattern( const UString & );
38     bool validUsername( const UString & );
39     bool choresDone();
40     EString readPassword( const EString & );
41     EString readNewPassword();
42 
43 private:
44     class AoxCommandData * d;
45 };
46 
47 
48 class AoxCommandMap
49 // NOT a Garbage inheritor. created early, not on the heap
50 {
51 public:
AoxCommandMap(const char * verb,const char * noun,const char * brief,const char * about)52     AoxCommandMap( const char * verb, const char * noun,
53                    const char * brief, const char * about )
54         : v( verb ), n( noun ), b( brief), a( about ), x( 0 ), c( 0 ) {
55         x = first;
56         first = this;
57     }
AoxCommandMap(const char * verb,const char * noun,AoxCommandMap * canonical)58     AoxCommandMap( const char * verb, const char * noun,
59                    AoxCommandMap * canonical )
60         : v( verb ), n( noun ),
61           b( canonical->b ), a( canonical->a ),
62           x( 0 ), c( canonical ) {
63         x = first;
64         first = this;
65     }
~AoxCommandMap()66     virtual ~AoxCommandMap() {}
67 
68     static AoxCommand * provide( const EString &, const EString &,
69                                  EStringList * );
70 
71     virtual AoxCommand * provide( EStringList * ) = 0;
72 
73     static EStringList * validVerbs();
74     static EStringList * validNouns( const EString & );
75     static EString aboutCommand( const EString &, const EString & );
76     static EString inBrief( const EString &, const EString & );
77     static bool needsNoun( const EString & );
78 
79     static EStringList * aliases();
80 
81 private:
82     const char * v;
83     const char * n;
84     const char * b;
85     const char * a;
86 
87     AoxCommandMap * x;
88 
89     AoxCommandMap * c;
90 
91     static AoxCommandMap * first;
92 };
93 
94 
95 
96 template<class T>
97 class AoxFactory
98     : public AoxCommandMap
99 {
100 public:
AoxFactory(const char * verb,const char * noun,const char * brief,const char * about)101     AoxFactory( const char * verb, const char * noun,
102                 const char * brief, const char * about )
103         : AoxCommandMap( verb, noun, brief, about ) {
104         if ( !strcmp( verb, "create" ) ) {
105             (void)new AoxFactory<T>( "add", noun, this );
106             (void)new AoxFactory<T>( "new", noun, this );
107         }
108         else if ( !strcmp( verb, "delete" ) ) {
109             (void)new AoxFactory<T>( "del", noun, this );
110             (void)new AoxFactory<T>( "remove", noun, this );
111         }
112         else if ( !strcmp( verb, "list" ) ) {
113             (void)new AoxFactory<T>( "ls", noun, this );
114         }
115     }
AoxFactory(const char * verb,const char * noun,AoxFactory<T> * canonical)116     AoxFactory( const char * verb, const char * noun,
117                 AoxFactory<T> * canonical )
118         : AoxCommandMap( verb, noun, canonical ) {}
provide(EStringList * l)119     AoxCommand * provide( EStringList * l ) { return new T( l ); }
120 };
121 
122 
123 #endif
124