~ubuntu-branches/ubuntu/wily/steam/wily

« back to all changes in this revision

Viewing changes to server/modules/types.pike

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (1.1.4) (0.1.4 trusty-proposed)
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
3
 
 *  This program is free software; you can redistribute it and/or modify
4
 
 *  it under the terms of the GNU General Public License as published by
5
 
 *  the Free Software Foundation; either version 2 of the License, or
6
 
 *  (at your option) any later version.
7
 
 *
8
 
 *  This program is distributed in the hope that it will be useful,
9
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 *  GNU General Public License for more details.
12
 
 *
13
 
 *  You should have received a copy of the GNU General Public License
14
 
 *  along with this program; if not, write to the Free Software
15
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
 
 * 
17
 
 * $Id: types.pike,v 1.1.1.1 2006/03/27 12:40:12 exodusd Exp $
18
 
 */
19
 
 
20
 
constant cvs_version="$Id: types.pike,v 1.1.1.1 2006/03/27 12:40:12 exodusd Exp $";
21
 
 
22
 
inherit "/kernel/module";
23
 
inherit "/base/xml_parser";
24
 
inherit "/base/xml_data";
25
 
 
26
 
#include <macros.h>
27
 
#include <database.h>
28
 
#include <classes.h>
29
 
#include <assert.h>
30
 
 
31
 
//! This module reads some configuration files located in the
32
 
//! servers config/ directory and stores information about all
33
 
//! mime types and what sTeam class to create for a specific mime type.
34
 
 
35
 
#define MIME_FILE _Server->query_config("config-dir")+"mimetypes.txt"
36
 
#define CLASS_FILE _Server->query_config("config-dir")+"classtypes.txt"
37
 
 
38
 
#define DATA_TYPE 0
39
 
#define DATA_MIME 1
40
 
#define DATA_DESC 2
41
 
#define DATA_PRIO 3
42
 
#define DATA_EXT  4
43
 
 
44
 
private mapping mMime;
45
 
private mapping mExts;
46
 
private array asDefaults = ({ "text/plain", "text/html", "text/wiki", "application/msword", "application/vnd.ms-powerpoint", "image/gif", "image/jpeg", "image/png", "application/pdf" });
47
 
                                 
48
 
 
49
 
 
50
 
static void init_module()
51
 
{
52
 
    init_mime();
53
 
}
54
 
 
55
 
/**
56
 
 * Register a new class with a filename to create and a mimetype.
57
 
 * This will change the content of the CLASS_FILE.
58
 
 *  
59
 
 * @param string cl - the class to register
60
 
 * @param string doc_class - the class to clone
61
 
 * @param string mime - the mimetype
62
 
 * @param string desc - the description for this document class.
63
 
 * @author Thomas Bopp (astra@upb.de) 
64
 
 */
65
 
static void 
66
 
register_class(string cl, string doc_class, string mime, string desc)
67
 
{
68
 
    if ( !_SECURITY->access_register_class(0, 0, CALLER) )
69
 
        return;
70
 
    mMime[cl] = ({ doc_class, mime, desc });
71
 
    Stdio.File f = Stdio.File(CLASS_FILE, "wct");
72
 
    f->write("<classes>\n");
73
 
    foreach(indices(mMime), string ext) {
74
 
        f->write("\t<type>\n\t\t<ext>"+ext+"</ext>\n\t\t<class>"+
75
 
                 mMime[ext][DATA_TYPE]+"</class>\n\t\t<desc>"+
76
 
                 mMime[ext][DATA_DESC]+"</desc>\n\t</type>\n");
77
 
    }
78
 
    f->write("</classes>\n");
79
 
    f->close();
80
 
}
81
 
 
82
 
/**
83
 
 * query_document_type, returns an array of document type information 
84
 
 * depending on the given filename.
85
 
 *  
86
 
 * @param filename - the filename
87
 
 * @return the document-type of that file (depends only on extension)
88
 
 * @author Thomas Bopp 
89
 
 * @see query_mime_type
90
 
 */
91
 
array(string)
92
 
query_document_type(string filename)
93
 
{
94
 
    string base_type, file_extension;
95
 
    array(string)              index;
96
 
    array(string)             ploder;
97
 
    int                            i;
98
 
 
99
 
    LOG("query_document_type()");
100
 
    if ( !mappingp(mMime) )
101
 
        init_mime();
102
 
    
103
 
    /* initialization */
104
 
    base_type      = "";
105
 
    file_extension = "";
106
 
    
107
 
    ploder = filename / "."; /* explode, hmm */
108
 
    if ( !sizeof(ploder) )
109
 
        return ({ "Document", "gen" });
110
 
 
111
 
    file_extension = ploder[sizeof(ploder)-1];
112
 
 
113
 
    for ( i = strlen(file_extension) - 1; i >= 0; i-- )
114
 
        if ( file_extension[i] >= 'A' && file_extension[i] <= 'Z' )
115
 
            file_extension[i] -= ('A' - 'a');
116
 
    file_extension = lower_case(file_extension);
117
 
    LOG("File Extension:" + file_extension);
118
 
    if ( arrayp(mMime[file_extension]) )
119
 
    {
120
 
        return ({ mMime[file_extension][DATA_TYPE], 
121
 
                      file_extension });
122
 
    }
123
 
 
124
 
    return ({ "Document", file_extension });
125
 
}
126
 
 
127
 
