aux-items.c 50 KB
Newer Older
1
/*
2
 * $Id: aux-items.c,v 1.10 1999/05/13 11:44:47 ceder Exp $
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 * Copyright (C) 1994, 1995, 1996  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM 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 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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 LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

David Byers's avatar
David Byers committed
26
27
28
29
30

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

31
32
33
34
35
36
37
38
#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include <setjmp.h>
39
#include <assert.h>
40
41
42
43
44

#include "kom-types.h"
#include "manipulate.h"
#include "aux-items.h"
#include "kom-errno.h"
David Byers's avatar
David Byers committed
45
#include "kom-memory.h"
46
47
48
49
50
51
52
#include "async.h"
#include "com.h"
#include "connections.h"
#include "server/smalloc.h"
#include "cache.h"
#include "s-string.h"
#include "lyskomd.h"
David Byers's avatar
David Byers committed
53
#include "kom-config.h"
54
55
#include "regex.h"
#include "log.h"
Per Cederqvist's avatar
Per Cederqvist committed
56
#include "services.h"
David Byers's avatar
David Byers committed
57
58
#include "admin.h"
#include "param.h"
59
60
61

#define AUX_ADJUST_FLAG(flg) item->flags.flg = (def->clear_flags.flg)?0:((def->set_flags.flg)?1:item->flags.flg)

David Byers's avatar
David Byers committed
62
63
/* Externally accessible variables (used by the parser) */

64
Aux_item_definition     *aux_item_definition_list = NULL;
David Byers's avatar
David Byers committed
65
66
67
68
unsigned long            num_aux_item_definitions = 0; 

/* Variables local to this file */

69
70
71
72
static Aux_item_definition compiled_aux_items[] =
{
};

73

David Byers's avatar
David Byers committed
74
75
/* Forward declaration of triggers */

76
77
void aux_item_trigger_mark_text(Aux_item_trigger_data *);
void aux_item_trigger_unmark_text(Aux_item_trigger_data *);
David Byers's avatar
David Byers committed
78
79
80
81
82
83
84
85
86
87
void aux_item_trigger_mirror_faq(Aux_item_trigger_data *);


/* Forward declarations of validators */

Bool aux_item_validate_existing_text(Aux_item_validation_data *data);



/* Symbol table for the triggers */
88

89
static Aux_item_trigger_mapping
90
91
92
93
aux_item_triggers [] =
{
    { "mark-text", aux_item_trigger_mark_text },
    { "unmark-text", aux_item_trigger_unmark_text },
David Byers's avatar
David Byers committed
94
    { "link-faq", aux_item_trigger_mirror_faq },
95
96
97
    { NULL, NULL }
};

David Byers's avatar
David Byers committed
98
99
100
101
102
103
104
105
106
107
108
109
110

/* Symbol table for the validators */

static Aux_item_validator_mapping
aux_item_validators [] =
{
    { "existing-readable-text", aux_item_validate_existing_text },
    { NULL, NULL }
};


/* Use this to initialize an empty definition */

111
112
113
114
115
116
117
118
Aux_item_definition empty_aux_item_definition =
{
    NULL,                   /* Name */           
    0,                      /* Tag */            
    { 0,0,0,0,0,0,0,0 },    /* Clear flags */    
    { 0,0,0,0,0,0,0,0 },    /* Set flags */      
    0,                      /* Author only */    
    0,                      /* Supervisor only */
David Byers's avatar
David Byers committed
119
    0,                      /* System only */
120
121
122
123
    0,                      /* Unique */         
    0,                      /* Can't delete */   
    0,                      /* Inherit limit */  
    FALSE,                  /* Texts */
124
    0,
125
    FALSE,                  /* Confs */
126
    0,
127
128
    FALSE,                  /* Letterbox */
    FALSE,                  /* System */
David Byers's avatar
David Byers committed
129
130
    0,                      /* Number of validators */
    NULL,                   /* Validator list */
131
132
133
    0, NULL,
    0, NULL,
    0, NULL,
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    NULL                    /* Next */
};

    


/*
 * simple_aux_item is the default definition for simple
 * items (non-reserved tags)
 */

static Aux_item_definition simple_aux_item =
{
    "simple",               /* Name */           
    0,                      /* Tag */            
    { 0,0,0,0,0,0,0,0 },    /* Clear flags */    
    { 0,0,0,0,0,0,0,0 },    /* Set flags */      
    0,                      /* Author only */    
    0,                      /* Supervisor only */
David Byers's avatar
David Byers committed
153
    0,                      /* System only */
154
155
156
157
    0,                      /* Unique */         
    0,                      /* Can't delete */   
    0,                      /* Inherit limit */  
    TRUE,                   /* Texts */
158
    0,
159
    TRUE,                   /* Confs */
160
    0,
161
162
    TRUE,                   /* Letterbox */
    TRUE,                   /* System */
David Byers's avatar
David Byers committed
163
    0, NULL,                /* Validators */
164
165
166
167
    0, NULL,                /* Add triggers */
    0, NULL,                /* Delete triggers */
    0, NULL,                /* Undelete triggers */
    NULL
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
};


/* Forward declarations */

static Bool
aux_item_check_unique(Aux_item *item,
                      Aux_item_definition *xdef,
                      Aux_item_list *add_to_list,
                      unsigned long start_looking_at);


/*
 * aux_item_default_definition
 *
 * Return the default definition of ITEM or NULL if ITEM cannot
 * be created without an explicit definition
 */

static Aux_item_definition *
aux_item_default_definition(Aux_item *item)
{
    /* A predefined aux_item that we didn't find a definition for is
       illegal. */

    if (AUX_IS_ILLEGAL(item->tag))
        return NULL;

    if (AUX_IS_PREDEFINED(item->tag))
        return NULL;

    
    /* This might be an extended item. This code is very horrendous,
       execially if you take a closer look at the AUX_CHECK_BIT macro.
       I wrote it this way because I think it will work in a
       wrong-endian environment, and I was unsure of how well simple
       masking would. */
    /* MMMM XFUP SSSS CCCC NNNN NNNN NNNN NNNN */

David Byers's avatar
David Byers committed
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
/*
  tmp = item->tag;

  extended                        = (tmp & 0x07000000) ? 1 : 0;
  def_s.author_only               = (tmp & 0x04000000) ? 1 : 0;
  def_s.one_per_person            = (tmp & 0x02000000) ? 1 : 0;
  def_s.may_not_delete            = (tmp & 0x01000000) ? 1 : 0;
  def_s.set_flags.deleted         = (tmp & 0x00700000) ? 1 : 0;
  def_s.set_flags.inherit         = (tmp & 0x00400000) ? 1 : 0;
  def_s.set_flags.secret          = (tmp & 0x00200000) ? 1 : 0;
  def_s.set_flags.hide_creator    = (tmp & 0x00100000) ? 1 : 0;
  def_s.clear_flags.deleted       = (tmp & 0x00070000) ? 1 : 0;
  def_s.clear_flags.inherit       = (tmp & 0x00040000) ? 1 : 0;
  def_s.clear_flags.secret        = (tmp & 0x00020000) ? 1 : 0;
  def_s.clear_flags.hide_creator  = (tmp & 0x00010000) ? 1 : 0;

  def_s.tag = item->tag;
  def_s.inherit_limit = 0;
  def_s.name = "extended-default";


  / * If it wasn't extended, then it was simple, because it sure
  wasn't predefined. * /

  if (!extended)
  {
  return &simple_aux_item;
  }

  return &def_s;
*/

    return &simple_aux_item;
240
241
242
}


