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.

165 lines
4.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 program will test automatic color replacement features. It uses the
  12. * colors itself, generates colored string in reaction to 'test' word, and
  13. * dumps the de-colored messages.
  14. */
  15. #include <string>
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include "libircclient.h"
  21. /*
  22. * We store data in IRC session context.
  23. */
  24. typedef struct
  25. {
  26. char * channel;
  27. char * nick;
  28. } irc_ctx_t;
  29. void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  30. {
  31. char * str = irc_color_convert_to_mirc ("[B]Hi[/B] [U]all[/U]. Could someone say 'test'?");
  32. irc_cmd_msg (session, params[0], str);
  33. free (str);
  34. }
  35. void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  36. {
  37. irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
  38. irc_cmd_join (session, ctx->channel, 0);
  39. }
  40. void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
  41. {
  42. printf ("'%s' said in channel %s: %s\n",
  43. origin ? origin : "someone",
  44. params[0], params[1] );
  45. char * stripped = irc_color_strip_from_mirc (params[1]);
  46. irc_cmd_msg (session, params[0], stripped);
  47. free (stripped);
  48. stripped = irc_color_convert_from_mirc (params[1]);
  49. irc_cmd_msg (session, params[0], stripped);
  50. free (stripped);
  51. if ( !strcmp (params[1], "test") )
  52. {
  53. char * str = irc_color_convert_to_mirc ("normal, [B]bold[/B], [I]reverse[/I], [U]underline[/U], [COLOR=RED]red on white[/COLOR], [COLOR=YELLOW/BLACK]yellow on black[/COLOR]");
  54. irc_cmd_msg (session, params[0], str);
  55. free (str);
  56. }
  57. if ( strstr (params[1], "testme") == params[1] )
  58. {
  59. char * str = irc_color_convert_to_mirc (params[1] + 7);
  60. irc_cmd_msg (session, params[0], str);
  61. free (str);
  62. }
  63. }
  64. void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
  65. {
  66. if ( event > 400 )
  67. {
  68. std::string fulltext;
  69. for ( unsigned int i = 0; i < count; i++ )
  70. {
  71. if ( i > 0 )
  72. fulltext += " ";
  73. fulltext += params[i];
  74. }
  75. printf ("ERROR %d: %s: %s\n", event, origin ? origin : "?", fulltext.c_str());
  76. }
  77. }
  78. int main (int argc, char **argv)
  79. {
  80. irc_callbacks_t callbacks;
  81. irc_ctx_t ctx;
  82. irc_session_t * s;
  83. unsigned short port = 6667;
  84. if ( argc != 4 )
  85. {
  86. printf ("Usage: %s <server> <nick> <channel>\n", argv[0]);
  87. return 1;
  88. }
  89. memset (&callbacks, 0, sizeof(callbacks));
  90. callbacks.event_connect = event_connect;
  91. callbacks.event_join = event_join;
  92. callbacks.event_channel = event_channel;
  93. callbacks.event_numeric = event_numeric;
  94. s = irc_create_session (&callbacks);
  95. if ( !s )
  96. {
  97. printf ("Could not create session\n");
  98. return 1;
  99. }
  100. ctx.channel = argv[3];
  101. ctx.nick = argv[2];
  102. irc_set_ctx (s, &ctx);
  103. // If the port number is specified in the server string, use the port 0 so it gets parsed
  104. if ( strchr( argv[1], ':' ) != 0 )
  105. port = 0;
  106. // To handle the "SSL certificate verify failed" from command line we allow passing ## in front
  107. // of the server name, and in this case tell libircclient not to verify the cert
  108. if ( argv[1][0] == '#' && argv[1][1] == '#' )
  109. {
  110. // Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net
  111. argv[1]++;
  112. irc_option_set( s, LIBIRC_OPTION_SSL_NO_VERIFY );
  113. }
  114. // Initiate the IRC server connection
  115. if ( irc_connect (s, argv[1], port, 0, argv[2], 0, 0) )
  116. {
  117. printf ("Could not connect: %s\n", irc_strerror (irc_errno(s)));
  118. return 1;
  119. }
  120. // and run into forever loop, generating events
  121. if ( irc_run (s) )
  122. {
  123. printf ("Could not connect or I/O error: %s\n", irc_strerror (irc_errno(s)));
  124. return 1;
  125. }
  126. return 1;
  127. }