#module NEWSCREATE "V4.02"
/*----------------------------------------------------------------------
        N E W S C R E A T E . C

 Author:
     Geoff Huston
       Computer Services Centre
       Australian National University

 Functionality:
        Create new newsgroups (manager function).

 Version:
    V4.0    30-Oct-1987     GIH     NEWS Version 4.0 Release
    V4.02   16-Nov-1987     GIH     Added /local qualifier (as distinct
                                    from network newsgroups)

----------------------------------------------------------------------*/

#include "newsinclude.h"
#include "newsdefine.h"
#include "newsextern.h"

/*----------------------------------------------------------------------
 * d o _ c r e g r p
 *
 * define verb CREATE
 *     routine do_cregrp
 *     parameter   P1,         label=NEWSGROUPS,value(list)
 *     qualifier   HOLD        value(default= "0")
 *     qualifier   ITEMHOLD    value(default="0")
 *     qualifier   MODERATOR   value
 *     qualifier   LOCAL
 *     qualifier   CLOSED
 *     qualifier   OPEN
 *     disallow    (CLOSED and OPEN)
 *
 * Create a new (empty) newsgroup
 *----------------------------------------------------------------------
 */

do_cregrp()
{
    char s[132],
         hold[132],
         itemhold[132],
         mod[132],
         delstr[132],
         oldsub[SUBJLEN];
    int g,
        i,
        old_lev,
        hold_pres = 0,
        itemhold_pres = 0,
        closed_pres = 0,
        open_pres = 0,
        mr_req = 0,
        mod_pres = 0;
    unsigned short hold_len,
                   itemhold_len,
                   hold_val,
                   itemhold_val,
                   cre_grp[20],
                   mod_len;
    $DESCRIPTOR(hold_dsc,hold);
    $DESCRIPTOR(itemhold_dsc,itemhold);
    $DESCRIPTOR(mod_dsc,mod);

    if (no_priv())
    {
        err_line("\tError: Create - No Priv\n");
        return(0);
    }

    auto_cre_grp = 1;
    old_lev = news_context;
    parse_newsgroups(s,"",0);
    if (!*s) return(0);
    do_new_group(s,1,cre_grp);
    if (!*cre_grp)
    {
        err_line("\tInfo: Create - No newsgroup created\n");
        return(0);
    }
    if (cli$present(c$dsc("HOLD")) & 1)
    {
        hold_pres = 1;
        cli$get_value(c$dsc("HOLD"),&hold_dsc,&hold_len);
        hold[hold_len] = '\0';
        if (!hold_len) hold_len = 0;
        if (!strcmp(hold,"*")) hold_val = ~0 ;
        else if (sscanf(hold,"%hd",&hold_val) != 1)
        {
            err_line("\tError: Create - /HOLD=val not an integer\n");
            hold_pres = 0;
        }
    }
    if (cli$present(c$dsc("ITEMHOLD")) & 1)
    {
        itemhold_pres = 1;
        cli$get_value(c$dsc("ITEMHOLD"),&itemhold_dsc,&itemhold_len);
        itemhold[itemhold_len] = '\0';
        if (!itemhold_len) itemhold_len = 0;
        if (!strcmp(itemhold,"*")) itemhold_val = ~0 ;
        else if (sscanf(itemhold,"%hd",&itemhold_val) != 1)
        {
            err_line("\tError: Create - /ITEMHOLD=val not an integer\n");
            itemhold_pres = 0;
        }
    }
    if (cli$present(c$dsc("OPEN")) & 1) open_pres = 1;
    if (cli$present(c$dsc("CLOSED")) & 1) closed_pres = 1;


    if ((status = cli$present(c$dsc("MODERATOR"))) & 1)
    {
        mod_pres = 1;
        mod_len = 0;
        cli$get_value(c$dsc("MODERATOR"),&mod_dsc,&mod_len);
        if (!mod_len) get_input(&mod_dsc,c$dsc("Moderator: "),&mod_len);
        if (!mod_len) mod_pres = 0;
        mod[mod_len] = '\0';
        s_to_lower(mod);
    }
    else if (status == CLI$_NEGATED) mod_pres = -1;

    *s = '\0';

    for (i = 0; g = cre_grp[i]; ++i)
    {

        if (!(g = ga_locate(g))) continue;
        if (*s) strcat(s,",");
        strcat(s,ga[g]->grp_name);

        if (hold_pres || itemhold_pres || closed_pres || open_pres || mod_pres)
        {
            if (hold_pres) ga[g]->grp_life = hold_val;
            if (itemhold_pres) ga[g]->grp_itmlife = itemhold_val;
            if ((open_pres) && (ga[g]->grp_closed))
            {
                ga[g]->grp_closed = 0;
                mr_req = 1;
            }
            else if ((closed_pres) && (!ga[g]->grp_closed))
            {
                ga[g]->grp_closed = 1;
                mr_req = 1;
            }
            if (mod_pres)
            {
                if ((mod_pres < 0) && (*(ga[g]->grp_moderator))) *(ga[g]->grp_moderator) = '\0';
                else if (mod_pres > 0)
                {
                    if (strlen(mod) >= MODLEN) mod[MODLEN - 1] = '\0';
                    strcpy(ga[g]->grp_moderator,mod);
                }
            }
            if (cli$present(c$dsc("LOCAL")) & 1) ga[g]->grp_local = 1;
            else ga[g]->grp_local = 0;
            update_newsgrp(g);
        }
        hold_val = ga[g]->grp_life;
        *itemhold = '\0';
        if (ga[g]->grp_itmlife == 65535) strcpy(itemhold,"Perm");
        else if (ga[g]->grp_itmlife) sprintf(itemhold,"%u",ga[g]->grp_itmlife);
        *hold = '\0';
        if (ga[g]->grp_life == 65535) strcpy(hold,"Perm");
        else if (ga[g]->grp_life) sprintf(hold,"%u",ga[g]->grp_life);
        if ((!((*hold) && (*itemhold))) && (g))
        {
            if (!*itemhold)
            {
                if (ga[0]->grp_itmlife == 65535) strcpy(itemhold,"[Perm]");
                else if (ga[0]->grp_itmlife) sprintf(itemhold,"[%u]",ga[0]->grp_itmlife);
            }
            if (!*hold)
            {
                if (ga[0]->grp_life == 65535) strcpy(hold,"[Perm]");
                else if (ga[0]->grp_life) sprintf(hold,"[%u]",ga[0]->grp_life);
                hold_val = ga[0]->grp_life;
            }
        }
        if (!*itemhold) sprintf(itemhold,"[%u]",EXP_TIME);
        if (!*hold)
        {
            sprintf(hold,"[%u]",GRP_TIME);
            hold_val = GRP_TIME ;
        }
        if (ga[g]->grp_count) sprintf(delstr,"#: %u",ga[g]->grp_count);
        else if (hold_val != 65535) sprintf(delstr,"Del: %s",gendate(ga[g]->grp_entdate + (hold_val * 86400)));
        else *delstr = '\0';
        sprintf(err_oline,"\t%s (Itm:%s, Grp:%s) %s %s",
            (screen_active) ? s : ga[g]->grp_name,itemhold,hold,delstr,((ga[g]->grp_closed) ? "X" : ""));
        if (*(ga[g]->grp_moderator))
        {
            strcat(err_oline," Mod:");
            strcat(err_oline,ga[g]->grp_moderator);
        }
        if (ga[g]->grp_local) strcat(err_oline," L");
        strcat(err_oline,"\n");
        err_line(err_oline);
    }
    if (mr_req)
    {
        strcpy(oldsub,ga[curr_g]->grp_name);
        mem_reset(cur_dir_type);
        do_select(oldsub,0,old_lev);
    }
    return(0);
}
