| summaryrefslogtreecommitdiff | 
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2016-05-27 13:27:44 +0200 | 
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2016-05-27 13:27:44 +0200 | 
| commit | ecdebd8f416b803b43ce93533666599e1442f62d (patch) | |
| tree | b6afeb478402efbfbba01247808da7ff70f8ae01 /src | |
| parent | 23abb2acd221762ec482985f91fa3eff1123fb9a (diff) | |
Working on improving the network handling.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/assimilate.c | 2 | ||||
| -rw-r--r-- | src/core/create_sentences.c | 116 | ||||
| -rw-r--r-- | src/core/knowledge.c | 48 | ||||
| -rw-r--r-- | src/core/knowledge.h | 38 | ||||
| -rw-r--r-- | src/core/main.c | 68 | ||||
| -rw-r--r-- | src/io/network.c | 417 | ||||
| -rw-r--r-- | src/io/network.h | 3 | ||||
| -rw-r--r-- | src/io/network_types.h | 10 | ||||
| -rw-r--r-- | src/tool/strings.c | 2 | ||||
| -rw-r--r-- | src/tool/strings.h | 2 | 
10 files changed, 437 insertions, 269 deletions
| diff --git a/src/core/assimilate.c b/src/core/assimilate.c index eb6aa17..28f4dd4 100644 --- a/src/core/assimilate.c +++ b/src/core/assimilate.c @@ -5,6 +5,8 @@  #include "knowledge.h" +/** Functions to assimilate sentences using a ZoO_knowledge structure *********/ +  static int link_to  (     ZoO_index links_count [const restrict static 1], diff --git a/src/core/create_sentences.c b/src/core/create_sentences.c index bc410e5..e999199 100644 --- a/src/core/create_sentences.c +++ b/src/core/create_sentences.c @@ -7,6 +7,17 @@  #include "knowledge.h" +/** Functions to create sentences using a ZoO_knowledge structure *************/ + +/* + * Returns the index of a element in {links} chosen randomly according + * to the distribution in {links_occurrences}. + * Pre: + *    (!= occurrences 0). + *    (== (length links_occurrences) (length links)). + *    (== (sum links_occurrences) occurrences). + *    (can_store ZoO_index (length links)). + */  static ZoO_index pick_an_index  (     ZoO_index const occurrences, @@ -17,29 +28,40 @@ static ZoO_index pick_an_index     ZoO_index result, accumulator, random_number;     result = 0; + +   /* +    * Safe: +    * (> (length links_occurrences) 0). +    */     accumulator = links_occurrences[0]; +     random_number = (((ZoO_index) rand()) % occurrences);     while (accumulator < random_number)     { -        /* -       * Should be safe: -       * result overflowing <-> sum('links_occurrences') > 'occurrences' -       * and sum('links_occurrences') == 'occurrences' +       * Safe: +       * (-> +       *    (and +       *       (== accumulator (sum links_occurrences[0:result])) +       *       (< accumulator random_number) +       *       (< random_number occurrences) +       *       (== occurrences (sum links_occurrences)) +       *       (can_store ZoO_index (length links)) +       *       (== (length links_occurrences) (length links)) +       *    ) +       *    (and +       *       (< result' (length links_occurrences)) +       *       (can_store ZoO_index result') +       *       (=< accumulator' occurrences) +       *    ) +       * )         */        result += 1; - -      /* -       * Should be safe: -       *  - sum('links_occurrences') == 'occurrences'. -       *  - 'occurrences' is safe. -       *  ---- -       *  'accumulator' is safe. -       */        accumulator += links_occurrences[result];     } +   /* Safe: (< result (length links)) */     return links[result];  } @@ -330,7 +352,7 @@ static unsigned char * extend_right  int ZoO_knowledge_extend  (     struct ZoO_knowledge k [const static 1], -   const struct ZoO_strings string [const static 1], +   const struct ZoO_strings string [const],     int const ignore_first_word,     ZoO_char * result [const static 1]  ) @@ -339,48 +361,56 @@ int ZoO_knowledge_extend     size_t sentence_size;     ZoO_index i, word_id, word_min_score, word_min_id, credits; -   word_found = 0;     credits = ZoO_MAX_REPLY_WORDS; -   if (ignore_first_word) -   { -      i = 1; -   } -   else +   if (string != (struct ZoO_strings *) NULL)     { -      i = 0; -   } +      word_found = 0; -   for (; i < string->words_count; ++i) -   { -      /* prevents k [restrict] */ -      if (ZoO_knowledge_find(k, string->words[i], &word_min_id) == 0) +      if (ignore_first_word)        { -         word_found = 1; -         word_min_score = k->words[word_min_id].occurrences; +         i = 1; +      } +      else +      { +         i = 0; +      } -         break; +      for (; i < string->words_count; ++i) +      { +         /* prevents k [restrict] */ +         if (ZoO_knowledge_find(k, string->words[i], &word_min_id) == 0) +         { +            word_found = 1; +            word_min_score = k->words[word_min_id].occurrences; + +            break; +         }        } -   } -   if (word_found == 0) -   { -      word_min_id = (rand() % k->words_count); -      word_min_score = k->words[word_min_id].occurrences; -   } +      if (word_found == 0) +      { +         word_min_id = (rand() % k->words_count); +         word_min_score = k->words[word_min_id].occurrences; +      } -   for (; i < string->words_count; ++i) -   { -      if -      ( -         (ZoO_knowledge_find(k, string->words[i], &word_id) == 0) -         && (k->words[word_id].occurrences < word_min_score) -      ) +      for (; i < string->words_count; ++i)        { -         word_min_score = k->words[word_id].occurrences; -         word_min_id = word_id; +         if +         ( +            (ZoO_knowledge_find(k, string->words[i], &word_id) == 0) +            && (k->words[word_id].occurrences < word_min_score) +         ) +         { +            word_min_score = k->words[word_id].occurrences; +            word_min_id = word_id; +         }        }     } +   else +   { +      word_min_id = (rand() % k->words_count); +   }     /* 3: 2 spaces + '\0' */     /* FIXME: not overflow-safe */ diff --git a/src/core/knowledge.c b/src/core/knowledge.c index 31ccb97..9b4e250 100644 --- a/src/core/knowledge.c +++ b/src/core/knowledge.c @@ -6,6 +6,8 @@  #include "knowledge.h" +/** Basic functions of the ZoO_knowledge structure ****************************/ +  /* XXX: are we as close to immutable as we want to be? */  unsigned int const ZoO_knowledge_punctuation_chars_count = 7;  const ZoO_char const ZoO_knowledge_punctuation_chars[7] = @@ -33,6 +35,7 @@ const ZoO_char const ZoO_knowledge_forbidden_chars[8]=        '>'     }; +/* See "knowledge.h". */  int ZoO_knowledge_find  (     const struct ZoO_knowledge k [const restrict static 1], @@ -54,7 +57,7 @@ int ZoO_knowledge_find     current_min = 0; -   /* overflow-safe: k->words_count >= 1 */ +   /* Safe: (>= k->words_count 1) */     current_max = (k->words_count - 1);     for (;;) @@ -124,6 +127,18 @@ static void word_init (struct ZoO_knowledge_word w [const restrict static 1])     w->backward_links = (ZoO_index *) NULL;  } +/* + * When returning 0: + *    All punctuation symbols were added to {k}. + * When returning -1: + *    The mandatory punctuation symbols have been added to {k}, but some of the + *    additional ones did not. This does not prevent ZoO from working, but + *    will result in some punctuation symbols to be handled exactly like + *    common words. + * When returning -2: + *    The mandatory punctuation symbols have not added to {k}. ZoO will not be + *    able to work. + */  static int add_punctuation_nodes  (     struct ZoO_knowledge k [const static 1] @@ -177,6 +192,7 @@ static int add_punctuation_nodes     return error;  } +/* See "knowledge.h" */  int ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])  {     k->words_count = 0; @@ -193,6 +209,10 @@ int ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1])     return 0;  } +/* + * Frees all the memory used by {w}, but not {w} itself. + * The values of {w}'s members are set to reflect the changes. + */  static void finalize_word  (     struct ZoO_knowledge_word w [const restrict static 1] @@ -237,13 +257,13 @@ static void finalize_word     w->backward_links_count = 0;  } +/* See "knowledge.h" */  void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1])  {     ZoO_index i;     for (i = 0; i < k->words_count; ++i)     { -      /* prevents k [restrict] */        finalize_word(k->words + i);     } @@ -264,6 +284,7 @@ void ZoO_knowledge_finalize (struct ZoO_knowledge k [const restrict static 1])     }  } +/* See "knowledge.h" */  int ZoO_knowledge_learn  (     struct ZoO_knowledge k [const static 1], @@ -291,7 +312,7 @@ int ZoO_knowledge_learn           return -1;        } -      /* overflow-safe */ +      /* overflow-safe: (< k->words[*result].occurrences ZoO_INDEX_MAX) */        k->words[*result].occurrences += 1;        return 0; @@ -310,7 +331,7 @@ int ZoO_knowledge_learn           (void *) k->words,           (              ( -               /* overflow-safe: k->words_count < ZoO_INDEX_MAX */ +               /* overflow-safe: (< k->words_count ZoO_INDEX_MAX) */                 (size_t) (k->words_count + 1)              )              * sizeof(struct ZoO_knowledge_word) @@ -336,7 +357,7 @@ int ZoO_knowledge_learn           (void *) k->sorted_indices,           (              ( -               /* overflow-safe: k->words_count < ZoO_INDEX_MAX */ +               /* overflow-safe: (< k->words_count ZoO_INDEX_MAX) */                 (size_t) (k->words_count + 1)              )              * sizeof(ZoO_index) @@ -359,22 +380,27 @@ int ZoO_knowledge_learn     k->sorted_indices = new_sorted_indices;     /* We can only move indices right of *result if they exist. */ -   if (*result != k->words_count) +   if (*result < k->words_count)     {        /* TODO: check if correct. */        memmove        (           /* -          * overflow-safe: -          *  - k->words_count < ZoO_INDEX_MAX -          *  - (k->sorted_indices + *result + 1) =< k->words_count +          * Safe: +          *  (-> +          *    (and +          *       (== (length k->sorted_indices) (+ k->words_count 1)) +          *       (< *result k->words_count) +          *    ) +          *    (< (+ *result 1) (length k->sorted_indices)) +          * )            */           (void *) (k->sorted_indices + *result + 1), -         /* overflow-safe: see above */ +         /* Safe: see above */           (const void *) (k->sorted_indices + *result),           (              ( -               /* overflow-safe: *result < k->words_count */ +               /* Safe: (< *result k->words_count) */                 (size_t) (k->words_count - *result)              )              * sizeof(ZoO_index) diff --git a/src/core/knowledge.h b/src/core/knowledge.h index f20cb16..c9ea342 100644 --- a/src/core/knowledge.h +++ b/src/core/knowledge.h @@ -5,10 +5,35 @@  #include "knowledge_types.h" +/* + * Initializes all of {k}'s members to sane values. + * + * When returning 0: + *    Initial punctuation nodes (including the mandatory "START OF LINE" and + *    "END OF LINE" ones) have successfully been added to {k}. + * + * When return -1: + *    Something went wrong, leading to {k} not being safe for use. + *    {k} has been finalized. + */  int ZoO_knowledge_initialize (struct ZoO_knowledge k [const static 1]); +/* + * Frees all the memory used by {k}, but not {k} itself. + * The values of {k}'s members are set to reflect the changes. + */  void ZoO_knowledge_finalize (struct ZoO_knowledge k [const static 1]); +/* + * When returning 0: + *    {word} is in {k}. + *    {word} is located at {k->words[*result]}. + * + * When returning -1: + *    {word} is not in {k}. + *    {*result} is where {word} was expected to be found in + *    {k->sorted_indices}. + */  int ZoO_knowledge_find  (     const struct ZoO_knowledge k [const restrict static 1], @@ -16,6 +41,17 @@ int ZoO_knowledge_find     ZoO_index result [const restrict static 1]  ); +/* + * When returning 0: + *    {word} was either added to {k} or its representation in {k} has its + *    occurrences count increased. + *    {*result} indicates where {word} is in {k->words}. + * + * When returning -1: + *    Something went wrong when adding the occurrence of {word} to {k}. + *    {k} remains semantically unchanged. + *    {*result} may or may not have been altered. + */  int ZoO_knowledge_learn  (     struct ZoO_knowledge k [const static 1], @@ -34,7 +70,7 @@ int ZoO_knowledge_assimilate  int ZoO_knowledge_extend  (     struct ZoO_knowledge k [const static 1], -   const struct ZoO_strings string [const static 1], +   const struct ZoO_strings string [const],     int const ignore_first_word,     ZoO_char * result [const static 1]  ); diff --git a/src/core/main.c b/src/core/main.c index 40b3705..1a8a40f 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -150,6 +150,43 @@ static int should_reply     return (param->reply_rate >= (rand() % 100));  } +static void handle_user_join +( +   struct ZoO_state s [const static 1] +) +{ +   ZoO_char * line; + +   if +   ( +      (s->param.reply_rate >= (rand() % 100)) +      && +      ( +         ZoO_knowledge_extend +         ( +            &(s->knowledge), +            (struct ZoO_strings *) NULL, +            0, +            &line +         ) == 0 +      ) +   ) +   { +      if (line[0] == ' ') +      { +         strcpy((s->network.out), (line + 1)); +      } +      else +      { +         strcpy((s->network.out), line); +      } + +      free((void *) line); + +      ZoO_network_send(&(s->network)); +   } +} +  static void handle_message  (     struct ZoO_state s [const static 1], @@ -167,7 +204,7 @@ static void handle_message        (           string,           (size_t) msg_size, -         (s->network.msg + msg_offset), +         (s->network.in + msg_offset),           ZoO_knowledge_punctuation_chars_count,           ZoO_knowledge_punctuation_chars        ) < 0 @@ -202,11 +239,11 @@ static void handle_message     {        if (line[0] == ' ')        { -         strcpy((s->network.msg), (line + 1)); +         strcpy((s->network.out), (line + 1));        }        else        { -         strcpy((s->network.msg), line); +         strcpy((s->network.out), line);        }        free((void *) line); @@ -226,10 +263,11 @@ static void handle_message     }  } -static int main_loop  (struct ZoO_state s [const static 1]) +static int main_loop (struct ZoO_state s [const static 1])  {     struct ZoO_strings string;     ssize_t msg_offset, msg_size; +   enum ZoO_msg_type msg_type;     msg_offset = 0;     msg_size = 0; @@ -238,9 +276,27 @@ static int main_loop  (struct ZoO_state s [const static 1])     while (run)     { -      if (ZoO_network_receive(&(s->network), &msg_offset, &msg_size) == 0) +      if +      ( +         ZoO_network_receive +         ( +            &(s->network), +            &msg_offset, +            &msg_size, +            &msg_type +         ) == 0 +      )        { -         handle_message(s, &string, msg_offset, msg_size); +         switch (msg_type) +         { +            case ZoO_JOIN: +               handle_user_join(s); +               break; + +            case ZoO_PRIVMSG: +               handle_message(s, &string, msg_offset, msg_size); +               break; +         }        }     } diff --git a/src/io/network.c b/src/io/network.c index c8d05a2..d2797c9 100644 --- a/src/io/network.c +++ b/src/io/network.c @@ -13,10 +13,10 @@  #include "network.h" -static int reconnect (struct ZoO_network net [const restrict static 1]) +static int re_create_socket (struct ZoO_network net [const restrict static 1])  {     struct timeval timeout; -   int old_errno = errno; +   const int old_errno = errno;     errno = 0;     timeout.tv_sec = ZoO_NETWORK_TIMEOUT; @@ -37,19 +37,11 @@ static int reconnect (struct ZoO_network net [const restrict static 1])     if (net->connection == -1)     { -      ZoO_FATAL -      ( -         "Could not create socket: %s.", -         strerror(errno) -      ); +      ZoO_ERROR("Could not create socket: %s.", strerror(errno)); -      errno = old_errno; - -      return -1; +      goto RETURN_FAILED;     } -   errno = 0; -     if     (        ( @@ -77,13 +69,9 @@ static int reconnect (struct ZoO_network net [const restrict static 1])     {        ZoO_ERROR("Could not set timeout on network socket: %s", strerror(errno)); -      errno = old_errno; - -      return -1; +      goto RETURN_FAILED;     } -   errno = old_errno; -     ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "(Re)connecting to network...");     if @@ -96,73 +84,67 @@ static int reconnect (struct ZoO_network net [const restrict static 1])        ) != 0     )     { -      ZoO_ERROR -      ( -         "Unable to connect to the network: %s", -         strerror(errno) -      ); +      ZoO_ERROR("Could not establish connection: %s", strerror(errno)); -      errno = old_errno; - -      return -1; +      goto RETURN_FAILED;     }     errno = old_errno; -   snprintf -   ( -      net->msg, -      512, -      "USER %s 8 * :%s\r\n", -      net->user, -      net->name -   ); +   return 0; -   errno = 0; +RETURN_FAILED: +   errno = old_errno; -   if (write(net->connection, net->msg, strlen(net->msg)) < 1) -   { -      ZoO_ERROR -      ( -         "Unable to write to the network: %s", -         strerror(errno) -      ); +   return -1; +} -      errno = old_errno; +static int reconnect (struct ZoO_network net [const restrict static 1]) +{ +   const int old_errno = errno; +   memset(net->in, 0, (sizeof(ZoO_char) * 513)); +   memset(net->out, 0, (sizeof(ZoO_char) * 513)); +   memset(net->buffer, 0, (sizeof(ZoO_char) * 513)); + +   if (re_create_socket(net) < 0) +   {        return -1;     } -   snprintf -   ( -      net->msg, -      512, -      "NICK %s\r\n", -      net->nick -   ); - -   errno = 0; +   snprintf(net->out, 512, "USER %s 8 * :%s\r\n", net->user, net->name); -   if (write(net->connection, net->msg, strlen(net->msg)) < 1) +   if (write(net->connection, net->out, strlen(net->out)) < 1)     { -      ZoO_ERROR -      ( -         "Unable to write to the network: %s", -         strerror(errno) -      ); +      goto RETURN_WRITE_FAILED; +   } -      errno = old_errno; +   snprintf(net->out, 512, "NICK %s\r\n", net->nick); -      return -1; +   if (write(net->connection, net->out, strlen(net->out)) < 1) +   { +      goto RETURN_WRITE_FAILED;     } -   errno = old_errno; -     net->buffer_remaining = 0;     net->buffer_index = 0; +     ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "(Re)connected."); +   errno = old_errno; +     return 0; + +RETURN_WRITE_FAILED: +   ZoO_ERROR +   ( +      "Unable to write to the network: %s", +      strerror(errno) +   ); + +   errno = old_errno; + +   return -1;  }  int ZoO_network_connect @@ -189,7 +171,9 @@ int ZoO_network_connect     net->buffer_remaining = 0;     memset(&hints, 0, sizeof(struct addrinfo)); -   memset(net->msg, 0, (sizeof(ZoO_char) * 513)); +   memset(net->in, 0, (sizeof(ZoO_char) * 513)); +   memset(net->out, 0, (sizeof(ZoO_char) * 513)); +   memset(net->buffer, 0, (sizeof(ZoO_char) * 513));     hints.ai_family = AF_INET;     hints.ai_socktype = SOCK_STREAM; @@ -222,215 +206,240 @@ int ZoO_network_connect        return -1;     } -   errno = 0; - +   errno = old_errno;     reconnect(net);     return 0;  } -int ZoO_network_receive +static void buffer_msg  ( -   struct ZoO_network net [const restrict static 1], -   size_t msg_offset [const restrict static 1], -   size_t msg_size [const restrict static 1] +   struct ZoO_network net [const static 1]  )  { -   int old_errno; -   ssize_t in_count, in_index, msg_index, cmd; +   ssize_t in_count, i; -   old_errno = errno; +   if (net->buffer_remaining > 0) +   { +      in_count = net->buffer_remaining; +      net->buffer_remaining = 0; + +      goto PARSE_READ; +   } -   for (;;) +READ_MORE: +   in_count = read(net->connection, net->buffer, 512); + +   if (in_count < 0)     { -      msg_index = 0; +      ZoO_ERROR("Could not read from network: %s", strerror(errno)); + +      reconnect(net); -      errno = 0; +      goto READ_MORE; +   } + +PARSE_READ: +   for (i = 0; i < in_count; ++i) +   { +      net->in[net->buffer_index] = net->buffer[i]; -      while +      if        ( -         ( -            (in_count = -               read( -                  net->connection, -                  (net->buffer + net->buffer_index), -                  (512 - net->buffer_index) -               ) -            ) > 0 -         ) +         (net->buffer_index > 0) +         && (net->in[net->buffer_index - 1] == '\r') +         && (net->in[net->buffer_index] == '\n')        )        { -         net->buffer_remaining += in_count; +         net->buffer_remaining = (in_count - (i + 1)); +         net->in_length = (net->buffer_index - 1); +         net->buffer_index = 0; -         for -         ( -            in_index = 0; -            in_index < net->buffer_remaining; -            ++in_index -         ) +         if (net->buffer_remaining > 0)           { -            net->msg[msg_index] = net->buffer[net->buffer_index + in_index]; - -            if +            memmove              ( -               (msg_index == 511) -               || -               ( -                  (msg_index > 0) -                  && (net->msg[msg_index - 1] == '\r') -                  && (net->msg[msg_index] == '\n') -               ) -            ) -            { -               net->msg[msg_index + 1] = '\0'; +               (void *) net->buffer, +               (const void *) (net->buffer + (i + 1)), +               net->buffer_remaining +            ); +         } +         return; +      } -               if (net->buffer_index != net->buffer_remaining) -               { -                  memmove -                  ( -                     net->buffer, -                     (net->buffer + net->buffer_index), -                     (size_t) net->buffer_remaining -                  ); +      net->buffer_index += 1; -                  net->buffer_index = 0; -               } +      if (net->buffer_index > 512) +      { +         ZoO_S_WARNING("Incoming message is too long. Discarded."); -               net->buffer_remaining -= (in_index + 1); +         net->buffer_index = 0; +         net->buffer_remaining = 0; -               errno = old_errno; +         break; +      } +   } -               goto READ_MSG; -            } +   goto READ_MORE; +} -            ++msg_index; -         } +void handle_ping (struct ZoO_network net [const restrict static 1]) +{ +   const int old_errno = errno; -         net->buffer_remaining = 0; -         net->buffer_index = 0; +   #if ZoO_DEBUG_NETWORK_PING == 1 +      net->in[net->in_length] = '\0'; -         errno = 0; -      } +      ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s", net->in); -      ZoO_ERROR -      ( -         "Something went wrong while trying to read from the network: %s.", -         strerror(errno) -      ); +      net->in[net->in_length] = '\r'; +   #endif + +   net->in[1] = 'O'; + +   errno = 0; + +   if (write(net->connection, net->in, (net->in_length + 2)) < 1) +   { +      ZoO_ERROR("Could not reply to PING request: %s", strerror(errno));        errno = old_errno; -      if (reconnect(net) < 0) +      while (reconnect(net) < 0)        { -         return -1; +         sleep(5);        } -      continue; +      return; +   } -      READ_MSG: +   errno = old_errno; -      ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s\n", net->msg); +#if ZoO_DEBUG_NETWORK_PING == 1 +   net->in[net->in_length] = '\0'; -      /* XXX: doesn't that prevent net [restrict]? */ -      if (ZoO_IS_PREFIX("PING", net->msg)) -      { -         errno = 0; +   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->in); +#endif -         net->msg[1] = 'O'; +} -         if (write(net->connection, net->msg, strlen(net->msg)) < 1) -         { -            ZoO_ERROR("Could not reply to PING request: %s", strerror(errno)); +int ZoO_network_receive +( +   struct ZoO_network net [const restrict static 1], +   size_t msg_offset [const restrict static 1], +   size_t msg_size [const restrict static 1], +   enum ZoO_msg_type type [const restrict static 1] +) +{ +   const int old_errno = errno; +   ssize_t cmd, i; -            errno = old_errno; +READ_NEW_MSG: +   buffer_msg(net); -            if (reconnect(net) < 0) -            { -               return -1; -            } +   net->in[net->in_length + 2] = '\0'; -            continue; -         } +   /* XXX: doesn't that prevent net [restrict]? */ +   if (ZoO_IS_PREFIX("PING", net->in)) +   { -         ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s\n", net->msg); +      handle_ping(net); -         errno = old_errno; -      } -      else if (net->msg[0] == ':') -      { -         cmd = 0; +      goto READ_NEW_MSG; +   } -         for (in_index = 1; in_index < 512; in_index++) -         { -            if (net->msg[in_index] == ' ') -            { -               cmd = (in_index + 1); +   if (net->in_length == 0) +   { +      goto READ_NEW_MSG; +   } -               break; -            } -         } +   net->in[net->in_length] = '\0'; + +   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s", net->in); + +   if (net->in[0] == ':') +   { +      cmd = 0; -         if (cmd == 0) +      for (i = 1; i < 512; i++) +      { +         if (net->in[i] == ' ')           { -            continue; +            cmd = (i + 1); + +            break;           } +      } + +      if (ZoO_IS_PREFIX("001", (net->in + cmd))) +      { +         snprintf +         ( +            net->out, +            512, +            "JOIN :%s\r\n", +            net->channel +         ); -         if (ZoO_IS_PREFIX("001", (net->msg + cmd))) +         errno = 0; + +         if (write(net->connection, net->out, strlen(net->out)) < 1)           { -            snprintf +            ZoO_ERROR              ( -               net->msg, -               512, -               "JOIN :%s\r\n", -               net->channel +               "Could not send JOIN request: %s", +               strerror(errno)              ); -            errno = 0; +            errno = old_errno; -            if (write(net->connection, net->msg, strlen(net->msg)) < 1) +            if (reconnect(net) < 0)              { -               ZoO_ERROR -               ( -                  "Could not send JOIN request: %s", -                  strerror(errno) -               ); - -               errno = old_errno; - -               if (reconnect(net) < 0) -               { -                  return -1; -               } +               return -1;              } +         } -            ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->msg); +         errno = old_errno; -            continue; -         } +         ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->out); -         if (ZoO_IS_PREFIX("PRIVMSG", (net->msg + cmd))) +         goto READ_NEW_MSG; +      } + +      if (ZoO_IS_PREFIX("JOIN", (net->in + cmd))) +      { +         *type = ZoO_JOIN; + +         return 0; +      } + +      if (ZoO_IS_PREFIX("PRIVMSG", (net->in + cmd))) +      { + +         for (; i < 512; i++)           { -            for (; in_index < 512; in_index++) +            if (net->in[i] == ':')              { -               if (net->msg[in_index] == ':') -               { -                  cmd = (in_index + 1); +               cmd = (i + 1); -                  break; -               } +               break;              } +         } -            *msg_offset = cmd; -            *msg_size = (msg_index - *msg_offset - 1); +         *msg_offset = cmd; +         *msg_size = (net->in_length - cmd); -            /*net->msg[*msg_size - 1] = '\0'; */ +         /*net->in[*msg_size - 1] = '\0'; */ -            return 0; -         } +         *type = ZoO_PRIVMSG; + +         return 0;        }     } + +   goto READ_NEW_MSG;  }  int ZoO_network_send (struct ZoO_network net [const restrict static 1]) @@ -439,16 +448,16 @@ int ZoO_network_send (struct ZoO_network net [const restrict static 1])     snprintf     ( -      net->buffer, +      net->in,        512,        "PRIVMSG %s :%s\r\n",        net->channel, -      net->msg +      net->out     );     errno = 0; -   if (write(net->connection, net->buffer, strlen(net->buffer)) < 1) +   if (write(net->connection, net->in, strlen(net->in)) < 1)     {        ZoO_ERROR        ( @@ -470,7 +479,7 @@ int ZoO_network_send (struct ZoO_network net [const restrict static 1])     errno = old_errno; -   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->buffer); +   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->in);     return 0;  } diff --git a/src/io/network.h b/src/io/network.h index ac7284a..647b19c 100644 --- a/src/io/network.h +++ b/src/io/network.h @@ -17,7 +17,8 @@ int ZoO_network_receive  (     struct ZoO_network net [const static 1],     size_t msg_offset [const restrict static 1], -   size_t msg_size [const restrict static 1] +   size_t msg_size [const restrict static 1], +   enum ZoO_msg_type type [const restrict static 1]  );  int ZoO_network_send (struct ZoO_network net [const restrict static 1]); diff --git a/src/io/network_types.h b/src/io/network_types.h index 16c81da..9a328a7 100644 --- a/src/io/network_types.h +++ b/src/io/network_types.h @@ -9,13 +9,21 @@  #include "../pervasive.h" +enum ZoO_msg_type +{ +   ZoO_PRIVMSG, +   ZoO_JOIN +}; +  struct ZoO_network  {     size_t buffer_index;     size_t buffer_remaining; +   size_t in_length;     struct addrinfo * addrinfo;     ZoO_char buffer [513]; -   ZoO_char msg [513]; +   ZoO_char in [513]; +   ZoO_char out [513];     int connection;     const char * restrict channel;     const char * restrict user; diff --git a/src/tool/strings.c b/src/tool/strings.c index fc4434a..9fe13f6 100644 --- a/src/tool/strings.c +++ b/src/tool/strings.c @@ -199,7 +199,7 @@ static int parse_word  int ZoO_strings_parse  (     struct ZoO_strings s [const restrict static 1], -   size_t const input_size, +   size_t input_size,     ZoO_char input [const restrict],     ZoO_index const punctuations_count,     const ZoO_char punctuations [const restrict static punctuations_count] diff --git a/src/tool/strings.h b/src/tool/strings.h index 6e6e211..6fbecf7 100644 --- a/src/tool/strings.h +++ b/src/tool/strings.h @@ -10,7 +10,7 @@ void ZoO_strings_finalize (struct ZoO_strings s [const restrict static 1]);  int ZoO_strings_parse  (     struct ZoO_strings s [const static 1], -   size_t const input_size, +   size_t input_size,     ZoO_char input [const restrict],     ZoO_index const punctuations_count,     const ZoO_char punctuations [const restrict static punctuations_count] | 


