text.c 52.1 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
/*
Per Cederqvist's avatar
Per Cederqvist committed
2
3
 * $Id: text.c,v 0.36 1995/11/02 21:49:11 ceder Exp $
 * Copyright (C) 1991, 1992, 1993, 1994, 1995  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
/*
 * text.c
 *
 * All atomic calls that deals with texts.
 */
Per Cederqvist's avatar
Per Cederqvist committed
30

Per Cederqvist's avatar
Per Cederqvist committed
31
static char *rcsid = "$Id: text.c,v 0.36 1995/11/02 21:49:11 ceder Exp $";
32
33
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
34

Per Cederqvist's avatar
Per Cederqvist committed
35
36
#include <errno.h>
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
37
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
38
#include <setjmp.h>
Per Cederqvist's avatar
Per Cederqvist committed
39
#include <sys/types.h>
40
41
42
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
43
#include <sys/types.h>
Per Cederqvist's avatar
Per Cederqvist committed
44
45

#include "server/smalloc.h"
46
47
#include "s-string.h"
#include "misc-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
48
49
#include "kom-types.h"
#include "services.h"
Per Cederqvist's avatar
Per Cederqvist committed
50
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
51
52
53
54
55
56
#include "lyskomd.h"
#include "kom-errno.h"
#include "config.h"
#include "com.h"
#include "connections.h"
#include "internal-connections.h"
Per Cederqvist's avatar
Per Cederqvist committed
57
58
59
60
#include "cache.h"
#include "log.h"
#include "minmax.h"
#include "admin.h"
Per Cederqvist's avatar
Per Cederqvist committed
61
#include "send-async.h"
62
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
63
#include "kom-memory.h"
Per Cederqvist's avatar
Per Cederqvist committed
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

/*
 * Static functions
 */


/*
 * Add text_no to the list of texts in a conference. Return the local number
 * the text gets in this conference.
 */

static Local_text_no
add_text_in_conf(Conference * conf_c,
		 Text_no      text_no)
{
    Text_list * t;

    time( & conf_c->last_written );
    
    /* Add number last on the text_list */
    t = &(conf_c->texts);
    t->texts = srealloc(t->texts, ++(t->no_of_texts) * sizeof(Text_no));
    t->texts[ t->no_of_texts - 1 ] = text_no;

    return t->first_local_no + t->no_of_texts - 1;
}


/*
 * Set the global text_no of a certain local text_no if that local text_no
 * exists in the conf. No action is taken if the local text_no doesn't exist.
 * This function is probably only used to set the number to zero when a text
 * is deleted.
 */

static void
set_loc_no (Conference    * conf_c,
	    Local_text_no   loc_no,
	    Text_no	    text_no)
{
    if ( loc_no < conf_c->texts.first_local_no
	|| loc_no > conf_c->texts.first_local_no + conf_c->texts.no_of_texts )
    {
	return;			/* Doesn't exist in conf.stat. */
    }

    conf_c->texts.texts[ loc_no - conf_c->texts.first_local_no ] = text_no;

    return;
}
/*
 * Count how many recipients and cc_recipients a text has.
 */

static int
count_recipients( Text_stat  *t_stat )
{
    int 	 n = 0;
    Misc_info  * misc;

    for ( misc = t_stat->misc_items;
	 misc < t_stat->misc_items + t_stat->no_of_misc; misc++ )
    {
	if ( misc->type == recpt || misc->type == cc_recpt )
	{
	    n++;
	}
    }

    return n;
}

/*
 * Count how many footnotes a text has.
 */

static int
count_footn( Text_stat  *t_stat )
{
    int 	 n = 0;
    Misc_info  * misc;

    for ( misc = t_stat->misc_items;
	 misc < t_stat->misc_items + t_stat->no_of_misc; misc++ )
    {
	if ( misc->type == footn_in )
	{
	    n++;
	}
    }

    return n;
}

/*
 * Count how many commments a text has.
 */

static int
count_comment( Text_stat  *t_stat )
{
    int 	 n = 0;
    Misc_info  * misc;

    for ( misc = t_stat->misc_items;
	 misc < t_stat->misc_items + t_stat->no_of_misc; misc++ )
    {
	if ( misc->type == comm_in )
	{
	    n++;
	}
    }

    return n;
}

/*
 * Check if ACTPERS is allowed to read this text.
 * Returns TRUE if he is allowed to read it.
 */


/*
 * Check if CONF_NO is a recipient of the text whose text_stat is given.
 */

190
191
192
193

int
find_recipient(Conf_no	 conf_no,
	       Text_stat * t_stat)
Per Cederqvist's avatar
Per Cederqvist committed
194
195
196
197
198
199
200
201
202
203
{
    int i;
    
    for ( i = 0; i < t_stat->no_of_misc; i++ )
    {
	switch( t_stat->misc_items[ i ].type )
	{
	case recpt:
	    if ( t_stat->misc_items[ i ].datum.recipient == conf_no )
	    {
204
		return i;
Per Cederqvist's avatar
Per Cederqvist committed
205
206
207
208
209
210
	    }
	    break;
	    
	case cc_recpt:
	    if ( t_stat->misc_items[ i ].datum.cc_recipient == conf_no )
	    {
211
		return i;
Per Cederqvist's avatar
Per Cederqvist committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
	    }
	    break;
	    
	case rec_time:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case sent_by:
	case sent_at:
	case loc_no:
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
227
	    restart_kom("find_recipient(): illegal misc_item\n");
Per Cederqvist's avatar
Per Cederqvist committed
228
229
230
231
#endif
	}
    }

232
    return -1;
Per Cederqvist's avatar
Per Cederqvist committed
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
}


/*
 * Check if comment is a comment to parent.
 */
static Bool
is_comment_to(Text_no    comment,
	      Text_stat *parent)
{
    int i;

    for ( i = 0; i < parent->no_of_misc; i++ )
    {
	switch( parent->misc_items[ i ].type )
	{
	case comm_in:
	    if ( parent->misc_items[ i ].datum.commented_in == comment )
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}

/*
 * Check if footnote is a footnote to parent.
 */
static Bool
is_footnote_to(Text_no    footnote,
	       Text_stat *parent)
{
    int i;

    for ( i = 0; i < parent->no_of_misc; i++ )
    {
	switch( parent->misc_items[ i ].type )
	{
	case footn_in:
	    if ( parent->misc_items[ i ].datum.footnoted_in == footnote )
		return TRUE;
	    break;
	default:
	    break;
	}
    }

    return FALSE;
}
/*
 * Return the conference which the text goes to. This is normally conf_no, but
 * it may be a super_conf. If ACTPERS is not allowed to submit a text to
 * conf_no or a super_conf, return 0.
 */
static Conf_no
submit_to(Conf_no	conf_no, /* The conference the user is trying to */
	  			 /* submit a text to. */
	  Conference  * conf_c)	 /* May be NULL */
{
    int i;
    Access acc;
    
    CHK_LOGIN(0);
    
    if ( conf_c == NULL )
	GET_C_STAT(conf_c, conf_no, 0);

303
    for ( i=0; i < param.max_super_conf_loop; i++)
Per Cederqvist's avatar
Per Cederqvist committed
304
    {
305
	acc = access_perm (conf_no, conf_c, ACTPERS, ACT_P);
Per Cederqvist's avatar
Per Cederqvist committed
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
	
	if ( acc <= none )
	    return 0;
	
	if (conf_c->permitted_submitters == 0
	    || acc == unlimited
	    || locate_membership( conf_c->permitted_submitters, ACT_P) != NULL)
	{
	    return conf_no;
	}


	if ((conf_no = conf_c->super_conf) == 0)
	    return 0;
	
	GET_C_STAT(conf_c, conf_no, 0);
    }

    return 0;
}


/*
 * Say that FOOTNOTE is a footnote to TEXT.
 */

static Success
do_add_footnote(Text_no footnote,
		Text_no text)
{
    Text_stat *foot_s, *text_s;

    GET_T_STAT(foot_s, footnote, FAILURE);
    GET_T_STAT(text_s, text, FAILURE);

    ADD_MISC(foot_s, footn_to, footnote_to, text);
    ADD_MISC(text_s, footn_in, footnoted_in, footnote);

    mark_text_as_changed( footnote );
    mark_text_as_changed( text );
    
    return OK;
}


/*
 * Say that COMMENT is a comment to TEXT.
 */

static Success
do_add_comment(Text_no comment,
	       Text_no text)
{
    Text_stat *comm_s, *text_s;
    
    GET_T_STAT(comm_s, comment, FAILURE);
    GET_T_STAT(text_s, text, FAILURE);

    ADD_MISC(comm_s, comm_to, comment_to, text);
    ADD_MISC(text_s, comm_in, commented_in, comment);

    mark_text_as_changed( comment );
    mark_text_as_changed( text );
    
    return OK;
}

    
/*
 * Say that RECEIVER is a recipient of TEXT.
 */
static Success
do_add_recpt(Text_no 	 text,
	     Text_stat * text_s, /* May be NULL */
	     Conf_no	 receiver)
{
    Conference *rece_c;

    if ( text_s == NULL )
    {
	GET_T_STAT(text_s, text, FAILURE);
    }
    
    GET_C_STAT(rece_c, receiver, FAILURE);

    ADD_MISC(text_s, recpt, recipient, receiver);
    ADD_MISC(text_s, loc_no, local_no, add_text_in_conf(rece_c, text));

    mark_text_as_changed(text);
    mark_conference_as_changed(receiver);
    
    return OK;
}
   

static Success
do_add_cc_recpt(Text_no new_text,
		Text_stat  *text_s, /* May be NULL */
		Conf_no receiver)
{
    Conference *rece_c;

    if ( text_s == NULL )
	GET_T_STAT(text_s, new_text, FAILURE);

    GET_C_STAT(rece_c, receiver, FAILURE);

    ADD_MISC(text_s, cc_recpt, cc_recipient, receiver);
    ADD_MISC(text_s, loc_no, local_no, add_text_in_conf(rece_c, new_text));

    mark_text_as_changed( new_text );
    mark_conference_as_changed( receiver );
    
    return OK;
}
    

/*
 * Return number of lines in a text
 */

static u_short
count_lines( String str )
{
    u_short l = 0;

    while ( str.len-- > 0 )
    {
	if( *str.string++ == '\n' )
	{
	    l++;
	}
    }

    return l;
}


/*
 * Delete misc_info at location loc.
 * If it is a recpt, cc_recpt, comm_to or footn_to delete any
 * loc_no, rec_time, sent_by or sent_at that might follow it.
 *
 * Note that the Misc_info is not reallocated.
 */

static void
do_delete_misc (u_short	  * no_of_misc,
		Misc_info * misc,
		int	    loc)
{
    int del = 1;		/* Number of items to delete. */
    				/* Always delete at least one item. */
    Bool ready;
    
    /* Check range of loc */

    if ( loc < 0 || loc >= *no_of_misc )
    {
	restart_kom("do_delete_misc() - loc out of range");
    }

    ready = FALSE;
    
    while ( ready == FALSE && loc + del < *no_of_misc )
    {
	switch ( misc[ loc + del ].type )
	{
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    del++;
	    break;

	case recpt:
	case cc_recpt:
	case footn_to:
	case footn_in:
	case comm_to:
	case comm_in:
	    ready = TRUE;
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
	    restart_kom("do_delete_misc() - illegal misc");
#endif
	}
    }

    *no_of_misc -= del;

    /* Move items beyond the deleted ones. */

    while ( loc < *no_of_misc )
    {
	misc[ loc ] = misc[ loc + del ];
	loc++;
    }
}

/*
 * Delete leading zeroes from the text_list.
 * Returns TRUE if some zeroes was deleted.
 */
static Bool
adjust_text_list(Text_list *text_list)
{
    u_long zeroes;
    u_long i;

    for (zeroes = 0;
	 zeroes < text_list->no_of_texts && text_list->texts[ zeroes ] == 0;
	 zeroes++)
	;

    if ( zeroes > 0 )
    {
	text_list->no_of_texts -= zeroes;
	text_list->first_local_no += zeroes;

	for ( i = 0; i < text_list->no_of_texts; i++)
	    text_list->texts[ i ] = text_list->texts[ i + zeroes ];

	text_list->texts = srealloc(text_list->texts,
				    (text_list->no_of_texts
				     * sizeof(Text_no)));
    }

536
    return (zeroes > 0) ? TRUE : FALSE;
Per Cederqvist's avatar
Per Cederqvist committed
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
}

    
/*
 * Delete a recipient from a text. Does not fail if the recipient doesn't
 * exist - that is not an error.
 */
static Success
do_sub_recpt (Text_no	     text_no,
	      Text_stat    * text_s,  /* May be NULL */
	      Conf_no	     conf_no,
	      Conference   * conf_s ) /* May be NULL */
{
    int i;

    if ( text_s == NULL )
	GET_T_STAT(text_s, text_no, FAILURE);
    
    if ( conf_s == NULL )
    {
	conf_s = cached_get_conf_stat( conf_no ); /* Might still be NULL. */
    }
    	
    for ( i = 0; i < text_s->no_of_misc; i++ )
    {
	switch ( text_s->misc_items[ i ].type)
	{
	case recpt:
	    if ( text_s->misc_items[ i ].datum.recipient == conf_no )
	    {
		if ( conf_s != NULL )
		{
		    /* Only if the conference exists: */
		    set_loc_no( conf_s,
			       text_s->misc_items[ i+1 ].datum.local_no, 0 );
		    adjust_text_list (&conf_s->texts);
		    mark_conference_as_changed (conf_no);
		}
		
		do_delete_misc ( &text_s->no_of_misc, text_s->misc_items, i );
		mark_text_as_changed (text_no);

		return OK;
	    }
	    break;

	case cc_recpt:
	    if ( text_s->misc_items[ i ].datum.cc_recipient == conf_no )
	    {
		if ( conf_s != NULL )
		{
		    set_loc_no( conf_s,
			       text_s->misc_items[ i+1 ].datum.local_no, 0 );
		    mark_conference_as_changed( conf_no );
		}
		
		do_delete_misc( &text_s->no_of_misc, text_s->misc_items, i );
		mark_text_as_changed( text_no );
		
		return OK;
	    }
	    break;

	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
Per Cederqvist's avatar
Per Cederqvist committed
612
	    log("%s: do_sub_recpt(): bad misc_item.\n", __FILE__);
Per Cederqvist's avatar
Per Cederqvist committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
	    break;
#endif
	}
    }

    kom_errno = KOM_NOT_RECIPIENT;
    return FAILURE;
}

/*
 * Delete the link between comment and comment_to.
 */
static void
do_sub_comment (Text_no	       comment,    /* The comment. */
		Text_stat    * text_s,
		Text_no	       comment_to, /* The commented. */
		Text_stat    * parent_s )
{
    int i;
    Bool ready;

    if ( text_s == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
635
	VOID_GET_T_STAT(text_s, comment);
Per Cederqvist's avatar
Per Cederqvist committed
636
637

    if ( parent_s == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
638
	VOID_GET_T_STAT(parent_s, comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
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
    
    for ( ready = FALSE, i = 0; !ready && i < text_s->no_of_misc; i++ )
    {
	switch ( text_s->misc_items[ i ].type)
	{
	case comm_to:
	    if ( text_s->misc_items[ i ].datum.comment_to == comment_to )
	    {
		do_delete_misc( &text_s->no_of_misc, text_s->misc_items, i );
		mark_text_as_changed( comment );
		ready = TRUE;
	    }
	    break;

	default:
	    break;
	}
    }

    if ( !ready )
	restart_kom("do_sub_comment(): part 1 failed.\n");


    for ( ready = FALSE, i = 0; !ready && i < parent_s->no_of_misc; i++ )
    {
	switch ( parent_s->misc_items[ i ].type)
	{
	case comm_in:
	    if ( parent_s->misc_items[ i ].datum.commented_in == comment )
	    {
		do_delete_misc (&parent_s->no_of_misc,
				parent_s->misc_items, i );
		mark_text_as_changed( comment_to );
		ready = TRUE;
	    }
	    break;

	default:
	    break;
	}
    }

    if ( !ready )
	restart_kom("do_sub_comment(): part 2 failed.\n");
    
    return;
}


/*
 * Delete the link between footnote and footnote_to.
 */
static void
do_sub_footnote (Text_no       footnote,
		Text_stat    * text_s,
		Text_no	       footnote_to,
		Text_stat    * parent_s )
{
    int i;
    Bool ready;

    if ( text_s == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
701
	VOID_GET_T_STAT(text_s, footnote);
Per Cederqvist's avatar
Per Cederqvist committed
702
703

    if ( parent_s == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
704
	VOID_GET_T_STAT(parent_s, footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773

    for ( ready = FALSE, i = 0; !ready && i < text_s->no_of_misc; i++ )
    {
	switch ( text_s->misc_items[ i ].type)
	{
	case footn_to:
	    if ( text_s->misc_items[ i ].datum.footnote_to == footnote_to )
	    {
		do_delete_misc( &text_s->no_of_misc, text_s->misc_items, i );
		mark_text_as_changed( footnote );
		ready = TRUE;
	    }
	    break;

	default:
	    break;
	}
    }

    if ( !ready )
	restart_kom("do_sub_footnote(): part 1 failed.\n");


    for ( ready = FALSE, i = 0; !ready && i < parent_s->no_of_misc; i++ )
    {
	switch ( parent_s->misc_items[ i ].type)
	{
	case footn_in:
	    if ( parent_s->misc_items[ i ].datum.footnoted_in == footnote )
	    {
		do_delete_misc (&parent_s->no_of_misc,
				parent_s->misc_items, i );
		mark_text_as_changed( footnote_to );
		ready = TRUE;
	    }
	    break;

	default:
	    break;
	}
    }

    if ( !ready )
	restart_kom("do_sub_footnote(): part 2 failed.\n");
    
    return;
}


/*
 * Who is sender of misc_item I? Returns 0 if there is no sent_by misc_item.
 */
static Pers_no
sender(Text_stat * t_stat,
       int	   i)
{
    for (i++ ; i < t_stat->no_of_misc; i++)
    {
	switch ( t_stat->misc_items[ i ].type )
	{
	case sent_by:
	    return t_stat->misc_items[ i ].datum.sender;

	case recpt:
	case cc_recpt:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
Per Cederqvist's avatar
Per Cederqvist committed
774
	case sent_at:
Per Cederqvist's avatar
Per Cederqvist committed
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
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
	    return 0;		/* No sender. */

	case loc_no:
	case rec_time:
	    break;		/* These may come before a sent_by. */

#ifndef COMPILE_CHECKS
	default:
	    log("ERROR: sender(): Illegal misc_item found.\n");
	    return 0;
#endif
	}
    }
    return 0;			/* No sender. */
}


/*
 * Check if ACTPERS has sent this text to conference CONF_NO
 */
static Bool
is_sender(Text_stat * text_s,
	  Conf_no     conf_no)
{
    int i;

    if ( !ACTPERS )
    {
	return FALSE;
    }
    
    for ( i = 0; i < text_s->no_of_misc; i++ )
    {
	switch( text_s->misc_items[ i ].type )
	{
	case recpt:
	    if ( text_s->misc_items[ i ].datum.recipient == conf_no
		&& sender ( text_s, i ) == ACTPERS )
	    {
		return TRUE;
	    }
	    break;

	case cc_recpt:
	    if ( text_s->misc_items[ i ].datum.cc_recipient == conf_no
		&& sender ( text_s, i ) == ACTPERS )
	    {
		return TRUE;
	    }
	    break;

	case loc_no:
	case rec_time:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case sent_by:
	case sent_at:
	    break;

#ifndef COMPILE_CHECKS
	default:
	    restart_kom("is_sender(): Illegal misc_item found.\n");
#endif
	}
    }

    return FALSE;
}


/*
 * Check if ACTPERS has sent this text as a comment to parent.
 */
static Bool
is_comm_sender(Text_stat * text_s,
	       Text_no     parent)
{
    int i;

    if ( !ACTPERS )
    {
	return FALSE;
    }
    
    for ( i = 0; i < text_s->no_of_misc; i++ )
    {
	switch( text_s->misc_items[ i ].type )
	{
	case comm_to:
	    if ( text_s->misc_items[ i ].datum.comment_to == parent
		&& sender ( text_s, i ) == ACTPERS )
	    {
		return TRUE;
	    }
	    break;


	case recpt:
	case cc_recpt:
	case loc_no:
	case rec_time:
	case comm_in:
	case footn_to:
	case footn_in:
	case sent_by:
	case sent_at:
	    break;

#ifndef COMPILE_CHECKS
	default:
	    restart_kom("is_comm_sender(): Illegal misc_item found.\n");
#endif
	}
    }

    return FALSE;
}

    
/*
 * Check if ACTPERS is allowed to add a footnote to a text. Sets errno if
 * there is an error.
 */

static Success
check_footn(Text_stat * t_stat)
{
    if ( t_stat->author != ACTPERS )
    {
	kom_errno = KOM_NOT_AUTHOR;
	return FAILURE;
    }

910
    if ( count_footn( t_stat ) >= param.max_foot )
Per Cederqvist's avatar
Per Cederqvist committed
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
    {
	kom_errno = KOM_FOOT_LIMIT;
	return FAILURE;
    }

    return OK;
}

/*
 * Check if ACTPERS is allowed to add a comment to a text. Sets errno if
 * there is an error. Note that it is allowed to comment a text even if
 * you are not allowed to read it.
 */

static Success
check_comm(Text_stat * t_stat)
{
928
    if ( count_comment( t_stat ) >= param.max_comm )
Per Cederqvist's avatar
Per Cederqvist committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
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
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
1017
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
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
    {
	kom_errno = KOM_COMM_LIMIT;
	return FAILURE;
    }

    return OK;
}


/*
 * Return a pointer to a Mark if pers_no has marked the text text_no.
 * Otherwise, return NULL.
 */
static Mark *
locate_mark(Pers_no pers_no,
	    Person *pers_p,	/* May be NULL. */
	    Text_no text_no)
{
    Mark *mp;
    Mark *result = NULL;
    int i;
    Mark_list mlist;

    if ( pers_p == NULL )
	GET_P_STAT(pers_p, pers_no, NULL);

    mlist = pers_p->marks;
    
    for ( i = mlist.no_of_marks, mp = mlist.marks;
	 i > 0 && result == NULL;
	 i--, mp++ )
    {
	if ( mp->text_no == text_no )
	    result = mp;
    }

    return result;
}

/*
 * Skip one misc-item with all additional data.
 * 
 * This is only used from get_text_stat.
 */
static void
skip_recp (Misc_info ** misc,
	   Text_stat  * text_stat )
{
    Bool ready = FALSE;
    
    ++(*misc);
    while ( !ready && *misc < text_stat->misc_items + text_stat->no_of_misc )
    {
	switch ( (*misc)->type )
	{
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    ++(*misc);
	    break;

	case recpt:
	case cc_recpt:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	    ready = TRUE;
	    break;
#ifndef COMPILE_CHECKS
	default:
	    restart_kom("skip_recp() - illegal misc\n");
#endif
	}
    }
}

/*
 * End of static functions.
 */

/*
 * Functions that are exported to the rest of the server.
 */

/*
 * Check if ACTPERS is allowed to read this text.
 * Returns TRUE if he is allowed to read it.
 */
Bool
text_read_access(Text_no     text_no,
		 Text_stat * text_stat)
{
    int i;
    Misc_info *misc;
    Conference *recipient;

    /*
     * Nope, people who aren't logged in, should NOT be
     * allowed to read any texts but motd_of_lyskom!
     */
    if ( text_no == kom_info.motd_of_lyskom )
	return TRUE;

    CHK_LOGIN(FALSE);
    
    if ( text_stat == NULL )
	GET_T_STAT(text_stat, text_no, FALSE);
    
    if ( ENA(wheel, 10))
	return TRUE;

    if ( ACTPERS )
    {
	if ( text_stat->author == ACTPERS )
	    return TRUE;
	
	/* Check if ACTPERS or current working conference is a recipient */
	
	for (i = text_stat->no_of_misc, misc = text_stat->misc_items;
	     i; i--, misc++)
	{
 	    if ( misc->type == recpt
		&& (misc->datum.recipient == active_connection->cwc
		    || misc->datum.recipient == ACTPERS ))
	    {
		return TRUE;
	    }

 	    if ( misc->type == cc_recpt
		&& (misc->datum.cc_recipient == active_connection->cwc
		    || misc->datum.cc_recipient == ACTPERS ))
	    {
		return TRUE;
	    }
	}

	/* Check if ACTPERS is member in any of the recipients */
	
	for (i = text_stat->no_of_misc, misc = text_stat->misc_items;
	     i; i--, misc++)
	{
 	    if ( misc->type == recpt
		&& locate_membership( misc->datum.recipient, ACT_P) != NULL)
	    {
		return TRUE;
	    }

 	    if ( misc->type == cc_recpt
		&& locate_membership( misc->datum.cc_recipient, ACT_P) != NULL)
	    {
		return TRUE;
	    }
	}

	if ( locate_mark(ACTPERS, ACT_P, text_no) != NULL )
	{
	    return TRUE;
	}
    }

    /* Check if any of the recipients is an open conference, */
    /* or if ACTPERS is a supervisor. (Note: ACTPERS is not */
    /* supervisor of anything if he isn't logged in.)       */

    for (i = text_stat->no_of_misc, misc = text_stat->misc_items;
	 i; i--, misc++)
    {
 	if ( misc->type == recpt
	    && (recipient =
		cached_get_conf_stat( misc->datum.recipient )) != NULL )
	{
	    if ( !recipient->type.rd_prot
1103
		|| is_supervisor(misc->datum.recipient, recipient,
Per Cederqvist's avatar
Per Cederqvist committed
1104
				 ACTPERS, ACT_P) == TRUE)
Per Cederqvist's avatar
Per Cederqvist committed
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
	    {
		return TRUE;
	    }
	}

 	if ( misc->type == cc_recpt
	    && (recipient =
		cached_get_conf_stat( misc->datum.cc_recipient )) != NULL )
	{
	    if ( !recipient->type.rd_prot
1115
		|| is_supervisor(misc->datum.cc_recipient, recipient,
Per Cederqvist's avatar
Per Cederqvist committed
1116
				 ACTPERS, ACT_P) == TRUE)
Per Cederqvist's avatar
Per Cederqvist committed
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
1149
1150
1151
1152
1153
1154
1155
1156
	    {
		return TRUE;
	    }
	}
    }

    return FALSE;
}


/*
 * Delete a text from the database. Deletes all "links" to/from other
 * texts/persons/conferences. (e. g. comm_to/comm_in)
 */
Success
do_delete_text(Text_no    text_no,
	       Text_stat *text_s)
{
    Person *author;
    Bool found = FALSE;
    u_long i;
    
    if ( text_s == NULL )
	GET_T_STAT(text_s, text_no, FAILURE);

    if ((author = cached_get_person_stat (text_s->author)) != NULL)
    {
	/* Delete this text from created_texts. */

	for ( i = 0; i < author->created_texts.no_of_texts; i++ )
	    if ( author->created_texts.texts[ i ] == text_no )
	    {
		author->created_texts.texts[ i ] = 0;
		adjust_text_list (&author->created_texts);
		mark_person_as_changed (text_s->author);
		found = TRUE;
		break;
	    }

	if ( !found )
Per Cederqvist's avatar
Per Cederqvist committed
1157
1158
	    log("ERROR: %s: do_delete_text(): %s", 
		__FILE__, "text not found in authors created_texts.");
Per Cederqvist's avatar
Per Cederqvist committed
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
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
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
    }

    while ( text_s->no_of_misc > 0 )
    {
	switch ( text_s->misc_items[ 0 ].type )
	{
	case recpt:
	    if ( do_sub_recpt(text_no, text_s,
			      text_s->misc_items[ 0 ].datum.recipient, NULL)
		!= OK )
		restart_kom("do_delete_text(): error pos 1.\n");
	    break;
	    
	case cc_recpt:
	    if ( do_sub_recpt(text_no, text_s,
			      text_s->misc_items[ 0 ].datum.cc_recipient, NULL)
		!= OK )
		restart_kom("do_delete_text(): error pos 2.\n");
	    break;
	    
	case comm_to:
	    do_sub_comment(text_no, text_s,
			   text_s->misc_items[ 0 ].datum.comment_to, NULL);
	    break;
	    
	case comm_in:
	    do_sub_comment(text_s->misc_items[ 0 ].datum.commented_in, NULL,
			   text_no, text_s);
	    break;
	    
	case footn_to:
	    do_sub_footnote(text_no, text_s,
			    text_s->misc_items[ 0 ].datum.footnote_to, NULL);
	    break;
	    
	case footn_in:
	    do_sub_footnote(text_s->misc_items[ 0 ].datum.footnoted_in, NULL,
			    text_no, text_s);
	    break;
	    

	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    restart_kom("do_delete_text(): Illegal misc-item syntax.\n");
#ifndef COMPILE_CHECKS
	default:
	    restart_kom("do_delete_text(): Illegal misc-item.\n");
#endif
	}
    }

    cached_delete_text(text_no);
    return OK;
}

/*
 * Atomic calls.
 */

/*
 * Calls to handle marks.
 *
 * Marks are secret. No else can know what you have marked.
 */

/*
 * Get text_nos of all marked texts.
 */
extern Success
get_marks( Mark_list *result )
{
    CHK_LOGIN(FAILURE);
    *result = ACT_P->marks;
    return OK;
}




/*
 *  Get the text. The text will not be marked as read until you
 *  explicitly mark_as_read() it. start_char = 0 && end_char = END_OF_STRING
 *  gives the entire text.
 */
extern Success
get_text (Text_no       text_no,
	  String_size   start_char,
	  String_size   end_char,
	  String      * result)
{
    Text_stat  * text_s;

    GET_T_STAT(text_s, text_no, FAILURE);
    
    /* Check if ACTPERS has read acess to the text */
    
    if ( text_read_access( text_no, text_s ) != TRUE )
    {
	kom_errno = KOM_NO_SUCH_TEXT;
	err_stat = text_no;
	
    	return FAILURE;
    }

    *result = cached_get_text( text_no );

    if ( start_char > result->len )
    {
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return FAILURE;
    }

Per Cederqvist's avatar
Per Cederqvist committed
1273
    /* Should use something in s-string.c to do this. +++ */
Per Cederqvist's avatar
Per Cederqvist committed
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
    result->string += start_char;
    result->len = min ( result->len - 1, end_char ) + 1 - start_char;

    if ( ACTPERS )
    {
	++ACT_P->no_of_text_fetches;
	mark_person_as_changed( ACTPERS );
    }
	
    return OK;
}

1286
1287


Per Cederqvist's avatar
Per Cederqvist committed
1288
/*
1289
1290
 * Check if person is a member in any of the recipients or cc_recipients
 * of text_s
Per Cederqvist's avatar
Per Cederqvist committed
1291
 */
1292
1293
1294
static  Bool
is_member_in_recpt(Person    *person,
		   Text_stat *text_s)
Per Cederqvist's avatar
Per Cederqvist committed
1295
{
1296
    int i;
Per Cederqvist's avatar
Per Cederqvist committed
1297

1298
    for ( i = 0; i < text_s->no_of_misc; i++ )
Per Cederqvist's avatar
Per Cederqvist committed
1299
    {
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
	switch(text_s->misc_items[ i ].type)
	{
	case recpt:
	    if ( locate_membership(text_s->misc_items[ i ].datum.recipient,
				   person) != NULL )
	    {
		return TRUE;
	    }
	    break;

	case cc_recpt:
	    if ( locate_membership(text_s->misc_items[ i ].datum.cc_recipient,
				   person) != NULL )
	    {
		return TRUE;
	    }
	    break;

	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    break;

#ifndef COMPILE_CHECKS
	default:
Per Cederqvist's avatar
Per Cederqvist committed
1330
	    log("%s: is_member_in_recpt(): bad misc_item.\n", __FILE__);
1331
1332
1333
	    break;
#endif
	}
Per Cederqvist's avatar
Per Cederqvist committed
1334
1335
    }

1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
    return FALSE;
}


/*
 * Copy the text_stat original into result, but only those part
 * that viewer is allowed to see. (Censor away information about
 * conferences that viewer is not allowed to know about).
 *
 * All memory is allocated via tmp_alloc().
 */
static void
filter_secret_info(Text_stat *result,
		   Text_stat *original,
		   Pers_no    viewer,
		   Person    *viewer_p)	/* May be NULL. */
{
    Misc_info	* orig;
    Misc_info	* copy;		/* Censored Misc_infos */
Per Cederqvist's avatar
Per Cederqvist committed
1355
1356
1357
1358

    /* ^^^ Possible optimisation:
       No need to copy unless there is a secret conf among the recipients. */

1359
1360
    *result = *original;

Per Cederqvist's avatar
Per Cederqvist committed
1361
1362
1363
    result->misc_items = tmp_alloc(result->no_of_misc * sizeof ( Misc_info));
    result->no_of_misc = 0;
    copy = result->misc_items;
1364
    orig = original->misc_items;    
Per Cederqvist's avatar
Per Cederqvist committed
1365
    
1366
    while ( orig < original->misc_items + original->no_of_misc )
Per Cederqvist's avatar
Per Cederqvist committed
1367
1368
1369
1370
    {
	switch( orig->type )
	{
	case recpt:
1371
1372
	    if ( (fast_access_perm (orig->datum.recipient, viewer, viewer_p)
		  <= none )
Per Cederqvist's avatar
Per Cederqvist committed
1373
1374
		&& !ENA(admin, 4))
	    {
1375
		skip_recp ( &orig, original );
Per Cederqvist's avatar
Per Cederqvist committed
1376
1377
1378
1379
1380
1381
1382
1383
1384
	    }
	    else
	    {
		*copy++ = *orig++;
		++result->no_of_misc;
	    }
	    break;

	case cc_recpt:
1385
1386
	    if ( (fast_access_perm (orig->datum.cc_recipient, viewer, viewer_p)
		  <= none)
Per Cederqvist's avatar
Per Cederqvist committed
1387
1388
		&& !ENA(admin, 4))
	    {
1389
		skip_recp ( &orig, original );
Per Cederqvist's avatar
Per Cederqvist committed
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
	    }
	    else
	    {
		*copy++ = *orig++;
		++result->no_of_misc;
	    }
	    break;

	case loc_no:
	case rec_time:
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case sent_by:
	case sent_at:
	    *copy++ = *orig++;
	    ++result->no_of_misc;
	    break;

#ifndef COMPILE_CHECKS
	default:
1412
	    restart_kom("filter_secret_info() - illegal misc_item!\n");    
Per Cederqvist's avatar
Per Cederqvist committed
1413
1414
1415
#endif
	}
    }
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
}

/*
 * Get text status.
 *
 * If there are recipients of the text that are secret confs
 * those misc-items will be censored.
 */
extern Success
get_text_stat (Text_no	  text_no,
	       Text_stat *result )
{
    Text_stat	* text_stat;

    GET_T_STAT(text_stat, text_no, FAILURE);
    
    if ( !text_read_access(text_no, text_stat) && !ENA(admin, 2))
    {
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }

    filter_secret_info(result, text_stat, ACTPERS, ACT_P);
Per Cederqvist's avatar
Per Cederqvist committed
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
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
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
    return OK;
}

/*
 * Functions local to create_text:
 */

/*
 * Check that the recipient or cc_recipient at LOC is not already a
 * recipient or cc_recipient of this text.
 */
static Success
check_double_subm (Misc_info	* misc,
		   int		  loc,
		   Conf_no	  addressee)
{
    int j;
    
    for ( j = 0; j < loc; j++)
    {
	switch ( misc[ j ].type )
	{
	case recpt:
	    if ( misc[ j ].datum.recipient == addressee )
		return FAILURE;

	    break;

	case cc_recpt:
	    if ( misc[ j ].datum.cc_recipient == addressee )
		return FAILURE;

	    break;

	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
	    log(__FILE__, ": check_double_subm(): bad misc_item.\n");
	    break;
#endif
	}
    }
    return OK;
}

/*
 * Check that none of the first 'pos' misc_items pointed to by misc
 * is a comment or footnote to text forbidden.
 */
static Success
check_double_comm (Misc_info *misc,
		   int        pos,
		   Text_no    forbidden)
{
    for(;pos > 0; pos--, misc++)
    {
	switch(misc->type)
	{
	case comm_to:
	    if ( misc->datum.comment_to == forbidden )
		return FAILURE;
	    break;

	case footn_to:
	    if ( misc->datum.footnote_to == forbidden )
		return FAILURE;
	    break;

	case recpt:
	case cc_recpt:
	case comm_in:
	case footn_in:
	case loc_no:
	case rec_time:
	case sent_by:
	case sent_at:
	    break;
	    
#ifndef COMPILE_CHECKS
	default:
Per Cederqvist's avatar
Per Cederqvist committed
1528
	    log("%s: check_double_subm(): bad misc_item.\n", __FILE__);
Per Cederqvist's avatar
Per Cederqvist committed
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
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
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
	    break;
#endif
	}
    }

    return OK;
}

	
    
/*
 * Check that all misc_items are legal. Return OK / FAILURE.
 * Update recpt & cc_recpt fields if the text goes to a super_conf.
 * Signal an error if a conference is recipient more than once, or if
 * a text is a comment to the same text more than once.
 */
static Success
create_text_check_misc (u_short     * no_of_misc,
			Misc_info   * misc )
{
    int		 i;
    Text_stat  * parent;
    Conf_no	 addressee;

    for ( i = 0; i < *no_of_misc; i++)
    {
	err_stat = i;	/* In case of an error, err_stat indicates which
			   misc_item caused the error. */

	switch( misc[ i ].type )
	{
	case footn_to:
	    /* Check that ACTPERS is the author to the text. */

	    GET_T_STAT( parent, misc[ i ].datum.footnote_to, FAILURE);

	    if ( check_footn(parent) == FAILURE )
		return FAILURE;

	    if ( check_double_comm(misc, i, misc[ i ].datum.footnote_to)
		!= OK )
	    {
		kom_errno = KOM_ILL_MISC;
		return FAILURE;
	    }
	    
	    break;

	case comm_to:
	    /* Check that the text exists. */

	    GET_T_STAT( parent, misc[ i ].datum.comment_to, FAILURE);

	    if ( check_comm(parent) == FAILURE )
		return FAILURE;
	    
	    if ( check_double_comm(misc, i, misc[ i ].datum.comment_to)
		!= OK )
	    {
		kom_errno = KOM_ILL_MISC;
		return FAILURE;
	    }
	    
	    break;

	case recpt:
	    /* Check that ACTPERS has write access to the conference or a
	       superconference. */
	    /* Superconfs are recursive */
 
 	    addressee = submit_to( misc[ i ].datum.recipient, NULL);

	    /* Update in case of super_conf */
	    
	    if ((misc[ i ].datum.recipient = addressee) == 0)
	    {
		kom_errno = KOM_ACCESS;
		return FAILURE;
	    }

	    /* Check that this recipient is not already a recipient. */

	    if ( check_double_subm(misc, i, addressee) != OK )
	    {
		kom_errno = KOM_ILL_MISC;
		return FAILURE;
	    }
	    
	    break;
	    
	case cc_recpt:
	    /* Check that ACTPERS has write access to the conference or a
	       superconference. */
	    /* Superconfs are recursive */

 	    addressee = submit_to( misc[ i ].datum.cc_recipient, NULL);

	    /* Update in case of super_conf */
	    
	    if ((misc[ i ].datum.cc_recipient = addressee) == 0)
	    {
		kom_errno = KOM_ACCESS;
		return FAILURE;
	    }

	    /* Check that this recipient is not already a recipient. */

	    if (check_double_subm(misc, i, addressee) != OK)
	    {
		kom_errno = KOM_ILL_MISC;
		return FAILURE;
	    }
	    
	    break;

	case loc_no:		/* Ignore loc_no */
	    break;

	case comm_in:
	case footn_in:
	case rec_time:
	case sent_by:
	case sent_at:
	    /* Fall through */
#ifndef COMPILE_CHECKS
 	default:
#endif
	    kom_errno = KOM_ILL_MISC;
	    return FAILURE;
	}
    }

    return OK;
}

/*
 * Fix all double references. Eg if misc contains comm_to, add a comm_in field.
 * No access-permission checking is done.
 */
static Success
create_text_add_miscs(Text_no     new_text,
		      int         no_of_misc,
		      Misc_info * misc)
{
    int		 i;

    for ( i = 0; i < no_of_misc; i++)
    {
	err_stat = i;	/* In case of an error, err_stat indicates which
			   misc_item caused the error. */

	switch( misc[ i ].type )
	{
	case footn_to:
	    if ( do_add_footnote(new_text, misc[ i ].datum.footnote_to) != OK)
		return FAILURE;

	    break;

	case comm_to:
	    if ( do_add_comment(new_text, misc[ i ].datum.comment_to) != OK )
		return FAILURE;

	    break;

	case recpt:
	    if ( do_add_recpt(new_text, NULL, misc[ i ].datum.recipient) != OK)
		return FAILURE;

	    break;

	case cc_recpt:
	    if ( do_add_cc_recpt(new_text, NULL,
				 misc[ i ].datum.cc_recipient) !=OK )
		return FAILURE;

	    break;

	case loc_no:		/* Ignore loc_no. */
	    break;

	case comm_in:
	case footn_in:
	case rec_time:
	case sent_by:
	case sent_at:
	    /* Fall through */
#ifndef COMPILE_CHECKS
	default:
#endif
	    restart_kom("create_text_add_misc() - illegal Info_type");
	}
    }
    return OK;
}

1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
/*
 * Send an asynchronous message, but filter any secret information.
 */

static void
send_async_new_text (Text_no text_no,
		     Text_stat *text_s)
{
    Connection *cptr;
    Session_no i = 0;
1735
1736
1737
    Text_stat filtered;

    init_text_stat(&filtered);
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751

    while ( (i = traverse_connections(i)) != 0 )
    {
	cptr = get_conn_by_number(i);

	/*
	 * filter_secret_info copies the text-stat to filtered, but
	 * since it is allocated with tmp_alloc we do not need to free
	 * the memory now.
	 */
	filter_secret_info(&filtered, text_s, 
			   cptr->pers_no, cptr->person);

	if ( cptr->person != NULL
Per Cederqvist's avatar
Per Cederqvist committed
1752
	    && is_member_in_recpt (cptr->person, &filtered) == TRUE)
1753
1754
1755
1756
1757
	{
	    async_new_text (cptr, text_no, &filtered);
	}
    }
}
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816

/* 
 * Special case handling of special text numbers.
 *
 * In the KOMmunity, there has arisen an odd phenomenon known as
 * "jubel" to the local citizens of LysKOM.  There are several kinds
 * of "jubel"; a text number divisible by 1000 is one famous such.
 *
 * There is a feeling that jubels, at least the more prominent jubels,
 * should not be created by automatic postings.  This code is
 * primarily aimed at stopping such things, but it can have other, eh,
 * interesting uses as well...
 */
struct jubel {
    struct jubel *next;
    Pers_no bad_guy;
    Text_no divisor;		/* 0 is used to represent infinity */
    Text_no remainder;
};
static struct jubel *jubel_root = NULL;

/*
 * register_jubel(char*)  -  tell lyskomd about a jubel, and a
 * person which is not allowed to create that jubel.  The argument is
 * a string consisting of two or three numbers:
 *       persno textno
 *       persno divisor remainder
 * meaning that PERSNO is not allowed to create text TEXTNO, or any
 * text number which fulfills the relation textno%DIVISOR==REMAINDER.
 */
void
register_jubel(Pers_no pno, 
	       Text_no divis,	/* 0 if no division should be made. */
	       Text_no tno)
{
    struct jubel *j = smalloc(sizeof(struct jubel));
    j->next = jubel_root;
    j->bad_guy = pno;
    j->divisor = divis;
    j->remainder = tno;
    jubel_root = j;
}

/* Free the jubel list */
void
free_all_jubel(void)
{
    struct jubel *a;
    struct jubel *b;
    
    a = jubel_root;
    while (a != NULL)
    {
	b = a;
	a = a->next;
	sfree(b);
    }
    jubel_root = NULL;
}
1817
    
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
/* Check if it is ok for ACTPERS to create the next text. */
static Bool
ok_to_create_next_text(void)
{
    struct jubel *j;
    Text_no next_tno;
    Bool is_jubel = FALSE;
    
    next_tno = query_next_text_num();
    for (j = jubel_root; j != NULL; j = j->next)
    {
	if ((j->divisor == 0 && next_tno == j->remainder)
	    || (j->divisor != 0 && next_tno%j->divisor == j->remainder))
	{
	    if (ACTPERS == j->bad_guy)
	    {
1834
		log("Stopped person %d from creating jubel %lu.\n",
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
		    ACTPERS, next_tno);
		return FALSE;
	    }
	    else
		is_jubel = TRUE;
	}
    }

    if (is_jubel)
	log("Granted jubel %lu to person %d.\n", next_tno, ACTPERS);

    return TRUE;
}

1849

Per Cederqvist's avatar
Per Cederqvist committed
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
/*
 * Create a text.
 *
 * The recipients may change. See doc for set_permitted_submitters.
 *
 * The only allowed Misc_items are recpt, cc_recpt, comm_to and footn_to.
 * loc_no are allowed, but ignored.
 *
 * Returns text_no of the created text, or 0 if there was an error.
 */
extern Text_no
1861
create_text(const String  message,
Per Cederqvist's avatar
Per Cederqvist committed
1862
1863
1864
1865
1866
1867
1868
1869
1870
	    u_short	  no_of_misc,
	    Misc_info   * misc )
{
    Text_no text;
    Text_stat * t_stat;
    extern int errno;

    CHK_LOGIN(0);

1871
1872
    /* Check the length of the text. */

1873
    if ( s_strlen (message) > param.text_len )
1874
1875
1876
1877
1878
    {
	kom_errno = KOM_LONG_STR;
	return 0;
    }

1879
1880
1881
1882
1883
1884
1885
1886
    /* Check that the author is allowed to write this text number */

    if (ok_to_create_next_text() == FALSE)
    {
	kom_errno = KOM_TEMPFAIL;
	return 0;
    }

Per Cederqvist's avatar
Per Cederqvist committed
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
    /* Check all misc-items */
    
    if ( create_text_check_misc(&no_of_misc, misc) != OK
	|| (text = cached_create_text( message )) == 0)
    {
	return 0;
    }

    if ( (t_stat = cached_get_text_stat( text )) == NULL )
    {
Per Cederqvist's avatar
Per Cederqvist committed
1897
1898
	restart_kom("%s.\nText == %d, kom_errno == %d, errno == %d\n",
		    "create_text: can't get text-stat of newly created text",
Per Cederqvist's avatar
Per Cederqvist committed
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
		    text, kom_errno, errno);
    }

    t_stat->author = ACTPERS;
    t_stat->creation_time = time(NULL);
    t_stat->no_of_lines = count_lines( message );
    t_stat->no_of_chars = s_strlen( message );
    
    if ( create_text_add_miscs(text, no_of_misc, misc) != OK )
    {
	log("ERROR: create_text(): can't add miscs.\n");
	return 0;
    }
    
    ACT_P->created_texts.texts
	= srealloc(ACT_P->created_texts.texts,
		   ++(ACT_P->created_texts.no_of_texts) * sizeof(Text_no));
    ACT_P->created_texts.texts[ ACT_P->created_texts.no_of_texts - 1 ] = text;
    ACT_P->created_lines += t_stat->no_of_lines;
    ACT_P->created_bytes += t_stat->no_of_chars;

1920
1921
    mark_person_as_changed (ACTPERS);
    mark_text_as_changed (text);
Per Cederqvist's avatar
Per Cederqvist committed
1922

1923
    send_async_new_text (text, t_stat);     /* Send asynchronous message. */
Per Cederqvist's avatar
Per Cederqvist committed
1924
1925
1926
1927

    return text;
}

Per Cederqvist's avatar
Per Cederqvist committed
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
/*
 * Create an anonymous text.
 *
 * This is just like create_text, but the author of the text is set to
 * Pers_no 0, to guarantee that the author is anonymous.
 *
 * (This should really be called create_ftp_text (in analogy with
 * change-conference which was truncated to CC, which was transmuted
 * into pepsi) but public demand said NO!)
 *
 * Returns text_no of the created text, or 0 if there was an error.
 */
extern Text_no
1941
create_anonymous_text(const String message,
Per Cederqvist's avatar
Per Cederqvist committed
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
		      u_short	  no_of_misc,
		      Misc_info   * misc )
{
    Text_no text;
    Text_stat * t_stat;
    extern int errno;

    CHK_LOGIN(0);

    /* Check the length of the text. */

1953
    if ( s_strlen (message) > param.text_len )
Per Cederqvist's avatar
Per Cederqvist committed
1954
1955
1956
1957
1958
    {
	kom_errno = KOM_LONG_STR;
	return 0;
    }

1959
1960
1961
1962
1963
1964
1965
1966
    /* Check that the author is allowed to write this text number */

    if (ok_to_create_next_text() == FALSE)
    {
	kom_errno = KOM_TEMPFAIL;
	return 0;
    }

Per Cederqvist's avatar
Per Cederqvist committed
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
    /* Check all misc-items */
    
    if ( create_text_check_misc(&no_of_misc, misc) != OK
	|| (text = cached_create_text( message )) == 0)
    {
	return 0;
    }

    if ( (t_stat = cached_get_text_stat( text )) == NULL )
    {
Per Cederqvist's avatar
Per Cederqvist committed
1977
1978
	restart_kom("%screated text.\nText = %d, kom_errno = %d, errno = %d\n",
		    "create_anonymous_text: can't get text-stat of newly ",
Per Cederqvist's avatar
Per Cederqvist committed
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
		    text, kom_errno, errno);
    }

    t_stat->author = 0;
    t_stat->creation_time = time(NULL);
    t_stat->no_of_lines = count_lines( message );
    t_stat->no_of_chars = s_strlen( message );
    
    if ( create_text_add_miscs(text, no_of_misc, misc) != OK )
    {
	log("ERROR: create_text(): can't add miscs.\n");
	return 0;
    }

1993
    mark_text_as_changed (text);
Per Cederqvist's avatar
Per Cederqvist committed
1994
1995
    /* Don't add this person to create - we want true anonymity! */

1996
    send_async_new_text (text, t_stat);
Per Cederqvist's avatar
Per Cederqvist committed
1997
1998
1999
2000

    return text;
}

Per Cederqvist's avatar
Per Cederqvist committed
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
/*
 * Delete a text.
 *
 * Only a supervisor of the author may delete a text.
 */
extern Success
delete_text( Text_no text_no )
{
    Text_stat *text_s;
    
    CHK_LOGIN(FAILURE);
    GET_T_STAT(text_s, text_no, FAILURE);

2014
2015
    if ( !is_supervisor(text_s->author, NULL, ACTPERS, ACT_P)
	&& !ENA(admin, 5) )
Per Cederqvist's avatar
Per Cederqvist committed
2016
2017
2018
2019
2020
2021
2022
2023
2024
    {
	kom_errno = (text_read_access(text_no, text_s)
		     ? KOM_NOT_AUTHOR : KOM_NO_SUCH_TEXT);
	return FAILURE;
    }

    return do_delete_text(text_no, text_s);
}

Per Cederqvist's avatar
Per Cederqvist committed
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
/* Return TRUE if ``a'' is greater than ``b'. */
static Bool
greater(struct tm *a,
	struct tm *b)
{
    if (a->tm_year < b->tm_year)
	return FALSE;
    if (a->tm_year > b->tm_year)
	return TRUE;

    if (a->tm_mon < b->tm_mon)
	return FALSE;
    if (a->tm_mon > b->tm_mon)
	return TRUE;

    if (a->tm_mday < b->tm_mday)
	return FALSE;
    if (a->tm_mday > b->tm_mday)
	return TRUE;

    if (a->tm_hour < b->tm_hour)
	return FALSE;
    if (a->tm_hour > b->tm_hour)
	return TRUE;

    if (a->tm_min  < b->tm_min )
	return FALSE;
    if (a->tm_min  > b->tm_min )
	return TRUE;

    if (a->tm_sec < b->tm_sec)
	return FALSE;
    if (a->tm_sec > b->tm_sec)
	return TRUE;

    /* ``a'' is not greater if it is equal to ``b''. */
    return FALSE;
}

Linus Tolke's avatar
Linus Tolke committed
2064
2065
/*
 * Lookup a text according to creation-time.
2066
2067
 * The text-no of the text created closest before WANTED_TIME is returned.
 * The returned text is not necessarily readable.
Linus Tolke's avatar
Linus Tolke committed
2068
 */
2069
extern Success
Per Cederqvist's avatar
Per Cederqvist committed
2070
get_last_text(struct tm *wanted_time,
2071
	      Text_no *result)
Linus Tolke's avatar
Linus Tolke committed
2072
{
Per Cederqvist's avatar
Per Cederqvist committed
2073
    struct tm *texttime;
Per Cederqvist's avatar
Per Cederqvist committed
2074
    Text_no lower  = 0;
2075
    Text_no higher = query_next_text_num() - 1;
Per Cederqvist's avatar
Per Cederqvist committed
2076
    Text_stat *text_stat = NULL;
2077
2078
2079
2080
2081
2082
    Text_no try;
    Text_no middle;

    /*
     * We search for the text in the interval [lower, higher]
     * (inclusive). Middle is set to the middle (rounded towards
Per Cederqvist's avatar
Per Cederqvist committed
2083
     * infinity), and we search from there and upward until we find a
2084
2085
     * text.
     */
Linus Tolke's avatar
Linus Tolke committed
2086

2087
    while (lower < higher)
Linus Tolke's avatar
Linus Tolke committed
2088
    {
2089
2090
2091
2092
	middle = (lower + higher)/2 + 1; /* Binary search */
	try = middle;
	text_stat = NULL;

Per Cederqvist's avatar
Per Cederqvist committed
2093
2094
        while (text_stat == NULL && try <= higher)
	    text_stat = cached_get_text_stat (try++);
Linus Tolke's avatar
Linus Tolke committed
2095

2096
2097
2098
	if ( text_stat == NULL )
	    higher = middle - 1;
	else
Linus Tolke's avatar
Linus Tolke committed
2099
	{
Per Cederqvist's avatar
Per Cederqvist committed
2100
	    texttime = localtime(&text_stat->creation_time);
Per Cederqvist's avatar
Per Cederqvist committed
2101
	    if (greater(wanted_time, texttime))
Per Cederqvist's avatar
Per Cederqvist committed
2102
		lower = try - 1;
2103
2104
2105
	    else		/* The intervall is "halved" */
		higher = middle - 1;
	}
Linus Tolke's avatar
Linus Tolke committed
2106
2107
    }

2108
    *result = lower;
Per Cederqvist's avatar
Per Cederqvist committed
2109

2110
    return OK;
Linus Tolke's avatar
Linus Tolke committed
2111
2112
2113
}


2114
/*
2115
 * Return next existing text-no, which ACTPERS is allowed to read.
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
 */

extern  Success
find_next_text_no (Text_no start,
		   Text_no *result)
{
    Text_no    highest = query_next_text_num();
    Text_stat *text_s;

    while (++start < highest)
    {
	text_s = cached_get_text_stat(start);
	if (text_s != NULL && text_read_access(start, text_s) == TRUE)
	{
	    *result = start;
	    return OK;
	}
    }

    kom_errno = KOM_NO_SUCH_TEXT;
    return FAILURE;
}


/*
2141
 * Return previous existing text-no, which ACTPERS is allowed to read.
2142
2143
2144
2145
2146
2147
2148
 */

extern  Success
find_previous_text_no (Text_no start,
		       Text_no *result)
{
    Text_stat *text_s;
2149
2150
2151
2152
    Text_no next_tno;
    
    if (start > (next_tno = query_next_text_num()))
	start = next_tno;
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167

    while (start-- > 0)
    {
	text_s = cached_get_text_stat(start);
	if (text_s != NULL && text_read_access(start, text_s) == TRUE)
	{
	    *result = start;
	    return OK;
	}
    }

    kom_errno = KOM_NO_SUCH_TEXT;
    return FAILURE;
}

Per Cederqvist's avatar
Per Cederqvist committed
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
/*
 * Add a recipient to a text.
 */
extern Success
add_recipient(	Text_no		text_no,
		Conf_no		conf_no,
		Info_type	type )	/* recpt or cc_recpt */
{
    Text_stat  * t_stat;
    Conference * conf_c;
2178
    int rcpt_index;
Per Cederqvist's avatar
Per Cederqvist committed
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
    
    CHK_LOGIN(FAILURE);
    GET_T_STAT(t_stat, text_no, FAILURE);
    GET_C_STAT(conf_c, conf_no, FAILURE);

    if ( !text_read_access(text_no, t_stat ) && !ENA(admin, 4))
    {
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }
2189
2190
2191

    rcpt_index = find_recipient(conf_no, t_stat); 
    if ( rcpt_index != -1 )
Per Cederqvist's avatar
Per Cederqvist committed
2192
    {
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
	/* You cannot add the same conference twice. */
	if (t_stat->misc_items[rcpt_index].type == type)
	{
	    kom_errno = KOM_ALREADY_RECIPIENT;
	    return FAILURE;
	}

	if (type != recpt && type != cc_recpt)
	{
	    kom_errno = KOM_ILLEGAL_INFO_TYPE;
	    return FAILURE;
	}

	if (!is_supervisor(conf_no, conf_c, ACTPERS, ACT_P))
	{
	    kom_errno = KOM_PERM;
	    return FAILURE;
	}

	/* Change a recpt to a cc_recpt or a cc_recpt to a recpt.
	   The ability to do this was introduced after a request
	   from Lisa Hallingstrom, who wanted to do this repeatedly
	   when managing the conference "Fragor och svar". */

	t_stat->misc_items[rcpt_index].type = type;
#if 0
	/* This should possibly be done, but differently.
	   ADD_MISC adds things lasts in the misc-info, and
	   that is not the correct thing to do.  The change is
	   now performed silently instead.  Also note that
	   these fields may already be present... FIXME. */
	if ( t_stat->author != ACTPERS )
	    ADD_MISC(t_stat, sent_by, sender, ACTPERS);

	ADD_MISC(t_stat, sent_at, sent_at, time(NULL));
#endif

	mark_text_as_changed( text_no );
	return OK;
Per Cederqvist's avatar
Per Cederqvist committed
2232
2233
    }

2234
    if ( count_recipients( t_stat ) >= param.max_recipients )
Per Cederqvist's avatar
Per Cederqvist committed
2235
2236
2237
2238
2239
    {
	kom_errno = KOM_RECIPIENT_LIMIT;
	return FAILURE;
    }

2240
2241
2242
2243
2244
2245
2246
2247
    /* FIXME: Should extract the "is allowed to write to this conference"
       test from submit_to(). */
    if (submit_to(conf_no, conf_c) != conf_no)
    {
	kom_errno = KOM_PERM;
	return FAILURE;
    }

Per Cederqvist's avatar
Per Cederqvist committed
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
    switch ( type )
    {
    case recpt:
	if ( do_add_recpt(text_no, t_stat, conf_no) != OK)
	    return FAILURE;
	break;

    case cc_recpt:
	if ( do_add_cc_recpt(text_no, t_stat, conf_no) != OK)
	    return FAILURE;
	break;

    case comm_to:
    case comm_in:
    case footn_to:
    case footn_in:
    case loc_no:
    case rec_time:
    case sent_by:
    case sent_at:
	/* Fall through */
#ifndef COMPILE_CHECKS
    default:
#endif
	kom_errno = KOM_ILLEGAL_INFO_TYPE;
	return FAILURE;
    }
    
    if ( t_stat->author != ACTPERS )
	ADD_MISC(t_stat, sent_by, sender, ACTPERS);

    ADD_MISC(t_stat, sent_at, sent_at, time(NULL));

    mark_text_as_changed( text_no );
    return OK;
}


/*
 * Subtract a recipient from a text.
 *
 * This may be done by
 *	a) a supervisor of the author.
 *	b) a supervisor of the recipient.
 *	c) the sender of the text to the recipient
 */
extern Success
sub_recipient(	Text_no		text_no,
		Conf_no		conf_no)
{
    Text_stat * text_s;
    Conference * conf_c;
    
    CHK_LOGIN(FAILURE);
    GET_T_STAT(text_s, text_no, FAILURE);

    if ( !text_read_access(text_no, text_s ) && !ENA(admin, 4))
    {
	kom_errno = KOM_NO_SUCH_TEXT;
	return FAILURE;
    }
    
2310
    if (find_recipient(conf_no, text_s) == -1)
Per Cederqvist's avatar
Per Cederqvist committed
2311
2312
2313
2314
2315
2316
2317
2318
    {
	kom_errno = KOM_NOT_RECIPIENT;
	return FAILURE;
    }
    
    GET_C_STAT(conf_c, conf_no, FAILURE);

    
2319
2320
2321
    if (   !is_supervisor (text_s->author, NULL, ACTPERS, ACT_P)
	&& !is_supervisor (conf_no, conf_c, ACTPERS, ACT_P)
	&& !is_sender (text_s, conf_no) )
Per Cederqvist's avatar
Per Cederqvist committed
2322
2323
2324
2325
2326
2327
2328
2329 </