membership.c 37.4 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: membership.c,v 0.33 1998/07/08 16:35:27 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 30
/*
 * membership.c
 *
 * All atomic calls that controlls who is a member in what.
 * (The person/conf relation).
 */
Per Cederqvist's avatar
Per Cederqvist committed
31

Per Cederqvist's avatar
Per Cederqvist committed
32 33
#define DEBUG_MARK_AS_READ

34 35
static const char *
rcsid = "$Id: membership.c,v 0.33 1998/07/08 16:35:27 ceder Exp $";
36 37
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
38

Per Cederqvist's avatar
Per Cederqvist committed
39
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
40 41 42
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
43
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
44
#include <setjmp.h>
45 46 47 48 49
#ifdef DEBUG_MARK_AS_READ
#  ifdef HAVE_STDARG_H
#    include <stdarg.h>
#  endif
#endif
50
#include <sys/types.h>
Per Cederqvist's avatar
Per Cederqvist committed
51 52

#include "misc-types.h"
53
#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
54 55 56
#include "kom-types.h"
#include "services.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
57 58
#include "lyskomd.h"
#include "com.h"
59
#include "async.h"
Per Cederqvist's avatar
Per Cederqvist committed
60 61
#include "connections.h"
#include "internal-connections.h"
Per Cederqvist's avatar
Per Cederqvist committed
62 63 64 65 66
#include "kom-errno.h"
#include "manipulate.h"
#include "cache.h"
#include "send-async.h"
#include "minmax.h"
Per Cederqvist's avatar
Per Cederqvist committed
67
#include "kom-memory.h"
68
#include "param.h"
Per Cederqvist's avatar
Per Cederqvist committed
69 70

#ifdef DEBUG_MARK_AS_READ
71 72
#  include "log.h"
#  include "ram-output.h"
Per Cederqvist's avatar
Per Cederqvist committed
73 74
#endif

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static void
set_membership_type_bits(Membership_type *type,
                    Bool invitation, Bool passive, Bool secret,
                    Bool reserved1, Bool reserved2, Bool reserved3, 
                    Bool reserved4, Bool reserved5)
{
    type->invitation = invitation;
    type->passive = passive;
    type->secret = secret;
    type->reserved1 = reserved1;
    type->reserved2 = reserved2;
    type->reserved3 = reserved3;
    type->reserved4 = reserved4;
    type->reserved5 = reserved5;
}

Per Cederqvist's avatar
Per Cederqvist committed
91 92 93 94 95 96 97 98 99
/*
 * Copy all information that ACTPERS is authorized to know about ORIG_P's
 * membership in all conferences to CENSOR_P.
 *
 * This function is used in get_membership().
 */
static void
copy_public_confs (Person   * censor_p,	/* The censored Person-struct */
		   Person   * orig_p,	/* The uncensored Person-struct */
100 101
		   Bool	      want_read,
                   Bool       copy_secret) /* Does ACTPERS want to know
Per Cederqvist's avatar
Per Cederqvist committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
					  * which texts are read?*/
{
    int i;			/* Number of mships lefte in ORIG_P */
    Membership * censor_m;		/* Pointer in CENSOR_P */
    Membership * orig_m;	/* Pointer in ORIG_P */
    

    /* Copy all information except the secret. */
		    
    censor_p->conferences.confs
	= tmp_alloc( orig_p->conferences.no_of_confs * sizeof(Membership));
    censor_p->conferences.no_of_confs = 0;

    censor_m = censor_p->conferences.confs;
    orig_m   = orig_p->conferences.confs;

    for ( i = 0; i < orig_p->conferences.no_of_confs; i++, orig_m++ )
    {
120 121
	if ( fast_access_perm (orig_m->conf_no, ACTPERS, ACT_P) > none &&
             (copy_secret || !orig_m->type.secret) )
Per Cederqvist's avatar
Per Cederqvist committed
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
	{
	    *censor_m = *orig_m;

	    if ( orig_p->flags.unread_is_secret || !want_read )
	    {
		censor_m->no_of_read = 0;
		censor_m->read_texts = NULL;
	    }

	    if ( orig_p->flags.unread_is_secret )
	    {
		censor_m->last_time_read = NO_TIME;
		censor_m->last_text_read = 0;
	    }
	    
	    ++censor_m;
	    ++censor_p->conferences.no_of_confs;
	}
    }
}

/*
 * Change the priority of a certain conference in a person.
 */
static void
do_change_priority (Membership * mship,
148 149
                    Conf_no conf_no,
                    Conference * conf_c,
150 151
		    unsigned char	 priority,
		    unsigned short	 where,
Per Cederqvist's avatar
Per Cederqvist committed
152
		    Pers_no	 pers_no,
153 154
		    Person     * pers_p,
                    Membership_type * type)
Per Cederqvist's avatar
Per Cederqvist committed
155 156
{
    Membership tmp_conf;
157
    unsigned short i;
Per Cederqvist's avatar
Per Cederqvist committed
158 159
    
    mship->priority = priority;
160
    mship->type = *type;
Per Cederqvist's avatar
Per Cederqvist committed
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 190 191
    
    /* Check range of where */
    
    if ( where >= pers_p->conferences.no_of_confs )
    {
	where = pers_p->conferences.no_of_confs - 1;
    }
    
    /* And now move the conference to slot number 'where' */
    
    if ( mship < pers_p->conferences.confs + where )
    {
	tmp_conf = *mship;
	while ( mship < pers_p->conferences.confs + where)
	{
	    *mship = *(mship + 1);
	    mship++;
	}
	*mship = tmp_conf;
    }
    else
    {
	tmp_conf = *mship;
	while ( mship > pers_p->conferences.confs + where)
	{
	    *mship = *(mship - 1);
	    mship--;
	}
	*mship = tmp_conf;
    }

192 193 194 195 196 197 198 199 200 201
    
    for (i = 0; i < conf_c->members.no_of_members; i++)
    {
        if (conf_c->members.members[i].member == pers_no)
        {
            conf_c->members.members[i].type = *type;
            mark_conference_as_changed( conf_no );
        }
    }

Per Cederqvist's avatar
Per Cederqvist committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    mark_person_as_changed( pers_no );
}


/*
 * Insert a rec_time misc item to a text_status.
 * The item is put at position POS on the list. (0 == first)
 * Take no action if the misc_item at POS is a rec_time.
 * This function is only used when a person marks his letters as read.
 *
 *	Text_stat * text_stat_pointer	Textstatus to modify
 *	int	    pos			Where to insert rec_time
 */

