text.c 66 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: text.c,v 0.56 1998/10/22 22:08:47 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y 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 const char *
32
rcsid = "$Id: text.c,v 0.56 1998/10/22 22:08:47 ceder Exp $";
33 34
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
35

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

#include "server/smalloc.h"
47 48
#include "s-string.h"
#include "misc-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
49 50
#include "kom-types.h"
#include "services.h"
Per Cederqvist's avatar
Per Cederqvist committed
51
#include "manipulate.h"
Per Cederqvist's avatar
Per Cederqvist committed
52 53 54 55
#include "lyskomd.h"
#include "kom-errno.h"
#include "config.h"
#include "com.h"
56
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
57 58
#include "connections.h"
#include "internal-connections.h"
Per Cederqvist's avatar
Per Cederqvist committed
59 60 61 62
#include "cache.h"
#include "log.h"
#include "minmax.h"
#include "admin.h"
Per Cederqvist's avatar
Per Cederqvist committed
63
#include "send-async.h"
64
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
65
#include "kom-memory.h"
66
#include "aux-items.h"
Per Cederqvist's avatar
Per Cederqvist committed
67
#include "internal-services.h"
68
#include "local-to-global.h"
Per Cederqvist's avatar
Per Cederqvist committed
69

70 71 72 73 74 75 76 77 78 79 80 81 82

/*
 * Forward declarations
 */

static Text_no
do_create_text(const String  message,
               u_short	  no_of_misc,
               Misc_info   * misc,
               Aux_item_list * aux,
               Bool anonymous,
               Text_stat **ret_stat);

Per Cederqvist's avatar
Per Cederqvist committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

/*
 * 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)
{
98
    Local_text_no res;
Per Cederqvist's avatar
Per Cederqvist committed
99 100 101 102

    time( & conf_c->last_written );
    
    /* Add number last on the text_list */
103 104 105
    res = l2g_first_appendable_key(&conf_c->texts);
    l2g_append(&conf_c->texts, res, text_no);
    return res;
Per Cederqvist's avatar
Per Cederqvist committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}


/*
 * 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++ )
    {
122 123 124
	if ( misc->type == recpt ||
             misc->type == cc_recpt ||
             misc->type == bcc_recpt )
Per Cederqvist's avatar
Per Cederqvist committed
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
	{
	    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.
 */

187

Per Cederqvist's avatar
Per Cederqvist committed
188
static int
189 190
find_recipient(Conf_no	 conf_no,
	       Text_stat * t_stat)
Per Cederqvist's avatar
Per Cederqvist committed
191 192 193 194 195 196 197 198 199 200
{
    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 )
	    {
201
		return i;
Per Cederqvist's avatar
Per Cederqvist committed
202 203 204 205 206 207
	    }
	    break;
	    
	case cc_recpt:
	    if ( t_stat->misc_items[ i ].datum.cc_recipient == conf_no )
	    {
208
		return i;
Per Cederqvist's avatar
Per Cederqvist committed
209 210
	    }
	    break;
211 212 213 214 215 216 217

        case bcc_recpt:
            if ( t_stat->misc_items[ i ].datum.bcc_recipient == conf_no )
            {
                return i;
            }
            break;
Per Cederqvist's avatar
Per Cederqvist committed
218 219 220 221 222 223 224 225 226 227 228 229 230
	    
	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:
231
	    restart_kom("find_recipient(): illegal misc_item\n");
Per Cederqvist's avatar
Per Cederqvist committed
232 233 234 235
#endif
	}
    }

236
    return -1;
Per Cederqvist's avatar
Per Cederqvist committed
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 303 304 305 306
}


/*
 * 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);

307
    for ( i=0; i < param.max_super_conf_loop; i++)
Per Cederqvist's avatar
Per Cederqvist committed
308
    {
309
	acc = access_perm (conf_no, conf_c, ACTPERS, ACT_P);
Per Cederqvist's avatar
Per Cederqvist committed
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
	
	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;
}
   

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
static Success
do_add_bcc_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, bcc_recpt, bcc_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;
}
    
Per Cederqvist's avatar
Per Cederqvist committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
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;
}
446

Per Cederqvist's avatar
Per Cederqvist committed
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

/*
 * 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:
508
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
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 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
	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 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: */
