LibIRCClient 1.10 Used by Probotic
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

131 lines
3.2KB

  1. /*
  2. * Copyright (C) 2004-2012 George Yunaev gyunaev@ulduzsoft.com
  3. *
  4. * This library is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. * License for more details.
  13. */
  14. static void libirc_add_to_set (int fd, fd_set *set, int * maxfd)
  15. {
  16. FD_SET (fd, set);
  17. if ( *maxfd < fd )
  18. *maxfd = fd;
  19. }
  20. #if defined (ENABLE_DEBUG)
  21. static void libirc_dump_data (const char * prefix, const char * buf, unsigned int length)
  22. {
  23. printf ("%s: ", prefix);
  24. for ( ; length > 0; length -- )
  25. printf ("%c", *buf++);
  26. }
  27. #endif
  28. /*
  29. * Finds a separator (\x0D\x0A), which separates two lines.
  30. */
  31. static int libirc_findcrlf (const char * buf, int length)
  32. {
  33. int offset = 0;
  34. for ( ; offset < length; offset++ )
  35. {
  36. if ( buf[offset] == 0x0D && offset < length - 1 && buf[offset+1] == 0x0A )
  37. return offset;
  38. if ( buf[offset] == 0x0A)
  39. return offset;
  40. }
  41. return 0;
  42. }
  43. static int libirc_findcrlf_offset(const char *buf, int offset, const int length)
  44. {
  45. for(; offset < length; offset++)
  46. {
  47. if(buf[offset] != 0x0D && buf[offset] != 0x0A)
  48. {
  49. break;
  50. }
  51. }
  52. return offset;
  53. }
  54. static int libirc_findcrorlf (char * buf, int length)
  55. {
  56. int offset = 0;
  57. for ( ; offset < length; offset++ )
  58. {
  59. if ( buf[offset] == 0x0D || buf[offset] == 0x0A )
  60. {
  61. buf[offset++] = '\0';
  62. if ( offset < (length - 1)
  63. && (buf[offset] == 0x0D || buf[offset] == 0x0A) )
  64. offset++;
  65. return offset;
  66. }
  67. }
  68. return 0;
  69. }
  70. static void libirc_event_ctcp_internal (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  71. {
  72. (void)event;
  73. (void)count;
  74. if ( origin )
  75. {
  76. char nickbuf[128], textbuf[256];
  77. irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
  78. if ( strstr (params[0], "PING") == params[0] )
  79. irc_cmd_ctcp_reply (session, nickbuf, params[0]);
  80. else if ( !strcmp (params[0], "VERSION") )
  81. {
  82. if ( !session->ctcp_version )
  83. {
  84. unsigned int high, low;
  85. irc_get_version (&high, &low);
  86. snprintf (textbuf, sizeof (textbuf), "VERSION libircclient by Georgy Yunaev ver.%d.%d", high, low);
  87. }
  88. else
  89. snprintf (textbuf, sizeof (textbuf), "VERSION %s", session->ctcp_version);
  90. irc_cmd_ctcp_reply (session, nickbuf, textbuf);
  91. }
  92. else if ( !strcmp (params[0], "FINGER") )
  93. {
  94. sprintf (textbuf, "FINGER %s (%s) Idle 0 seconds",
  95. session->username ? session->username : "nobody",
  96. session->realname ? session->realname : "noname");
  97. irc_cmd_ctcp_reply (session, nickbuf, textbuf);
  98. }
  99. else if ( !strcmp (params[0], "TIME") )
  100. {
  101. time_t now = time(0);
  102. #if defined (ENABLE_THREADS) && defined (HAVE_LOCALTIME_R)
  103. struct tm tmtmp, *ltime = localtime_r (&now, &tmtmp);
  104. #else
  105. struct tm * ltime = localtime (&now);
  106. #endif
  107. strftime (textbuf, sizeof(textbuf), "%a %b %d %H:%M:%S %Z %Y", ltime);
  108. irc_cmd_ctcp_reply (session, nickbuf, textbuf);
  109. }
  110. }
  111. }