Beispiel #1
0
int main(int argc, char **argv) {
    char* server, *port;

    if (argc != 3) {
        printf("Usage: %s <server> <port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    server = argv[1];   /* The IRC server */
    port = argv[2];     /* The IRC server port */

    /* Bind message commands to custom functions */
    irc_bind_command("!op", (Callback) give_op);
    irc_bind_command("!deop", (Callback) take_op);
    irc_bind_command("!voice", (Callback) give_voice);
    irc_bind_command("!devoice", (Callback) take_voice);

    /* Connect, login and join the configured channel */
    irc_connect(server, port);
    irc_login(CONF_NICK, "Circus", "Circus IRC bot");
    irc_join(CONF_CHAN);

    /* Start listening to events.
     * This method blocks until a quit signal is received */
    irc_listen();

    /* Send quit message and close connection */
    irc_quit("Bye");
    irc_disconnect();

    return 0;
}
Beispiel #2
0
int main(int argc, char **argv) {
    char* server, *port;

    if (argc != 3) {
        printf("Usage: %s <server> <port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    server = argv[1];   /* The IRC server */
    port = argv[2];     /* The IRC server port */

    /* Bind IRC event to custom functions.
     * All bindable events are defined in codes.h */
    irc_bind_event(ERR_NICKNAMEINUSE, (Callback) on_nick_in_use);
    irc_bind_event(ERROR, (Callback) on_error);
    irc_bind_event(JOIN, (Callback) on_join);
    irc_bind_event(ALL, (Callback) on_message);

    /* Connect, login and join the configured channel */
    irc_connect(server, port);
    irc_login(CONF_NICK, "Circus", "Circus IRC bot");
    irc_join(CONF_CHAN);

    /* Start listening to events.
     * This method blocks until a quit signal is received */
    irc_listen();

    /* Send quit message and close connection */
    irc_quit("Bye");
    irc_disconnect();

    return 0;
}
Beispiel #3
0
/*
====================
IRC_ConnectSession

Connects a session handle created by IRC_CreateSession to an IRC server.
Returns 0 on success, -1 on invalid handle, error code on libircclient error.
====================
*/
int IRC_ConnectSession(int handle, const char *server, unsigned short port, const char *server_password, const char *nick, const char *username, const char *realname) {
    irc_session_t *s;
    int err;
    
    if(!IS_VALID_IRC_SESSION(handle)) {
        IRC_Printf("Attempted to connect an invalid session %i\n", handle);
        return -1;
    }
    
    if(IRC_SessionIsConnected(handle))
        IRC_DisconnectSession(handle, "Changing server");
    
    s = &irc_sessions[handle];
    strlcpy(s->server, server, sizeof(s->server));
    strlcpy(s->nick, nick, sizeof(s->nick));
    strlcpy(s->username, username, sizeof(s->username));
    strlcpy(s->realname, realname, sizeof(s->realname));
    if(server_password)
        strlcpy(s->password, server_password, sizeof(s->password));
    else
        memset(s->password, 0, sizeof(s->password));
    
    irc_disconnect(s->session);
    if(irc_connect(s->session, server, port, server_password, nick, username, realname)) {
        err = irc_errno(s->session);
        IRC_Printf("Connection error on session %i: %s\n", handle, irc_strerror(err));
        return err;
    }
    
    return 0;
}
Beispiel #4
0
void IRC::stop()
{
    // this is a mess ...

    /*
    irc_disconnect(_session);
    _ircEventThread.join();
    */

    _run = false;

    {
        std::map<irc_session_t*, ConfigHandler::IRCOptions_Server>::iterator it;
        for(it = IRC::_sessionServerMap.begin(); it !=IRC::_sessionServerMap.end(); it++)
        {
            std::cout << "IRC::stop() disconnecting " << it->second.address << std::endl;
            irc_disconnect(it->first);
        }
    }

//    {
//        std::vector<std::thread*>::iterator it;
//        for(it = _ircEventThreadVector.begin(); it != _ircEventThreadVector.end(); it++)
//            (*it)->join();
//    }
    _ircEventThread.join();


}
Beispiel #5
0
void Twitch::Disconnect() {
	if ( session ) {
		irc_disconnect( session );
		
		status = TWITCH_DISCONNECTED;
		OnDisconnected();
	}
}
Beispiel #6
0
static int disconnect_hook(int argc, char *argv[])
{
    char *server = NULL;

    if(argc == 1) {
        server = irc_pick_server();
    } else {
        server = argv[1];
    }

    return irc_disconnect(server);
}
Beispiel #7
0
////////////////////////////////////////////////////////////////////////////////
// leave
eSysStateReturn GaMatchmakingState::leave()
{
	BcScopedLock< BcMutex > Lock( Lock_ );
	BcPrintf( "Leave!\n" );
	if( pSession_ )
	{
		irc_disconnect( pSession_ );
		BcSleep( 0.1f );
		BcThread::join();
		BcSleep( 0.1f );
	}
	return sysSR_FINISHED;
}
Beispiel #8
0
/* Use this instead of exit() to prevent memory leaks */
void clean_exit(unsigned int error) {
    irc_disconnect();
    free_xion_memory();
    event_call(EVENT_EXIT, 0);
    if(error) {
        log_write("*** Program was halted due to an error. Allocation Calls: %d Free Calls: %d",
            stats.alloc_calls, stats.free_calls);
        exit(EXIT_FAILURE);
    }
    else {
        log_write("*** Program was halted. Allocation Calls: %d Free Calls: %d", stats.alloc_calls, stats.free_calls);
        exit(EXIT_SUCCESS);
    }
    
    return ;
}
Beispiel #9
0
void ServerState::prepareDisconnected(Server &server, fd_set &, fd_set &, int &)
{
	const ServerInfo &info = server.info();
	ServerSettings &settings = server.settings();

	// if ServerSettings::recotries it set to -1, reconnection is completely disabled.
	if (settings.recotries < 0) {
		Logger::warning() << "server " << info.name << ": reconnection disabled, skipping" << std::endl;
		server.next(ServerState::Dead);
	} else if ((settings.recocurrent + 1) > settings.recotries) {
		Logger::warning() << "server " << info.name << ": giving up" << std::endl;
		server.next(ServerState::Dead);
	} else {
		if (m_timer.elapsed() > static_cast<unsigned>(settings.recotimeout * 1000)) {
			irc_disconnect(server.session());

			settings.recocurrent ++;
			server.next(ServerState::Connecting);
		}
	}
}
Beispiel #10
0
/*
====================
IRC_ReconnectSession

Reconnects the session to the last server it connected to.
Returns 0 on success, -1 on invalid handle, error code on libircclient error.
====================
*/
int IRC_ReconnectSession(int handle) {
    irc_session_t *s;
    int err;
    
    if(!IS_VALID_IRC_SESSION(handle)) {
        IRC_Printf("Attempted to reconnect an invalid session %i\n", handle);
        return -1;
    }
    
    if(IRC_SessionIsConnected(handle))
        IRC_DisconnectSession(handle, "Reconnecting");
    
    s = &irc_sessions[handle];
    irc_disconnect(s->session);
    if(irc_connect(s->session, s->server, s->port, s->password, s->nick, s->username, s->realname)) {
        err = irc_errno(s->session);
        IRC_Printf("Connection error on session %i: %s\n", handle, irc_strerror(err));
        return err;
    }
    
    return 0;
}
Beispiel #11
0
static void irc_handle(void) {
  char line[BUFLEN];
  char msg[BUFLEN];
  char *nick;
  char *endline;
  char *p;

  if(get_line(irc_fd, line, BUFLEN) == -1) irc_disconnect();
  if((endline = strpbrk(line, "\r\n"))) *endline = '\0';

  if(verbose > IRC_MSG) safe_print('<', line);

  recv_time = time(NULL);

  if(strncmp(line, "PING ", 5) == 0) {
    line[1] = 'O';/* PING -> PONG */
    irc_write(irc_fd, line);
  }

  p = strchr(line, ' ');
  if(p && strncmp(p, " PRIVMSG ", 9) == 0) {
    *endline = '\n';
    *(endline + 1) = '\0';
    if((p = strchr(p, ':'))) {
      write(program_fd, p + 1, strlen(p + 1));
    }

    /* handle ctcp version */
    if(strcmp(p, ":\x01VERSION\x01\n") == 0) {
      nick = line + 1;/* skip the leading colon */
      if((p = strchr(line, '!'))) *p = '\0';/* lose everything after the nick */
      snprintf(msg, BUFLEN, "NOTICE %s :\x01VERSION fifoirc\x01", nick);
      irc_write(irc_fd, msg);
    }
  }
}
Beispiel #12
0
IRCSession::~IRCSession() {
  irc_disconnect(session);
  IRC::session_map.erase(session);
}
Beispiel #13
0
/* Disconnect if the nick is in use */
void on_nick_in_use(ErrorEvent* event) {
    printf("Nick %s is already in use\n", event->params[1]);
    irc_quit("Bye");
    irc_disconnect();
    exit(EXIT_FAILURE);
}
Beispiel #14
0
void IRCSession::disconnect() {
  irc_disconnect(session);
}
void ChatNetworkingIRC::destroyConnection()
{
    irc_disconnect( _p_session );
    // wait until thread is shut down
    while( isRunning() );
}
Beispiel #16
0
int main(int argc, char **argv) {
  int c, i;
  int status;
  struct pollfd fd[3];
  char msg[BUFLEN];
  char *home;
  char *p;

  if(argc <= 1) usage();

  opterr = 0;

  while((c = getopt(argc, argv, "c:e:f:F:m:n:p:P:rs:v")) != -1) {
    switch(c) {
    case 'c': channel = optarg;                      break;
    case 'e': program = optarg;                      break;
    case 'f': fifo = optarg;                         break;
    case 'F': fullname = optarg;                     break;
    case 'm': fifo_perms = strtoul(optarg, NULL, 8); break;
    case 'n': nickname = optarg;                     break;
    case 'p': port = atoi(optarg);                   break;
    case 'P': nspasswd = optarg;                     break;
    case 'r': reconnect = 1;                         break;
    case 's': server = optarg;                       break;
    case 'v': verbose++;                             break;
    default:  usage();                               break;
    }
  }

  /* keep passwords out of process lists */
  if(nspasswd && strlen(nspasswd) > 1) {
    p = nspasswd;
    nspasswd = strdup(nspasswd);

    p[0] = '?';
    for(i = 1; p[i]; i++)
      p[i] = '\0';
  }

  if(optind != argc) usage();

  if(!fifo) {
    if(!(home = getenv("HOME"))) home = "/tmp";
    fifo = malloc(strlen(home) + strlen("/irc-pipe") + 1);
    sprintf(fifo, "%s/irc-pipe", home);
  }

  if(strlen(channel) > 200) {
    fprintf(stderr, "fifoirc: %s: channels must be at most 200 characters\n",
            channel);
    return 1;
  }

  if(!nickname) {
    fprintf(stderr, "fifoirc: no nickname specified\n");
    return 1;
  }

  if(!fullname) fullname = nickname;

  if(make_fifo() == -1) return 1;
  if(verbose > INFO) printf(" -- fifo at %s\n", fifo);

  atexit(unlink_fifo);

  if(program && start_program() == -1) return 1;
  if(verbose > INFO) printf(" -- started '%s'\n", program);

  irc_connect();

  signal(SIGINT, quit);
  signal(SIGTERM, quit);
  signal(SIGHUP, quit);

  while(1) {
    i = 0;
    fd[i].fd = fifo_fd;
    fd[i].events = POLLIN;
    i++;
    fd[i].fd = irc_fd;
    fd[i].events = POLLIN;
    i++;
    if(program) {
      fd[i].fd = program_fd;
      fd[i].events = POLLIN;
      i++;
    }

    c = poll(fd, i, 600000);

    /* restart the child if it died */
    if(program && waitpid(childpid, &status, WNOHANG))
        if(start_program() == -1) break;

    if(c == -1) {
      perror("fifoirc: poll");
      break;
    } else if(c == 0) {/* timeout */
      if(time(NULL) > recv_time + 600) {
        fprintf(stderr, "fifoirc: ping timeout: %d seconds\n",
                (int)(time(NULL) - recv_time));
        irc_disconnect();
      }

      snprintf(msg, BUFLEN, "PING :%s", server);
      irc_write(irc_fd, msg);
    } else {
      if(fd[0].revents & POLLIN) text_handle(fifo_fd);
      if(fd[0].revents & POLLHUP)
        if(make_fifo() == -1) break;

      if(fd[1].revents & POLLIN) irc_handle();
      if(fd[1].revents & POLLHUP) irc_disconnect();

      if(program) {
        if(fd[2].revents & POLLIN) text_handle(program_fd);
        if(fd[2].revents & POLLHUP)
          if(start_program() == -1) break;
      }
    }
  }

  quit(0);

  return 0;
}
Beispiel #17
0
int main(int argc, char **argv)
{
	struct bot_callbacks bot_calls;
	bot_calls.send_channel = send_channel;
	bot_calls.send_user = send_user;
	boti.conns = NULL;
	boti.name = NULL;
	boti.know = bot_know_init(&bot_calls);
	boti.passwd = NULL;
	boti.authed = NULL;
	bot_know_set_talky(boti.know, 30);

	signal(SIGKILL, sig_shutdown);
	signal(SIGTERM, sig_shutdown);
	signal(SIGQUIT, sig_shutdown);

#ifndef IRC

	char buf[512];
	char buf2[512];
	bot_know_load(bot, "merastorum_buggy");
	bot_know_save(bot, "merastorum_buggy2");
	struct bot_message msg = {NULL, buf2, buf};
	strcpy(buf, "um was wollen wir wetten dass er jetzt kaputt geht?^^");
	strcpy(buf2, "#merastorum");
	bot_know_channel_msg(bot, &msg, NULL, 1);
	sleep(10);
	//bot_know_save(bot, "test");
	//bot_know_load(bot, "test");
	//bot_know_save(bot, "test2");

/*	strcpy(buf, "bli blub");
	bot_know_channel_msg(bot, &msg, NULL);*/

//	strcpy(buf, "blub blubba bla");
//	bot_know_channel_msg(bot, &msg, NULL);

#else
	GIOChannel *in = g_io_channel_unix_new(fileno(stdin));
	char *buf;
	gsize read_bytes;
	while (G_IO_STATUS_NORMAL == g_io_channel_read_line(in, &buf, &read_bytes, NULL, NULL) && !shutdown) {
		buf[read_bytes - 1] = '\0';
		if (buf[0] == '!') {
			struct irc_message msg = {
					.source = NULL,
					.cmd = NULL,
					.middle = NULL,
					.suffix = buf
			};
			bot_cmds_parse_msg(&boti, NULL, &msg);
		}
		g_free(buf);
	}
	while (!shutdown) {
		sleep(1);
	}
	GSList *cur = boti.conns;
	if (cur != NULL) {
		do {
			irc_disconnect(cur->data, "cya");
		} while ((cur = g_slist_next(cur)) != NULL);
	}
#endif
	return 0;
}