Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 3f9d2b309b
Fetching contributors…

Cannot retrieve contributors at this time

file 108 lines (95 sloc) 3.905 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
%%%----------------------------------------------------------------------
%%% File : mdb_connection.erl
%%% Author : Mickaël Rémond <mickael.remond@erlang-fr.org>
%%% Purpose : Connection management library.
%%% Used by mdb_bot.erl
%%% Created : 16 Sep 2001, Mickaël Rémond <mickael.remond@erlang-fr.org>
%%%----------------------------------------------------------------------
%%%
%%% This program is free software; you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation; either version 2 of the License, or
%%% (at your option) any later version.
%%%
%%%----------------------------------------------------------------------
%%%
%%% See LICENSE for detailled license
%%%
%%% In addition, as a special exception, you have the permission to
%%% link the code of this program with any library released under
%%% the EPL license and distribute linked combinations including
%%% the two. If you modify this file, you may extend this exception
%%% to your version of the file, but you are not obligated to do
%%% so. If you do not wish to do so, delete this exception
%%% statement from your version.
%%%----------------------------------------------------------------------

-module(mdb_connection).

-author('mickael.remond@erlang-fr.org').
-created('Date: 20010916').
-revision(' $Id$ ').
-vsn(' $Revision$ ').

%% External exports (API)
-export([connect/2, log/4, manage_reconnect/1]).

%% Configure debugging mode:
-include("mdb_macros.hrl").
-include("config.hrl").
-include("mdb.hrl").

%%----------------------------------------------------------------------
%% connect/2
%% Physically connects to the IRC server
%%----------------------------------------------------------------------
connect(Server, Ip_port) ->
    %% TCP connection to the IRC server
    Connect = fun() -> gen_tcp:connect(Server, Ip_port,
[binary,
{packet, 0},
{nodelay, true},
{keepalive, true},
{active, true},
{reuseaddr, true}])
end,

    case Connect() of
{ok, Sock} ->
         mdb_logger:debug("Connected to: ~p~n", [Server]),
{ok, Sock};

{error, Reason} ->
%% If there is an error, wait 30 secondes and try to reconnect
mdb_logger:warn("Server connection error: ~p~n", [Reason]),
timer:sleep(30000),
connect(Server, Ip_port)
    end.

%%----------------------------------------------------------------------
%% log/3
%% connect to a given channel
%%----------------------------------------------------------------------
log(Sock, Channel = #channel{}, Passwd, RealName) ->
    %% Logging in
    log_in(Sock, Channel#channel.botname, Passwd, RealName),

    %% Join the given channel
    irc_lib:join(Sock, Channel#channel.name).

%%----------------------------------------------------------------------
%% log_in/3
%% Logging in: Give nick and realname to the server
%%----------------------------------------------------------------------
%%log_in(Sock, Nickname, RealName, Password) ->
log_in(Sock, Nickname, Passwd, RealName) ->
    irc_lib:login(Sock, Nickname, Passwd, RealName).
    %%irc_lib:passwd(Sock, "Password")

%%----------------------------------------------------------------------
%% manage_reconnect/1
%% When something fails, automatically reconnects the bot
%%----------------------------------------------------------------------
manage_reconnect(State) ->
    Host = State#state.host,
    Port = State#state.port,
    Chan = State#state.channel,
    Pass = State#state.passwd,
    Nick = State#state.nickname,
    RealName = State#state.realname,

    {ok, Sock} = connect(Host, Port),
    log(Sock, #channel{name=Chan, botname=Nick}, Pass, RealName),

    {ok, State#state{socket = Sock,
date = calendar:local_time(),
buffer = << >>,
joined = false
}}.
Something went wrong with that request. Please try again.