243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * aux_item_find_trigger
 */

Aux_item_trigger
aux_item_find_trigger(char *trigger_name)
{
    unsigned long i = 0;

    while (aux_item_triggers[i].name != NULL)
    {
        if (!strcmp(aux_item_triggers[i].name, trigger_name))
            return aux_item_triggers[i].function;
        i += 1;
    }
    return NULL;
}

David Byers's avatar
David Byers committed
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
Aux_item_validation_function
aux_item_find_validator(char *validator_name)
{
    unsigned long i = 0;

    while (aux_item_validators[i].name != NULL)
    {
        if (!strcmp(aux_item_validators[i].name, validator_name))
            return aux_item_validators[i].function;
        i += 1;
    }
    return NULL;
}


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
/*
 * find_aux_item_definition
 *
 * Return the definition of ITEM or NULL if it does not
 * have a valid information.
 */

Aux_item_definition *
find_aux_item_definition(Aux_item *item)
{
    Aux_item_definition *def;

    def = aux_item_definition_list;
    while (def != NULL)
    {
        if (def->tag == item->tag)
            return def;
        def = def->next;
    }

    return aux_item_default_definition(item);
}


/* 
 * ======================================================================
 * UTILITY FUNCTIONS
 * ======================================================================
 */
 
/*
 * initialize_aux_items
 *
 * Initialize this code and its data structures
 */

void
aux_item_definition_add(Aux_item_definition *def)
{
    Aux_item_definition     *new_definition;

    new_definition = smalloc(sizeof(Aux_item_definition));
    if (new_definition == NULL)
    {
David Byers's avatar
David Byers committed
320
        restart_kom("out of memory adding aux-item definition");
321
322
323
324
325
326
327
328
    }
    *new_definition = *def;
    num_aux_item_definitions += 1;

    new_definition->next = aux_item_definition_list;
    aux_item_definition_list = new_definition;
}

David Byers's avatar
David Byers committed
329
330
331
332
333
334
335


/*
 * Compile the verification regexp for the definition in DEF
 * and put the result somewhere convenient.
 */

Per Cederqvist's avatar
Per Cederqvist committed
336
static void
David Byers's avatar
David Byers committed
337
338
aux_item_definition_cache_regexp(Aux_item_definition *def,
                                 unsigned long ix)
339
340
341
342
343
344
{
    struct re_pattern_buffer   *pat_buf;
    const char                 *errmsg;

    re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
            
David Byers's avatar
David Byers committed
345
346
    def->validators[ix].v.re.cached_re_buf = smalloc(sizeof(*pat_buf));
    if (def->validators[ix].v.re.cached_re_buf == NULL)
347
    {
David Byers's avatar
David Byers committed
348
349
350
        kom_log("Out of memory compiling aux-item regexp %lu (%s).\n",
                def->tag,
                def->name);
351
352
        return;
    }
David Byers's avatar
David Byers committed
353
    pat_buf = def->validators[ix].v.re.cached_re_buf;
354
355
356
357
358
359
360
    pat_buf->translate = DEFAULT_COLLAT_TAB;
    pat_buf->translate = NULL;
    pat_buf->fastmap = 0;
    pat_buf->allocated = 0;
    pat_buf->buffer = 0;

    if ((errmsg =
David Byers's avatar
David Byers committed
361
362
         re_compile_pattern(def->validators[ix].v.re.regexp,
                            strlen(def->validators[ix].v.re.regexp),
363
364
365
                            pat_buf))
        != NULL)
    {
David Byers's avatar
David Byers committed
366
        kom_log("%s in validate regexp of aux-item definition %lu (%s).\n",
367
368
369
                errmsg,
                def->tag,
                def->name);
David Byers's avatar
David Byers committed
370
371
372
        if (def->validators[ix].v.re.cached_re_buf)
            sfree(def->validators[ix].v.re.cached_re_buf);
        def->validators[ix].v.re.cached_re_buf = NULL;
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
        return;
    }
}

void
initialize_aux_items(char *aux_def_file)
{
    unsigned long                i;
    Aux_item_definition         *def;

    /* Set up precompiled items */

    for (i = 0;
         i < (sizeof(compiled_aux_items)/sizeof(*compiled_aux_items));
         i++)
    {
        aux_item_definition_add(&compiled_aux_items[i]);
    }

    /* Read definitions from configuration file */

    parse_aux_item_definitions(aux_def_file);

    def = aux_item_definition_list;
David Byers's avatar
David Byers committed
397

398
399
    while (def != NULL)
    {
David Byers's avatar
David Byers committed
400
401
402
        /* FIXME: If we have validators, go through and cache their
           FIXME: regexps, where appropriate */

403
404
405
406
407
        def = def->next;
    }
}


408
409
410
/* TRIGGER THINGS */


Per Cederqvist's avatar
Per Cederqvist committed
411
412
static void
aux_item_call_add_triggers(Aux_item_definition *def,
David Byers's avatar
David Byers committed
413
			   Object_type object_type,
Per Cederqvist's avatar
Per Cederqvist committed
414
415
			   unsigned long item_index,
			   unsigned long integer_argument,
David Byers's avatar
David Byers committed
416
417
			   void     * pointer_argument,
                           Aux_item * item)
418
419
420
421
422
423
424
425
426
{
    unsigned long i;
    Aux_item_trigger_data data;
    
    data.action = AUX_ITEM_ADD_ACTION;
    data.object_type = object_type;
    data.item_index = item_index;
    data.object_no = integer_argument;
    data.object = pointer_argument;
David Byers's avatar
David Byers committed
427
    data.item = item;
428
429
430

    for (i = 0; i < def->num_add_triggers; i++)
    {
David Byers's avatar
David Byers committed
431
        (*(def->add_triggers[i]))(&data);
432
433
434
    }
}

Per Cederqvist's avatar
Per Cederqvist committed
435
436
static void
aux_item_call_delete_triggers(Aux_item_definition *def,
David Byers's avatar
David Byers committed
437
438
439
440
441
                              Object_type object_type,
                              unsigned long item_index,
                              unsigned long integer_argument,
                              void     * pointer_argument,
                              Aux_item *item)
442
443
444
445
446
447
448
449
450
{
    unsigned long i;
    Aux_item_trigger_data data;
    
    data.action = AUX_ITEM_DELETE_ACTION;
    data.object_type = object_type;
    data.item_index = item_index;
    data.object_no = integer_argument;
    data.object = pointer_argument;
David Byers's avatar
David Byers committed
451
    data.item = item;
452
453
454

    for (i = 0; i < def->num_delete_triggers; i++)
    {
David Byers's avatar
David Byers committed
455
        (*(def->delete_triggers[i]))(&data);
456
457
458
    }
}

Per Cederqvist's avatar
Per Cederqvist committed
459
460
static void
aux_item_call_undelete_triggers(Aux_item_definition *def,
David Byers's avatar
David Byers committed
461
				Object_type object_type,
Per Cederqvist's avatar
Per Cederqvist committed
462
463
				unsigned long item_index,
				unsigned long integer_argument,
David Byers's avatar
David Byers committed
464
465
				void     * pointer_argument,
                                Aux_item * item)
466
467
468
469
470
471
472
473
474
{
    unsigned long i;
    Aux_item_trigger_data data;
    
    data.action = AUX_ITEM_UNDELETE_ACTION;
    data.object_type = object_type;
    data.item_index = item_index;
    data.object_no = integer_argument;
    data.object = pointer_argument;
David Byers's avatar
David Byers committed
475
    data.item = item;
476
477
478

    for (i = 0; i < def->num_undelete_triggers; i++)
    {
David Byers's avatar
David Byers committed
479
        (*(def->undelete_triggers[i]))(&data);
480
481
    }
}
482
483
484
485
486
487
488
489
490
491
492
493
494

/*
 * prepare_aux_item_list
 *
 * Prepare items in LIST for checking and addition. You *have*
 * to call this before any of the addition or checking
 * function. CREATOR is the person who wants to create the
 * item list. This function will set various fields in the
 * items. UTSL.
 */

void
prepare_aux_item_list(Aux_item_list *list,
David Byers's avatar
David Byers committed
495
496
                      Pers_no creator,
                      time_t now)
497
498
499
500
501
502
503
504
{
    unsigned long i;

    if (list == NULL)
        return;
    
    for (i = 0; i < list->length; i++)
    {
David Byers's avatar
David Byers committed
505
        prepare_aux_item(&list->items[i], creator, NULL, now);
506
507
508
509
510
    }
}

void prepare_aux_item(Aux_item *item,
                      Pers_no creator,
David Byers's avatar
David Byers committed
511
512
                      Aux_item_definition *def,
                      time_t now)
513
514
515
516
517
518
519
520
521
522
{
    if (item == NULL)
        return;
    def = def ? def : find_aux_item_definition(item);
    if (def == NULL)
        return;

    AUX_ADJUST_FLAG(inherit);
    AUX_ADJUST_FLAG(secret);
    AUX_ADJUST_FLAG(hide_creator);
523
    AUX_ADJUST_FLAG(dont_garb);
524
525
526
527
528
529
530
531
532
533
534
535
    AUX_ADJUST_FLAG(reserved3);
    AUX_ADJUST_FLAG(reserved4);
    AUX_ADJUST_FLAG(reserved5);
    /* AUX_ADJUST_FLAG(deleted); */
    item->flags.deleted = 0;

    if (def->inherit_limit != 0 &&
        (item->inherit_limit > def->inherit_limit ||
         item->inherit_limit == 0))
        item->inherit_limit = def->inherit_limit;

    item->creator = creator;
David Byers's avatar
David Byers committed
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
    item->sent_at = now;
    init_aux_item_link(&item->linked_item);
}



static Bool aux_item_validate(Aux_item_validation_data validation_data)
{
    unsigned long               i;
    Aux_item_definition        *def;

    def = validation_data.def;
    for (i = 0; i < def->num_validators; i++)
    {
        switch (def->validators[i].type)
        {
        case AUX_VALIDATE_FUNCTION:
            /*
             * Call the validation function
             * If it returns false, then abort validation
             * If it returns true, then continue with the
             * next validator
             */

            if ((*def->validators[i].v.fn.function)(&validation_data) == FALSE)
            {
                return FALSE;
            }
            break;

        case AUX_VALIDATE_REGEXP:

            /*
             * Ensure that the regexp is cached
             */
            
            if (def->validators[i].v.re.cached_re_buf == NULL)
            {
                aux_item_definition_cache_regexp(def, i);
            }

            /*
             * If the RE is still not cached there is a problem
             * in this case to not validate the item at all and
             * log a problem
             */

            if (def->validators[i].v.re.cached_re_buf == NULL)
            {
                kom_log("Failed to cache regexp \"%s\". Rejecting all \"%s\" items",
                        def->validators[i].v.re.regexp,
                        def->name);
                def->validators[i].type = AUX_VALIDATE_REJECT;
                kom_errno = KOM_ILL_AUX;
                return FALSE;
            }

            /*
             * RE is compiled and can be used
             */

            switch (re_search(def->validators[i].v.re.cached_re_buf,
                              validation_data.item->data.string,
                              s_strlen(validation_data.item->data),
                              0,
                              s_strlen(validation_data.item->data),
                              NULL))
            {
            case -1:
                /* No match */
                kom_errno = KOM_ILL_AUX;
                return FALSE;
            case -2:
                /* Internal error may (be temporary) */
                kom_log("Internal error in regex matching aux-item data.");
                kom_errno = KOM_ILL_AUX;
                return FALSE;
                break;
            default:
                /* Matched */
                break;
            }
            

            break;

        case AUX_VALIDATE_REJECT:
            /*
             * Just reject the item. Used when we detect a problem
             * with a validator
             */

            kom_errno = KOM_ILL_AUX;
            return FALSE;
            break;

        default:
            kom_log("BUG: unknown aux_item validator type");
            break;
        }
    }

    /*
     * If we got all the way to here we passed all the validators
     */

    return TRUE;

644
645
}

David Byers's avatar
David Byers committed
646

647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/*
 * aux_item_add_perm
 *
 * Check if it is alright to add an aux_item.
 *
 * ITEM is the item that you want to add.
 * DEF is the item's definition or NULL.
 * ITEM_CREATOR is the person who wants to create the item.
 * OBJECT_CREATOR is the person who created the object to which
 *     the item might be added. For texts it is the author, for
 *     persons it is the person and for conferences it is the
 *     conference supervisor.
 * OWNER_CHECK is TRUE when this function is supposed to check author
 *     and supervisor-brelated flags. The only case when this is to
 *     be FALSE is when checking author or supervisor makes not sense,
 *     such as when creating a completely new conference.
 * ADD_TO_LIST is the item list to add to, or NULL.
 * START_LOOKING_AT is the first index in add_to_list to start
 *     looking at for possible duplicates.
666
 * CREATING is true if we are checking while creating an object
667
668
669
670
671
672
673
674
675
 */

Bool
aux_item_add_perm(Aux_item *item,
                  Aux_item_definition *def,
                  Pers_no item_creator,
                  Pers_no object_creator,
                  Bool owner_check,
                  Aux_item_list *add_to_list,
676
677
                  unsigned long start_looking_at,
                  Bool creating,
David Byers's avatar
David Byers committed
678
                  Object_type object_type
679
                  )