564 565
		    l2g_delete(&conf_s->texts,
			       text_s->misc_items[ i+1 ].datum.local_no);
Per Cederqvist's avatar
Per Cederqvist committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
		    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 )
		{
581 582
		    l2g_delete(&conf_s->texts,
			       text_s->misc_items[ i+1 ].datum.local_no);
Per Cederqvist's avatar
Per Cederqvist committed
583 584 585 586 587 588 589 590 591 592
		    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;

593 594 595 596 597
	case bcc_recpt:
	    if ( text_s->misc_items[ i ].datum.bcc_recipient == conf_no )
	    {
		if ( conf_s != NULL )
		{
598 599
		    l2g_delete(&conf_s->texts,
			       text_s->misc_items[ i+1 ].datum.local_no);
600 601 602 603 604 605 606 607 608 609
		    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;

Per Cederqvist's avatar
Per Cederqvist committed
610 611 612 613 614 615 616 617 618 619 620 621
	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
622
	    log("%s: do_sub_recpt(): bad misc_item.\n", __FILE__);
Per Cederqvist's avatar
Per Cederqvist committed
623 624 625 626 627
	    break;
#endif
	}
    }

628
    err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
    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
646
	VOID_GET_T_STAT(text_s, comment);
Per Cederqvist's avatar
Per Cederqvist committed
647 648

    if ( parent_s == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
649
	VOID_GET_T_STAT(parent_s, comment_to);
Per Cederqvist's avatar
Per Cederqvist committed
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 701 702 703 704 705 706 707 708 709 710 711
    
    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
712
	VOID_GET_T_STAT(text_s, footnote);
Per Cederqvist's avatar
Per Cederqvist committed
713 714

    if ( parent_s == NULL )
Per Cederqvist's avatar
Per Cederqvist committed
715
	VOID_GET_T_STAT(parent_s, footnote_to);
Per Cederqvist's avatar
Per Cederqvist committed
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 774 775 776 777 778 779 780

    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:
781
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
782 783 784 785
	case comm_to:
	case comm_in:
	case footn_to:
	case footn_in:
Per Cederqvist's avatar
Per Cederqvist committed
786
	case sent_at:
Per Cederqvist's avatar
Per Cederqvist committed
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
	    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;

838 839 840 841 842 843 844 845
	case bcc_recpt:
	    if ( text_s->misc_items[ i ].datum.bcc_recipient == conf_no
		&& sender ( text_s, i ) == ACTPERS )
	    {
		return TRUE;
	    }
	    break;

Per Cederqvist's avatar
Per Cederqvist committed
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
	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:
896
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
	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.
920 921
 *
 * Note: Caller must set err_stat
Per Cederqvist's avatar
Per Cederqvist committed
922 923 924 925 926 927 928 929 930 931 932
 */

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

933
    if ( count_footn( t_stat ) >= param.max_foot )
Per Cederqvist's avatar
Per Cederqvist committed
934
    {
935
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
	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)
{
952
    if ( count_comment( t_stat ) >= param.max_comm )
Per Cederqvist's avatar
Per Cederqvist committed
953
    {
954
        err_stat = 0;
Per Cederqvist's avatar
Per Cederqvist committed
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
	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:
1018
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
	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
	}
    }
}

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 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 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 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 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 1273 1274 1275 1276 1277 1278 1279 1280


/*
 * Check if person is a member in any of the recipients or cc_recipients
 * of text_s
 */
static  Bool
is_member_in_recpt(Person    *person,
		   Text_stat *text_s)
{
    int i;

    for ( i = 0; i < text_s->no_of_misc; i++ )
    {
	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 bcc_recpt:
	    if ( locate_membership(text_s->misc_items[ i ].datum.bcc_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:
	    log("%s: is_member_in_recpt(): bad misc_item.\n", __FILE__);
	    break;
#endif
	}
    }

    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. */
                   Bool       output_bcc)
{
    Misc_info	* orig;
    Misc_info	* copy;		/* Censored Misc_infos */

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

    *result = *original;

    filter_aux_item_list(&original->aux_item_list,
                         &result->aux_item_list,
                         viewer,
                         viewer_p);

    result->misc_items = tmp_alloc(result->no_of_misc * sizeof ( Misc_info));
    result->no_of_misc = 0;
    copy = result->misc_items;
    orig = original->misc_items;

    while ( orig < original->misc_items + original->no_of_misc )
    {
	switch( orig->type )
	{
	case recpt:
	    if ( (fast_access_perm (orig->datum.recipient, viewer, viewer_p)
		  <= none )
		&& !ENA(admin, 4))
	    {
		skip_recp ( &orig, original );
	    }
	    else
	    {
		*copy++ = *orig++;
		++result->no_of_misc;
	    }
	    break;

	case cc_recpt:
	    if ( (fast_access_perm (orig->datum.cc_recipient, viewer, viewer_p)
		  <= none)
		&& !ENA(admin, 4))
	    {
		skip_recp ( &orig, original );
	    }
	    else
	    {
		*copy++ = *orig++;
		++result->no_of_misc;
	    }
	    break;

        case bcc_recpt:
            if (viewer_p == NULL)
                viewer_p = cached_get_person_stat(viewer);

            if (viewer_p == NULL ||
                (!is_supervisor(orig->datum.bcc_recipient, NULL,
                                viewer, viewer_p) &&
                 locate_membership(orig->datum.bcc_recipient,
                                   viewer_p) == NULL &&
                 !ENA(admin, 4)))
            {

                skip_recp ( &orig, original );
            }
            else if (!output_bcc)
            {
                *copy = *orig;
                copy->type = cc_recpt;
                copy += 1;
                orig += 1;
                ++result->no_of_misc;
            }
            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:
	    restart_kom("filter_secret_info() - illegal misc_item!\n");    
#endif
	}
    }
}


/*
 * Send message to all recipients that a text has been deleted
 */

static void
send_async_deleted_text (Text_no text_no, Text_stat *text_s)
{
    Connection *cptr;
    Text_stat filtered;
    Session_no i = 0;

    init_text_stat(&filtered);

    while ((i = traverse_connections(i)) != 0)
    {
        cptr = get_conn_by_number(i);
        filter_secret_info(&filtered, text_s,
                           cptr->pers_no, cptr->person, TRUE);
        if (cptr->person != NULL
            && is_member_in_recpt(cptr->person, &filtered) == TRUE)
        {
            async_deleted_text(cptr, text_no, &filtered);
        }
    }

    /*
     * This is not strictly necessary since the text-stat is sent
     * in the previous message, and the clients should be able to
     * update their caches from that information.
     */
    
    /*    for (misc = text_s->misc_items;
         misc < text_stat->misc_items + text_stat->no_of_misc;
         misc++)
    {
        if (misc->type == footn_in ||
            misc->type == comm_in  ||
            misc->type == comm_to  ||
            misc->type == footn_to)
        {
            i = 0;
            c_text_no = 0;
            if (misc->type == comm_to)
                c_text_no = misc->datum.comment_to;
            else if (misc->type == comm_in)
                c_text_no = misc->datum.comment_in;
            else if (misc->type == footn_in)
                c_text_no = misc->datum.footnote_in;
            else if (misc->type == footn_to)
                c_text_no = misc->datum.footnote_to;

            c_text = get_text_stat(c_text_no);
            if (c_text)
            {
                while ((i = traverse_connections(i)) != 0)
                {
                    cptr = get_conn_by_number(i);
                    if (cptr->person != NULL
                        && is_member_in_recpt(cptr->person, c_text) == TRUE)
                    {
                        async_invalidate_text(cptr,
                                              c_text_no);
                    }
                }
            }
        }
    }

    */
}


Per Cederqvist's avatar
Per Cederqvist committed
1281 1282 1283 1284 1285 1286 1287 1288 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 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
/*
 * 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;
	    }
1339 1340 1341 1342 1343 1344 1345 1346

            if ( misc->type == bcc_recpt
		&& (misc->datum.bcc_recipient == active_connection->cwc
		    || misc->datum.bcc_recipient == ACTPERS ))
	    {
		return TRUE;
	    }
                 
Per Cederqvist's avatar
Per Cederqvist committed
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
	}

	/* 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;
	    }
1365 1366 1367 1368 1369 1370 1371 1372

            if ( misc->type == bcc_recpt
		&& locate_membership( misc->datum.bcc_recipient, ACT_P)
                 != NULL)
	    {
		return TRUE;
	    }
}
Per Cederqvist's avatar
Per Cederqvist committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

	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 )
	{
1391 1392 1393 1394
	    if ( !recipient->type.rd_prot ||
                 ENA(wheel, 8) ||
                 is_supervisor(misc->datum.recipient, recipient,
                               ACTPERS, ACT_P) == TRUE)
Per Cederqvist's avatar
Per Cederqvist committed
1395 1396 1397 1398 1399 1400 1401 1402 1403
	    {
		return TRUE;
	    }
	}

 	if ( misc->type == cc_recpt
	    && (recipient =
		cached_get_conf_stat( misc->datum.cc_recipient )) != NULL )
	{
1404 1405 1406 1407
	    if ( !recipient->type.rd_prot ||
                 ENA(wheel, 8) ||
                 is_supervisor(misc->datum.cc_recipient, recipient,
                               ACTPERS, ACT_P) == TRUE)
Per Cederqvist's avatar
Per Cederqvist committed
1408 1409 1410 1411
	    {
		return TRUE;
	    }
	}
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423

        if (misc->type == bcc_recpt
            && (recipient =
                cached_get_conf_stat( misc->datum.bcc_recipient )) != NULL )
        {
            if ( ENA(wheel, 8) ||
                 is_supervisor(misc->datum.bcc_recipient, recipient,
                               ACTPERS, ACT_P) == TRUE )
            {
                return TRUE;
            }
        }
Per Cederqvist's avatar
Per Cederqvist committed
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
    }

    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;
    
    if ( text_s == NULL )
	GET_T_STAT(text_s, text_no, FAILURE);

1443 1444
    send_async_deleted_text(text_no, text_s);

Per Cederqvist's avatar
Per Cederqvist committed
1445
    if ((author = cached_get_person_stat (text_s->author)) != NULL)
1446
	l2g_delete_global_in_sorted(&author->created_texts, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464

    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;
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474

        case bcc_recpt:
	    if ( do_sub_recpt(text_no, text_s,
			      text_s->misc_items[ 0 ].datum.bcc_recipient,
                              NULL)
		!= OK )
		restart_kom("do_delete_text(): error pos 2.\n");
	    break;

            
Per Cederqvist's avatar
Per Cederqvist committed
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 1528 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
	    
	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 )
    {
1556

Per Cederqvist's avatar
Per Cederqvist committed
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
	kom_errno = KOM_NO_SUCH_TEXT;
	err_stat = text_no;
	
    	return FAILURE;
    }

    *result = cached_get_text( text_no );

    if ( start_char > result->len )
    {
1567
        err_stat = start_char;
Per Cederqvist's avatar
Per Cederqvist committed
1568 1569 1570 1571
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return FAILURE;
    }

Per Cederqvist's avatar
Per Cederqvist committed
1572
    /* Should use something in s-string.c to do this. +++ */
Per Cederqvist's avatar
Per Cederqvist committed
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
    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;
}

1585

Per Cederqvist's avatar
Per Cederqvist committed
1586
/*
1587 1588 1589 1590
 * Get text status.
 *
 * If there are recipients of the text that are secret confs
 * those misc-items will be censored.
Per Cederqvist's avatar
Per Cederqvist committed
1591
 */
1592 1593 1594 1595

extern Success
get_text_stat_old (Text_no text_no,
                   Text_stat *result)
Per Cederqvist's avatar
Per Cederqvist committed
1596
{
1597
    Text_stat	* text_stat;
Per Cederqvist's avatar
Per Cederqvist committed
1598

1599 1600 1601
    GET_T_STAT(text_stat, text_no, FAILURE);
    
    if ( !text_read_access(text_no, text_stat) && !ENA(admin, 2))
Per Cederqvist's avatar
Per Cederqvist committed
1602
    {
1603 1604 1605
	kom_errno = KOM_NO_SUCH_TEXT;
        err_stat = text_no;
	return FAILURE;
Per Cederqvist's avatar
Per Cederqvist committed
1606 1607
    }

1608 1609
    filter_secret_info(result, text_stat, ACTPERS, ACT_P, FALSE);
    return OK;
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
}

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;
1623
        err_stat = text_no;
1624 1625 1626
	return FAILURE;
    }

