Skip to content

Instantly share code, notes, and snippets.

@projectgus
Created February 14, 2017 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save projectgus/8596f0783754b31a9c38d4a6f0b5fbfa to your computer and use it in GitHub Desktop.
Save projectgus/8596f0783754b31a9c38d4a6f0b5fbfa to your computer and use it in GitHub Desktop.
Modification of IRC bouncer code by "feelfree" https://esp32.com/viewtopic.php?f=13&t=1140
/* Hello World Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "driver/gpio.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include <lwip/sockets.h>
#include <lwip/netdb.h>
#include <errno.h>
using namespace std;
int ircSock, bncSock;
bool connected = false;
err_t err;
esp_err_t event_handler(void *ctx, system_event_t *event)
{
if(event->event_id == SYSTEM_EVENT_STA_CONNECTED){
connected = true;
printf("connected!\n");
}
return ESP_OK;
}
// Sends IRC command
int sendCMD( string command, int socket ){
return( send( socket, (void*)command.c_str(), command.length(), 0 ) );
}
// Tests, is data available on socket
int isData( int socket ){
int count;
int res = ioctl( socket, FIONREAD, &count );
return count;
}
void client_task( void *pvParameter )
{
int clientSock = 0;
bool clientConnected = false;
int res;
while( !connected );
vTaskDelay( 1000/ portTICK_RATE_MS );
printf( "Starting IRC client...\n" );
struct sockaddr_in ircAddr, bncAddr, clientAddr;
char buffer[500];
// TODO Replace this IP with gethostbyname() DNS
ircSock = socket( AF_INET, SOCK_STREAM, 0 );
bncSock = socket( AF_INET, SOCK_STREAM, 0 );
ircAddr.sin_addr.s_addr = inet_addr("149.56.134.238");
ircAddr.sin_family = AF_INET;
ircAddr.sin_port = htons( 6667 );
bncAddr.sin_addr.s_addr = INADDR_ANY;
bncAddr.sin_family = AF_INET;
bncAddr.sin_port = htons( 1337 );
bzero( &(bncAddr.sin_zero), 8 ); // empty it
int clientSCK = 0;
bind( bncSock, (sockaddr *)&bncAddr, sizeof(struct sockaddr) );
listen( bncSock, 1 );
// We don't want to block server socket
fcntl( bncSock, F_SETFL, O_NONBLOCK );
do {
printf("Open TCP connection...\n");
res = connect( ircSock, (sockaddr*)&ircAddr, sizeof( ircAddr ) );
if (res != 0) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
} while(res != 0);
printf("TCP connected.\n");
// empty buffer, and send needed auth messages
bzero( buffer, 500 );
sendCMD( "USER esp32-test 8 * ESP32 test\n", ircSock );
sendCMD( "NICK esp32-test\n", ircSock );
vTaskDelay( 300/ portTICK_RATE_MS );
while(1){
// Client socket have data.
if(isData(ircSock)){
bzero( buffer, sizeof(buffer) );
recv( ircSock, buffer, sizeof(buffer)-1, 0 );
string line = buffer;
// handle PING command
if(line.substr(0, 4)=="PING"){
printf("PING!\n");
line[1] ='O';
sendCMD( line, ircSock );
}
if( clientConnected ){
sendCMD( line, clientSCK );
printf("Sending message to client, ID %i \n", clientSCK);
}
cout << line;
// TODO Add logging, basic bouncer functionality
}
socklen_t socklen = sizeof(struct sockaddr_in);
if( (clientSock = accept( bncSock, (sockaddr *)&clientAddr, &socklen ) ) > 0){
clientConnected = true;
clientSCK = clientSock;
printf("Client connected :) %i \n", clientSCK);
sendCMD( "HI! \n", clientSCK );
}
if(clientConnected){
if(isData(clientSCK)){
bzero( buffer, sizeof(buffer) );
recv( clientSCK, buffer, sizeof(buffer)-1, 0 );
string line = buffer;
sendCMD( line, ircSock );
printf("Got message from client, sending it to freenode. Whoo!\n");
}
//if(clientSock<1){
// clientConnected = false;
//}
}
vTaskDelay(1);
}
}
extern "C" void app_main()
{
nvs_flash_init();
system_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config;
sprintf((char*)sta_config.sta.ssid, "SSID");
sprintf((char*)sta_config.sta.password, "PSK");
sta_config.sta.bssid_set = false;
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
xTaskCreate(&client_task, "client_task", 4096, NULL, 5, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment