Changeset 4284008 in code for icb.c


Ignore:
Timestamp:
Mar 2, 2014, 1:50:39 AM (11 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
2e37e9f
Parents:
9195a6a
git-author:
Mike Belopuhov <mike@…> (03/02/14 01:45:33)
git-committer:
Mike Belopuhov <mike@…> (03/02/14 01:50:39)
Message:

Revamp "who" command handling; rename "moder" to "mod".

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icb.c

    r9195a6a r4284008  
    11/*
    2  * Copyright (c) 2009, 2010 Mike Belopuhov
     2 * Copyright (c) 2009, 2010, 2013 Mike Belopuhov
    33 *
    44 * Permission to use, copy, modify, and distribute this software for any
     
    3434void   icb_groupmsg(struct icb_session *, char *);
    3535void   icb_login(struct icb_session *, char *, char *, char *);
     36int    icb_dowho(struct icb_session *, struct icb_group *);
    3637char  *icb_nextfield(char **);
    3738
     
    199200        /* notify user */
    200201        icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
    201             icb_ismoder(ig, is) ? " as moderator" : "");
     202            icb_ismod(ig, is) ? " as moderator" : "");
    202203
    203204        /* send user a topic name */
     
    285286                otype = "ec";
    286287                break;
     288        case CMDOUT_WG:
     289                otype = "wg";
     290                break;
     291        case CMDOUT_WH:
     292                otype = "wh";
     293                break;
    287294        case CMDOUT_WL:
    288295                otype = "wl";
    289296                break;
    290         case CMDOUT_WG:
    291                 otype = "wg";
    292                 break;
    293297        default:
    294298                icb_log(is, LOG_ERR, "unknown cmdout type");
    295299                return;
    296300        }
    297         icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP, outmsg);
     301        if (outmsg)
     302                icb_sendfmt(is, "%c%s%c%s", ICB_M_CMDOUT, otype, ICB_M_SEP,
     303                    outmsg);
     304        else
     305                icb_sendfmt(is, "%c%s", ICB_M_CMDOUT, otype);
    298306}
    299307
     
    386394{
    387395        if (is->group) {
    388                 if (icb_ismoder(is->group, is))
     396                if (icb_ismod(is->group, is))
    389397                        (void)icb_pass(is->group, is, NULL);
    390398                LIST_REMOVE(is, entry);
     
    414422                strlcpy(ig->mpass, mpass, sizeof ig->mpass);
    415423        if (is)
    416                 ig->moder = is;
     424                ig->mod = is;
    417425        LIST_INIT(&ig->sess);
    418426        LIST_INSERT_HEAD(&groups, ig, entry);
     
    441449
    442450/*
    443  *  icb_who: sends a list of users of the specified group (or the current
    444  *           one otherwise) in the "wl" format
    445  */
    446 void
    447 icb_who(struct icb_session *is, struct icb_group *ig)
     451 *  icb_dowho: a helper function that sends out a group header as a command
     452 *             output and user information in the "wl" format
     453 */
     454int
     455icb_dowho(struct icb_session *is, struct icb_group *ig)
    448456{
    449457        char buf[ICB_MSGSIZE];
    450458        struct icb_session *s;
    451 
    452         if (!ig)
    453                 ig = is->group;
     459        int nusers = 0;
     460
     461        icb_cmdout(is, CMDOUT_CO, " ");
     462        snprintf(buf, sizeof buf, "Group: %-8s (%cvl) Mod: %-13s Topic: %s",
     463            ig->name, ig->mod ? 'm' : 'p', ig->mod ? ig->mod->nick : "(None)",
     464            strlen(ig->topic) > 0 ? ig->topic : "(None)");
     465        icb_cmdout(is, CMDOUT_CO, buf);
    454466        LIST_FOREACH(s, &ig->sess, entry) {
    455467                (void)snprintf(buf, sizeof buf,
    456468                    "%c%c%s%c%lld%c0%c%lld%c%s%c%s%c%s",
    457                     icb_ismoder(ig, s) ? '*' : ' ', ICB_M_SEP,
     469                    icb_ismod(ig, s) ? 'm' : ' ', ICB_M_SEP,
    458470                    s->nick, ICB_M_SEP, getmonotime() - s->last,
    459471                    ICB_M_SEP, ICB_M_SEP, s->login, ICB_M_SEP,
    460472                    s->client, ICB_M_SEP, s->host, ICB_M_SEP, " ");
    461473                icb_cmdout(is, CMDOUT_WL, buf);
    462         }
    463 }
    464 
    465 /*
    466  *  icb_ismoder: checks whether group is moderated by "is"
     474                nusers++;
     475        }
     476        return (nusers);
     477}
     478
     479/*
     480 *  icb_who: sends a list of users of either the specified group or all
     481 *           groups found on the server
     482 */
     483void
     484icb_who(struct icb_session *is, struct icb_group *ig)
     485{
     486        char buf[ICB_MSGSIZE];
     487        struct icb_group *g;
     488
     489        if (!ig) {
     490                int nusers = 0, ngroups = 0;
     491
     492                LIST_FOREACH(g, &groups, entry) {
     493                        nusers += icb_dowho(is, g);
     494                        ngroups++;
     495                }
     496                if (nusers > 0) {
     497                        (void)snprintf(buf, sizeof buf,
     498                            "Total: %d %s in %d %s",
     499                            nusers, nusers > 1 ? "users" : "user",
     500                            ngroups, ngroups > 1 ? "groups" : "group");
     501                } else
     502                        (void)snprintf(buf, sizeof buf, "No users found.");
     503                icb_cmdout(is, CMDOUT_CO, buf);
     504        } else
     505                (void)icb_dowho(is, ig);
     506}
     507
     508/*
     509 *  icb_ismod: checks whether group is moderated by "is"
    467510 */
    468511int
    469 icb_ismoder(struct icb_group *ig, struct icb_session *is)
    470 {
    471         if (ig->moder && ig->moder == is)
     512icb_ismod(struct icb_group *ig, struct icb_session *is)
     513{
     514        if (ig->mod && ig->mod == is)
    472515                return (1);
    473516        return (0);
     
    484527    struct icb_session *to)
    485528{
    486         if (ig->moder && ig->moder != from)
     529        if (ig->mod && ig->mod != from)
    487530                return (-1);
    488531        if (!from && !to)
    489532                return (-1);
    490         ig->moder = to;
     533        ig->mod = to;
    491534        if (to)
    492535                icb_status(to, STATUS_NOTIFY, "%s just passed you moderation"
     
    498541
    499542/*
    500  *  icb_nextfield: advances through a given buffer returning pointer to
    501  *                 the beginning of the icb field or an empty string otherwise
     543 *  icb_nextfield: advances through a given buffer returning pointer to the
     544 *                 beginning of the icb field or an empty string otherwise
    502545 */
    503546char *
Note: See TracChangeset for help on using the changeset viewer.