/**
128
 
 * Returns the documents extensions for a given mimetype.
129
 
 *  
130
 
 * @param string mtype - the mimetype
131
 
 * @return array of extensions for this mime type with information
132
 
 */
133
 
array query_document_type_mime(string mtype)
134
 
{
135
 
    if ( arrayp(mExts[mtype]) )
136
 
        return ({ mExts[mtype][DATA_TYPE], "" });
137
 
    else
138
 
        return ({ "Document", "" });
139
 
}
140
 
 
141
 
/**
142
 
 * Return the array of possible extensions for the given mimetype.
143
 
 *  
144
 
 * @param string mtype - the mimetype
145
 
 * @return array of extensions for mtype
146
 
 */
147
 
array query_mimetype_extensions(string mtype)
148
 
{
149
 
    array extensions = ({ });
150
 
    if ( !arrayp(mExts[mtype]) )
151
 
        return extensions;
152
 
 
153
 
    foreach( mExts[mtype], mixed ext ) {
154
 
        extensions += ({ ext[DATA_EXT] });
155
 
    }
156
 
    return extensions;
157
 
}
158
 
 
159
 
void 
160
 
register_extensions(string mtype, string desc, array extensions, void|int def)
161
 
{
162
 
    int i = 0;
163
 
    if ( mExts[mtype] )
164
 
        error("Cannot reregister extension !");
165
 
    mExts[mtype] = extensions;
166
 
 
167
 
    foreach(extensions, string ext) {
168
 
        i++;
169
 
        mMime[ext] = ({ "Document", mtype, desc, i, ext });
170
 
    }
171
 
    if ( def && search(asDefaults, mtype) == -1 )
172
 
        asDefaults += ({ mtype });
173
 
}
174
 
 
175
 
array(string) get_default_mimetypes()
176
 
{
177
 
    return copy_value(asDefaults);
178
 
}
179
 
 
180
 
/**
181
 
 * Return the mimetype for a given extension.
182
 
 *  
183
 
 * @param doc - the document extension
184
 
 * @return the mime-type of the document
185
 
 * @author Thomas Bopp 
186
 
 * @see query_document_type
187
 
 */
188
 
string
189
 
query_mime_type(string doc)
190
 
{
191
 
    if ( ! stringp( doc ) )
192
 
        return "application/x-unknown-content-type";
193
 
 
194
 
    array(mixed) data;
195
 
 
196
 
    if ( !mappingp(mMime) )
197
 
        init_mime();
198
 
 
199
 
    data = mMime[lower_case(doc)];
200
 
    if ( !arrayp(data) )
201
 
        return "application/x-unknown-content-type";
202
 
    
203
 
    return data[DATA_MIME];
204
 
}
205
 
 
206
 
/**
207
 
 * Return the mime description for an extension.
208
 
 *  
209
 
 * @param doc - the document extension
210
 
 * @return the description of the mime-type
211
 
 * @author Thomas Bopp 
212
 
 * @see query_mime_type
213
 
 */
214
 
string
215
 
query_mime_desc(string doc)
216
 
{
217
 
    array(mixed) data;
218
 
 
219
 
    if ( !mappingp(mMime) )
220
 
        init_mime();
221
 
 
222
 
    data = mMime[lower_case(doc)];
223
 
    if ( !arrayp(data) )
224
 
        return "Generic";
225
 
    
226
 
    return data[DATA_DESC];
227
 
}
228
 
 
229
 
/**
230
 
 * init_mime
231
 
 *  
232
 
 * @author Thomas Bopp 
233
 
 * @see query_types
234
 
 */
235
 
nomask void
236
 
init_mime()
237
 
