raw_irc_example.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Raw IRC circapi.cpp example for use with circapi (circapi.sourceforge.net)
  2. // this is similar to telnet, but ping responces are automated
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. #include <circapi.h>
  12. #include <pthread.h>
  13. unsigned char EXIT;
  14. int irc_socket;
  15. void *keyboard_input_loop(void *v)
  16. {
  17. char *p;
  18. char buff[8192];
  19. p=buff;
  20. while(1)
  21. {
  22. sleep(1);
  23. // get input text
  24. if(EXIT >= 1)
  25. {
  26. return NULL;
  27. }
  28. memset(buff, '\0', sizeof(buff));
  29. p=buff;
  30. while((*p = getchar()) != EOF && *p != '\n')
  31. {
  32. ++p;
  33. }
  34. *p = '\r';
  35. ++p;
  36. *p = '\n';
  37. ++p;
  38. *p = '\0';
  39. if(!strncmp(buff, "COMMAND FORCEQUIT", 17))
  40. {
  41. EXIT=1;
  42. return NULL;
  43. }
  44. else if(!strncmp(buff, "COMMAND QUIT", 12))
  45. {
  46. irc_send_server_text(&irc_socket, "quit :exited terminal\r\n");
  47. EXIT=1;
  48. return NULL;
  49. }
  50. else if(irc_send_server_text(&irc_socket, buff) <= 0)
  51. {
  52. fprintf(stderr, "Unable to send text, try again? [%s]\n", buff);
  53. }
  54. }
  55. }
  56. void *irc_input_loop(void *v)
  57. {
  58. struct ircofs ofs;
  59. char buff[8192];
  60. int attempt, x;
  61. puts("Connecting...");
  62. for(attempt=0; attempt < 5
  63. && (x=irc_connect(&irc_socket, ((char **)v)[1], ((char **)v)[2], ((char **)v)[3], "guest", 1, ((char **)v)[4], 0)) <= 0; ++attempt)
  64. {
  65. printf("Could not connect to %s:%s, trying again.\n", ((char **)v)[1], ((char **)v)[2]);
  66. }
  67. if(x <= 0)
  68. {
  69. fputs("Unable to connect.", stderr);
  70. ++EXIT;
  71. return NULL;
  72. }
  73. while(1)
  74. {
  75. sleep(1);
  76. memset(buff, '\0', sizeof(buff));
  77. if(EXIT == 1) return NULL;
  78. if(irc_get_server_text(&irc_socket, buff, sizeof(buff)) > 0)
  79. {
  80. printf(buff);
  81. irc_ircofs_init(&ofs);
  82. irc_buff2ircofs(&ofs, buff);
  83. for(x=0; x<ofs.line_ct; ++x)
  84. {
  85. if(irc_is_ping(ofs.ln[x]))
  86. {
  87. memset(buff, '\0', sizeof(buff));
  88. irc_ping_responce(ofs.ln[x], buff);
  89. if(irc_send_server_text(&irc_socket, buff) <= 0)
  90. {
  91. fputs("Warning: unable to send PING responce!", stderr);
  92. }
  93. else
  94. {
  95. puts("PING? PONG!");
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. int main(int argc, char *argv[], char *env[])
  103. {
  104. pthread_t kbl, ircl;
  105. int retval1, retval2;
  106. EXIT=0;
  107. if(argc <= 4)
  108. {
  109. printf("Useage:\n\n\t%s <server hostname> <server port> <nick> <real name>\n", argv[0]);
  110. return 0;
  111. }
  112. retval1 = pthread_create(&kbl, NULL, &keyboard_input_loop, (void *)argv);
  113. retval2 = pthread_create(&ircl, NULL, &irc_input_loop, (void *)argv);
  114. pthread_join(kbl, NULL);
  115. pthread_join(kbl, NULL);
  116. return 0;
  117. }