Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

channels.c 8.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/******************************************************************************
CHANNELS.C   Channel auxilliary functions.

    Copyright 1993 - 2000 Paul J. Sidorsky

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    This program 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 this program; if not, write to the Free Software
Deucе's avatar
Deucе committed
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

This module implements the auxilliary functions related to conferences and
channels, such as loading CHANNELS.CFG and displaying the channels list.  The
heavy-duty work of actually managing the channels is performed by the Channel
Management Interface (CMI), which is wholly contained in CMI.C.
******************************************************************************/

#include "top.h"

/* loadchan() - Loads channel and conference definitions from CHANNELS.CFG.
   Parameters:  None.
   Returns:  TRUE on success, FALSE if an error occurred.
*/
char loadchan(void)
{
FILE *chanfil = NULL; /* File stream used to load cfg file. */
unsigned long curch = 0; /* Current channel number. */
unsigned long nextconf = 4001000000UL; /* Number for the next conference. */
/* Pointer to the start of the options for the current setting. */
unsigned char *opt = NULL;
unsigned char loadstr[256]; /* Input buffer when loading the file. */
long chcount = -1; /* Number of channel definitions. */
XINT cres; /* Result code. */

/* Open the configuration file. */
sprintf(outbuf, "%schannels.cfg", cfg.toppath);
chanfil = fopen(outbuf, "rt");
if (!chanfil)
    {
    return 0;
    }

/* Read and process each line in the file. */
while (!feof(chanfil))
    {
    /* Read the next line. */
    if (!fgets(loadstr, 256, chanfil))
        {
        /* Abort if an error occurs while reading.  Usually this means an
           EOF in the middle of a line. */
        break;
        }
    /* Skip commented lines. */
    if (loadstr[0] == ';')
        {
        continue;
        }
    /* Strip newline.  Not sure why I didn't use the stripcr() macro here. */
    if (loadstr[strlen(loadstr) - 1] == '\n')
        {
        loadstr[strlen(loadstr) - 1] = 0;
        }
    /* Break the string into words. */
    cres = split_string(loadstr);

    if (cres > 1)
        {
        /* Only process if there are at least two words. */

        /* Grab the position of the start of the second word and use this
           as the options string. */
        opt = &word_str[word_pos[1]];

        /* Channel number or type. */
        if (!stricmp(get_word(0), "Channel"))
            {
            if (!stricmp(opt, "Conference"))
                {
                /* Channel is a conference, use the next internal conference
                   number. */
                curch = nextconf++;
                }
            else
                {
                /* Get the channel number. */
                curch = strtoul(opt, NULL, 10);
                if (curch > 3999999999UL)
                    {
                    /* Unlike many other areas of TOP the maximum limit
                       here is actually checked and enforced. */
                    curch = 3999999999UL;
                    }
                }
            /* Set the channel number for the current channel definition. */
            chan[++chcount].channel = curch;
            continue;
            }
        /* Fixed channel topic. */
        if (!stricmp(get_word(0), "Topic"))
            {
            memset(chan[chcount].topic, 0, 71);
            strncpy(chan[chcount].topic, opt, 70);
            continue;
            }
        /* Alternate names for the channel or conference. */
        if (!stricmp(get_word(0), "JoinAliases"))
            {
            memset(chan[chcount].joinaliases, 0, 31);
            strncpy(chan[chcount].joinaliases, opt, 30);
            continue;
            }
        /* Minimum security to join the channel. */
        if (!stricmp(get_word(0), "MinSecurity"))
            {
            chan[chcount].minsec = atol(opt);
            }
        /* Maximum security to join the channel. */
        if (!stricmp(get_word(0), "MaxSecurity"))
            {
            chan[chcount].maxsec = atol(opt);
            }
        /* Minimum security for automatic moderator status. */
        if (!stricmp(get_word(0), "ModeratorSecurity"))
            {
            chan[chcount].modsec = atol(opt);
            }
        /* Show this channel in a SCAN? */
        if (!stricmp(get_word(0), "Listed"))
            {
            chan[chcount].listed = seektruth(opt);
            }
        }
    }

fclose(chanfil);

/* Replace the configured "expected" maximum with the actual amount of
   definitions, so TOP doesn't get confused with blank ones. */
cfg.maxchandefs = ++chcount;

return 1;
}