680
{
681
682
    short can_add_when = 0;
    
David Byers's avatar
David Byers committed
683
684
    kom_errno = KOM_NO_ERROR;

685
686
687
688
689
    def = def ? def : def = find_aux_item_definition(item);

    /* Can't create an item with no definition */
      
    if (def == NULL)
David Byers's avatar
David Byers committed
690
691
    {
        kom_errno = KOM_ILL_AUX;
692
        return FALSE;
David Byers's avatar
David Byers committed
693
    }
694

695
696
697
698
699
700
701
702
703
704
705
706
    switch (object_type)
    {
    case TEXT_OBJECT_TYPE: can_add_when = def->text_a; break;
    case CONF_OBJECT_TYPE: can_add_when = def->conf_a; break;
    default: can_add_when = 0;
    }

    if (can_add_when)
    {
        if (!((creating  && (can_add_when & AUX_ITEM_ADD_ON_CREATE)) ||
              (!creating && (can_add_when & AUX_ITEM_ADD_ON_MODIFY))))
        {
David Byers's avatar
David Byers committed
707
            kom_errno = KOM_AUX_PERM;
708
709
710
711
            return FALSE;
        }
    }

David Byers's avatar
David Byers committed
712
713
714
715
716
717
718
    if (def->system_only &&
        owner_check)
    {
        kom_errno = KOM_AUX_PERM;
        return FALSE;
    }

719
720
721
722
723
724
    if (def->author_only &&
        owner_check &&
        object_creator != item_creator &&
        !is_strictly_supervisor(object_creator, NULL, item_creator, NULL) &&
        !(item_creator == ACTPERS && ENA(wheel, 8)))
    {
David Byers's avatar
David Byers committed
725
726
        kom_errno = KOM_AUX_PERM;
        return FALSE;
727
728
729
730
731
732
733
    }

    if (def->supervisor_only &&
         owner_check &&
        !is_strictly_supervisor(object_creator, NULL, item_creator, NULL) &&
        !(item_creator == ACTPERS && ENA(wheel,8)))
    {
David Byers's avatar
David Byers committed
734
        kom_errno = KOM_AUX_PERM;
735
736
737
738
739
740
        return FALSE;
    }

    /* Even the administrator can't override this */

    if (!aux_item_check_unique(item, def, add_to_list, start_looking_at))
David Byers's avatar
David Byers committed
741
742
    {
        kom_errno = KOM_AUX_PERM;
743
        return FALSE;
David Byers's avatar
David Byers committed
744
    }
745
746
747

    /* Check the contents */

David Byers's avatar
David Byers committed
748
    if (def->num_validators > 0)
749
    {
David Byers's avatar
David Byers committed
750
751
752
753
754
755
756
757
758
759
760
761
762
        Aux_item_validation_data validation_data;

        validation_data.item = item;
        validation_data.def = def;
        validation_data.item_creator = item_creator;
        validation_data.object_creator = object_creator;
        validation_data.owner_check = owner_check;
        validation_data.add_to_list = add_to_list;
        validation_data.start_looking_at = start_looking_at;
        validation_data.creating = creating;
        validation_data.object_type = object_type;

        if (aux_item_validate(validation_data) != TRUE)
David Byers's avatar
David Byers committed
763
764
        {
            kom_errno = KOM_ILL_AUX;
765
            return FALSE;
David Byers's avatar
David Byers committed
766
        }
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
    }

    return TRUE;
}


/*
 * aux_item_check_unique
 *
 * Check whether there are items that block creation of a new item.
 * ITEM is the item we want to add.
 * DEF is the item's definitioon
 * ADD_TO_LIST is the list we want to add to
 * START_LOOKING_AT is the first index in the list to look at.
 */

static Bool
aux_item_check_unique(Aux_item *item,
                      Aux_item_definition *def,
                      Aux_item_list *add_to_list,
                      unsigned long start_looking_at)
{
    unsigned long i;

    if (add_to_list == NULL ||
        item == NULL ||
        start_looking_at >= add_to_list->length)
        return TRUE;

    def = def ? def : find_aux_item_definition(item);
    if (def == NULL || !def->one_per_person)
        return TRUE;

    for (i = start_looking_at; i < add_to_list->length; i++)
    {
        if (add_to_list->items[i].tag == item->tag &&
            add_to_list->items[i].creator == item->creator &&
            !add_to_list->items[i].flags.deleted)
            return FALSE;
    }

    return TRUE;
}


/*
 * aux_inherit_items
 *
 * Inherit aux items from PARENT to TARGET. COUNTER is a pointer
 * to the aux_no counter in the object being inherited to and CREATOR
 * is the author or equivalent of the target object.
 */

void
aux_inherit_items(Aux_item_list *target,
                  Aux_item_list *parent,
                  unsigned long *counter,
824
825
                  Pers_no target_creator,
                  Bool creating,
David Byers's avatar
David Byers committed
826
                  Object_type object_type,
827
828
                  unsigned long object_no,
                  void *object)
829
830
831
832
833
{
    int i;
    Aux_item item;
    Aux_item_definition *def;

David Byers's avatar
David Byers committed
834
835
    /* FIXME: Inheriting linked items is strange, but works. */

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
    if (!target || !parent)
        return;

    for (i = 0; i < parent->length; i++)
    {
        if (parent->items[i].flags.inherit &&
            !parent->items[i].flags.deleted &&
            parent->items[i].inherit_limit != 1)
            {
            def = find_aux_item_definition(&parent->items[i]);
            if (def == NULL ||
                !aux_item_add_perm(&parent->items[i],
                                   def,
                                   parent->items[i].creator,
                                   target_creator,
                                   TRUE,
                                   target,
853
854
855
                                   0,
                                   creating,
                                   object_type
856
857
858
                                   ))
                continue;

David Byers's avatar
David Byers committed
859
            copy_aux_item(&item, &parent->items[i]);
David Byers's avatar
David Byers committed
860
861
            prepare_aux_item(&item, parent->items[i].creator,
                             def, time(NULL));
862
863
864
            if (item.inherit_limit != 0)
                item.inherit_limit -= 1;
            
David Byers's avatar
David Byers committed
865
866
867
868
869
870
            target->items = srealloc(target->items,
                                    (target->length + 1) * sizeof(Aux_item));
            target->items[target->length] = item;
            *counter += 1;
            target->items[target->length].aux_no = *counter;
            target->length += 1;
871
872
873
874
875

            aux_item_call_add_triggers(def,
                                       object_type,
                                       target->length - 1,
                                       object_no,
David Byers's avatar
David Byers committed
876
877
                                       object,
                                       &target->items[target->length]);
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
        }
    }
}

/*
 * filter_aux_item_list
 *
 * Filter out information in ORIGINAL that VIEWER is not allowed
 * to see. Copies of the items are placed in RESULT. VIEWER_P is
 * the Person corresponding to viewer or NULL. All memory is
 * allocated with tmp_alloc
 */

void
filter_aux_item_list(Aux_item_list *original,
                     Aux_item_list *result,
                     Pers_no        viewer,
                     Person        *viewer_p)
{
    Aux_item        *orig_aux;
    unsigned long    from, to;

    result->items = tmp_alloc(original->length * sizeof (Aux_item));
    result->length = 0;
    to = 0;

    for (from = 0; from < original->length; from++)
    {
        orig_aux = &original->items[from];
        if ( orig_aux->flags.secret &&
             !is_supervisor(orig_aux->creator,NULL, viewer, viewer_p) &&
             !ENA(admin, 4) )
            continue;

        result->items[to] = *orig_aux;

        if (orig_aux->flags.hide_creator &&
             !is_supervisor(orig_aux->creator,NULL, viewer, viewer_p) &&
             !ENA(admin, 4) )
            result->items[to].creator = 0;

        result->length += 1;
        to += 1;
    }
}

