Guest User

Untitled

a guest
Sep 10th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <3ds.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7. #include <arpa/inet.h>
  8. #include <unistd.h>
  9.  
  10. #define maxLines 20
  11.  
  12. int connectIRC(char *server, unsigned int port, int *sockfd);
  13. int sendIRC(int sockfd, char *out, int debug);
  14. int readIRC(int sockfd, char *recvline, int debug);
  15.  
  16. int main(int argc, char **argv)
  17. {
  18. int sockfd, n, debug;
  19. char recvline[maxLines+1], out[maxLines+1];
  20. char *cmd, *pos;
  21. char server[] = "209.20.85.14"; // aka irc.badnik.net
  22. debug = 1;
  23.  
  24. gfxInitDefault();
  25.  
  26. //Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
  27. consoleInit(GFX_TOP, NULL);
  28.  
  29. connectIRC(server, 6667, &sockfd);
  30.  
  31. if (connectIRC(server, 6667, &sockfd) == 0) {
  32. printf("Failed to connect to %s.\n", server);
  33. exit(1);
  34. }
  35.  
  36. sendIRC(sockfd, "NICK Hello World\r\n", debug);
  37. sendIRC(sockfd, "USER 3ds 3ds 3ds : banana\r\n", debug);
  38. sendIRC(sockfd, "JOIN #ducks\r\n", debug);
  39.  
  40. // Main loop
  41. while (aptMainLoop())
  42. {
  43. //Scan all the inputs. This should be done once for each frame
  44. hidScanInput();
  45.  
  46. //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
  47. u32 kDown = hidKeysDown();
  48.  
  49. if (kDown & KEY_START) break; // break in order to return to hbmenu
  50.  
  51. recvline[0] = 0;
  52. n = readIRC(sockfd, recvline, debug);
  53.  
  54. if (n > 0) {
  55. recvline[n] = 0;
  56. if (strstr(recvline, "PING") != NULL) {
  57. out[0] = 0;
  58. pos = strstr(recvline, " ")+1;
  59. sprintf(out, "PONG %s\r\n", pos);
  60. sendIRC(sockfd, out, debug);
  61. }
  62. }
  63.  
  64. // Flush and swap frame buffers
  65. gfxFlushBuffers();
  66. gfxSwapBuffers();
  67.  
  68. //Wait for VBlank
  69. gspWaitForVBlank();
  70. }
  71.  
  72. gfxExit();
  73. return 0;
  74. }
  75.  
  76. int connectIRC(char *server, unsigned int port, int *sockfd) {
  77. struct sockaddr_in serv_addr;
  78. memset(&serv_addr, '0', sizeof(serv_addr));
  79.  
  80. serv_addr.sin_family = AF_INET;
  81. serv_addr.sin_port = htons(port);
  82.  
  83. int listenIRC = socket(AF_INET, SOCK_STREAM, 0);
  84.  
  85. if(listenIRC <= 0)
  86. {
  87. return 0;
  88. }
  89.  
  90. if (inet_pton(AF_INET, server, &serv_addr.sin_addr) <= 0) {
  91. return 0;
  92. }
  93.  
  94. if (connect(*sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0 ) {
  95. return 0;
  96. }
  97.  
  98. return 1;
  99. }
  100.  
  101. int sendIRC(int sockfd, char *out, int debug) {
  102. if (debug) {
  103. printf("OUT: %s", out);
  104. }
  105. return send(sockfd, out, strlen(out), 0);
  106. }
  107.  
  108. int readIRC(int sockfd, char *recvline, int debug) {
  109. int n;
  110. n = read(sockfd, recvline, maxLines);
  111. if (n > 0 && debug) {
  112. printf("IN: %s", recvline);
  113. }
  114. return n;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment