circapi_client.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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, y;
  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. irc_ircofs_init(&ofs); // initialise the IRC (O)rganized (F)ormatted (S)tring structure
  81. irc_buff2ircofs(&ofs, buff); // convert the buffer containing text from the server into ofs format
  82. for(x=0; x<ofs.line_ct; ++x)
  83. {
  84. if(!(y=dispatch(ofs.ln[x]))) // if there's an error processing a message, it will display it
  85. {
  86. fprintf(stderr, "***[Client Error]*** Unable to process line %d!\n"
  87. "***[Client Error Details]*** [%s]\n", x, ofs.ln[x]);
  88. }
  89. else if(y == -1) // dispatch will return -1 when a disconnect message is recieved
  90. {
  91. puts("***[Server Disconnected Us!]***");
  92. return NULL;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. int main(int argc, char *argv[], char *env[])
  99. {
  100. pthread_t kbl, ircl;
  101. int retval1, retval2;
  102. EXIT=0;
  103. if(argc <= 4)
  104. {
  105. printf("Useage:\n\n\t%s <server hostname> <server port> <nick> <real name>\n", argv[0]);
  106. return 0;
  107. }
  108. retval1 = pthread_create(&kbl, NULL, &keyboard_input_loop, (void *)argv);
  109. retval2 = pthread_create(&ircl, NULL, &irc_input_loop, (void *)argv);
  110. pthread_join(kbl, NULL);
  111. pthread_join(kbl, NULL);
  112. return 0;
  113. }
  114. int dispatch(char *line)
  115. {
  116. // This function will scan through each line of text recieved from the server and
  117. // perform functions on each line accordingly (process pings, user part/quit messages, etc...
  118. // some buffers to use for formatting and temperary string storage
  119. char buff[16384];
  120. char buff2[16384];
  121. char buff3[16384];
  122. char nick[64];
  123. char nick2[64];
  124. char ip[64];
  125. char channel[64];
  126. switch(irc_translate(line))
  127. {
  128. case PING_MSG: // This is a ping request from the server, formulate a ping responce & send it
  129. memset(buff, '\0', sizeof(buff));
  130. irc_ping_responce(line, buff);
  131. if(irc_send_server_text(&irc_socket, buff) <= 0)
  132. {
  133. fputs("Warning: unable to send PING responce!", stderr);
  134. }
  135. else
  136. {
  137. puts("PING? PONG!");
  138. }
  139. return 1;
  140. break;
  141. case NOTICEAUTH_MSG:
  142. printf("NOTICE AUTH: %s\n", notice_auth_message_index_message(line));
  143. return 1;
  144. break;
  145. case WELCOME_MSG:
  146. printf("The server welcomes you (%s)\n", welcome_message_index_message(line));
  147. return 1;
  148. break;
  149. case YOURHOST_MSG:
  150. printf("Your Host: (%s)\n", yourhost_message_index_message(line));
  151. return 1;
  152. break;
  153. case CREATED_MSG:
  154. printf("Server Creation: (%s)\n", created_message_index_message(line));
  155. return 1;
  156. break;
  157. case MYINFO_MSG:
  158. printf("Your Info: (%s)\n", myinfo_message_index_message(line));
  159. return 1;
  160. break;
  161. case BOUNCE_MSG:
  162. printf("Bounce Message: (%s)\n", bounce_message_index_message(line));
  163. return 1;
  164. break;
  165. case DISCONNECT_MSG:
  166. irc_disconnect(&irc_socket);
  167. return -1;
  168. break;
  169. case CHANNEL_PRIVMSG_MSG:
  170. irc_sender_message(line, buff2);
  171. irc_sender_nick(line, nick);
  172. irc_sender_ip(line, ip);
  173. irc_sender_channel(line, channel);
  174. printf("[%s] [%s] [%s]: %s\n", channel, ip, nick, buff2);
  175. return 1;
  176. break;
  177. case USER_PRIVMSG_MSG:
  178. irc_sender_message(line, buff2);
  179. irc_sender_nick(line, nick);
  180. irc_sender_ip(line, ip);
  181. printf("*** PRIVATE MESSAGE *** FROM [%s] [%s]: %s", ip, nick, buff2);
  182. return 1;
  183. break;
  184. case MODE_CHANGE_MSG:
  185. break;
  186. case USER_JOIN_MSG:
  187. puts(join_message_index_nick(line));
  188. return 1;
  189. break;
  190. case USER_PART_MSG:
  191. puts(part_message_index_nick(line));
  192. break;
  193. case USER_QUIT_MSG:
  194. puts(quit_message_index_nick(line));
  195. return 1;
  196. break;
  197. case CHANNEL_NAMES_MSG:
  198. index2string(channel, nick_list_index_channel(line));
  199. index2string(buff2, nick_list_index_first_nick(line));
  200. printf("***Current Users in Channel %s***\n%s\n", channel, buff2);
  201. return 1;
  202. break;
  203. case CHANNEL_NAMES_COMPLETE_MSG:
  204. return 1;
  205. break;
  206. case CHANNEL_JOIN_REJECT_MSG:
  207. index2string(channel, join_reject_index_channel(line));
  208. printf("***Unable to join channel %s***\n", channel);
  209. return 1;
  210. break;
  211. case USER_NICK_CHANGE_MSG:
  212. index2string(nick2, nick_change_index_oldnick(line));
  213. index2string(nick, nick_change_index_newnick(line));
  214. printf("***%s has changed his nick to %s\n", nick2, nick);
  215. return 1;
  216. break;
  217. case MOTD_MSG:
  218. printf("MOTD: %s\n", motd_index_message(line));
  219. return 1;
  220. break;
  221. case MOTD_END_MSG:
  222. printf("***MOTD COMPLETE (%s) ***\n", motd_end_index_message(line));
  223. return 1;
  224. break;
  225. case NOTICE_MSG:
  226. printf("***NOTICE*** [%s]\n", notice_index_message(line));
  227. return 1;
  228. break;
  229. case UNKNOWN_MSG:
  230. printf("***UNKNOWN MESSAGE!!*** (Write this down so I can impliment it!)\n%s\n***END OF UNKNOWN***\n",line);
  231. return 1;
  232. break;
  233. default:
  234. break;
  235. }
  236. return 0;
  237. }