/* list_channels() - Display a list of channels and conferences.
   Parameters:  None.
   Returns:  Nothing.
*/
void list_channels(void)
{
long chc; /* Counter. */

/* Prepare the screen. */
top_output(OUT_SCREEN, getlang("ChannelListHdr"));
top_output(OUT_SCREEN, getlang("ChannelListSep"));

/* Display each definition. */
for (chc = 0; chc < cfg.maxchandefs; chc++)
    {
    if (!chan[chc].listed || user.security < chan[chc].minsec ||
        user.security > chan[chc].maxsec)
        {
        /* Don't show if the user's security isn't in range, or if the
           channel is flagged unlisted. */
        continue;
        }

    /* Show the channel. */
    top_output(OUT_SCREEN, getlang("ChannelListFormat"),
               channelname(chan[chc].channel), chan[chc].topic);
    }

}

/* channelname() - Get the short name of a channel.
   Parameters:  chnum - Channel number to get the name of.
   Returns:  String containing the name.
   Notes:  Normally used in a SCAN command or channel listing to get a
           displayable name.  Always returns a pointer to the global
           variable wordret, which is used as an output buffer.
*/
unsigned char *channelname(unsigned long chnum)
{
XINT cd; /* Counter. */
long cdef; /* Channel definition number for channel #chnum. */

/* Clear the buffer. */
memset(wordret, 0, 31);

/* Conference. */
if (chnum > 4000999999UL && chnum < 0xFFFFFFFFUL)
    {
    /* Find the channel definition for this channel.  It is assumed that
       one will exist because this function is only used during output. */
    cdef = findchannelrec(chnum);
    // Should assert the definition's existence.

    /* Change underscores to spaces inside the name.  The intention was to
       allow multi-word channels to be defined with underscores.
       Unfortunately, the current command processor does not support this. */
    for (cd = 0; chan[cdef].joinaliases[cd] != ' ' &&
         cd < strlen(chan[cdef].joinaliases); cd++)
        {
        wordret[cd] = chan[cdef].joinaliases[cd];
        if (wordret[cd] == '_')
            {
            wordret[cd] = ' ';
            }
        }
    }
/* Personal channel. */
if (chnum >= 4000000000UL && chnum <= 4000999999UL)
    {
    /* The short name of a personal channel is simply the owner's handle. */
    strcpy(wordret, top_output(OUT_STRINGNF, getlang("UserChanShortName"),
           handles[(XINT) (chnum - 4000000000UL)].string));
    }
/* Normal (numeric) channel. */
if (chnum < 4000000000UL)
    {
    /* Simply returns the channel number as a string. */
    ultoa(chnum, wordret, 10);
    }

return wordret;
}

/* checkchannelname() - Gets the channel number for a name or alias.
   Parameters:  chnam - Name or alias of channel to check.
   Returns:  Channel number for the name given, or 0 if none is found.
*/
unsigned long checkchannelname(unsigned char *chnam)
{
XINT cc; /* Counter. */

/* Check each channel definition. */
for (cc = 0; cc < cfg.maxchandefs; cc++)
    {
    /* Find a match for all aliases. */
    if (checkcmdmatch(chnam, chan[cc].joinaliases) > 0)
        {
        return chan[cc].channel;
        }
    }

/* No match could be found. */
return 0L;
}

/* findchannelrec() - Finds the channel definition number for a channel.
   Parameters:  fchan - Channel number to find a definition for.
   Returns:  Channel definition number for the given channel, or -1 if
             none can be found.
*/
long findchannelrec(unsigned long fchan)
{
long fc; /* Counter. */

/* Check each channel definition. */
for (fc = 0; fc < cfg.maxchandefs; fc++)
    {
    /* A match occurs if the defined channel matches the given one. */
    if (fchan == chan[fc].channel)
        {
        return fc;
        }
    }

/* No match could be found. */
return -1L;
}