Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Add topic event
Browse files Browse the repository at this point in the history
  • Loading branch information
erming committed Apr 19, 2015
1 parent 67e4677 commit 58b294b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/irc-events/topic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
var _ = require("lodash");
var Msg = require("../models/msg");

module.exports = function(irc, client, network) {
irc.on("topic", function(data) {
var chan = _.findWhere(network.channels, {name: data.channel});
if (!chan) {
return;
}

var from = data.nick || chan.name;
var topic = data.topic;

chan.topic = topic;

var msg = new Msg({
type: Msg.Type.TOPIC,
from: from,
text: topic
});
chan.messages.push(msg),
client.emit("msg", {
chan: chan.id,
msg: msg
});

client.emit("topic", {
chan: chan.id,
topic: _.escape(topic)
});
});
};
37 changes: 37 additions & 0 deletions test/irc-events/topic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
var tape = require("tape");
var events = require("events");
var topic = require("../../src/irc-events/topic");
var Chan = require("../../src/models/chan");

tape("topic", function(t) {
t.plan(2);

var irc = new events.EventEmitter();
irc.me = "foo";

var client = {};
client.emit = function(e, msg) {
switch(e) {
case "msg":
t.pass();
break;

case "topic":
if (msg.topic == "foo") {
t.pass();
}
break;
}
};

var network = {
id: 0,
channels: [new Chan({
name: "#bar"
})]
};

topic(irc, client, network);

irc.emit("topic", {
channel: "#bar",
nick: "",
topic: "foo"
});

t.end();
});

0 comments on commit 58b294b

Please sign in to comment.