/*
 * check_delete_aux
 *
 * Return TRUE if ACTPERS may delete ITEM. DEF is the item's definition
 * or NULL.
 */

Success
check_delete_aux_item_list(Number_list *items_to_delete,
                           Aux_item_list *list_to_delete_from)
{
David Byers's avatar
David Byers committed
935
    long i;
936
937
938
939
940
941
942
943
944
945
946
947
948
949
    Aux_item *item;
    Aux_item_definition *def;

    for (i = 0; i < items_to_delete->length; i++)
    {
        if (items_to_delete->data[i] == 0)
            continue;


        /* Get pointer to the item and to its definition */

        if ((item = find_aux_item(list_to_delete_from,
                                  items_to_delete->data[i])) == NULL)
        {
David Byers's avatar
David Byers committed
950
            kom_errno = KOM_AUX_PERM;
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
            err_stat = i;
            return FAILURE;
        }


        /* Check all sorts of permissions */

        def = find_aux_item_definition(item);
        if ((def == NULL)                                           ||
            (def->may_not_delete)                                   ||
            (item->creator == 0 && !ENA(wheel, 8))                  ||
            (item->creator != ACTPERS &&
             !is_supervisor(item->creator, NULL, ACTPERS, NULL) &&
             !ENA(wheel,8)))
        {
            kom_errno = KOM_AUX_PERM;
            err_stat = i;
            return FAILURE;
        }


        /* For already deleted items, clear them out of the list */

        if (item->flags.deleted)
            items_to_delete->data[i] = 0;
    }

    return OK;
}



David Byers's avatar
David Byers committed
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
long
find_aux_item_index(Aux_item_list *list, unsigned long aux_no)
{
    unsigned long i;

    for (i = 0; i < list->length; i++)
    {
        if (list->items[i].aux_no == aux_no)
            return i;
    }

    return -1;
}


/* Find the aux item list containing the item that ITEM is linked to */

static Aux_item_list *
find_linked_aux_item_list(Aux_item *item)
{
    Text_stat       *text_stat;
    Conference      *conf_stat;
    
    switch (item->linked_item.target_type)
    {
    case NO_OBJECT_TYPE:
        return NULL;

    case TEXT_OBJECT_TYPE:
        GET_T_STAT(text_stat, item->linked_item.target_object.text, NULL);
        return &text_stat->aux_item_list;

    case CONF_OBJECT_TYPE:
        GET_C_STAT(conf_stat, item->linked_item.target_object.conf, NULL);
1017
        return &conf_stat->aux_item_list;
David Byers's avatar
David Byers committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071

    case INFO_OBJECT_TYPE:
        return &kom_info.aux_item_list;

    case PERS_OBJECT_TYPE:
    default:
        /* No lists in these items */
        kom_log("find_linked_aux_item_list: Bad aux_item somewhere: link to person or other kind.");
        return NULL;
    }
}

/* Find the aux item that ITEM is linked to */

static Aux_item *
find_linked_aux_item(Aux_item *item)
{
    Aux_item_list       *target_list;

    target_list = find_linked_aux_item_list(item);
    if (target_list == NULL)
        return NULL;

    return find_aux_item(target_list, item->linked_item.target_item);
}

/* Mark the object linked to by an aux_item as changed */

static void
mark_linked_object_as_changed(Aux_item *item)
{
    Text_stat       *text_stat;
    Conference      *conf_stat;

    switch (item->linked_item.target_type)
    {
    case TEXT_OBJECT_TYPE:
        VOID_GET_T_STAT(text_stat, item->linked_item.target_object.text);
        mark_text_as_changed(item->linked_item.target_object.text);
        break;

    case CONF_OBJECT_TYPE:
        VOID_GET_C_STAT(conf_stat, item->linked_item.target_object.conf);
        mark_conference_as_changed(item->linked_item.target_object.conf);
        break;

    case INFO_OBJECT_TYPE:
    case PERS_OBJECT_TYPE:
    default:
        /* Need no commit for these objects */
    }
}


1072
1073
1074
1075
1076
/*
 * delete_aux_item_list
 *
 * Mark ITEM as deleted. LIST is the list we're deleting
 * the item from.
David Byers's avatar
David Byers committed
1077
1078
 *
 * Call commit_aux_item_list to make the changes permanent
1079
1080
1081
1082
 */
 
void
delete_aux_item_list(Number_list *items_to_delete,
1083
                     Aux_item_list *list_to_delete_from,
David Byers's avatar
David Byers committed
1084
                     Object_type object_type,
1085
1086
                     unsigned long object_no,
                     void *object)
1087
{
David Byers's avatar
David Byers committed
1088
    long i;
1089
    Aux_item *item;
David Byers's avatar
David Byers committed
1090
    Aux_item *linked_item;
David Byers's avatar
David Byers committed
1091
    long item_index;
David Byers's avatar
David Byers committed
1092
1093
1094
1095
1096
    long            item_to_delete;
    unsigned long   linked_object_no;
    Number_list     linked_delete;
    Aux_item_list   *linked_item_list;
    void            *linked_object;
1097
1098
1099
1100
1101
1102

    for (i = 0; i < items_to_delete->length; i++)
    {
        if (items_to_delete->data[i] == 0)
            continue;
        
David Byers's avatar
David Byers committed
1103
1104
1105
1106
1107
        item_index = find_aux_item_index(list_to_delete_from,
                                         items_to_delete->data[i]);
        if (item_index == -1)
            restart_kom("Call to delete_aux_item_list without call to check_delete_aux_item_list");
        item = &list_to_delete_from->items[item_index];
1108
        item->flags.deleted = 1;
David Byers's avatar
David Byers committed
1109
1110
1111
1112
        aux_item_call_delete_triggers(find_aux_item_definition(item),
                                      object_type,
                                      item_index,
                                      object_no,
David Byers's avatar
David Byers committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
                                      object,
                                      item);
        /* If we have linked items, undelete them too.
           It is safe to do this with a recursive call
           since we have marked this item as undeleted.
           But just to avoid pointless recursion, only
           do the recursive call if the corresponding
           item is marked as deleted */

        linked_item = find_linked_aux_item(item);
        if (linked_item && !linked_item->flags.deleted)
        {
            /* Set up the undelete list for the target object */
            item_to_delete = item->linked_item.target_item;
            linked_delete.length = 1;
            linked_delete.data = &item_to_delete;

            /* Find the item list to undelete from */
            linked_item_list = find_linked_aux_item_list(item);

            /* Set up the object and object_no parameters for undelete */
            switch (item->linked_item.target_type)
            {
            case CONF_OBJECT_TYPE:
                linked_object_no = item->linked_item.target_object.conf;
                linked_object = (void*)cached_get_text_stat(linked_object_no);
                break;
            case TEXT_OBJECT_TYPE:
                linked_object_no = item->linked_item.target_object.text;
                linked_object = (void*)cached_get_conf_stat(linked_object_no);
                break;
            case INFO_OBJECT_TYPE:
                linked_object_no = 0;
                linked_object = NULL;
                break;
            default:
1149
		restart_kom("delete_aux_item_list(): bad target type\n");
David Byers's avatar
David Byers committed
1150
1151
1152
1153
1154
1155
1156
1157
            }

            delete_aux_item_list(&linked_delete,
                                 linked_item_list,
                                 item->linked_item.target_type,
                                 linked_object_no,
                                 linked_object);
        }
1158
1159
1160
    }
}

