LibIRCClient 1.10 Used by Probotic
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

214 行
5.4KB

  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 'guards' the channel against using abusive language. When
  12. * someone says a bad word, it takes some action against him/her, taking
  13. * in account the number of times the person uses it. The first time, it just
  14. * warns the person through a private message, the second time it warns him
  15. * publically in channel, and after the second time it will kicks the insolent
  16. * out of the channel.
  17. *
  18. * To keep it simple, this example reacts only on 'fuck' word, however
  19. * is is easy to add more.
  20. *
  21. * Features used:
  22. * - nickname parsing;
  23. * - handling 'channel' event to track the messages;
  24. * - handling 'nick' event to track nickname changes;
  25. * - generating channel and private messages, and kicking.
  26. */
  27. #include <map>
  28. #include <string>
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #if !defined (_WIN32)
  33. #include <unistd.h>
  34. #endif
  35. #include "libircclient.h"
  36. /*
  37. * We store data in IRC session context.
  38. */
  39. typedef struct
  40. {
  41. char * channel;
  42. char * nick;
  43. std::map <std::string, unsigned int> insolents;
  44. } irc_ctx_t;
  45. void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  46. {
  47. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  48. irc_cmd_join (session, ctx->channel, 0);
  49. }
  50. void event_nick (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  51. {
  52. char nickbuf[128];
  53. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  54. if ( !origin || count != 1 )
  55. return;
  56. irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
  57. if ( ctx->insolents.find(nickbuf) != ctx->insolents.end() )
  58. {
  59. printf ("%s has changed its nick to %s to prevent penalties - no way!\n",
  60. nickbuf, params[0]);
  61. ctx->insolents[params[0]] = ctx->insolents[nickbuf];
  62. ctx->insolents.erase (nickbuf);
  63. }
  64. }
  65. void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  66. {
  67. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  68. if ( !origin || count != 2 )
  69. return;
  70. if ( strstr (params[1], "fuck") == 0 )
  71. return;
  72. char nickbuf[128], text[256];
  73. irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
  74. if ( ctx->insolents.find(nickbuf) == ctx->insolents.end() )
  75. ctx->insolents[nickbuf] = 0;
  76. ctx->insolents[nickbuf]++;
  77. printf ("'%s' swears in the channel '%s' %d times\n",
  78. nickbuf,
  79. params[1],
  80. ctx->insolents[nickbuf]);
  81. switch (ctx->insolents[nickbuf])
  82. {
  83. case 1:
  84. // Send a private message
  85. sprintf (text, "%s, please do not swear in this channel.", nickbuf);
  86. irc_cmd_msg (session, nickbuf, text);
  87. break;
  88. case 2:
  89. // Send a channel message
  90. sprintf (text, "%s, do not swear in this channel, or you'll leave it.", nickbuf);
  91. irc_cmd_msg (session, params[0], text);
  92. break;
  93. default:
  94. // Send a channel notice, and kick the insolent
  95. sprintf (text, "kicked %s from %s for swearing.", nickbuf, params[0]);
  96. irc_cmd_me (session, params[0], text);
  97. irc_cmd_kick (session, nickbuf, params[0], "swearing");
  98. break;
  99. }
  100. }
  101. void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
  102. {
  103. if ( event > 400 )
  104. {
  105. std::string fulltext;
  106. for ( unsigned int i = 0; i < count; i++ )
  107. {
  108. if ( i > 0 )
  109. fulltext += " ";
  110. fulltext += params[i];
  111. }
  112. printf ("ERROR %d: %s: %s\n", event, origin ? origin : "?", fulltext.c_str());
  113. }
  114. }
  115. int main (int argc, char **argv)
  116. {
  117. irc_callbacks_t callbacks;
  118. irc_ctx_t ctx;
  119. unsigned short port = 6667;
  120. if ( argc != 4 )
  121. {
  122. printf ("Usage: %s <[#]server[:port]> <nick> <channel>\n", argv[0]);
  123. return 1;
  124. }
  125. // Initialize the callbacks
  126. memset (&callbacks, 0, sizeof(callbacks));
  127. // Set up the callbacks we will use
  128. callbacks.event_connect = event_connect;
  129. callbacks.event_channel = event_channel;
  130. callbacks.event_nick = event_nick;
  131. callbacks.event_numeric = event_numeric;
  132. // And create the IRC session; 0 means error
  133. irc_session_t * s = irc_create_session (&callbacks);
  134. if ( !s )
  135. {
  136. printf ("Could not create IRC session\n");
  137. return 1;
  138. }
  139. ctx.channel = argv[3];
  140. ctx.nick = argv[2];
  141. irc_set_ctx (s, &ctx);
  142. // If the port number is specified in the server string, use the port 0 so it gets parsed
  143. if ( strchr( argv[1], ':' ) != 0 )
  144. port = 0;
  145. // To handle the "SSL certificate verify failed" from command line we allow passing ## in front
  146. // of the server name, and in this case tell libircclient not to verify the cert
  147. if ( argv[1][0] == '#' && argv[1][1] == '#' )
  148. {
  149. // Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net
  150. argv[1]++;
  151. irc_option_set( s, LIBIRC_OPTION_SSL_NO_VERIFY );
  152. }
  153. // Initiate the IRC server connection
  154. if ( irc_connect (s, argv[1], port, 0, argv[2], 0, 0) )
  155. {
  156. printf ("Could not connect: %s\n", irc_strerror (irc_errno(s)));
  157. return 1;
  158. }
  159. // and run into forever loop, generating events
  160. if ( irc_run (s) )
  161. {
  162. printf ("Could not connect or I/O error: %s\n", irc_strerror (irc_errno(s)));
  163. return 1;
  164. }
  165. return 0;
  166. }