ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/irc.c
(Generate patch)

Comparing hopm/trunk/src/irc.c (file contents):
Revision 5115 by michael, Wed Dec 24 22:17:42 2014 UTC vs.
Revision 5116 by michael, Thu Dec 25 15:04:31 2014 UTC

# Line 1 | Line 1
1 /* vim: set shiftwidth=3 softtabstop=3 expandtab: */
2
1   /*
2   * Copyright (C) 2002-2003  Erik Fears
3   *
# Line 233 | Line 231 | irc_init(void)
231  
232    if (IRC_FD == -1)
233    {
234 <      switch(errno)
235 <      {
236 <         case EINVAL:
237 <         case EPROTONOSUPPORT:
238 <            log_printf("IRC -> socket(): SOCK_STREAM is not "
239 <                "supported on this domain");
240 <            break;
241 <         case ENFILE:
242 <            log_printf("IRC -> socket(): Not enough free file "
243 <                "descriptors to allocate IRC socket");
244 <            break;
245 <         case EMFILE:
246 <            log_printf("IRC -> socket(): Process table overflow when "
247 <                "requesting file descriptor");
248 <            break;
249 <         case EACCES:
250 <            log_printf("IRC -> socket(): Permission denied to create "
251 <                "socket of type SOCK_STREAM");
252 <            break;
253 <         case ENOMEM:
254 <            log_printf("IRC -> socket(): Insufficient memory to "
255 <                "allocate socket");
258 <            break;
259 <         default:
260 <            log_printf("IRC -> socket(): Unknown error allocating "
261 <                "socket");
262 <            break;
263 <      }
234 >    switch (errno)
235 >    {
236 >      case EINVAL:
237 >      case EPROTONOSUPPORT:
238 >        log_printf("IRC -> socket(): SOCK_STREAM is not supported on this domain");
239 >        break;
240 >      case ENFILE:
241 >        log_printf("IRC -> socket(): Not enough free file descriptors to allocate IRC socket");
242 >        break;
243 >      case EMFILE:
244 >        log_printf("IRC -> socket(): Process table overflow when requesting file descriptor");
245 >        break;
246 >      case EACCES:
247 >        log_printf("IRC -> socket(): Permission denied to create socket of type SOCK_STREAM");
248 >        break;
249 >      case ENOMEM:
250 >        log_printf("IRC -> socket(): Insufficient memory to allocate socket");
251 >        break;
252 >      default:
253 >        log_printf("IRC -> socket(): Unknown error allocating socket");
254 >        break;
255 >    }
256  
257 <      exit(EXIT_FAILURE);
258 <   }
257 >    exit(EXIT_FAILURE);
258 >  }
259  
260 <   /* Bind */
261 <   if (!EmptyString(IRCItem->vhost))
262 <   {
263 <      int bindret = 0;
260 >  /* Bind */
261 >  if (!EmptyString(IRCItem->vhost))
262 >  {
263 >    int bindret = 0;
264  
265 <      if (!inet_pton(AF_INET, IRCItem->vhost, &(IRC_LOCAL.in4.s_addr)))
266 <      {
267 <         log_printf("IRC -> bind(): %s is an invalid address", IRCItem->vhost);
268 <         exit(EXIT_FAILURE);
269 <      }
265 >    if (!inet_pton(AF_INET, IRCItem->vhost, &(IRC_LOCAL.in4.s_addr)))
266 >    {
267 >      log_printf("IRC -> bind(): %s is an invalid address", IRCItem->vhost);
268 >      exit(EXIT_FAILURE);
269 >    }
270  
271 <      bsaddr.sa4.sin_addr.s_addr = IRC_LOCAL.in4.s_addr;
272 <      bsaddr.sa4.sin_family = AF_INET;
273 <      bsaddr.sa4.sin_port = htons(0);
271 >    bsaddr.sa4.sin_addr.s_addr = IRC_LOCAL.in4.s_addr;
272 >    bsaddr.sa4.sin_family = AF_INET;
273 >    bsaddr.sa4.sin_port = htons(0);
274  
275 <      bindret = bind(IRC_FD, (struct sockaddr *) &(bsaddr.sa4), sizeof(bsaddr.sa4));
275 >    bindret = bind(IRC_FD, (struct sockaddr *) &(bsaddr.sa4), sizeof(bsaddr.sa4));
276  
277 <      if (bindret)
277 >    if (bindret)
278 >    {
279 >      switch (errno)
280        {
281 <         switch(errno)
282 <         {
283 <            case EACCES:
284 <               log_printf("IRC -> bind(): No access to bind to %s",
285 <                   IRCItem->vhost);
286 <               break;
287 <            default:
288 <               log_printf("IRC -> bind(): Error binding to %s (%d)",
289 <                   IRCItem->vhost, errno);
296 <               break;
297 <         }
298 <         exit(EXIT_FAILURE);
281 >        case EACCES:
282 >          log_printf("IRC -> bind(): No access to bind to %s", IRCItem->vhost);
283 >          break;
284 >        default:
285 >          log_printf("IRC -> bind(): Error binding to %s (%d)", IRCItem->vhost, errno);
286 >          break;
287 >      }
288 >
289 >      exit(EXIT_FAILURE);
290      }
291    }
292   }
# Line 468 | Line 459 | irc_reconnect(void)
459   static void
460   irc_read(void)
461   {
462 <   int len;
463 <   char c;
462 >  int len;
463 >  char c;
464  
465 <   while ((len = read(IRC_FD, &c, 1)) > 0)
466 <   {
467 <      if (c == '\r')
468 <         continue;
465 >  while ((len = read(IRC_FD, &c, 1)) > 0)
466 >  {
467 >    if (c == '\r')
468 >      continue;
469  
470 <      if (c == '\n')
471 <      {
472 <         /* Null string. */
473 <         IRC_RAW[IRC_RAW_LEN] = '\0';
474 <         /* Parse line. */
475 <         irc_parse();
476 <         /* Reset counter. */
486 <         IRC_RAW_LEN = 0;
487 <         break;
488 <      }
470 >    if (c == '\n')
471 >    {
472 >      /* Null string. */
473 >      IRC_RAW[IRC_RAW_LEN] = '\0';
474 >
475 >      /* Parse line. */
476 >      irc_parse();
477  
478 <      if (c != '\r' && c != '\n' && c != '\0')
491 <         IRC_RAW[IRC_RAW_LEN++] = c;
492 <   }
493 <
494 <   if((len <= 0) && (errno != EAGAIN))
495 <   {
496 <      if(OPT_DEBUG >= 2)
497 <         log_printf("irc_read -> errno=%d len=%d", errno, len);
498 <      irc_reconnect();
478 >      /* Reset counter. */
479        IRC_RAW_LEN = 0;
480 <      return;
481 <   }
480 >      break;
481 >    }
482 >
483 >    if (c != '\r' && c != '\n' && c != '\0')
484 >      IRC_RAW[IRC_RAW_LEN++] = c;
485 >  }
486 >
487 >  if ((len <= 0) && (errno != EAGAIN))
488 >  {
489 >    if (OPT_DEBUG >= 2)
490 >      log_printf("irc_read -> errno=%d len=%d", errno, len);
491 >
492 >    irc_reconnect();
493 >    IRC_RAW_LEN = 0;
494 >    return;
495 >  }
496   }
497  
498   /* irc_parse
# Line 512 | Line 506 | irc_read(void)
506   static void
507   irc_parse(void)
508   {
509 <   struct UserInfo *source_p;
510 <   char *pos;
517 <   unsigned int i;
518 <
519 <   /*
520 <      parv stores the parsed token, parc is the count of the parsed
521 <      tokens
522 <    
523 <      parv[0] is ALWAYS the source, and is the server name of the source
524 <      did not exist
525 <   */
509 >  struct UserInfo *source_p;
510 >  char *pos;
511  
512 <   static char            *parv[17];
513 <   static unsigned int     parc;
514 <   static char             msg[MSGLENMAX];    /* Temporarily stores IRC msg to pass to handlers */
515 <
516 <   parc = 1;
517 <
518 <   if(IRC_RAW_LEN <= 0)
519 <      return;
520 <
521 <   if (OPT_DEBUG >= 2)
537 <      log_printf("IRC READ -> %s", IRC_RAW);
512 >  /*
513 >   * parv stores the parsed token, parc is the count of the parsed
514 >   * tokens
515 >   *
516 >   * parv[0] is ALWAYS the source, and is the server name of the source
517 >   * did not exist
518 >   */
519 >  static char            *parv[17];
520 >  static unsigned int     parc;
521 >  static char             msg[MSGLENMAX];    /* Temporarily stores IRC msg to pass to handlers */
522  
523 <   time(&IRC_LAST);
523 >  parc = 1;
524  
525 <   /* Store a copy of IRC_RAW for the handlers (for functions that need PROOF) */
526 <   strlcpy(msg, IRC_RAW, sizeof(msg));
525 >  if (IRC_RAW_LEN <= 0)
526 >    return;
527  
528 <   /* parv[0] is always the source */
529 <   if(IRC_RAW[0] == ':')
546 <      parv[0] = IRC_RAW + 1;
547 <   else
548 <   {
549 <      parv[0] = IRCItem->server;
550 <      parv[parc++] = IRC_RAW;
551 <   }
528 >  if (OPT_DEBUG >= 2)
529 >    log_printf("IRC READ -> %s", IRC_RAW);
530  
531 <   pos = IRC_RAW;
531 >  time(&IRC_LAST);
532  
533 <   while((pos = strchr(pos, ' ')) && parc <= 17)
534 <   {
533 >  /* Store a copy of IRC_RAW for the handlers (for functions that need PROOF) */
534 >  strlcpy(msg, IRC_RAW, sizeof(msg));
535  
536 <      /* Avoid excessive spaces and end of IRC_RAW */
537 <      if(*(pos + 1) == ' ' && *(pos + 1) == '\0')
538 <      {
539 <         pos++;
540 <         continue;
541 <      }
536 >  /* parv[0] is always the source */
537 >  if (IRC_RAW[0] == ':')
538 >    parv[0] = IRC_RAW + 1;
539 >  else
540 >  {
541 >    parv[0] = IRCItem->server;
542 >    parv[parc++] = IRC_RAW;
543 >  }
544  
545 <      /* Anything after a : is considered the final string of the
566 <            message */
567 <      if(*(pos + 1) == ':')
568 <      {
569 <         parv[parc++] = pos + 2;
570 <         *pos = '\0';
571 <         break;
572 <      }
545 >  pos = IRC_RAW;
546  
547 <      /* Set the next parv at this position and replace the space with a
548 <         \0 for the previous parv */
549 <      parv[parc++] = pos + 1;
550 <      *pos = '\0';
547 >  while ((pos = strchr(pos, ' ')) && parc <= 17)
548 >  {
549 >    /* Avoid excessive spaces and end of IRC_RAW */
550 >    if (*(pos + 1) == ' ' && *(pos + 1) == '\0')
551 >    {
552        pos++;
553 <   }
553 >      continue;
554 >    }
555  
556 <   /* Generate a UserInfo struct from the source */
556 >    /* Anything after a : is considered the final string of the message */
557 >    if (*(pos + 1) == ':')
558 >    {
559 >      parv[parc++] = pos + 2;
560 >      *pos = '\0';
561 >      break;
562 >    }
563  
564 <   source_p = userinfo_create(parv[0]);
564 >    /*
565 >     * Set the next parv at this position and replace the space with a
566 >     * \0 for the previous parv
567 >     */
568 >    parv[parc++] = pos + 1;
569 >    *pos = '\0';
570 >    pos++;
571 >  }
572  
573 <   /* Determine which command this is from the command table
574 <      and let the handler for that command take control */
573 >  /* Generate a UserInfo struct from the source */
574 >  source_p = userinfo_create(parv[0]);
575  
576 <   for(i = 0; i < (sizeof(COMMAND_TABLE) / sizeof(struct CommandHash)); i++)
577 <      if(strcasecmp(COMMAND_TABLE[i].command, parv[1]) == 0)
578 <      {
579 <         (*COMMAND_TABLE[i].handler)(parv, parc, msg, source_p);
580 <         break;
581 <      }
576 >  /*
577 >   * Determine which command this is from the command table
578 >   * and let the handler for that command take control
579 >   */
580 >  for (unsigned int i = 0; i < (sizeof(COMMAND_TABLE) / sizeof(struct CommandHash)); ++i)
581 >  {
582 >    if (strcasecmp(COMMAND_TABLE[i].command, parv[1]) == 0)
583 >    {
584 >      (*COMMAND_TABLE[i].handler)(parv, parc, msg, source_p);
585 >      break;
586 >    }
587 >  }
588  
589 <   userinfo_free(source_p);
589 >  userinfo_free(source_p);
590   }
591  
592   /* irc_timer
# Line 645 | Line 639 | static struct ChannelConf *
639   get_channel(const char *channel)
640   {
641    node_t *node;
648  struct ChannelConf *item;
642  
643    LIST_FOREACH(node, IRCItem->channels->head)
644    {
645 <    item = node->data;
645 >    struct ChannelConf *item = node->data;
646  
647      if (strcasecmp(item->name, channel) == 0)
648        return item;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines