Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

irc parser in c

Name: Anonymous 2011-06-18 19:24

I've been writting an irc bot in c but I'm stuck trying to parse the irc buffer into an array.

here's my connecting function:


void connecting(int sockfd)
{
        char buf[MAX], temp[MAX];

        while(1)
        {
                recv(sockfd, buf, sizeof(buf), 0);

                printf("%s", buf);

                if (strstr(buf, "Checking Ident") != NULL)
                {
                        sprintf(temp, "USER something something something :something\r\n");
                        send(sockfd, temp, strlen(temp), 0);

                        sprintf(temp, "NICK %s\r\n", NICK);
                        send(sockfd, temp, strlen(temp), 0);

                        sprintf(temp, "JOIN %s\r\n", CHAN);
                        send(sockfd, temp, strlen(temp), 0);
}

                if (strstr(buf, ":!hi"))
                {
                        sprintf(temp, "PRIVMSG %s :Hi bitch\r\n", CHAN);
                        send(sockfd, temp, strlen(temp), 0);
                }


Could somebody write a parse for me or help me out?

Name: Anonymous 2011-06-18 19:28

Lack of abstraction. Thread over.

Name: Anonymous 2011-06-18 19:51

Use (ir)regular expressions:
(Understanding the regexps and the code and converting it to C is left as an exercise for the reader)

(define nick-regex #px":([][\\`_^{}|[:alpha:]-][][\\`_^{}|[:alnum:]-]*)(?:!([^\\s!@]+))?(?:@([^\\s]+))?")
(define server-regex
#px":(?:(?:[[:alnum:]]+(?:\\.[[:alnum:]-]+)+)|(?:\\d{,3}\\.\\d{,3}\\.\\d{,3}\\.\\d{,3})|(?:(?:[[:xdigit:]]?(?::[[:xdigit:]]?)+)|(?:0:0:0:0:0:(?:0|FFFF):\\d{3}\\.\\d{3}\\.\\d{3}\\.\\d{3})))")
(define message-regex #px"^(?:(:[[:graph:]]+) )?([[:alpha:]]+|[0-9]{3})(?: ((?:[^:\0\\s]\\S* ?){,14}))?(:[^\0\r\n]*)?\r?\n?$")

(struct irc-message (sender command params text))

(define (parse-nick x)
  (cdr (regexp-match nick-regex x)))
(define (parse-server x)
  (regexp-match server-regex x))

(define (parse-sender s)
  (cond ((not s) #f)
        ((or (parse-server s)
             (parse-nick s)) => values)
        (else (error "invalid sender" s))))

(define (parse-message x)
  (regexp-match message-regex x))

(define (make-irc-message s)
  (let ((x (parse-message s)))
    (unless x (error "invalid irc message" s))
    (let ((sender (parse-sender (list-ref x 1)))
          (params (filter (λ (x) (not (string=? "" x))) (regexp-split #rx" " (list-ref x 3))))
          (text (list-ref x 4)))
      (irc-message sender (list-ref x 2)
                   params (and text (substring text 1))))))

Name: Anonymous 2011-06-19 0:58

>>3
1/10

Name: Anonymous 2011-06-19 20:16

>>3
yuck, POSIX classes

Name: Anonymous 2011-06-19 21:17

>>3
I would have expected better from a lisp hacker

Name: Anonymous 2011-06-20 2:40

Name: Ryu 2011-06-20 3:25

>>7
Hadouken!

Name: Alex C 2011-06-20 5:25

Take a look at RFC 2812 (IRC Client Protocol): http://tools.ietf.org/html/rfc2812

This should give you some tips on the proper behaviour your client should implement.

In your code snipped, whenever someone sends you a PRIVMSG/NOTICE that included "Checking Ident" (which is not sent by all servers, mind you), you will send a USER and NICK message to the server...

Name: Anonymous 2011-06-20 5:35

sizeof(buf) != MAX-1

Name: Anonymous 2011-06-20 5:37

Actually, this is entirely embarrassing.

Name: Anonymous 2011-06-20 6:42

>>1

It just seems C is not for you, but fear not! There are languages where nice looking turtles move on screen and draw stuff. You should try one of them.

Name: Anonymous 2012-04-02 3:55

bump

Name: Anonymous 2012-04-02 3:56

>>12
name pls

Name: Anonymous 2012-04-02 3:57

void ParseMessage(char* MESSAGE) //remember to free params strings
{
    char prefix[128], command[128];
    char* params[15];
    int i = 0, n;
   
    prefix[0] = 0;
    if(MESSAGE[0] == ':')
    {
        i = strcspn(MESSAGE, " ");
        memcpy(prefix, MESSAGE + 1, i - 1);
        prefix[i - 1] = 0;
        MESSAGE = MESSAGE + i + 1;
    }

    i = strcspn(MESSAGE, " ");
    memcpy(command, MESSAGE, i);
    command[i] = 0;

    for (n = 0; n < 15; n++)
        params[n] = NULL;

    MESSAGE = MESSAGE + i;
    for (n = 0; n < 15; n++)
    {
        if (MESSAGE[0] != ' ')
        {
          n = n - 1;
            break;
        }
        if (MESSAGE[1] == ':')
        {
            params[n] = malloc(strlen(MESSAGE) - 1);
            params[n][strlen(MESSAGE) - 2] = 0;
            memcpy(params[n], MESSAGE + 2, strlen(MESSAGE) - 2);
            break;
        }
        i = strcspn(MESSAGE + 1, " ");
        params[n] = malloc(i + 1);
        *(params[n] + i) = 0;
        memcpy(params[n], MESSAGE + 1, i);
        MESSAGE = MESSAGE + i + 1;
    }


    printf("prefix: %s\tcommand: %s\t\nparams=", prefix, command);
    for (i = 0; i <= n; i++)
        printf("%s,", params[i]);
    printf("\n");

    for (i = 0; i < 15; i++)
        free(params[i]);
}

>implying I can into text boards

Name: Anonymous 2012-04-02 4:33

Why not look at some code that sucks less? ii is the best irc client, because it doesn't force a crappy graphical user interface on you. You can, for instance, use echo and tail -f to write and read channels. The code is simple since it's only the irc stuff: http://hg.suckless.org/ii/file/d163c8917af7/ii.c

Name: Anonymous 2012-04-02 4:38

Remember the two rules.

1. If it ain't Lisp, it's crap.
2. Lisp is shit.

Name: Anonymous 2012-04-03 14:56

parse => perl

Name: Anonymous 2012-04-03 15:03

sprintf
U MENA std::stringstream!??!?!?!!??!

Name: Anonymous 2012-04-03 16:21

``IRC clients'' are for faggots. Real men open a socket and type in the commands.

Name: Anonymous 2012-04-03 16:25

>>19
He probably didn't mean that, since the title of his post contained the phrase in c.

Name: Anonymous 2012-04-04 2:16

Name: Anonymous 2012-04-04 5:19

>>20

Do you respond to all of the ping requests manually as well? =D

Name: Anonymous 2012-04-04 5:29

You know you can open a FILE * on the socket using fdopen() and then fprintf to it, right?

Name: Anonymous 2012-04-04 6:21

>>21
in c
U MENA in c++!??!?!?!!??!

Name: Anonymous 2012-04-04 8:04

`
open a file on the socket

PLAN NINE... IN MY SEE?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List