Re: CODE: Regexp/email check

From: Daniel A. Koepke (
Date: 06/09/01

On Sat, 9 Jun 2001, Torgny 'Artovil' Bjers wrote:

> When I put this in I got a warning for em:

That was Mailer Code(tm).  In other words, it was written in my mailer
without any previous testing, guarantees of it working, etc.  It was meant
more as a guide to what to do rather than code to do what you wanted to

> utils.c: In function `valid_email':
> utils.c:913: warning: assignment discards qualifiers from pointer target type

This is a warning about my sloppy use of const.  There were other problems
with that code snippet (namely, it let through some addresses as valid
that weren't and blocked some that were valid).  A more compact and
correct version, with a few more features:

  #define ATEXT(c)      ((c) && (isalnum(c) || strchr(punct_okay, (c))))

  bool valid_email(const char *em)
    static char punct_okay[] = "!#$%&'*+=/?^_`{|}~-.";
    const char *mbox = em;

    /* First character can't be a dot. */
    if (*mbox == '.')
     return (FALSE);

    /* Don't allow someone to use root/postmaster/abuse/etc. mailbox. */
    if (!strn_cmp(mbox, "root@", 5) ||
        !strn_cmp(mbox, "postmaster@", 11) ||
        !strn_cmp(mbox, "abuse@", 6) ||
        !strn_cmp(mbox, "webmaster@", 10))
      return (FALSE);

    while (*mbox) {
      if (*mbox == '.')
      if (!ATEXT(*mbox))


    if (mbox == em || *mbox != '@' || !*(++mbox) || *mbox == '.')
      return (FALSE);

    /* Don't allow someone to be "@localhost", etc. */
    if (!str_cmp(mbox, "localhost") ||
        !str_cmp(mbox, "") ||
        !strn_cmp(mbox, "192.168.", 8))
      return (FALSE);

    while (*mbox) {
      if (*mbox == '.')
      if (!ATEXT(*mbox))


    /* If we made it to the end, we have a valid e-mail address. */
    return (!*mbox);

This code, I think, works.  In addition to just checking if the e-mail
address consists of any valid characters (and sequences of characters) and
does not omit any part, I've also included (mainly for demonstration
purposes) examples of how to check for certain e-mail mailboxes or hosts
that you may not want people to be able to deliver to.  You'd write your
code to restrict sending to an xemails list or similar in the same manner.

As with before, this can be written more tersely, but clarity is the goal

> [snip question about books/tutorials on C]

First things first, which always needs to be said, a Mud is a complex
piece of software and while it might seem like (and, in reality, be) a fun
way to learn how to program it's still a very complex piece of software.
Generally, one does not take 20k+ lines of code for a starting project, so
you may want to reconsider using this as your starting point.  On the
other hand, Muds do have a way of motivating people to learn not just the
concepts but how to apply them in practical situations, making the
appropriate trade-offs in design, elegance, conciseness, etc., so they're
so ill-suited for the task that I'll tell you that you've taken the wrong
approach.  You still may want to build up to a Mud, though.

Second, if you want to look for C tutorials/books/etc. on the Internet,
then a good search engine is your friend.  Google is my favorite.  So try with a reasonable query ("C programming tutorial")
and see what comes up.  Google keeps track of the popularity of particular
offered links, weighting the ones that get used more higher and moving
them closer to the head of the list.  This helps it return useful results,
so there shouldn't be too much frustration given a good query on a subject
that generates a lot of interest.

Daniel A. Koepke (dak),
Caveat emptor: I say what I mean and mean what I say.  Listen well.
Caveat venditor: Say what you mean, mean what you say.  Say it well.

   | FAQ: |
   | Archives: |

This archive was generated by hypermail 2b30 : 12/05/01 PST