static void
do_add_rec_time (Text_stat    * text_stat_ptr,
		 int		pos)
{
    int i;

    /* Defensive checks */
    if ( pos < 0 || pos > text_stat_ptr->no_of_misc )
    {
	restart_kom("do_add_rec_time() - illegal pos\n");
    }

    /* Check that no rec_time exists */

    if ( pos < text_stat_ptr->no_of_misc
	&& text_stat_ptr->misc_items[ pos ].type == rec_time )
    {
	return;
    }
    
    /* Allocate space */
    text_stat_ptr->misc_items
    = srealloc(text_stat_ptr->misc_items,
	       (++(text_stat_ptr->no_of_misc)) * sizeof(Misc_info));

    /* Move items. */

    for ( i = text_stat_ptr->no_of_misc - 1; i > pos; i-- )
    {
	text_stat_ptr->misc_items[ i ] = text_stat_ptr->misc_items[ i - 1 ];
    }

    /* Set type */
    text_stat_ptr->misc_items[ pos ].type = rec_time;

    /* Set value */
    time( & text_stat_ptr->misc_items[ pos ].datum.received_at);
}

/*
 * add_rec_time adds a 'rec_time'  misc_item to text number LOC_NO in
257
 * conference CONF_C. The item will follow a recpt or cc_recpt to ACTPERS.
Per Cederqvist's avatar
Per Cederqvist committed
258 259 260 261
 * No action is taken if ACTPERS is not a recipient of the text, or the text
 * no longer exists, or the text has already been received.
 */
