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

344 行
8.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 tests most features of libirc. It can join the specific
  12. * channel, welcoming all the people there, and react on some messages -
  13. * 'help', 'quit', 'dcc chat', 'dcc send', 'ctcp'. Also it can reply to
  14. * CTCP requests, receive DCC files and accept DCC chats.
  15. *
  16. * Features used:
  17. * - nickname parsing;
  18. * - handling 'channel' event to track the messages;
  19. * - handling dcc and ctcp events;
  20. * - using internal ctcp rely procedure;
  21. * - generating channel messages;
  22. * - handling dcc send and dcc chat events;
  23. * - initiating dcc send and dcc chat.
  24. *
  25. * $Id: irctest.c 124 2013-11-28 05:44:10Z gyunaev $
  26. */
  27. #include <stdio.h>
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  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. void addlog (const char * fmt, ...)
  41. {
  42. FILE * fp;
  43. char buf[1024];
  44. va_list va_alist;
  45. va_start (va_alist, fmt);
  46. #if defined (_WIN32)
  47. _vsnprintf (buf, sizeof(buf), fmt, va_alist);
  48. #else
  49. vsnprintf (buf, sizeof(buf), fmt, va_alist);
  50. #endif
  51. va_end (va_alist);
  52. printf ("%s\n", buf);
  53. if ( (fp = fopen ("irctest.log", "ab")) != 0 )
  54. {
  55. fprintf (fp, "%s\n", buf);
  56. fclose (fp);
  57. }
  58. }
  59. void dump_event (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  60. {
  61. char buf[512];
  62. int cnt;
  63. buf[0] = '\0';
  64. for ( cnt = 0; cnt < count; cnt++ )
  65. {
  66. if ( cnt )
  67. strcat (buf, "|");
  68. strcat (buf, params[cnt]);
  69. }
  70. addlog ("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf);
  71. }
  72. void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  73. {
  74. dump_event (session, event, origin, params, count);
  75. irc_cmd_user_mode (session, "+i");
  76. irc_cmd_msg (session, params[0], "Hi all");
  77. }
  78. void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  79. {
  80. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  81. dump_event (session, event, origin, params, count);
  82. irc_cmd_join (session, ctx->channel, 0);
  83. }
  84. void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  85. {
  86. dump_event (session, event, origin, params, count);
  87. printf ("'%s' said me (%s): %s\n",
  88. origin ? origin : "someone",
  89. params[0], params[1] );
  90. }
  91. void dcc_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
  92. {
  93. static int count = 1;
  94. char buf[12];
  95. switch (status)
  96. {
  97. case LIBIRC_ERR_CLOSED:
  98. printf ("DCC %d: chat closed\n", id);
  99. break;
  100. case 0:
  101. if ( !data )
  102. {
  103. printf ("DCC %d: chat connected\n", id);
  104. irc_dcc_msg (session, id, "Hehe");
  105. }
  106. else
  107. {
  108. printf ("DCC %d: %s\n", id, data);
  109. sprintf (buf, "DCC [%d]: %d", id, count++);
  110. irc_dcc_msg (session, id, buf);
  111. }
  112. break;
  113. default:
  114. printf ("DCC %d: error %s\n", id, irc_strerror(status));
  115. break;
  116. }
  117. }
  118. void dcc_file_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
  119. {
  120. if ( status == 0 && length == 0 )
  121. {
  122. printf ("File sent successfully\n");
  123. if ( ctx )
  124. fclose ((FILE*) ctx);
  125. }
  126. else if ( status )
  127. {
  128. printf ("File sent error: %d\n", status);
  129. if ( ctx )
  130. fclose ((FILE*) ctx);
  131. }
  132. else
  133. {
  134. if ( ctx )
  135. fwrite (data, 1, length, (FILE*) ctx);
  136. printf ("File sent progress: %d\n", length);
  137. }
  138. }
  139. void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  140. {
  141. char nickbuf[128];
  142. if ( count != 2 )
  143. return;
  144. printf ("'%s' said in channel %s: %s\n",
  145. origin ? origin : "someone",
  146. params[0], params[1] );
  147. if ( !origin )
  148. return;
  149. irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
  150. if ( !strcmp (params[1], "quit") )
  151. irc_cmd_quit (session, "of course, Master!");
  152. if ( !strcmp (params[1], "help") )
  153. {
  154. irc_cmd_msg (session, params[0], "quit, help, dcc chat, dcc send, ctcp");
  155. }
  156. if ( !strcmp (params[1], "ctcp") )
  157. {
  158. irc_cmd_ctcp_request (session, nickbuf, "PING 223");
  159. irc_cmd_ctcp_request (session, nickbuf, "FINGER");
  160. irc_cmd_ctcp_request (session, nickbuf, "VERSION");
  161. irc_cmd_ctcp_request (session, nickbuf, "TIME");
  162. }
  163. if ( !strcmp (params[1], "dcc chat") )
  164. {
  165. irc_dcc_t dccid;
  166. irc_dcc_chat (session, 0, nickbuf, dcc_recv_callback, &dccid);
  167. printf ("DCC chat ID: %d\n", dccid);
  168. }
  169. if ( !strcmp (params[1], "dcc send") )
  170. {
  171. irc_dcc_t dccid;
  172. irc_dcc_sendfile (session, 0, nickbuf, "irctest.c", dcc_file_recv_callback, &dccid);
  173. printf ("DCC send ID: %d\n", dccid);
  174. }
  175. if ( !strcmp (params[1], "topic") )
  176. irc_cmd_topic (session, params[0], 0);
  177. else if ( strstr (params[1], "topic ") == params[1] )
  178. irc_cmd_topic (session, params[0], params[1] + 6);
  179. if ( strstr (params[1], "mode ") == params[1] )
  180. irc_cmd_channel_mode (session, params[0], params[1] + 5);
  181. if ( strstr (params[1], "nick ") == params[1] )
  182. irc_cmd_nick (session, params[1] + 5);
  183. if ( strstr (params[1], "whois ") == params[1] )
  184. irc_cmd_whois (session, params[1] + 5);
  185. }
  186. void irc_event_dcc_chat (irc_session_t * session, const char * nick, const char * addr, irc_dcc_t dccid)
  187. {
  188. printf ("DCC chat [%d] requested from '%s' (%s)\n", dccid, nick, addr);
  189. irc_dcc_accept (session, dccid, 0, dcc_recv_callback);
  190. }
  191. void irc_event_dcc_send (irc_session_t * session, const char * nick, const char * addr, const char * filename, unsigned long size, irc_dcc_t dccid)
  192. {
  193. FILE * fp;
  194. printf ("DCC send [%d] requested from '%s' (%s): %s (%lu bytes)\n", dccid, nick, addr, filename, size);
  195. if ( (fp = fopen ("file", "wb")) == 0 )
  196. abort();
  197. irc_dcc_accept (session, dccid, fp, dcc_file_recv_callback);
  198. }
  199. void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
  200. {
  201. char buf[24];
  202. sprintf (buf, "%d", event);
  203. dump_event (session, buf, origin, params, count);
  204. }
  205. int main (int argc, char **argv)
  206. {
  207. irc_callbacks_t callbacks;
  208. irc_ctx_t ctx;
  209. irc_session_t * s;
  210. unsigned short port = 6667;
  211. if ( argc != 4 )
  212. {
  213. printf ("Usage: %s <server> <nick> <channel>\n", argv[0]);
  214. return 1;
  215. }
  216. memset (&callbacks, 0, sizeof(callbacks));
  217. callbacks.event_connect = event_connect;
  218. callbacks.event_join = event_join;
  219. callbacks.event_nick = dump_event;
  220. callbacks.event_quit = dump_event;
  221. callbacks.event_part = dump_event;
  222. callbacks.event_mode = dump_event;
  223. callbacks.event_topic = dump_event;
  224. callbacks.event_kick = dump_event;
  225. callbacks.event_channel = event_channel;
  226. callbacks.event_privmsg = event_privmsg;
  227. callbacks.event_notice = dump_event;
  228. callbacks.event_invite = dump_event;
  229. callbacks.event_umode = dump_event;
  230. callbacks.event_ctcp_rep = dump_event;
  231. callbacks.event_ctcp_action = dump_event;
  232. callbacks.event_unknown = dump_event;
  233. callbacks.event_numeric = event_numeric;
  234. callbacks.event_dcc_chat_req = irc_event_dcc_chat;
  235. callbacks.event_dcc_send_req = irc_event_dcc_send;
  236. s = irc_create_session (&callbacks);
  237. if ( !s )
  238. {
  239. printf ("Could not create session\n");
  240. return 1;
  241. }
  242. ctx.channel = argv[3];
  243. ctx.nick = argv[2];
  244. irc_set_ctx (s, &ctx);
  245. // If the port number is specified in the server string, use the port 0 so it gets parsed
  246. if ( strchr( argv[1], ':' ) != 0 )
  247. port = 0;
  248. // To handle the "SSL certificate verify failed" from command line we allow passing ## in front
  249. // of the server name, and in this case tell libircclient not to verify the cert
  250. if ( argv[1][0] == '#' && argv[1][1] == '#' )
  251. {
  252. // Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net
  253. argv[1]++;
  254. irc_option_set( s, LIBIRC_OPTION_SSL_NO_VERIFY );
  255. }
  256. // Initiate the IRC server connection
  257. if ( irc_connect (s, argv[1], port, 0, argv[2], 0, 0) )
  258. {
  259. printf ("Could not connect: %s\n", irc_strerror (irc_errno(s)));
  260. return 1;
  261. }
  262. // and run into forever loop, generating events
  263. if ( irc_run (s) )
  264. {
  265. printf ("Could not connect or I/O error: %s\n", irc_strerror (irc_errno(s)));
  266. return 1;
  267. }
  268. return 1;
  269. }