home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / CHANNELS.C < prev    next >
C/C++ Source or Header  |  2000-07-13  |  9KB  |  278 lines

  1. /******************************************************************************
  2. CHANNELS.C   Channel auxilliary functions.
  3.  
  4.     Copyright 1993 - 2000 Paul J. Sidorsky
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License, version 2, as
  8.     published by the Free Software Foundation.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19. This module implements the auxilliary functions related to conferences and
  20. channels, such as loading CHANNELS.CFG and displaying the channels list.  The
  21. heavy-duty work of actually managing the channels is performed by the Channel
  22. Management Interface (CMI), which is wholly contained in CMI.C.
  23. ******************************************************************************/
  24.  
  25. #include "top.h"
  26.  
  27. /* loadchan() - Loads channel and conference definitions from CHANNELS.CFG.
  28.    Parameters:  None.
  29.    Returns:  TRUE on success, FALSE if an error occurred.
  30. */
  31. char loadchan(void)
  32. {
  33. FILE *chanfil = NULL; /* File stream used to load cfg file. */
  34. unsigned long curch = 0; /* Current channel number. */
  35. unsigned long nextconf = 4001000000UL; /* Number for the next conference. */
  36. /* Pointer to the start of the options for the current setting. */
  37. unsigned char *opt = NULL;
  38. unsigned char loadstr[256]; /* Input buffer when loading the file. */
  39. long chcount = -1; /* Number of channel definitions. */
  40. XINT cres; /* Result code. */
  41.  
  42. /* Open the configuration file. */
  43. sprintf(outbuf, "%sCHANNELS.CFG", cfg.toppath);
  44. chanfil = fopen(outbuf, "rt");
  45. if (!chanfil)
  46.     {
  47.     return 0;
  48.     }
  49.  
  50. /* Read and process each line in the file. */
  51. while (!feof(chanfil))
  52.     {
  53.     /* Read the next line. */
  54.     if (!fgets(loadstr, 256, chanfil))
  55.         {
  56.         /* Abort if an error occurs while reading.  Usually this means an
  57.            EOF in the middle of a line. */
  58.         break;
  59.         }
  60.     /* Skip commented lines. */
  61.     if (loadstr[0] == ';')
  62.         {
  63.         continue;
  64.         }
  65.     /* Strip newline.  Not sure why I didn't use the stripcr() macro here. */
  66.     if (loadstr[strlen(loadstr) - 1] == '\n')
  67.         {
  68.         loadstr[strlen(loadstr) - 1] = 0;
  69.         }
  70.     /* Break the string into words. */
  71.     cres = split_string(loadstr);
  72.  
  73.     if (cres > 1)
  74.         {
  75.         /* Only process if there are at least two words. */
  76.  
  77.         /* Grab the position of the start of the second word and use this
  78.            as the options string. */
  79.         opt = &word_str[word_pos[1]];
  80.  
  81.         /* Channel number or type. */
  82.         if (!stricmp(get_word(0), "Channel"))
  83.             {
  84.             if (!stricmp(opt, "Conference"))
  85.                 {
  86.                 /* Channel is a conference, use the next internal conference
  87.                    number. */
  88.                 curch = nextconf++;
  89.                 }
  90.             else
  91.                 {
  92.                 /* Get the channel number. */
  93.                 curch = strtoul(opt, NULL, 10);
  94.                 if (curch > 3999999999UL)
  95.                     {
  96.                     /* Unlike many other areas of TOP the maximum limit
  97.                        here is actually checked and enforced. */
  98.                     curch = 3999999999UL;
  99.                     }
  100.                 }
  101.             /* Set the channel number for the current channel definition. */
  102.             chan[++chcount].channel = curch;
  103.             continue;
  104.             }
  105.         /* Fixed channel topic. */
  106.         if (!stricmp(get_word(0), "Topic"))
  107.             {
  108.             memset(chan[chcount].topic, 0, 71);
  109.             strncpy(chan[chcount].topic, opt, 70);
  110.             continue;
  111.             }
  112.         /* Alternate names for the channel or conference. */
  113.         if (!stricmp(get_word(0), "JoinAliases"))
  114.             {
  115.             memset(chan[chcount].joinaliases, 0, 31);
  116.             strncpy(chan[chcount].joinaliases, opt, 30);
  117.             continue;
  118.             }
  119.         /* Minimum security to join the channel. */
  120.         if (!stricmp(get_word(0), "MinSecurity"))
  121.             {
  122.             chan[chcount].minsec = atol(opt);
  123.             }
  124.         /* Maximum security to join the channel. */
  125.         if (!stricmp(get_word(0), "MaxSecurity"))
  126.             {
  127.             chan[chcount].maxsec = atol(opt);
  128.             }
  129.         /* Minimum security for automatic moderator status. */
  130.         if (!stricmp(get_word(0), "ModeratorSecurity"))
  131.             {
  132.             chan[chcount].modsec = atol(opt);
  133.             }
  134.         /* Show this channel in a SCAN? */
  135.         if (!stricmp(get_word(0), "Listed"))
  136.             {
  137.             chan[chcount].listed = seektruth(opt);
  138.             }
  139.         }
  140.     }
  141.  
  142. fclose(chanfil);
  143.  
  144. /* Replace the configured "expected" maximum with the actual amount of
  145.    definitions, so TOP doesn't get confused with blank ones. */
  146. cfg.maxchandefs = ++chcount;
  147.  
  148. return 1;
  149. }
  150.  
  151. /* list_channels() - Display a list of channels and conferences.
  152.    Parameters:  None.
  153.    Returns:  Nothing.
  154. */
  155. void list_channels(void)
  156. {
  157. long chc; /* Counter. */
  158.  
  159. /* Prepare the screen. */
  160. top_output(OUT_SCREEN, getlang("ChannelListHdr"));
  161. top_output(OUT_SCREEN, getlang("ChannelListSep"));
  162.  
  163. /* Display each definition. */
  164. for (chc = 0; chc < cfg.maxchandefs; chc++)
  165.     {
  166.     if (!chan[chc].listed || user.security < chan[chc].minsec ||
  167.         user.security > chan[chc].maxsec)
  168.         {
  169.         /* Don't show if the user's security isn't in range, or if the
  170.            channel is flagged unlisted. */
  171.         continue;
  172.         }
  173.  
  174.     /* Show the channel. */
  175.     top_output(OUT_SCREEN, getlang("ChannelListFormat"),
  176.                channelname(chan[chc].channel), chan[chc].topic);
  177.     }
  178.  
  179. }
  180.  
  181. /* channelname() - Get the short name of a channel.
  182.    Parameters:  chnum - Channel number to get the name of.
  183.    Returns:  String containing the name.
  184.    Notes:  Normally used in a SCAN command or channel listing to get a
  185.            displayable name.  Always returns a pointer to the global
  186.            variable wordret, which is used as an output buffer.
  187. */
  188. unsigned char *channelname(unsigned long chnum)
  189. {
  190. XINT cd; /* Counter. */
  191. long cdef; /* Channel definition number for channel #chnum. */
  192.  
  193. /* Clear the buffer. */
  194. memset(wordret, 0, 31);
  195.  
  196. /* Conference. */
  197. if (chnum > 4000999999UL && chnum < 0xFFFFFFFFUL)
  198.     {
  199.     /* Find the channel definition for this channel.  It is assumed that
  200.        one will exist because this function is only used during output. */
  201.     cdef = findchannelrec(chnum);
  202.     // Should assert the definition's existence.
  203.  
  204.     /* Change underscores to spaces inside the name.  The intention was to
  205.        allow multi-word channels to be defined with underscores.
  206.        Unfortunately, the current command processor does not support this. */
  207.     for (cd = 0; chan[cdef].joinaliases[cd] != ' ' &&
  208.          cd < strlen(chan[cdef].joinaliases); cd++)
  209.         {
  210.         wordret[cd] = chan[cdef].joinaliases[cd];
  211.         if (wordret[cd] == '_')
  212.             {
  213.             wordret[cd] = ' ';
  214.             }
  215.         }
  216.     }
  217. /* Personal channel. */
  218. if (chnum >= 4000000000UL && chnum <= 4000999999UL)
  219.     {
  220.     /* The short name of a personal channel is simply the owner's handle. */
  221.     strcpy(wordret, top_output(OUT_STRINGNF, getlang("UserChanShortName"),
  222.            handles[(XINT) (chnum - 4000000000UL)].string));
  223.     }
  224. /* Normal (numeric) channel. */
  225. if (chnum < 4000000000UL)
  226.     {
  227.     /* Simply returns the channel number as a string. */
  228.     ultoa(chnum, wordret, 10);
  229.     }
  230.  
  231. return wordret;
  232. }
  233.  
  234. /* checkchannelname() - Gets the channel number for a name or alias.
  235.    Parameters:  chnam - Name or alias of channel to check.
  236.    Returns:  Channel number for the name given, or 0 if none is found.
  237. */
  238. unsigned long checkchannelname(unsigned char *chnam)
  239. {
  240. XINT cc; /* Counter. */
  241.  
  242. /* Check each channel definition. */
  243. for (cc = 0; cc < cfg.maxchandefs; cc++)
  244.     {
  245.     /* Find a match for all aliases. */
  246.     if (checkcmdmatch(chnam, chan[cc].joinaliases) > 0)
  247.         {
  248.         return chan[cc].channel;
  249.         }
  250.     }
  251.  
  252. /* No match could be found. */
  253. return 0L;
  254. }
  255.  
  256. /* findchannelrec() - Finds the channel definition number for a channel.
  257.    Parameters:  fchan - Channel number to find a definition for.
  258.    Returns:  Channel definition number for the given channel, or -1 if
  259.              none can be found.
  260. */
  261. long findchannelrec(unsigned long fchan)
  262. {
  263. long fc; /* Counter. */
  264.  
  265. /* Check each channel definition. */
  266. for (fc = 0; fc < cfg.maxchandefs; fc++)
  267.     {
  268.     /* A match occurs if the defined channel matches the given one. */
  269.     if (fchan == chan[fc].channel)
  270.         {
  271.         return fc;
  272.         }
  273.     }
  274.  
  275. /* No match could be found. */
  276. return -1L;
  277. }
  278.