Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

One can now PART a channel using existing API :P Also added functiona…

…lity to track topics in a channel
  • Loading branch information...
commit 75aa9f2c4355762396dc663e0f5eb66f5dd90654 1 parent a50ab75
@mazenharake authored
View
3  include/eirc.hrl
@@ -45,6 +45,7 @@
-define(PRIVMSG(Nick, Msg), ?CMD(["PRIVMSG ",Nick," :",Msg])).
-define(NOTICE(Nick, Msg), ?CMD(["NOTICE ",Nick," :",Msg])).
-define(JOIN(Chan, Key), ?CMD(["JOIN ",Chan," ",Key])).
+-define(PART(Chan), ?CMD(["PART ",Chan])).
-define(QUIT(Msg), ?CMD(["QUIT :",Msg])).
%% CTCP Responses
@@ -62,6 +63,8 @@
-define(RPL_ISUPPORT, "005"). %% Defacto standard for server support
-define(RPL_BOUNCE, "010"). %% Defacto replacement of "005" in RFC2812
+-define(RPL_TOPIC, "332").
+
-define(ERR_NONICKNAMEGIVEN, "431").
-define(ERR_ERRONEUSNICKNAME, "432").
-define(ERR_NICKNAMEINUSE, "433").
View
10 src/eirc.erl
@@ -68,6 +68,9 @@ join(Client, Channel) ->
join(Client, Channel, Key) ->
eirc_cl:join(Client, Channel, Key).
+part(Client, Channel) ->
+ eirc_cl:part(Client, Channel).
+
quit(Client, QuitMsg) ->
eirc_cl:quit(Client, QuitMsg).
@@ -77,8 +80,11 @@ is_logged_on(Client) ->
channels(Client) ->
eirc_cl:channels(Client).
-chan_users(Client, ChanName) ->
- eirc_cl:chan_users(Client, ChanName).
+chan_users(Client, Channel) ->
+ eirc_cl:chan_users(Client, Channel).
+
+chan_topic(Client, Channel) ->
+ eirc_cl:chan_topic(Client, Channel).
%% =============================================================================
%% Internal Functions
View
18 src/eirc_chan.erl
@@ -62,7 +62,6 @@ set_topic(Struct, ChanName, Topic) ->
Channel = gb_trees:get(Name, Struct),
gb_trees:enter(Name, Channel#chan{ topic = Topic }, Struct).
-
%% =============================================================================
%% Users JOIN/PART/AKAs(namechange)
%% =============================================================================
@@ -109,14 +108,21 @@ channels(Struct) ->
[ ChanName || {ChanName, _Chan} <- gb_trees:to_list(Struct) ].
chan_users(Struct, ChanName) ->
- Name = normalize(ChanName),
- case gb_trees:lookup(Name, Struct) of
- {value, Channel} -> Channel#chan.users;
- none -> {error, no_such_channel}
- end.
+ get_attr(Struct, ChanName, fun(#chan{ users = Users }) -> Users end).
+
+chan_topic(Struct, ChanName) ->
+ get_attr(Struct, ChanName, fun(#chan{ topic = Topic }) -> Topic end).
+
%% =============================================================================
%% Internal functions
%% =============================================================================
normalize(ChanName) -> string:to_lower(ChanName).
+get_attr(Struct, ChanName, Fun) ->
+ Name = normalize(ChanName),
+ case gb_trees:lookup(Name, Struct) of
+ {value, Channel} -> Fun(Channel);
+ none -> {error, no_such_channel}
+ end.
+
View
39 src/eirc_cl.erl
@@ -54,6 +54,9 @@ cmd(Client, RawCmd) ->
join(Client, Channel, Key) ->
gen_server:call(Client, {join, Channel, Key}, infinity).
+part(Client, Channel) ->
+ gen_server:call(Client, {part, Channel}, infinity).
+
quit(Client, QuitMsg) ->
gen_server:call(Client, {quit, QuitMsg}, infinity).
@@ -63,8 +66,11 @@ is_logged_on(Client) ->
channels(Client) ->
gen_server:call(Client, channels).
-chan_users(Client, ChanName) ->
- gen_server:call(Client, {chan_users, ChanName}).
+chan_users(Client, Channel) ->
+ gen_server:call(Client, {chan_users, Channel}).
+
+chan_topic(Client, Channel) ->
+ gen_server:call(Client, {chan_topic, Channel}).
%% =============================================================================
%% Behaviour callback API
@@ -112,6 +118,10 @@ handle_call({join, Channel, Key}, _From, State) ->
gen_tcp:send(State#state.socket, ?JOIN(Channel, Key)),
{reply, ok, State};
+handle_call({part, Channel}, _From, State) ->
+ gen_tcp:send(State#state.socket, ?PART(Channel)),
+ {reply, ok, State};
+
handle_call({cmd, RawCmd}, _From, State) ->
gen_tcp:send(State#state.socket, ?CMD(RawCmd)),
{reply, ok, State};
@@ -122,8 +132,11 @@ handle_call(is_logged_on, _From, State) ->
handle_call(channels, _From, State) ->
{reply, eirc_chan:channels(State#state.channels), State};
-handle_call({chan_users, ChanName}, _From, State) ->
- {reply, eirc_chan:chan_users(State#state.channels, ChanName), State};
+handle_call({chan_users, Channel}, _From, State) ->
+ {reply, eirc_chan:chan_users(State#state.channels, Channel), State};
+
+handle_call({chan_topic, Channel}, _From, State) ->
+ {reply, eirc_chan:chan_topic(State#state.channels, Channel), State};
handle_call(_, _, State) ->
{reply, ok, State}.
@@ -184,6 +197,24 @@ handle_data(#ircmsg{ nick = Nick, cmd = "JOIN" } = Msg,
Channels = eirc_chan:join(State#state.channels, hd(Msg#ircmsg.args)),
{noreply, State#state{ channels = Channels }};
+%% Topic message on join
+handle_data(#ircmsg{ cmd = ?RPL_TOPIC } = Msg, State) ->
+ case Msg#ircmsg.args of
+ [_MyNick, Channel, Topic] ->
+ %% Not RFC compliant but _very_ common
+ ok;
+ [Channel, Topic] ->
+ %% RFC expected response
+ ok
+ end,
+ Channels = eirc_chan:set_topic(State#state.channels, Channel, Topic),
+ {noreply, State#state{ channels = Channels }};
+
+%% Topic message while in channel
+handle_data(#ircmsg{ cmd = "TOPIC", args = [Channel, Topic] }, State) ->
+ Channels = eirc_chan:set_topic(State#state.channels, Channel, Topic),
+ {noreply, State#state{ channels = Channels }};
+
%% We left a channel
handle_data(#ircmsg{ nick = Nick, cmd = "PART" } = Msg,
#state{ nick = Nick } = State) ->
Please sign in to comment.
Something went wrong with that request. Please try again.