~ubuntu-branches/ubuntu/quantal/lurker/quantal

« back to all changes in this revision

Viewing changes to index/list.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Meurer
  • Date: 2004-09-26 16:27:51 UTC
  • Revision ID: james.westby@ubuntu.com-20040926162751-z1ohcjltv7ojtg6z
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $Id: list.cpp,v 1.8 2004/08/27 15:04:05 terpstra Exp $
 
2
 *  
 
3
 *  list.cpp - Parse the config file for helper scripts
 
4
 *  
 
5
 *  Copyright (C) 2002 - Wesley W. Terpstra
 
6
 *  
 
7
 *  License: GPL
 
8
 *  
 
9
 *  Authors: 'Wesley W. Terpstra' <wesley@terpstra.ca>
 
10
 *  
 
11
 *    This program is free software; you can redistribute it and/or modify
 
12
 *    it under the terms of the GNU General Public License as published by
 
13
 *    the Free Software Foundation; version 2.
 
14
 *    
 
15
 *    This program is distributed in the hope that it will be useful,
 
16
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 *    GNU General Public License for more details.
 
19
 *    
 
20
 *    You should have received a copy of the GNU General Public License
 
21
 *    along with this program; if not, write to the Free Software
 
22
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
 
 
25
#define _XOPEN_SOURCE 500
 
26
#define _FILE_OFFSET_BITS 64
 
27
 
 
28
#include <ConfigFile.h>
 
29
 
 
30
#include <iostream>
 
31
#include <vector>
 
32
 
 
33
#include <unistd.h>
 
34
 
 
35
using namespace std;
 
36
 
 
37
void help(const char* name)
 
38
{
 
39
        cerr << "Lurker-list (v" << VERSION << ") parses lists from the config file.\n";
 
40
        cerr << "\n";
 
41
        cerr << "Usage: " << name << " -c <config-file> -f <locale>\n";
 
42
        cerr << "                         [-i -g -t -a -l -d -s -o] [listid ...]\n";
 
43
        cerr << "\n";
 
44
        cerr << "\t-c <config-file> Use this config file for lurker settings\n";
 
45
        cerr << "\t-f <locale>      Output the fields for this locale\n";
 
46
        cerr << "\t-i               Output only the list id\n";
 
47
        cerr << "\t-g               Output only the group\n";
 
48
        cerr << "\t-t               Output only the title\n";
 
49
        cerr << "\t-a               Output only the email address\n";
 
50
        cerr << "\t-l               Output only the url link\n";
 
51
        cerr << "\t-d               Output only the description\n";
 
52
        cerr << "\t-s               Output only the language\n";
 
53
        cerr << "\t-o               Output only the offline status\n";
 
54
        cerr << "\n";
 
55
        cerr << "Output various lurker settings from the config file for use in shell scripts.\n";
 
56
        cerr << "Do not use sed/grep/etc, instead use this as it respects include.\n";
 
57
        cerr << "\n";
 
58
}
 
59
 
 
60
int main(int argc, char** argv)
 
61
{
 
62
        int c;
 
63
        
 
64
        const char* config   = 0;
 
65
        int         fields   = 0;
 
66
        bool        ids      = false;
 
67
        bool        group    = false;
 
68
        bool        title    = false;
 
69
        bool        address  = false;
 
70
        bool        link     = false;
 
71
        bool        desc     = false;
 
72
        bool        language = false;
 
73
        bool        offline  = false;
 
74
        string lc;
 
75
        
 
76
        while ((c = getopt(argc, (char*const*)argv, "c:f:igtaldso?")) != -1)
 
77
        {
 
78
                switch ((char)c)
 
79
                {
 
80
                case 'c':
 
81
                        config = optarg;
 
82
                        break;
 
83
                case 'f':
 
84
                        lc = optarg;
 
85
                        break;
 
86
                case 'i':
 
87
                        ++fields;
 
88
                        ids = true;
 
89
                        break;
 
90
                case 'g':
 
91
                        ++fields;
 
92
                        group = true;
 
93
                        break;
 
94
                case 't':
 
95
                        ++fields;
 
96
                        title = true;
 
97
                        break;
 
98
                case 'a':
 
99
                        ++fields;
 
100
                        address = true;
 
101
                        break;
 
102
                case 'l':
 
103
                        ++fields;
 
104
                        link = true;
 
105
                        break;
 
106
                case 'd':
 
107
                        ++fields;
 
108
                        desc = true;
 
109
                        break;
 
110
                case 's':
 
111
                        ++fields;
 
112
                        language = true;
 
113
                        break;
 
114
                case 'o':
 
115
                        ++fields;
 
116
                        offline = true;
 
117
                        break;
 
118
                default:
 
119
                        help(argv[0]);
 
120
                        return 1;
 
121
                }
 
122
        }
 
123
        
 
124
        if (!config)
 
125
        {
 
126
                help(argv[0]);
 
127
                return 1;
 
128
        }
 
129
        
 
130
        if (fields >= 2)
 
131
        {
 
132
                cerr << "Please either retrieval all fields or exactly one.\n";
 
133
                return 1;
 
134
        }
 
135
        
 
136
        Config cfg;
 
137
        if (cfg.load(config) != 0)
 
138
        {
 
139
                cerr << cfg.getError() << flush;
 
140
                return 1;
 
141
        }
 
142
        
 
143
        vector<List> output;
 
144
        for (; optind < argc; ++optind)
 
145
        {
 
146
                Config::Lists::const_iterator l = cfg.lists.find(argv[optind]);
 
147
                if (l == cfg.lists.end())
 
148
                {
 
149
                        cerr << "'" << argv[optind] << "' is not a mailing list id.\n";
 
150
                        return 1;
 
151
                }
 
152
                output.push_back(l->second);
 
153
        }
 
154
        
 
155
        if (output.empty())
 
156
        {
 
157
                Config::Lists::const_iterator l;
 
158
                for (l = cfg.lists.begin(); l != cfg.lists.end(); ++l)
 
159
                        output.push_back(l->second);
 
160
        }
 
161
        
 
162
        if (lc != "" && !lstring::locale_normalize(lc))
 
163
        {
 
164
                cerr << "'" << lc << "' is not a valid locale\n";
 
165
                return 1;
 
166
        }
 
167
        
 
168
        vector<List>::const_iterator o;
 
169
        for (o = output.begin(); o != output.end(); ++o)
 
170
        {
 
171
                if (!fields || ids)     cout << o->mbox            << "\n";
 
172
                if (!fields || group)   cout << o->group           << "\n";
 
173
                if (!fields || title)   cout << o->title(lc)       << "\n";
 
174
                if (!fields || address) cout << o->address         << "\n";
 
175
                if (!fields || link)    cout << o->link(lc)        << "\n";
 
176
                if (!fields || desc)    cout << o->description(lc) << "\n";
 
177
                if (!fields || language)cout << o->language        << "\n";
 
178
                if (!fields || offline) cout << (o->offline?"true\n":"false\n");
 
179
        }
 
180
        
 
181
        return 0;
 
182
}