static void
262
add_rec_time(Conference * conf_c,
Per Cederqvist's avatar
Per Cederqvist committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
	     Local_text_no local_no)
{
    Bool 	  found;
    Text_no	  text_no;
    Text_stat 	* t_stat;
    int		  i;
    
    if ( local_no >= conf_c->texts.first_local_no + conf_c->texts.no_of_texts
	|| local_no < conf_c->texts.first_local_no )
    {
	return;			/* No longer exists in conf. */
    }

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

    if ( text_no == 0 )
    {
	return;			/* Text is deleted. */
    }
    
Per Cederqvist's avatar
Per Cederqvist committed
283
    VOID_GET_T_STAT(t_stat, text_no);
Per Cederqvist's avatar
Per Cederqvist committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

    /* locate the misc_item which says that ACTPERS is a recipient */

    for ( found = FALSE, i = 0; !found && i < t_stat->no_of_misc; i++ )
    {
	switch ( t_stat->misc_items[ i ].type )
	{
	case recpt:
	    if ( t_stat->misc_items[ i ].datum.recipient == ACTPERS )
	    {
		do_add_rec_time( t_stat, i + 2 ); /* Add after loc_no */
		found = TRUE;
	    }
	    break;

	case cc_recpt:
	    if ( t_stat->misc_items[ i ].datum.cc_recipient == ACTPERS )
	    {
		do_add_rec_time( t_stat, i + 2 );
		found = TRUE;
	    }
	    break;

307 308 309 310 311 312 313 314
	case bcc_recpt:
	    if ( t_stat->misc_items[ i ].datum.bcc_recipient == ACTPERS )
	    {
		do_add_rec_time( t_stat, i + 2 );
		found = TRUE;
	    }
	    break;

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

    if( found ==  FALSE )
    {
	log("ERROR: add_rec_time(): found==FALSE\n");
    }
    
    mark_text_as_changed( text_no);
    return;
}

/*
 * Check if there are some texts immediately following last_text_read
 * that are read or deleted. If so, update last_text_read and delete them
 * from read_texts.
 *
 * This is only used from mark_as_read().
 */
static void
adjust_read( Membership * m,
	     const Conference * conf)
{
347 348 349 350
    /* The first HANDLED texts in read_texts in M are already
       included in last_text_read. */
    unsigned short  handled;
    unsigned short  i;
Per Cederqvist's avatar
Per Cederqvist committed
351 352 353
    Local_text_no * locp;
    Local_text_no   conf_max;	/* Highest used local_text_no in conf */
    Local_text_no   conf_min;	/* Lowest used local_text_no in conf */
354
#ifndef NDEFENSIVE_CHECKS
Per Cederqvist's avatar
Per Cederqvist committed
355 356 357 358 359 360 361
    Local_text_no prev;
#endif

    /* (conf_min <= x <= conf_max) if x is an existing local_text_no */
    conf_max = conf->texts.first_local_no + conf->texts.no_of_texts - 1;
    conf_min = conf->texts.first_local_no;

362 363 364
    /* Flag all removed texts as read, if that is not already done. */
    if (m->last_text_read < conf_min - 1)
	m->last_text_read = conf_min - 1;
Per Cederqvist's avatar
Per Cederqvist committed
365

366 367 368 369 370 371
    /* Skip any texts in read_texts which are already handled. */
    for (handled = 0; handled < m->no_of_read
		      && m->read_texts[handled] < m->last_text_read + 1; )
    {
	    handled++;
    }
Per Cederqvist's avatar
Per Cederqvist committed
372

373 374 375 376 377 378 379 380 381 382
    /* This loops advances m->last_text_read as far as possible,
       advancing handled along when appropriate.  m->last_text_read
       can be increased for one of two reasons:
            The text is present in read_texts.
	    The text is deleted.
       This loop handles both cases. */
    for ( ; ; )
    {
	if (handled < m->no_of_read
	    && m->read_texts[handled] == m->last_text_read + 1)
Per Cederqvist's avatar
Per Cederqvist committed
383
	{
384 385 386
	    /* This text is present in read_texts. */
	    m->last_text_read++;
	    handled++;
Per Cederqvist's avatar
Per Cederqvist committed
387
	}
388 389
	else if (m->last_text_read < conf_max
		 && conf->texts.texts[m->last_text_read + 1 - conf_min] == 0)
390 391 392 393 394 395 396 397 398 399
	{
	    /* This text is deleted. */
	    m->last_text_read++;
	}
	else
	    break;
    }
    
    /* Delete all handled entries in read_texts. */
    if (handled > 0)
Per Cederqvist's avatar
Per Cederqvist committed
400
    {
401
	m->no_of_read -= handled;
Per Cederqvist's avatar
Per Cederqvist committed
402 403 404 405 406
    
	for (locp = m->read_texts;
	     locp  < m->read_texts + m->no_of_read;
	     locp++)
	{
407
	    *locp = *(locp + handled);
Per Cederqvist's avatar
Per Cederqvist committed
408 409 410
	}
    }

411
#ifndef NDEFENSIVE_CHECKS
Per Cederqvist's avatar
Per Cederqvist committed
412 413 414 415 416 417 418 419 420 421 422 423
    
    /* Check that the items in read_texts really ARE sorted in ascending order.
       If not, there is probably a bug in this routine or in mark_as_read */
    
    prev = m->last_text_read;
    
    for ( i = 0; i < m->no_of_read; i++)
    {
	if ( prev >= m->read_texts[ i ] )
	{
	    log("Bug in adjust_read. Conference %lu, Priority %lu\n"
		"\tprev = %lu, i = %lu, m->read_texts[i] = %lu\n",
424
		(unsigned long)m->conf_no, (unsigned long)m->priority,
425 426
		(unsigned long)prev, (unsigned long)i,
		(unsigned long)m->read_texts[i]);
Per Cederqvist's avatar
Per Cederqvist committed
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
	}

	prev = m->read_texts[ i ];
    }
#endif
}

/*
 * insert TEXT in the list of read_texts in M. The texts are sorted.
 * m->no_of_read is updated. m->read_texts is never reallocated, and must
 * thus be big enough to hold the new number.
 *
 * Returns FAILURE if the text is already read.
 *
 * This is only used from mark_as_read().
 */

static Success
insert_loc_no(Local_text_no   text,
	      Membership    * m)
{
    Local_text_no * seek, * move;
    
    
    if ( text <= m->last_text_read )
    {
	return FAILURE;			/* This text was already read. */
    }

    for ( seek = m->read_texts; seek < m->read_texts + m->no_of_read;  seek++)
    {
	if ( text == *seek )
	{
	    return FAILURE;		/* This text was already read. */
	}

	if ( text < *seek )
	{			/* The text should be entered here. */
	    for ( move = m->read_texts + m->no_of_read; move > seek; move--)
	    {
		*move = *(move - 1);
	    }

	    *seek = text;
	    ++(m->no_of_read);

	    return OK;
	}
    }

    *seek = text;		/* The text had a higher number than any */
    ++(m->no_of_read);		/* previously read text.		 */
    
    return OK;
}

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

/*
 * Add a member to a conference. All errorchecking should already
 * be done when this function is called. The person must not already
 * be a member of the conference. It is _not_ an error to make WHERE bigger
 * than the number of conferences the person is a member in.
 */

void
do_add_member(Conf_no	   conf_no,  /* Conference to add a new member to. */
	      Conference * conf_c,   /* Conf. status. Must NOT be NULL.  */
	      Pers_no	   pers_no,  /* Person to be added. */
	      Person	 * pers_p,   /* Pers. status. Must NOT be NULL. */
503
	      unsigned char	   priority, /* Prioritylevel to assign to this conf */
504 505 506
	      unsigned short	   where,   /* Sequence number in the list */
              Membership_type      * type
              )
Per Cederqvist's avatar
Per Cederqvist committed
507 508
{
    Membership  * mship;
509
    Member	* mbr;
Per Cederqvist's avatar
Per Cederqvist committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

    /* First add the conference in the person-struct.
     * Make room for it.
     */
    
    pers_p->conferences.confs = srealloc( pers_p->conferences.confs,
					 ++(pers_p->conferences.no_of_confs)
					 * sizeof(Membership));
    
    /* Fill in the room */

    /* Find last slot */    
    mship = pers_p->conferences.confs + pers_p->conferences.no_of_confs - 1 ;
    
    /* Move all data beyond WHERE */
    while ( mship > pers_p->conferences.confs + where )
    {
	*mship = *(mship - 1);
	mship--;
    }

531
    init_membership(mship);
Per Cederqvist's avatar
Per Cederqvist committed
532
    
533 534
    mship->added_by = ACTPERS;
    mship->added_at = time(NULL);
Per Cederqvist's avatar
Per Cederqvist committed
535 536 537 538 539 540
    mship->conf_no = conf_no;
    mship->priority = priority;
    mship->last_time_read = time(NULL);
    mship->last_text_read = 0;
    mship->no_of_read = 0;
    mship->read_texts = NULL;
541 542
    mship->type = *type;

Per Cederqvist's avatar
Per Cederqvist committed
543 544 545 546 547 548 549 550
    /* Make room for the person in the conference */
    
    conf_c->members.members = srealloc( conf_c->members.members,
				       ++(conf_c->members.no_of_members)
				       * sizeof(Member));

    /* New members go to the end of the list */
    
551 552 553 554 555
    mbr = (conf_c->members.members + conf_c->members.no_of_members - 1);
    mbr->member = pers_no;
    mbr->added_by = ACTPERS;
    mbr->added_at = time(NULL);
    mbr->type = *type;
Per Cederqvist's avatar
Per Cederqvist committed
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
    
    mark_conference_as_changed( conf_no );
    mark_person_as_changed( pers_no );
    
    return;
}

/*
 * Send an asynchronous message to person pers_no (if he is logged on)
 * and tell him that he is no longer a member of conf_no. Also calls
 * leave_conf(). 
 */
extern void
forced_leave_conf(Pers_no pers_no,
		  Conf_no conf_no)
{
    Connection *real_active_connection;
    Session_no i = 0;
    
    real_active_connection = active_connection;

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

	if ( active_connection->pers_no == pers_no )
	{
	    async_forced_leave_conf(active_connection, conf_no);

	    if ( active_connection->cwc == conf_no )
		leave_conf();
	}
    }

    active_connection = real_active_connection;
}

/*
 * Delete a member from a conference.
 * No checks are made on the parameters.
 * The dynamically allocated areas conf_c->members.members and
 * pers_p->confs are NOT reallocated since they will anyhow sooner or later
 * be flushed from core.
 */

Success
do_sub_member(Conf_no	   conf_no, /* Conf to delete member from. */
	      Conference * conf_c,  /* May be NULL */
604
	      Member     * mbr,     /* May be NULL */
Per Cederqvist's avatar
Per Cederqvist committed
605 606 607 608 609 610 611 612 613 614 615 616 617 618
	      Pers_no	   pers_no, /* Person to be deleted. */
	      Person	 * pers_p,  /* May be NULL */
	      Membership * mship)   /* Pointer to the persons membership in
				       conf., or NULL if not known. */
{
    if ( conf_c == NULL )
	GET_C_STAT(conf_c, conf_no, FAILURE);
    
    if ( pers_p == NULL )
	GET_P_STAT(pers_p, pers_no, FAILURE);
    
    if ( mship == NULL && (mship = locate_membership(conf_no, pers_p)) == NULL)
	restart_kom("do_sub_member() - can't find mship\n");

619
    if ( mbr == NULL && (mbr = locate_member(pers_no, conf_c)) == NULL)
Per Cederqvist's avatar
Per Cederqvist committed
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
	restart_kom("do_sub_member() - can't find member.\n");

    forced_leave_conf(pers_no, conf_no);
    
    /* Delete from Person */

    sfree( mship->read_texts );
    --pers_p->conferences.no_of_confs;
    while ( mship
	   < pers_p->conferences.confs + pers_p->conferences.no_of_confs )
    {
	*mship = *(mship + 1);
	++mship;
    }

    /* Delete from Conference */

    --conf_c->members.no_of_members;
638
    while ( mbr < conf_c->members.members + conf_c->members.no_of_members )
Per Cederqvist's avatar
Per Cederqvist committed
639
    {
640 641
	*mbr = *(mbr + 1);
	++mbr;
Per Cederqvist's avatar
Per Cederqvist committed
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
    }

    mark_person_as_changed( pers_no );
    mark_conference_as_changed( conf_no );
    
    return OK;    
}


/*
 * VICTIM is a person or a conference.
 * Meaning of return values:
 *	unlimited: ACTPERS is supervisor of VICTIM, or ACTPERS is admin,
 *		   or ACTPERS is VICTIM
 *	none:	   VICTIM is secret, and ACTPERS is not a member
 *	member:	   ACTPERS is a member in VICTIM, but doesn't have unlimited
 *		   access.
 *	read_protected: The conference is rd_prot and ACTPERS is not a member.
 *	limited:   otherwise.
 *	error:	   see kom_errno
 */

Access
access_perm(Conf_no 	  victim,
666 667 668
	    Conference	* victim_c,	/* May be NULL */
	    Pers_no	  viewer,
	    Person	* viewer_p)     /* May be NULL */
Per Cederqvist's avatar
Per Cederqvist committed
669
{
670
    if (victim == viewer)
Per Cederqvist's avatar
Per Cederqvist committed
671 672 673 674 675
    	return unlimited;

    if (victim_c == NULL)
	GET_C_STAT(victim_c, victim, error);
    
676
    if ( viewer != 0 && ENA(admin, 2) )
Per Cederqvist's avatar
Per Cederqvist committed
677 678
    	return unlimited;

679 680
    if ( is_supervisor(victim, victim_c, viewer, viewer_p) ||
        ENA(wheel, 8))
Per Cederqvist's avatar
Per Cederqvist committed
681 682
    	return unlimited;

683 684 685 686 687 688 689 690
    if ( viewer != 0 )
    {
	if ( viewer_p == NULL )
	    GET_P_STAT(viewer_p, viewer, error);

	if ( locate_membership( victim, viewer_p ) != NULL )
	    return member;
    }
Per Cederqvist's avatar
Per Cederqvist committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704

    if ( victim_c->type.secret )
        return none;

    if ( victim_c->type.rd_prot )
	return read_protected;
    
    return limited;
}


/*
 * Fast version of access_perm. See comment in file server/manipulate.h
 * where Access is defined.
705 706 707
 *
 * Check if viewer is allowed to look at victiom. viewer_p, if
 * supplied, should be a pointer to the pers-stat of viewer.
Per Cederqvist's avatar
Per Cederqvist committed
708 709
 */
Access
710 711 712
fast_access_perm(Conf_no  victim,
		 Pers_no  viewer,
		 Person  *viewer_p) /* May be NULL. */
Per Cederqvist's avatar
Per Cederqvist committed
713 714
{
    Conf_type conf_type;
715 716 717 718

    if ( !cached_conf_exists(victim) )
	return error;

719
    if ( viewer != 0 && (ENA(admin, 2) || ENA(wheel,8) || viewer == victim) )
Per Cederqvist's avatar
Per Cederqvist committed
720 721
    	return unlimited;

722 723 724 725 726 727 728 729
    if ( viewer != 0 )
    {
	if ( viewer_p == NULL )
	    GET_P_STAT(viewer_p, viewer, error);

	if ( locate_membership( victim, viewer_p ) != NULL )
	    return member;
    }
Per Cederqvist's avatar
Per Cederqvist committed
730

Per Cederqvist's avatar
Per Cederqvist committed
731 732 733
    /* Only read in conference struct when really necessary. */
    conf_type = cached_get_conf_type (victim);
    if ( conf_type.secret )
734
	return access_perm(victim, NULL, viewer, viewer_p);
Per Cederqvist's avatar
Per Cederqvist committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749

    if ( conf_type.rd_prot )
	return read_protected;
    
    return limited;
}

/*
 * Locate the Member struct in CONF_C for person PERS_NO
 */

Member *
locate_member(Pers_no      pers_no,
	      Conference * conf_c)
{
750
    Member * mbr;
Per Cederqvist's avatar
Per Cederqvist committed
751 752
    int      i;

753 754
    for(mbr = conf_c->members.members, i = conf_c->members.no_of_members;
	i > 0; i--, mbr++)
Per Cederqvist's avatar
Per Cederqvist committed
755
    {
756
	if ( mbr->member == pers_no )
Per Cederqvist's avatar
Per Cederqvist committed
757
	{
758
	    return mbr;
Per Cederqvist's avatar
Per Cederqvist committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	}
    }

    return NULL;
}


/*
 * Find the data about PERS_P:s membership in CONF_NO.
 * Return NULL if not found
 */

Membership *
locate_membership(Conf_no     conf_no,
		  Person    * pers_p)
{
    Membership * confp;
    int    i;

    for(confp = pers_p->conferences.confs, i = pers_p->conferences.no_of_confs;
	i > 0; i--, confp++)
    {
	if ( confp->conf_no == conf_no )
	{
	    return confp;
	}
    }

    return NULL;
}

/*
 * Atomic functions.
 */

/*
 * Unsubscribe from a conference.
 *
 * You must be supervisor of either conf_no or pers_no to be allowed to
 * do this.
 *
 * BUGS: There is no passive membership.
 */
extern Success
sub_member(	Conf_no		conf_no,
		Pers_no		pers_no )
{
    Conference  * conf_c;
    Membership	* mship;
    Person	* pers_p;
    
    CHK_LOGIN(FAILURE);
    GET_C_STAT(conf_c, conf_no, FAILURE);
    GET_P_STAT(pers_p, pers_no, FAILURE);

    if( (mship = locate_membership(conf_no, pers_p) ) == NULL)
    {
816
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
817 818 819 820
	kom_errno = conf_c->type.secret ? KOM_UNDEF_CONF : KOM_NOT_MEMBER;
	return FAILURE;
    }

821 822 823 824
    if ( !is_supervisor(conf_no, conf_c, ACTPERS, ACT_P) &&
	 !is_supervisor(pers_no, NULL, ACTPERS, ACT_P) &&
         !ENA(wheel,8) &&
         !ENA(admin, 4) )
Per Cederqvist's avatar
Per Cederqvist committed
825
    {
826
        err_stat = conf_no;
827 828 829 830 831 832 833
        if (conf_c->type.secret)
          kom_errno = KOM_UNDEF_CONF;
        else if (mship->type.secret)
          kom_errno = KOM_NOT_MEMBER;
        else
          kom_errno = KOM_PERM;

Per Cederqvist's avatar
Per Cederqvist committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
	return FAILURE;
    }

    return do_sub_member(conf_no, conf_c, NULL, pers_no, pers_p, mship);    
}


/*
 * Add a member to a conference (join a conference) or
 * Change the priority of a conference.
 *
 * Anyone may add anyone as a member as long as the new member is not
 * secret and the conference is not rd_prot. This might be a bug.
 *
 * PRIORITY is the assigned priority for the conference. WHERE says
 * where on the list the person wants the conference. 0 is first. WHERE
 * is automatically set to the number of conferences that PERS_NO is member
 * in if WHERE is too big, so it is not an error to give WHERE == ~0 as
 * a parameter.
 *
 * You can only re-prioritize if you are supervisor of pers_no.
 */
856 857 858 859 860 861 862 863 864 865 866 867 868 869

extern Success
add_member_old(Conf_no	conf_no,
               Pers_no	pers_no,
               unsigned char	priority,
               unsigned short	where)
{
    Membership_type type;

    set_membership_type_bits(&type, param.invite_by_default,0,0,0,0,0,0,0);
    return add_member(conf_no, pers_no, priority, where, &type);
}


Per Cederqvist's avatar
Per Cederqvist committed
870 871
extern Success
add_member(Conf_no	conf_no,
872 873 874 875 876
           Pers_no	pers_no,
           unsigned char	priority,
           unsigned short	where,		/* Range of where is [0..] */
           Membership_type     *type
           )
Per Cederqvist's avatar
Per Cederqvist committed
877 878 879 880 881 882 883
{
    Conference  * conf_c, * pers_c;
    Person	* pers_p;
    Membership	* mship;

    CHK_LOGIN(FAILURE);
    GET_C_STAT(conf_c, conf_no, FAILURE);
884
    GET_C_STAT(pers_c, pers_no, FAILURE);
Per Cederqvist's avatar
Per Cederqvist committed
885 886
    GET_P_STAT(pers_p, pers_no, FAILURE);

887 888 889 890 891 892 893 894 895 896 897 898 899 900
    if (param.invite_by_default &&
        !is_supervisor(pers_no, pers_c, ACTPERS, ACT_P) &&
        !ENA(admin, 4))
    {
        type->invitation = 1;
    }

    if (!param.secret_memberships && type->secret)
    {
        err_stat = 0;
        kom_errno = KOM_INVALID_MEMBERSHIP_TYPE;
        return FAILURE;
    }

901
    if ( access_perm(conf_no, conf_c, ACTPERS, ACT_P) < limited
902
	&& !ENA(wheel, 8) )
Per Cederqvist's avatar
Per Cederqvist committed
903
    {
904
        err_stat = conf_no;
905
	kom_errno = (conf_c->type).secret ? KOM_UNDEF_CONF : KOM_ACCESS;
Per Cederqvist's avatar
Per Cederqvist committed
906 907 908 909 910 911 912 913 914 915
	return FAILURE;
    }

    /* Is he already a member? */

    if ( (mship = locate_membership( conf_no, pers_p )) != NULL)
    {
	/* He is already a member. Only change the priority. */

	GET_C_STAT(pers_c, pers_no, FAILURE);
916 917
	if( !is_supervisor (pers_no, pers_c , ACTPERS, ACT_P) &&
            !ENA(wheel, 8) )
918 919
	{
	    /* Noone else can change one's priorities. */
920
            err_stat = pers_no;
Per Cederqvist's avatar
Per Cederqvist committed
921 922 923 924
	    kom_errno = KOM_PERM;
	    return FAILURE;
	}

925
	do_change_priority( mship, conf_no, conf_c, priority, where, pers_no, pers_p, type);
Per Cederqvist's avatar
Per Cederqvist committed
926 927 928
    }
    else
    {
929 930 931 932 933 934
	if (pers_no != ACTPERS)
	    log("Person %lu added to conference %lu by %lu.\n",
		(unsigned long)pers_no,
		(unsigned long)conf_no,
		(unsigned long)ACTPERS);

935
	do_add_member(conf_no, conf_c, pers_no, pers_p, priority, where, type);
Per Cederqvist's avatar
Per Cederqvist committed
936 937 938 939 940 941 942 943 944 945 946
    }

    return OK;
}

#ifdef DEBUG_MARK_AS_READ
static int
check_membership(Pers_no pno,
		 const Conference *conf,
		 const Membership *mship)
{
947
    int errors=0;
Per Cederqvist's avatar
Per Cederqvist committed
948 949 950 951 952 953 954 955 956
    int i;
    Local_text_no last=0;
    int log_no=0;
    
    /* Check read texts */
    if ( mship->last_text_read >
	conf->texts.first_local_no + conf->texts.no_of_texts - 1)
    {
	if ( log_no++ < 80 )
Per Cederqvist's avatar
Per Cederqvist committed
957 958
	    log("%s%d) Person %lu has read text %lu in conf %lu%s%lu texts.\n",
		"membership.c: check_membership(): (",
Per Cederqvist's avatar
Per Cederqvist committed
959
		log_no,
960 961 962
		(unsigned long)pno,
		(unsigned long)mship->last_text_read,
		(unsigned long)mship->conf_no,
Per Cederqvist's avatar
Per Cederqvist committed
963
		", which only has ",
964
		(unsigned long)(conf->texts.first_local_no
Per Cederqvist's avatar
Per Cederqvist committed
965
			 + conf->texts.no_of_texts - 1));
966
	errors++;
Per Cederqvist's avatar
Per Cederqvist committed
967 968 969 970 971 972 973 974
    }

    last = mship->last_text_read;

    for ( i = 0; i < mship->no_of_read; i++)
    {
	if ( mship->read_texts[i] <= last )
	{
975
	    errors++;
Per Cederqvist's avatar
Per Cederqvist committed
976 977 978 979 980
	}

	last = mship->read_texts[i];
    }

981
    return errors;
Per Cederqvist's avatar
Per Cederqvist committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
}
#endif

/*
 * mark_as_read() is used to tell LysKOM which texts you have read.
 * You can mark several texts in one chunk, but the chunk should not
 * be too big to prevent users from having to re-read texts in case of
 * a [server/client/network]-crash.
 *
 * The texts are marked per conference. If there are several recipients
 * to a text it should be mark_as_read() in all the recipients.
 *
 * If conference is ACTPERS mailbox it will add a rec_time item in the
 * misc_items field.
 *
 * It is only possible to mark texts as read in a conference you are
 * member in.
 *
 * Attempts to mark non-existing texts as read are ignored if the text
 * has existed. If the text has not yet been created KOM_NO_SUCH_LOCAL_TEXT
 * will be returned in kom_errno.
 *
1004 1005 1006 1007 1008 1009 1010
 * If CONFERENCE is the current working conference of ACTPERS, and the
 * text has not previously been marked as read, ACT_P->read_texts will
 * be increased. If the client cooperates this will be correct. If the
 * client change_conference()s to all recipients of a text before
 * marking it as read the read_texts field will be too big. (If anyone
 * cares about that, feel free to rewrite this code as long as it
 * doen't get too CPU- intensive.)
Per Cederqvist's avatar
Per Cederqvist committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
 */
extern Success
mark_as_read (Conf_no		      conference,
	      int		      no_of_texts,
	      const Local_text_no  * text_arr )
{
    int		 i;
    Membership * m;
    int		 allocflg = 0;	/* read_texts is not re-allocated yet */
    Conference * conf_c;
    Success	 retval = OK;
#ifdef DEBUG_MARK_AS_READ
    const Local_text_no  * const text_arr_start = text_arr;
    Membership   original;
    int		 loop;
    static int   log_no = 0;
#endif

    
    CHK_LOGIN(FAILURE);
    GET_C_STAT(conf_c, conference, FAILURE);

    if ( (m = locate_membership( conference, ACT_P)) == NULL)
    {
1035
        err_stat = conference;
Per Cederqvist's avatar
Per Cederqvist committed
1036 1037 1038 1039 1040 1041 1042 1043
	kom_errno = KOM_NOT_MEMBER;
	return FAILURE;
    }

#ifdef DEBUG_MARK_AS_READ
    if ( m->read_texts == NULL && m->no_of_read != 0 )
    {
	log("mark_as_read(): m->read_texts == NULL && m->no_of_read == %lu (corrected).",
1044
	    (unsigned long)m->no_of_read);
Per Cederqvist's avatar
Per Cederqvist committed
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
	m->no_of_read = 0;
    }
    
    original = *m;
    original.read_texts = smalloc(m->no_of_read * sizeof(Local_text_no));
    memcpy(original.read_texts, m->read_texts,
	   m->no_of_read * sizeof(Local_text_no));
#endif
    
    for( i = no_of_texts; i > 0; i--, text_arr++ )
    {
	if ( *text_arr >= ( conf_c->texts.first_local_no
			   + conf_c->texts.no_of_texts ))
	{
	    kom_errno = KOM_NO_SUCH_LOCAL_TEXT;
	    err_stat = no_of_texts - i;
	    retval = FAILURE;
	    break;		/* Exit for-loop */
	}

	if ( *text_arr == 0 )
	{
	    kom_errno = KOM_LOCAL_TEXT_ZERO;
	    err_stat = no_of_texts - i;
	    retval = FAILURE;
	    break;		/* Exit for-loop */
	}
	
	/* Is it a letter to ACTPERS? If so, add a rec_time item. */

	if ( conference == ACTPERS )
1076
	    add_rec_time (conf_c, *text_arr);
Per Cederqvist's avatar
Per Cederqvist committed
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
	
	/* Update the Membership struct */

	if ( *text_arr == m->last_text_read + 1 )
	{
	    ++m->last_text_read;
	    if ( active_connection->cwc == conference )
		++ACT_P->read_texts;
	}
	else
	{
	    if ( allocflg == 0 )
	    {
		/* Realloc as much as is needed, and probably more. */
		/* Better than to execute srealloc 100 times... */
		
		m->read_texts = srealloc( m->read_texts,
					 (m->no_of_read + i)
					 * sizeof(Local_text_no));
		allocflg = 1;
	    }

	    if ( insert_loc_no( *text_arr, m ) == OK
		&& active_connection->cwc == conference )
	    {
		++ACT_P->read_texts;
	    }
	}
    }

    adjust_read( m, conf_c ); /* Delete initial part
				 of read_texts in the membership. */
    /* Realloc to correct size */
    
    m->read_texts = srealloc( m->read_texts,
			     (m->no_of_read) * sizeof(Local_text_no));
    
    mark_conference_as_changed ( conference );

    if ( active_connection->cwc == conference )
	mark_person_as_changed( ACTPERS );
#ifdef DEBUG_MARK_AS_READ
    /* Check that the membership is correct. Otherwise log all info. */
    if ( check_membership(ACTPERS, conf_c, m) > 0 && log_no++ < 40 )
    {
Per Cederqvist's avatar
Per Cederqvist committed
1122
	log("mark_as_read(): (Msg no %d) Person %lu %s:\n",
1123
	    log_no, (unsigned long)ACTPERS,
Per Cederqvist's avatar
Per Cederqvist committed
1124 1125 1126
	    "has a corrupt membership");
	log("Dump of data follows: <original membership> %s\n",
	    "<updated membership> <texts to mark>");
Per Cederqvist's avatar
Per Cederqvist committed
1127 1128 1129
	foutput_membership(stderr, &original);
	putc('\n', stderr);
	foutput_membership(stderr, m);
1130
	fprintf(stderr, "\n%lu { ", (unsigned long)no_of_texts);
Per Cederqvist's avatar
Per Cederqvist committed
1131 1132
	for ( loop = 0; loop < no_of_texts; loop++ )
	{
1133
	    fprintf(stderr, "\n%lu ", (unsigned long)text_arr_start[loop]);
Per Cederqvist's avatar
Per Cederqvist committed
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	}
	fprintf(stderr, "}\n");
    }

    sfree(original.read_texts);
#endif
    return retval;
}

/*
 * Ask what conferences a person is a member of.
 *
 * /// The following might be slightly misleading: /// /ceder
 * Only ask for information about at most NO_OF_CONFS conferences
 * starting with conferece FIRST. WANT_READ_TEXTS has the same
 * function as GETP_READ_TEXTS in the get_person_stat call.
 * ///
 */
1152

Per Cederqvist's avatar
Per Cederqvist committed
1153 1154
extern  Success
get_membership (Pers_no		  pers_no,
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
                unsigned short		  first,
                unsigned short		  no_of_confs,
                Bool		  want_read_texts,
                Membership_list	* memberships )
{
    return get_membership_old (pers_no,
                               first,
                               no_of_confs,
                               want_read_texts,
                               memberships );
}


extern  Success
get_membership_old (Pers_no		  pers_no,
                    unsigned short		  first,
                    unsigned short		  no_of_confs,
                    Bool		  want_read_texts,
                    Membership_list	* memberships )
Per Cederqvist's avatar
Per Cederqvist committed
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
{
    Person		* p_orig;
    Person		  temp_pers;
    Conference		* pers_c;
    Access		  acc;
    int			  i;
            
    CHK_LOGIN (FAILURE);
    
    GET_P_STAT (p_orig, pers_no, FAILURE);
    GET_C_STAT (pers_c, pers_no, FAILURE);
    
1186
    acc = access_perm (pers_no, pers_c, ACTPERS, ACT_P);
Per Cederqvist's avatar
Per Cederqvist committed
1187 1188 1189 1190 1191 1192
  
    if (acc == error)
	return  FAILURE;

    if (acc == none)
    {
1193
        err_stat = pers_no;
Per Cederqvist's avatar
Per Cederqvist committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
	kom_errno = KOM_UNDEF_PERS;
	return  FAILURE;
    }

    /* Make a copy of the struct. */
    
    temp_pers = *p_orig;
    
    /* Delete all secret information. */

    if ( acc != unlimited )
1205
	copy_public_confs (&temp_pers, p_orig, want_read_texts, FALSE);
Per Cederqvist's avatar
Per Cederqvist committed
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    else if ( !want_read_texts )
    {
	/* Delete info about read texts. */
	temp_pers.conferences.confs
	    = tmp_alloc(temp_pers.conferences.no_of_confs
			* sizeof(Membership));

	memcpy(temp_pers.conferences.confs,
	       p_orig->conferences.confs,
	       (temp_pers.conferences.no_of_confs
		* sizeof(Membership) ));
	
	for ( i = 0; i < temp_pers.conferences.no_of_confs; i++ )
	    temp_pers.conferences.confs[ i ].read_texts = NULL;
    }
    

    *memberships = temp_pers.conferences;

    if ( first >= memberships->no_of_confs )
    {
1227
        err_stat = first;
Per Cederqvist's avatar
Per Cederqvist committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return FAILURE;
    }
    
    memberships->confs += first;
    memberships->no_of_confs = min( memberships->no_of_confs - first,
				   no_of_confs);

    return OK;
}

/*
 * +++///
 *
 * first starts at 0.
 */
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

extern Success
get_members_old (Conf_no conf_no,
                 unsigned short first,
                 unsigned short no_of_members,
                 Member_list * members)
{
    return get_members (conf_no,
                        first,
                        no_of_members,
                        members);
}

Per Cederqvist's avatar
Per Cederqvist committed
1257 1258
extern  Success
get_members (Conf_no	  conf_no,
1259 1260 1261
             unsigned short	  first,
             unsigned short	  no_of_members,
             Member_list * members )
Per Cederqvist's avatar
Per Cederqvist committed
1262 1263 1264
{
    Conference * conf_c;
    Access	 acc;
1265
    unsigned long src, dst;
Per Cederqvist's avatar
Per Cederqvist committed
1266 1267 1268
        
    GET_C_STAT(conf_c, conf_no, FAILURE);

1269
    acc = access_perm (conf_no, conf_c, ACTPERS, ACT_P);
Per Cederqvist's avatar
Per Cederqvist committed
1270 1271 1272 1273 1274 1275

    if ( acc == error )
	return FAILURE;

    if ( acc == none )
    {
1276
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
1277 1278 1279 1280
	kom_errno = KOM_UNDEF_CONF;
	return FAILURE;
    }

1281
    if ( first >= (conf_c->members).no_of_members )
Per Cederqvist's avatar
Per Cederqvist committed
1282
    {
1283
        err_stat = first;
Per Cederqvist's avatar
Per Cederqvist committed
1284 1285 1286 1287
	kom_errno = KOM_INDEX_OUT_OF_RANGE;
	return FAILURE;
    }

1288
    *members = conf_c->members;
Per Cederqvist's avatar
Per Cederqvist committed
1289
    members->members += first;
1290

Per Cederqvist's avatar
Per Cederqvist committed
1291 1292
    members->no_of_members = min (no_of_members,
				  members->no_of_members - first);
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
    members->members = tmp_alloc(members->no_of_members *
                                 sizeof(Member));
    src = first;
    dst = 0;

    while (dst < members->no_of_members)
    {
        if (!(conf_c->members).members[src].type.secret ||
            is_supervisor((conf_c->members).members[src].member,
                          NULL,
                          ACTPERS,
                          ACT_P))
        {
            members->members[dst] = (conf_c->members).members[src];
        }
        else
        {
            members->members[dst].member = 0;
            members->members[dst].added_by = 0;
            members->members[dst].added_at = 0;
            set_membership_type_bits(&members->members[dst].type,
                                0,0,0,0,0,0,0,0);
        }

        src += 1;
        dst += 1;
    }

Per Cederqvist's avatar
Per Cederqvist committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
    return OK;
}

/*
 * Get a list of all conferences where it is possible that a person has
 * unread articles.
 */

Success
get_unread_confs(Pers_no       pers_no,
		 Conf_no_list *result)
{
    Person *pers_p;
    Membership *confs;
1335
    unsigned short n;
Per Cederqvist's avatar
Per Cederqvist committed
1336

Per Cederqvist's avatar
Per Cederqvist committed
1337
    CHK_LOGIN(FAILURE);
Per Cederqvist's avatar
Per Cederqvist committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
    GET_P_STAT(pers_p, pers_no, FAILURE);

    result->conf_nos = tmp_alloc (pers_p->conferences.no_of_confs
				  * sizeof(Conf_no));
    result->no_of_confs = 0;
        
    for ( n = 0, confs = pers_p->conferences.confs;
    	 n < pers_p->conferences.no_of_confs;
	 n++, confs++ )
    {
Per Cederqvist's avatar
Per Cederqvist committed
1348 1349
	if ((confs->last_text_read
	     < cached_get_highest_local_no (confs->conf_no))
Per Cederqvist's avatar
Per Cederqvist committed
1350
	    && (fast_access_perm(confs->conf_no, ACTPERS, ACT_P)
Per Cederqvist's avatar
Per Cederqvist committed
1351
		> read_protected))
Per Cederqvist's avatar
Per Cederqvist committed
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	{
	    result->conf_nos[ result->no_of_confs++ ] = confs->conf_no;
	}
    }
    return OK;
}


/*
 * Tell the server that I want to mark/unmark texts as read so that I
 * get (approximately) no_of_unread unread texts in conf_no.
 */
extern  Success
set_unread (Conf_no   conf_no,
	    Text_no   no_of_unread)
{
    Membership  *mship;
    Conference  *conf_c;
    Local_text_no highest;
    
    CHK_LOGIN(FAILURE);

    GET_C_STAT(conf_c, conf_no, FAILURE);
    
    if ( (mship = locate_membership(conf_no, ACT_P)) == NULL )
    {
1378
        err_stat = conf_no;
Per Cederqvist's avatar
Per Cederqvist committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
	kom_errno = KOM_NOT_MEMBER;
	return FAILURE;
    }

    highest = conf_c->texts.first_local_no + conf_c->texts.no_of_texts - 1;
    
    mship->last_text_read = ((highest > no_of_unread)
			     ? (highest - no_of_unread) : 0);
    
    sfree(mship->read_texts);
    mship->read_texts = NULL;
    mship->no_of_read = 0;

    mark_person_as_changed(ACTPERS);
    return OK;
}

Kent Engström's avatar
Kent Engström committed
1396 1397 1398 1399 1400
/*
 * Tell the server that I want to mark/unmark texts as read so that
 * last_read is the last read text in conf_no.
 */
extern  Success
1401 1402
set_last_read (Conf_no conf_no,
	       Local_text_no last_read)
Kent Engström's avatar
Kent Engström committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
{
    Membership  *mship;
    Conference  *conf_c;
    Local_text_no last;
    
    CHK_LOGIN(FAILURE);

    GET_C_STAT(conf_c, conf_no, FAILURE);
    
    if ( (mship = locate_membership(conf_no, ACT_P)) == NULL )
    {
1414
        err_stat = conf_no;
Kent Engström's avatar
Kent Engström committed
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
	kom_errno = KOM_NOT_MEMBER;
	return FAILURE;
    }

    last = conf_c->texts.first_local_no + conf_c->texts.no_of_texts - 1;
    
    mship->last_text_read = ((last_read <= last)
			     ? last_read : last);
    
    sfree(mship->read_texts);
    mship->read_texts = NULL;
    mship->no_of_read = 0;

    mark_person_as_changed(ACTPERS);
    return OK;
}

1432 1433 1434 1435 1436 1437 1438
extern Success set_membership_type(Pers_no pers_no,
                                   Conf_no conf_no,
                                   Membership_type *type)
{
    Conference      *conf_c, *pers_c;
    Person          *pers_p;
    Membership      *membership;
1439
    Member          *mbr;
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
    Access           acc;

    /* Check for logon */
    CHK_LOGIN(FAILURE);

    /* Find the conference and person in question */
    GET_C_STAT(conf_c, conf_no, FAILURE);
    GET_C_STAT(pers_c, pers_no, FAILURE);
    GET_P_STAT(pers_p, pers_no, FAILURE);
    
    /* Make sure that ACTPERS may know about conf */
    acc = access_perm(conf_no, conf_c, ACTPERS, ACT_P);

    if (acc == error)
        return FAILURE;

    if (acc == none)
    {
        err_stat = conf_no;
        kom_errno = KOM_UNDEF_CONF;
        return FAILURE;
    }

    /* Check that ACTPERS may modify memberships of person */
    acc = access_perm(pers_no, pers_c, ACTPERS, ACT_P);
    if (acc != unlimited &&
        !ENA(wheel, 8) &&
        !ENA(admin, 6))
    {
        err_stat = pers_no;
        kom_errno = conf_c->type.secret ? KOM_UNDEF_CONF : KOM_PERM;
        return FAILURE;
    }

    /* Find person's membership in the conference */
    membership = locate_membership(conf_no, pers_p);
    if (membership == NULL)
    {
        err_stat = conf_no;
        kom_errno = KOM_NOT_MEMBER;
        return FAILURE;
    }

    /* Find entry in conference's member list */
    /* FIXME: If this happens we should do something more drastic
              since it indicates that the database is fucked up. */

1487 1488
    mbr = locate_member(pers_no, conf_c);
    if (mbr == NULL)
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
    {
        err_stat = conf_no;
        kom_errno = KOM_NOT_MEMBER;
        return FAILURE;
    }

    /* Check type restrictions */
    if ((!param.secret_memberships && type->secret) ||
        (!param.allow_reinvite && type->invitation && !membership->type.invitation))
    {
        err_stat = 0;
        kom_errno = KOM_INVALID_MEMBERSHIP_TYPE;
        return FAILURE;
    }

    /* Modify member and membership */
    membership->type = *type;
1506
    mbr->type = *type;
1507 1508 1509 1510 1511 1512 1513 1514
    
    /* Mark the conference and person as changed */
    mark_conference_as_changed(conf_no);
    mark_person_as_changed(pers_no);

    /* Return success */
    return OK;
}
Kent Engström's avatar
Kent Engström committed
1515 1516