1627
    filter_secret_info(result, text_stat, ACTPERS, ACT_P, TRUE);
Per Cederqvist's avatar
Per Cederqvist committed
1628 1629 1630 1631 1632 1633 1634
    return OK;
}

/*
 * Functions local to create_text:
 */

1635 1636 1637 1638 1639 1640

/*
 * Check that the text can be submitted anonymously
 */

static Success
1641
check_anonymous_subm(Conf_no addressee)
1642 1643 1644 1645
{
    Conf_type		conf_type;

    conf_type = cached_get_conf_type(addressee);
1646
    if (conf_type.allow_anon)
1647 1648 1649 1650 1651
	return OK;
    else
	return FAILURE;
}

Per Cederqvist's avatar
Per Cederqvist committed
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
/*
 * 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;

1679 1680 1681 1682 1683 1684
	case bcc_recpt:
	    if ( misc[ j ].datum.bcc_recipient == addressee )
		return FAILURE;

	    break;

Per Cederqvist's avatar
Per Cederqvist committed
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
	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:
1697
	    log("%s: check_double_subm(): bad misc_item.\n", __FILE__);
Per Cederqvist's avatar
Per Cederqvist committed
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 1725 1726 1727 1728 1729
	    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:
1730
        case bcc_recpt:
Per Cederqvist's avatar
Per Cederqvist committed
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
	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
1741
	    log("%s: check_double_subm(): bad misc_item.\n", __FILE__);
Per Cederqvist's avatar
Per Cederqvist committed
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	    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,
1760 1761
			Misc_info   * misc,
			Bool	      anonymous )
Per Cederqvist's avatar
Per Cederqvist committed
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
{
    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:
1792
	    /* Check that the text exists and is readable. */
