LibIRCClient 1.10 Used by Probotic
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

214 lignes
5.1KB

  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 spams the specified channels with words 'HAHA', 'HEHE' and
  12. * 'HUHU' using three threads. Its main purpose is to test multithreading
  13. * support of libircclient.
  14. */
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #if defined (_WIN32)
  19. #include <windows.h>
  20. #define CREATE_THREAD(id,func,param) (CreateThread(0, 0, func, param, 0, id) == 0)
  21. #define THREAD_FUNCTION(funcname) static DWORD WINAPI funcname (LPVOID arg)
  22. #define thread_id_t DWORD
  23. #define sleep(a) Sleep (a*1000)
  24. #else
  25. #include <unistd.h>
  26. #include <pthread.h>
  27. #define CREATE_THREAD(id,func,param) (pthread_create (id, 0, func, (void *) param) != 0)
  28. #define THREAD_FUNCTION(funcname) static void * funcname (void * arg)
  29. #define thread_id_t pthread_t
  30. #endif
  31. #include "libircclient.h"
  32. /*
  33. * We store data in IRC session context.
  34. */
  35. typedef struct
  36. {
  37. char * channel;
  38. char * nick;
  39. } irc_ctx_t;
  40. /*
  41. * Params that we give to our threads.
  42. */
  43. typedef struct
  44. {
  45. irc_session_t * session;
  46. const char * phrase;
  47. const char * channel;
  48. int timer;
  49. } spam_params_t;
  50. THREAD_FUNCTION(gen_spam)
  51. {
  52. spam_params_t * sp = (spam_params_t *) arg;
  53. while ( 1 )
  54. {
  55. if ( irc_cmd_msg (sp->session, sp->channel, sp->phrase) )
  56. break;
  57. sleep(sp->timer);
  58. }
  59. return 0;
  60. }
  61. void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  62. {
  63. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  64. if ( !origin )
  65. return;
  66. // We need to know whether WE are joining the channel, or someone else.
  67. // To do this, we compare the origin with our nick.
  68. // Note that we have set LIBIRC_OPTION_STRIPNICKS to obtain 'parsed' nicks.
  69. if ( !strcmp(origin, ctx->nick) )
  70. {
  71. static spam_params_t spam1;
  72. static spam_params_t spam2;
  73. static spam_params_t spam3;
  74. thread_id_t tid;
  75. spam1.session = spam2.session = spam3.session = session;
  76. spam1.channel = spam2.channel = spam3.channel = ctx->channel;
  77. spam1.phrase = "HEHE";
  78. spam2.phrase = "HAHA";
  79. spam3.phrase = "HUHU";
  80. spam1.timer = 2;
  81. spam2.timer = 3;
  82. spam3.timer = 4;
  83. printf ("We just joined the channel %s; starting the spam threads\n", params[1]);
  84. if ( CREATE_THREAD (&tid, gen_spam, &spam1)
  85. || CREATE_THREAD (&tid, gen_spam, &spam2)
  86. || CREATE_THREAD (&tid, gen_spam, &spam3) )
  87. printf ("CREATE_THREAD failed: %s\n", strerror(errno));
  88. else
  89. printf ("Spammer thread was started successfully.\n");
  90. }
  91. else
  92. {
  93. char textbuf[168];
  94. sprintf (textbuf, "Hey, %s, hi!", origin);
  95. irc_cmd_msg (session, params[0], textbuf);
  96. }
  97. }
  98. void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  99. {
  100. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  101. irc_cmd_join (session, ctx->channel, 0);
  102. }
  103. void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
  104. {
  105. if ( event > 400 )
  106. {
  107. printf ("ERROR %d: %s: %s %s %s %s\n",
  108. event,
  109. origin ? origin : "unknown",
  110. params[0],
  111. count > 1 ? params[1] : "",
  112. count > 2 ? params[2] : "",
  113. count > 3 ? params[3] : "");
  114. }
  115. }
  116. int main (int argc, char **argv)
  117. {
  118. irc_callbacks_t callbacks;
  119. irc_ctx_t ctx;
  120. irc_session_t * s;
  121. unsigned short port = 6667;
  122. if ( argc != 4 )
  123. {
  124. printf ("Usage: %s <server> <nick> <channel>\n", argv[0]);
  125. return 1;
  126. }
  127. // Initialize the callbacks
  128. memset (&callbacks, 0, sizeof(callbacks));
  129. // Set up the callbacks we will use
  130. callbacks.event_connect = event_connect;
  131. callbacks.event_join = event_join;
  132. callbacks.event_numeric = event_numeric;
  133. ctx.channel = argv[3];
  134. ctx.nick = argv[2];
  135. // And create the IRC session; 0 means error
  136. s = irc_create_session (&callbacks);
  137. if ( !s )
  138. {
  139. printf ("Could not create IRC session\n");
  140. return 1;
  141. }
  142. irc_set_ctx (s, &ctx);
  143. irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
  144. // If the port number is specified in the server string, use the port 0 so it gets parsed
  145. if ( strchr( argv[1], ':' ) != 0 )
  146. port = 0;
  147. // To handle the "SSL certificate verify failed" from command line we allow passing ## in front
  148. // of the server name, and in this case tell libircclient not to verify the cert
  149. if ( argv[1][0] == '#' && argv[1][1] == '#' )
  150. {
  151. // Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net
  152. argv[1]++;
  153. irc_option_set( s, LIBIRC_OPTION_SSL_NO_VERIFY );
  154. }
  155. // Initiate the IRC server connection
  156. if ( irc_connect (s, argv[1], port, 0, argv[2], 0, 0) )
  157. {
  158. printf ("Could not connect: %s\n", irc_strerror (irc_errno(s)));
  159. return 1;
  160. }
  161. // and run into forever loop, generating events
  162. if ( irc_run (s) )
  163. {
  164. printf ("Could not connect or I/O error: %s\n", irc_strerror (irc_errno(s)));
  165. return 1;
  166. }
  167. return 1;
  168. }