David Byers's avatar
David Byers committed
1161
1162
1163
1164
1165
1166
/*
 * Reverse tentative deletions from an aux-item-list
 * This must be called before commit_aux_item_list is
 * called
 */

1167
1168
void
undelete_aux_item_list(Number_list *items_to_undelete,
1169
                       Aux_item_list *list_to_undelete_from,
David Byers's avatar
David Byers committed
1170
                       Object_type object_type,
1171
1172
                       unsigned long object_no,
                       void *object)
1173
{
David Byers's avatar
David Byers committed
1174
1175
1176
1177
1178
1179
1180
1181
1182
    long            i;
    Aux_item       *item;
    Aux_item       *linked_item;
    Aux_item_list  *linked_item_list;
    long            item_index;
    long            item_to_undelete;
    unsigned long   linked_object_no;
    Number_list     linked_undelete;
    void           *linked_object;
1183
1184
1185
1186
1187
1188
    
    for (i = 0; i < items_to_undelete->length; i++)
    {
        if (items_to_undelete->data[i] == 0)
            continue;
        
David Byers's avatar
David Byers committed
1189
1190
1191
1192
1193
        item_index = find_aux_item_index(list_to_undelete_from,
                                         items_to_undelete->data[i]);
        if (item_index == -1)
            restart_kom("Call to undelete_aux_item_list without call to check_delete_aux_item_list");
        item = &list_to_undelete_from->items[item_index];
1194
        item->flags.deleted = 0;
David Byers's avatar
David Byers committed
1195
1196
1197
1198
        aux_item_call_undelete_triggers(find_aux_item_definition(item),
                                        object_type,
                                        item_index,
                                        object_no,
David Byers's avatar
David Byers committed
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
                                        object,
                                        item);

        /* If we have linked items, undelete them too.
           It is safe to do this with a recursive call
           since we have marked this item as undeleted.
           But just to avoid pointless recursion, only
           do the recursive call if the corresponding
           item is marked as deleted */

        linked_item = find_linked_aux_item(item);
        if (linked_item && linked_item->flags.deleted)
        {
            /* Set up the undelete list for the target object */
            item_to_undelete = item->linked_item.target_item;
            linked_undelete.length = 1;
            linked_undelete.data = &item_to_undelete;

            /* Find the item list to undelete from */
            linked_item_list = find_linked_aux_item_list(item);

            /* Set up the object and object_no parameters for undelete */
            switch (item->linked_item.target_type)
            {
            case CONF_OBJECT_TYPE:
                linked_object_no = item->linked_item.target_object.conf;
                linked_object = (void*)cached_get_text_stat(linked_object_no);
                break;
            case TEXT_OBJECT_TYPE:
                linked_object_no = item->linked_item.target_object.text;
                linked_object = (void*)cached_get_conf_stat(linked_object_no);
                break;
            case INFO_OBJECT_TYPE:
                linked_object_no = 0;
                linked_object = NULL;
                break;
            default:
1236
		restart_kom("undelete_aux_item_list(): bad link type\n");
David Byers's avatar
David Byers committed
1237
1238
1239
1240
1241
1242
1243
1244
            }

            undelete_aux_item_list(&linked_undelete,
                                   linked_item_list,
                                   item->linked_item.target_type,
                                   linked_object_no,
                                   linked_object);
        }
1245
1246
1247
    }
}

David Byers's avatar
David Byers committed
1248

David Byers's avatar
David Byers committed
1249
1250
1251
1252
1253
1254
1255
/*
 * Commit deletions in the aux-item-list. After this has been called
 * undelete_aux_item_list has no effect. The memory associated with
 * the deleted items is not released until the entire list is
 * released from memory or reallocated.
 */

David Byers's avatar
David Byers committed
1256
1257
static void
commit_aux_item_list_internal(Aux_item_list *list_to_commit, Bool shallow)
David Byers's avatar
David Byers committed
1258
{
David Byers's avatar
David Byers committed
1259
1260
1261
1262
1263
    long             i;             /* Loop index */
    long             target;        /* Where to move items when compacting */
    Aux_item        *linked_item;
    Aux_item_list   *aux_item_list;

David Byers's avatar
David Byers committed
1264
1265
1266
1267
1268
1269

    target = 0;
    for (i = 0; i < list_to_commit->length; i++)
    {
        if (list_to_commit->items[i].flags.deleted)
        {
David Byers's avatar
David Byers committed
1270
1271
            /* Clear the data in the aux item */
            
David Byers's avatar
David Byers committed
1272
            s_clear(&list_to_commit->items[i].data);
David Byers's avatar
David Byers committed
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288

            /* If the item we're looking at is linked
               to another item, we have to commit the list
               of the object the link points to. To avoid
               infinite recursion only do this if the
               linked item is marked as deleted */

            linked_item = find_linked_aux_item(&list_to_commit->items[i]);
            if (linked_item                     &&
                linked_item->flags.deleted      &&
                !shallow)
            {
                aux_item_list = find_linked_aux_item_list(&list_to_commit->items[i]);
                commit_aux_item_list_internal(aux_item_list, TRUE);
                mark_linked_object_as_changed(&list_to_commit->items[i]);
            }
David Byers's avatar
David Byers committed
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
            continue;
        }
        else if (target != i)
        {
            /* If we have deleted an item earlier, compact the list */
            list_to_commit->items[target] = list_to_commit->items[i];
            list_to_commit->items[i].data = EMPTY_STRING;
            target += 1;
        }
        else
        {
            /* If we have not deleted an item, bump target up one */
            target += 1;
        }
    }

    /*
      Zero out all the unused slots in the array. This is not
      strictly necessary but looks better and makes finding
      bugs a bit easier
    */

    memset(&list_to_commit->items[target], 0,
           (list_to_commit->length - target) * sizeof(Aux_item));

    /* Target points to one index more than the last non-deleted item */
    list_to_commit->length = target;
}

1318

David Byers's avatar
David Byers committed
1319
1320
1321
1322
1323
1324
1325
1326
void commit_aux_item_list(Aux_item_list *list_to_commit)
{
    commit_aux_item_list_internal(list_to_commit, FALSE);
}




1327
1328
1329
1330
1331
1332
1333
1334
1335

/*
 * find_aux_item
 *
 * Return a pointer to the item in LIST with aux_no AUX-NO
 */
 
Aux_item *
find_aux_item(Aux_item_list *list, unsigned long aux_no)
David Byers's avatar
David Byers committed
1336
1337
1338
1339
1340
1341
1342
1343
1344
{
    long item_index = find_aux_item_index(list, aux_no);

    if (item_index == -1)
        return NULL;

    return &list->items[item_index];
}

1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378