Per Cederqvist's avatar
Per Cederqvist committed
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804

	    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;
	    }
1805 1806 1807 1808 1809 1810

	    if (!text_read_access(misc[i].datum.comment_to, parent))
	    {
		kom_errno = KOM_NO_SUCH_TEXT;
		return FAILURE;
	    }
Per Cederqvist's avatar
Per Cederqvist committed
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835
	    
	    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;
	    }
1836 1837 1838

	    /* Check that an anonymous text can be created */

1839
	    if (anonymous && check_anonymous_subm(addressee) != OK)
1840
	    {
1841
		kom_errno = KOM_ANON_REJECTED;
1842 1843 1844
		return FAILURE;
	    }
	       
Per Cederqvist's avatar
Per Cederqvist committed
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
	    
	    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;
	    }
1870 1871 1872

	    /* Check that an anonymous text can be created */

1873
	    if (anonymous && check_anonymous_subm(addressee) != OK)
1874
	    {
1875
		kom_errno = KOM_ANON_REJECTED;
1876 1877 1878
		return FAILURE;
	    }
	       
Per Cederqvist's avatar
Per Cederqvist committed
1879
	    
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
	    break;

	case bcc_recpt:
	    /* Check that ACTPERS has write access to the conference or a
	       superconference. */
	    /* Superconfs are recursive */

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

	    /* Update in case of super_conf */
	    
	    if ((misc[ i ].datum.bcc_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;
	    }

	    /* Check that an anonymous text can be created */

	    if (anonymous && check_anonymous_subm(addressee) != OK)
	    {
		kom_errno = KOM_ANON_REJECTED;
		return FAILURE;
	    }
	       
	    
Per Cederqvist's avatar
Per Cederqvist committed
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
	    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;
}

1936 1937 1938 1939 1940 1941

/*
 * Add the aux_infos to the new text stat
 */
static Success
create_text_add_aux(Text_stat *t_stat,
1942
                    Text_no text_no,
1943 1944 1945 1946 1947 1948
                    Aux_item_list *aux,
                    Pers_no creator)
{
    u_short i;
    Text_stat *parent;

David Byers's avatar