{
238
 
    string type, mimeType, baseType, desc;
239
 
    array(string)                   lines;
240
 
    array(string)                  tokens;
241
 
    string                            buf;
242
 
    int                  start, i, j, len;
243
 
    mixed                            data;
244
 
 
245
 
    buf = Stdio.read_file(CLASS_FILE);
246
 
 
247
 
    ASSERTINFO(stringp(buf),
248
 
               "Initialization error: class file missing ["+CLASS_FILE+"]\n");
249
 
 
250
 
    //lines = buf / "\n";
251
 
    mMime = ([ ]);
252
 
    NodeXML n = parse_data(buf);
253
 
    foreach(n->children, NodeXML type) {
254
 
        NodeXML t = type->get_node("ext");
255
 
        mMime[t->data] = ({ type->get_node("class")->data,
256
 
                            "application/x-unknown-content-type",
257
 
                            type->get_node("desc")->data, 0 });
258
 
    }
259
 
 
260
 
    // read the mimetypes.txt 
261
 
    buf = Stdio.read_file(MIME_FILE);
262
 
    if ( !stringp(buf) ) {
263
 
        FATAL("Initialization error: mime file missing! ["+MIME_FILE +"]\n");
264
 
        return;
265
 
    }
266
 
    lines = buf / "\n";
267
 
    
268
 
    for ( i = sizeof(lines) - 1; i >= 0; i-- ) {
269
 
        len = strlen(lines[i]);
270
 
        if ( len == 0 || lines[i][0] == '#' )
271
 
            continue;
272
 
        j = 0; start = -1; tokens = ({ });
273
 
        while ( j < len ) {
274
 
            if ( lines[i][j] == ' ' || lines[i][j] == '\t' ) {
275
 
                if ( start != -1 ) {
276
 
                    tokens += ({ lines[i][start..j-1] });
277
 
                }
278
 
                start = -1;
279
 
            }
280
 
            else if ( start == -1 ) {
281
 
                start = j;
282
 
            }
283
 
            j++;
284
 
        }
285
 
        if ( start != -1 )
286
 
            tokens += ({ lines[i][start..] });
287
 
 
288
 
        if ( sizeof(tokens) > 1 ) {
289
 
            for ( j = sizeof(tokens) - 1; j >= 1; j-- ) {
290
 
                data = mMime[tokens[j]];
291
 
                
292
 
                if ( arrayp(data) ) {
293
 
                    // remember the priority of extensions
294
 
                    // html htm wiki, then html should have priority (lowest j)
295
 
                    mMime[tokens[j]][DATA_MIME] = tokens[0];
296
 
                    mMime[tokens[j]][DATA_PRIO] = j;
297
 
                }
298
 
                else
299
 
                    mMime[tokens[j]] = 
300
 
                        ({ "Document", tokens[0], "Generic", j });
301
 
            }
302
 
        }
303
 
    }
304
 
    mExts = ([ ]);
305
 
    foreach ( indices(mMime), string ext ) {
306
 
        // mExts mapping holds mimetype:data
307
 
        if ( arrayp(mExts[mMime[ext][DATA_MIME]]) )
308
 
            mExts[mMime[ext][DATA_MIME]] += ({ mMime[ext]+({ ext }) });
309
 
        else {
310
 
            mExts[mMime[ext][DATA_MIME]]  = ({ mMime[ext]+({ ext })  });
311
 
        }
312
 
    }
313
 
    foreach ( indices(mExts), string mtype ) {
314
 
        if ( sizeof(mExts[mtype]) > 1 ) {
315
 
            array sorter = ({ });
316
 
            foreach(mExts[mtype], mixed ext)
317
 
                sorter += ({ ext[DATA_PRIO] });
318
 
            sort(mExts[mtype], sorter);
319
 
        }
320
 
    }
321
 
}
322
 
 
323
 
/**
324
 
 * Return the whole mapping of saved types. The mapping contains
325
 
 * file extension: informations (array)
326
 
 *  
327
 
 * @return the mapping with all type definitions
328
 
 * @author Thomas Bopp 
329
 
 * @see init_mime
330
 
 */
331
 
mapping
332
 
query_types()
333
 
{
334
 
    return copy_value(mMime);
335
 
}
336
 
 
337
 
 
338
 
/**
339
 
 * return the class for a doctype (extension of a file).
340
 
 *  
341
 
 * @param doc - the doctype (or extension)
342
 
 * @return the class
343
 
 * @author Thomas Bopp (astra@upb.de) 
344
 
 */
345
 
string query_document_class(string doc)
346
 
{
347
 
    mixed data;
348
 
 
349
 
    data = mMime[doc];
350
 
    if ( !arrayp(data) )
351
 
        return CLASS_NAME_DOCUMENT;
352
 
    LOG("class is " + data[DATA_TYPE]);
353
 
    return data[DATA_TYPE];
354
 
}
355
 
 
356
 
/**
357
 
 * Return the document class for the given mimetype.
358
 
 * The class can be found in the classes/ Folder of sTeam.
359
 
 *  
360
 
 * @param string mtype - the mimetype
361
 
 * @return string of class type used
362
 
 */
363
 
string query_document_class_mime(string mtype)
364
 
{
365
 
    if ( arrayp(mExts[mtype]) )
366
 
        return mExts[mtype][0][DATA_TYPE];
367
 
    else
368
 
        return CLASS_NAME_DOCUMENT;
369
 
}
370
 
 
371
 
 
372
 
string get_identifier() { return "types"; }