/* **********************************************************************
 * **********************************************************************
 *
 * The following calls are specific for various types of items. They
 * are here simply to collect all of them in the same place.
 *
 * The following functions are defined for each type
 *
 * X_check_add_aux_item_list(X *obj, Aux_item_list *list,
 *                      Pers_no item_creator)
 *
 * Return OK if CREATOR is allowed to add ITEM or LIST to OBJ.
 * DEF is a pointer to the definition of ITEM or NULL. You must call
 * prepare_aux_item_list before calling these.
 *
 * 
 * X_add_aux_item_list(X *obj, Aux_item_definition *list, Pers_no creator)
 *
 * Add ITEM or all the items in LIST to OBJ. CREATOR is the person
 * adding the items. The creator field of the items are set to
 * creator. This call does not check permissions first. You need
 * to call prepare_aux_item_list before calling these, and you
 * should use X_check_add_aux_item_list or X_check_add_aux_item
 * before calling these.
 *
 *
 */


Success
text_stat_check_add_aux_item_list(Text_stat *text_s,
                                  Aux_item_list *list,
1379
1380
                                  Pers_no item_creator,
                                  Bool creating)
1381
1382
1383
1384
1385
1386
1387
1388
{
    Aux_item *item;
    unsigned long i;
    Aux_item_definition *def;

    if (list == NULL)
        return OK;

David Byers's avatar
David Byers committed
1389
1390
    kom_errno = KOM_NO_ERROR;

1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
    for (i = 0; i < list->length; i++)
    {
        item = &list->items[i];

        def = find_aux_item_definition(item);
        if (def == NULL)
        {
            kom_errno = KOM_ILL_AUX;
            err_stat = i;
            return FAILURE;
        }
        
1403
1404
1405
        if (!aux_item_add_perm(item,
                               def,
                               item_creator,
1406
1407
                               text_s?text_s->author:item_creator,
                               TRUE,
1408
1409
1410
1411
                               text_s?&text_s->aux_item_list:NULL,
                               0,
                               creating,
                               TEXT_OBJECT_TYPE) ||
1412
1413
1414
            !aux_item_check_unique(item, def, list, i + 1) ||
            !def->texts)
        {
David Byers's avatar
David Byers committed
1415
            kom_errno = kom_errno?kom_errno:KOM_AUX_PERM;
1416
1417
1418
1419
1420
1421
1422
1423
1424
            err_stat = i;
            return FAILURE;
        }
    }

    return OK;
}

void text_stat_add_aux_item_list(Text_stat *text_s,
1425
                                 Text_no text_no,
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
                                 Aux_item_list *item_list,
                                 Pers_no item_creator)
{
    unsigned long i;

    if (item_list == NULL)
        return;

    text_s->aux_item_list.items = srealloc(text_s->aux_item_list.items,
                                           (text_s->aux_item_list.length +
                                            item_list->length) *
                                           sizeof(Aux_item));
1438

1439
1440
1441
1442
1443
    for (i = 0; i < item_list->length; i++)
    {
        text_s->highest_aux += 1;
        item_list->items[i].aux_no = text_s->highest_aux;
        item_list->items[i].creator = item_creator;
David Byers's avatar
David Byers committed
1444

David Byers's avatar
David Byers committed
1445
1446
1447
        copy_aux_item(
            &text_s->aux_item_list.items[text_s->aux_item_list.length],
            &item_list->items[i]);
1448
        text_s->aux_item_list.length += 1;
1449
1450
1451
        aux_item_call_add_triggers(
            find_aux_item_definition(&item_list->items[i]),
            TEXT_OBJECT_TYPE,
David Byers's avatar
David Byers committed
1452
            text_s->aux_item_list.length - 1,
1453
            text_no,
David Byers's avatar
David Byers committed
1454
1455
            text_s,
            &item_list->items[i]);
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
    }
}


/* ================================================================== */


Success
conf_stat_check_add_aux_item_list(Conference    *conf,
                                  Conf_no        conf_no,
                                  Aux_item_list *list,
1467
1468
                                  Pers_no        creator,
                                  Bool           creating)
1469
1470
1471
1472
1473
1474
1475
1476
{
    unsigned long i;
    Aux_item *item;
    Aux_item_definition *def;

    if (list == NULL)
        return OK;
    
David Byers's avatar
David Byers committed
1477
1478
    kom_errno = KOM_NO_ERROR;

1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
    for (i = 0; i < list->length; i++)
    {
        item = &list->items[i];
        def = find_aux_item_definition(item);
        if (def == NULL)
        {
            kom_errno = KOM_ILL_AUX;
            err_stat = i;
            return FAILURE;
        }

        /* If CONF is non-NULL it already exists. We consider conf to be
         * its own "object creator" since this means that author_only
         * will allow only the supervisor of the conference to set
         * the aux_item (the conference creator is irrelevant here, as
         * everywhere else.) The semantics of supervisor_only will
         * mimic author_only.
         */

        if (!aux_item_add_perm(item,
                               def,
                               creator,
                               conf_no,
                               TRUE,
                               &conf->aux_item_list,
1504
1505
                               0,
                               creating,
David Byers's avatar
David Byers committed
1506
1507
1508
1509
                               CONF_OBJECT_TYPE)            ||
            !aux_item_check_unique(item, def, list, i + 1)  ||
            (!conf->type.letter_box && !def->confs)         ||
            (conf->type.letter_box && !def->letterboxes))
1510
        {
David Byers's avatar
David Byers committed
1511
            kom_errno = kom_errno?kom_errno:KOM_AUX_PERM;
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
            err_stat = i;
            return FAILURE;
        }
    }

    return OK;
}

void
conf_stat_add_aux_item_list(Conference    *conf,
                            Conf_no        conf_no,
                            Aux_item_list *item_list,
                            Pers_no        item_creator)
{
    unsigned long i;

    if (item_list == NULL)
        return;

    conf->aux_item_list.items = srealloc(conf->aux_item_list.items,
                                         (conf->aux_item_list.length +
                                          item_list->length) *
                                         sizeof(Aux_item));
    for (i = 0; i < item_list->length; i++)
    {
        conf->highest_aux += 1;
        item_list->items[i].aux_no = conf->highest_aux;
        item_list->items[i].creator = item_creator;
David Byers's avatar
David Byers committed
1540
1541
        copy_aux_item(&conf->aux_item_list.items[conf->aux_item_list.length],
                      &item_list->items[i]);
1542
        conf->aux_item_list.length += 1;
1543
1544
1545
        aux_item_call_add_triggers(
            find_aux_item_definition(&item_list->items[i]),
            CONF_OBJECT_TYPE,
David Byers's avatar
David Byers committed
1546
            conf->aux_item_list.length - 1,
1547
            conf_no,
David Byers's avatar
David Byers committed
1548
1549
            conf,
            &item_list->items[i]);
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
    }
}

Success system_check_add_aux_item_list(Info *info,
                                       Aux_item_list *list,
                                       Pers_no creator)
{
    unsigned long i;
    Aux_item *item;
    Aux_item_definition *def;

    if (list == NULL)
        return OK;
    
David Byers's avatar
David Byers committed
1564
1565
    kom_errno = KOM_NO_ERROR;

1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
    for (i = 0; i < list->length; i++)
    {
        item = &list->items[i];
        def = find_aux_item_definition(item);
        if (def == NULL)
        {
            kom_errno = KOM_ILL_AUX;
            err_stat = i;
            return FAILURE;
        }

David Byers's avatar
David Byers committed
1577
1578
1579
1580
1581
1582
1583
1584
1585
        if (!aux_item_add_perm(item,                    /* item */
                               def,                     /* definition */
                               creator,                 /* item_creator */
                               creator,                 /* object_creator */
                               TRUE,                    /* owner_check */
                               &info->aux_item_list,    /* add_to_list */
                               0,                       /* start_looking_at */
                               FALSE,                   /* creating */
                               INFO_OBJECT_TYPE) ||
1586
1587
1588
            !aux_item_check_unique(item, def, list, i + 1) ||
            !def->system)
        {
David Byers's avatar
David Byers committed
1589
            kom_errno = kom_errno?kom_errno:KOM_AUX_PERM;
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
            err_stat = i;
            return FAILURE;
        }
    }

    return OK;
}


void
system_add_aux_item_list(Info    *info,
                         Aux_item_list *item_list,
                         Pers_no        item_creator)
{
    unsigned long i;

    if (item_list == NULL)
        return;

    info->aux_item_list.items = srealloc(info->aux_item_list.items,
                                         (info->aux_item_list.length +
                                          item_list->length) *
                                         sizeof(Aux_item));
    for (i = 0; i < item_list->length; i++)
    {
        info->highest_aux_no += 1;
        item_list->items[i].aux_no = info->highest_aux_no;
        item_list->items[i].creator = item_creator;
David Byers's avatar
David Byers committed
1618
1619
        copy_aux_item(&info->aux_item_list.items[info->aux_item_list.length],
                      &item_list->items[i]);
1620
        info->aux_item_list.length += 1;
David Byers's avatar
David Byers committed
1621
1622
1623
1624
1625
1626

        aux_item_call_add_triggers(
            find_aux_item_definition(&item_list->items[i]),
            INFO_OBJECT_TYPE,
            info->aux_item_list.length - 1,
            0,
David Byers's avatar
David Byers committed
1627
1628
            info,
            &item_list->items[i]);
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
    }
}

extern Success
query_predefined_aux_items(Number_list *result)
{
    Aux_item_definition *def;

    result->data = tmp_alloc(sizeof(unsigned long) * num_aux_item_definitions);
    if (result->data == NULL)
    {
        restart_kom("Out of memory in query_predefined_aux_items.\n");
    }

    result->length = 0;
    def = aux_item_definition_list;
    while (def != NULL)
    {
        if (def->tag != 0)
        {
David Byers's avatar
David Byers committed
1649
	    assert((unsigned long)result->length < num_aux_item_definitions);
1650
1651
1652
            result->data[result->length] = def->tag;
            result->length += 1;
        }
1653
	def = def->next;
1654
1655
1656
1657
    }

    return OK;
}
1658
1659
1660
1661


void aux_item_trigger_mark_text(Aux_item_trigger_data *data)
{
David Byers's avatar
David Byers committed
1662
1663
1664
1665
    Text_no text_no;
    Text_stat *text_stat;
    String_size ill_char;

David Byers's avatar
David Byers committed
1666
1667
1668
1669
    text_no = s_strtol(data->item->data, &ill_char, 10);
    VOID_GET_T_STAT(text_stat, text_no);
    if (text_stat->no_of_marks < param.max_marks_text)
      text_stat->no_of_marks += 1;
David Byers's avatar
David Byers committed
1670
    mark_text_as_changed(text_no);
1671
1672
1673
1674
}

void aux_item_trigger_unmark_text(Aux_item_trigger_data *data)
{
David Byers's avatar
David Byers committed
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
    Text_no          text_no;
    Text_stat       *text_stat;
    String_size      ill_char;

    text_no = s_strtol(data->item->data, &ill_char, 10);
    VOID_GET_T_STAT(text_stat, text_no);
    if (text_stat->no_of_marks > 0)
      text_stat->no_of_marks -= 1;
    mark_text_as_changed(text_no);
}
David Byers's avatar
David Byers committed
1685

David Byers's avatar
David Byers committed
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
void aux_item_trigger_mirror_faq(Aux_item_trigger_data *data)
{
    Conference      *conf_stat;
    Conf_no          conf_no;
    Text_no          text_no;
    Text_stat       *text_stat;
    Aux_item_list    items;
    Aux_item         item_data;
    char             conf_no_string[40];
    String_size      ill_char;
    String_size      end_pos;
    
1698
    if (data->object_type != CONF_OBJECT_TYPE || data->object == NULL)
David Byers's avatar
David Byers committed
1699
1700
        return;

David Byers's avatar
David Byers committed
1701
    init_aux_item(&item_data);
David Byers's avatar
David Byers committed
1702
    conf_stat = (Conference *)data->object;
David Byers's avatar
David Byers committed
1703
    conf_no = (Conf_no)data->object_no;
David Byers's avatar
David Byers committed
1704
1705
1706
1707
    text_no = s_strtol(conf_stat->aux_item_list.items[data->item_index].data,
                       &ill_char,
                       10);

David Byers's avatar
David Byers committed
1708
    VOID_GET_T_STAT(text_stat, text_no);
David Byers's avatar
David Byers committed
1709

David Byers's avatar
David Byers committed
1710
1711
1712
1713
1714
1715
1716
    sprintf(conf_no_string, "%-40lu", (unsigned long)conf_no);
    
    item_data.tag = 28;         /* Mirror of FAQ item */
    item_data.data = s_fcrea_str(conf_no_string);
    end_pos = s_strchr(item_data.data, ' ', 0);
    if (end_pos != -1)
        s_strdel(&item_data.data, end_pos, s_strlen(item_data.data) - 1);
David Byers's avatar
David Byers committed
1717

David Byers's avatar
David Byers committed
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
    /* Set up the link from the text to the conference */
    
    item_data.linked_item.target_type = CONF_OBJECT_TYPE;
    item_data.linked_item.target_object.conf = conf_no;
    item_data.linked_item.target_item = data->item->aux_no;
   
    items.length = 1;
    items.items = &item_data;

    prepare_aux_item_list(&items, 0, time(NULL));

    text_stat_add_aux_item_list(text_stat, text_no, &items, 0);
David Byers's avatar
David Byers committed
1730
    mark_text_as_changed(text_no);
David Byers's avatar
David Byers committed
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755

    /* Set up the link from the conference to the text */

    data->item->linked_item.target_type = TEXT_OBJECT_TYPE;
    data->item->linked_item.target_object.text = text_no;
    data->item->linked_item.target_item = text_stat->highest_aux;
    mark_conference_as_changed(conf_no);
}


/*
 * Aux item validators
 */


Bool aux_item_validate_existing_text(Aux_item_validation_data *v_data)
{
    Text_stat       *text_stat;
    Text_no          text_no;
    String_size      ill_char;

    text_no = s_strtol(v_data->item->data, &ill_char, 10);
    GET_T_STAT(text_stat, text_no, FALSE);
    return person_text_read_access(text_no, text_stat,
                                   v_data->item_creator, NULL);
1756
}