LibIRCClient 1.10 Used by Probotic
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
Це архівний репозитарій. Ви можете переглядати і клонувати файли, але не можете робити пуш або відкривати питання/запити.

192 рядки
4.3KB

  1. /*
  2. * Copyright (C) 2004-2009 Georgy Yunaev gyunaev@ulduzsoft.com
  3. *
  4. * This example is free, and not covered by LGPL license. There is no
  5. * restriction applied to their modification, redistribution, using and so on.
  6. * You can study them, modify them, use them in your own program - either
  7. * completely or partially. By using it you may give me some credits in your
  8. * program, but you don't have to.
  9. *
  10. *
  11. * This example emulates a simple file server. Only 'list' and 'get' commands
  12. * are supported.
  13. *
  14. * Features used:
  15. * - automatic nickname parsing using LIBIRC_OPTION_STRIPNICKS;
  16. * - handling privmsg events to parse commands;
  17. * - generating listings and DCC file transfer;
  18. *
  19. * $Id: ircftp.cpp 94 2012-01-18 08:04:49Z gyunaev $
  20. */
  21. #include <map>
  22. #include <string>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include <dirent.h>
  28. #include "libircclient.h"
  29. #define FILES_DIR "."
  30. /*
  31. * We store data in IRC session context.
  32. */
  33. typedef struct
  34. {
  35. std::string channel;
  36. std::string nick;
  37. } irc_ctx_t;
  38. void dcc_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
  39. {
  40. if ( status == 0 && length == 0 )
  41. {
  42. printf ("File sent successfully\n");
  43. }
  44. else if ( status )
  45. {
  46. printf ("File sent error: %s (%d)\n", irc_strerror(status), status);
  47. }
  48. else
  49. {
  50. printf ("File sent progress: %d\n", length);
  51. }
  52. }
  53. void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  54. {
  55. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  56. irc_cmd_join (session, ctx->channel.c_str(), 0);
  57. }
  58. void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  59. {
  60. if ( !origin || count != 2 )
  61. return;
  62. if ( !strcasecmp (params[1], "list") )
  63. {
  64. dirent *d;
  65. DIR *dir = opendir(FILES_DIR);
  66. if ( !dir )
  67. return;
  68. while ( (d = readdir (dir)) != 0 )
  69. {
  70. if ( !strcmp (d->d_name, ".") )
  71. continue;
  72. irc_cmd_msg (session, origin, d->d_name);
  73. }
  74. closedir (dir);
  75. }
  76. else if ( strstr (params[1], "get ") == params[1] )
  77. {
  78. irc_dcc_t dccid;
  79. if ( irc_dcc_sendfile (session, 0, origin, params[1] + 4, dcc_callback, &dccid) )
  80. irc_cmd_msg (session, origin, "Could not send this file");
  81. }
  82. else
  83. {
  84. irc_cmd_msg (session, origin, "Commands: send | list");
  85. }
  86. }
  87. void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
  88. {
  89. if ( event > 400 )
  90. {
  91. std::string fulltext;
  92. for ( unsigned int i = 0; i < count; i++ )
  93. {
  94. if ( i > 0 )
  95. fulltext += " ";
  96. fulltext += params[i];
  97. }
  98. printf ("ERROR %d: %s: %s\n", event, origin ? origin : "?", fulltext.c_str());
  99. }
  100. }
  101. int main (int argc, char **argv)
  102. {
  103. irc_callbacks_t callbacks;
  104. irc_ctx_t ctx;
  105. unsigned short port = 6667;
  106. if ( argc != 4 )
  107. {
  108. printf ("Usage: %s <server> <nick> <channel>\n", argv[0]);
  109. return 1;
  110. }
  111. // Initialize the callbacks
  112. memset (&callbacks, 0, sizeof(callbacks));
  113. // Set up the callbacks we will use
  114. callbacks.event_connect = event_connect;
  115. callbacks.event_privmsg = event_privmsg;
  116. callbacks.event_numeric = event_numeric;
  117. // And create the IRC session; 0 means error
  118. irc_session_t * s = irc_create_session (&callbacks);
  119. if ( !s )
  120. {
  121. printf ("Could not create IRC session\n");
  122. return 1;
  123. }
  124. ctx.channel = argv[3];
  125. ctx.nick = argv[2];
  126. irc_set_ctx (s, &ctx);
  127. irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
  128. // If the port number is specified in the server string, use the port 0 so it gets parsed
  129. if ( strchr( argv[1], ':' ) != 0 )
  130. port = 0;
  131. // To handle the "SSL certificate verify failed" from command line we allow passing ## in front
  132. // of the server name, and in this case tell libircclient not to verify the cert
  133. if ( argv[1][0] == '#' && argv[1][1] == '#' )
  134. {
  135. // Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net
  136. argv[1]++;
  137. irc_option_set( s, LIBIRC_OPTION_SSL_NO_VERIFY );
  138. }
  139. // Initiate the IRC server connection
  140. if ( irc_connect (s, argv[1], port, 0, argv[2], 0, 0) )
  141. {
  142. printf ("Could not connect: %s\n", irc_strerror (irc_errno(s)));
  143. return 1;
  144. }
  145. // and run into forever loop, generating events
  146. if ( irc_run (s) )
  147. {
  148. printf ("Could not connect or I/O error: %s\n", irc_strerror (irc_errno(s)));
  149. return 1;
  150. }
  151. return 1;
  152. }