Changeset 4284008 in code


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".

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • cmd.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
     
    113113        /* to boot or not to boot, that is the question */
    114114        ig = is->group;
    115         if (!icb_ismoder(ig, is)) {
     115        if (!icb_ismod(ig, is)) {
    116116                icb_status(is, STATUS_NOTIFY, "Sorry, booting is a privilege "
    117117                    "you don't possess");
     
    180180        if (is->group) {
    181181                changing = 1;
    182                 if (icb_ismoder(is->group, is))
     182                if (icb_ismod(is->group, is))
    183183                        (void)icb_pass(is->group, is, NULL);
    184184                LIST_REMOVE(is, entry);
     
    196196        /* acknowledge successful join */
    197197        icb_status(is, STATUS_STATUS, "You are now in group %s%s", ig->name,
    198             icb_ismoder(ig, is) ? " as moderator" : "");
     198            icb_ismod(ig, is) ? " as moderator" : "");
    199199
    200200        /* send user a topic name */
     
    285285        struct icb_session *s;
    286286
    287         if (!ig->moder)         /* if there is no mod, let anyone grab it */
    288                 (void)icb_pass(ig, ig->moder, is);
    289         else if (icb_ismoder(ig, is)) {
     287        if (!ig->mod)           /* if there is no mod, let anyone grab it */
     288                (void)icb_pass(ig, ig->mod, is);
     289        else if (icb_ismod(ig, is)) {
    290290                LIST_FOREACH(s, &ig->sess, entry) {
    291291                        if (strcmp(s->nick, arg) == 0)
     
    296296                        return;
    297297                }
    298                 (void)icb_pass(ig, ig->moder, s);
     298                (void)icb_pass(ig, ig->mod, s);
    299299        }
    300300}
     
    312312                        icb_status(is, STATUS_TOPIC, "The topic is not set.");
    313313        } else {                /* setting the topic */
    314                 if (!icb_ismoder(ig, is)) {
     314                if (!icb_ismod(ig, is)) {
    315315                        icb_status(is, STATUS_NOTIFY, "Setting the topic is "
    316316                            "only for moderators.");
     
    324324
    325325void
    326 icb_cmd_who(struct icb_session *is, char *arg __attribute__((unused)))
    327 {
    328         icb_who(is, NULL);
    329 }
     326icb_cmd_who(struct icb_session *is, char *arg)
     327{
     328        struct icb_group *ig;
     329
     330        if (strlen(arg) == 0)
     331                return icb_who(is, NULL);
     332
     333        LIST_FOREACH(ig, &groups, entry) {
     334                if (strcmp(ig->name, arg) == 0)
     335                        break;
     336        }
     337        if (ig == NULL) {
     338                icb_error(is, "The group %s doesn't exist.", arg);
     339                return;
     340        }
     341        icb_who(is, ig);
     342}
  • 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 *
  • icb.h

    r9195a6a r4284008  
    5050         CMDOUT_CO,
    5151         CMDOUT_EC,
     52         CMDOUT_WG,
     53         CMDOUT_WH,
    5254         CMDOUT_WL,
    53          CMDOUT_WG,
    5455};
    5556#define ICB_M_PROTO              'j'
     
    9394        LIST_ENTRY(icb_group)    entry;
    9495        LIST_HEAD(, icb_session) sess;
    95         struct icb_session      *moder;
     96        struct icb_session      *mod;
    9697};
    9798
     
    118119void icb_init(struct icbd_callbacks *);
    119120void icb_input(struct icb_session *);
    120 int  icb_ismoder(struct icb_group *, struct icb_session *);
     121int  icb_ismod(struct icb_group *, struct icb_session *);
    121122int  icb_pass(struct icb_group *, struct icb_session *, struct icb_session *);
    122123void icb_privmsg(struct icb_session *, char *, char *);
Note: See TracChangeset for help on using the changeset viewer.