Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <3ds.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #define maxLines 20
- int connectIRC(char *server, unsigned int port, int *sockfd);
- int sendIRC(int sockfd, char *out, int debug);
- int readIRC(int sockfd, char *recvline, int debug);
- int main(int argc, char **argv)
- {
- int sockfd, n, debug;
- char recvline[maxLines+1], out[maxLines+1];
- char *cmd, *pos;
- char server[] = "209.20.85.14"; // aka irc.badnik.net
- debug = 1;
- gfxInitDefault();
- //Initialize console on top screen. Using NULL as the second argument tells the console library to use the internal console structure as current one
- consoleInit(GFX_TOP, NULL);
- connectIRC(server, 6667, &sockfd);
- if (connectIRC(server, 6667, &sockfd) == 0) {
- printf("Failed to connect to %s.\n", server);
- exit(1);
- }
- sendIRC(sockfd, "NICK Hello World\r\n", debug);
- sendIRC(sockfd, "USER 3ds 3ds 3ds : banana\r\n", debug);
- sendIRC(sockfd, "JOIN #ducks\r\n", debug);
- // Main loop
- while (aptMainLoop())
- {
- //Scan all the inputs. This should be done once for each frame
- hidScanInput();
- //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
- u32 kDown = hidKeysDown();
- if (kDown & KEY_START) break; // break in order to return to hbmenu
- recvline[0] = 0;
- n = readIRC(sockfd, recvline, debug);
- if (n > 0) {
- recvline[n] = 0;
- if (strstr(recvline, "PING") != NULL) {
- out[0] = 0;
- pos = strstr(recvline, " ")+1;
- sprintf(out, "PONG %s\r\n", pos);
- sendIRC(sockfd, out, debug);
- }
- }
- // Flush and swap frame buffers
- gfxFlushBuffers();
- gfxSwapBuffers();
- //Wait for VBlank
- gspWaitForVBlank();
- }
- gfxExit();
- return 0;
- }
- int connectIRC(char *server, unsigned int port, int *sockfd) {
- struct sockaddr_in serv_addr;
- memset(&serv_addr, '0', sizeof(serv_addr));
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_port = htons(port);
- int listenIRC = socket(AF_INET, SOCK_STREAM, 0);
- if(listenIRC <= 0)
- {
- return 0;
- }
- if (inet_pton(AF_INET, server, &serv_addr.sin_addr) <= 0) {
- return 0;
- }
- if (connect(*sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0 ) {
- return 0;
- }
- return 1;
- }
- int sendIRC(int sockfd, char *out, int debug) {
- if (debug) {
- printf("OUT: %s", out);
- }
- return send(sockfd, out, strlen(out), 0);
- }
- int readIRC(int sockfd, char *recvline, int debug) {
- int n;
- n = read(sockfd, recvline, maxLines);
- if (n > 0 && debug) {
- printf("IN: %s", recvline);
- }
- return n;
- }
Advertisement
Add Comment
Please, Sign In to add comment