2010-09-11 16:03:28 +00:00
|
|
|
// SparkleShare, an instant update workflow to Git.
|
|
|
|
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
|
|
|
|
//
|
|
|
|
// 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 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
using Meebey.SmartIrc4net;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
2010-09-11 18:11:34 +00:00
|
|
|
using System.Threading;
|
2010-09-11 16:03:28 +00:00
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
2010-10-03 10:43:28 +00:00
|
|
|
// A persistent connection to the server that
|
|
|
|
// listens for change notifications
|
2010-09-11 18:11:34 +00:00
|
|
|
public class SparkleListener
|
2010-09-11 16:03:28 +00:00
|
|
|
{
|
|
|
|
|
2010-10-03 10:43:28 +00:00
|
|
|
// FIXME: The IrcClient is a public property because
|
2010-10-02 17:11:03 +00:00
|
|
|
// extending it causes crashes
|
2010-09-12 13:00:07 +00:00
|
|
|
public IrcClient Client;
|
2010-09-14 19:11:55 +00:00
|
|
|
private Thread Thread;
|
|
|
|
public readonly string Server;
|
|
|
|
public readonly string Channel;
|
|
|
|
public readonly string Nick;
|
|
|
|
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 13:00:07 +00:00
|
|
|
public SparkleListener (string server, string channel, string nick)
|
|
|
|
{
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 13:00:07 +00:00
|
|
|
Server = server;
|
|
|
|
Channel = channel;
|
2010-11-20 23:18:55 +00:00
|
|
|
Nick = nick;
|
2010-11-19 01:07:59 +00:00
|
|
|
|
2010-11-20 23:18:55 +00:00
|
|
|
if (!Nick.Equals (""))
|
|
|
|
Nick = nick.Replace ("@", "_at_").Replace (".", "_dot_");
|
2010-11-19 01:07:59 +00:00
|
|
|
else
|
2010-11-20 23:18:55 +00:00
|
|
|
Nick = "anonymous";
|
2010-11-19 01:07:59 +00:00
|
|
|
|
2010-10-03 21:15:24 +00:00
|
|
|
// Keep the nick short
|
2010-09-12 17:15:24 +00:00
|
|
|
if (Nick.Length > 9)
|
|
|
|
Nick = Nick.Substring (0, 9);
|
|
|
|
|
2010-09-12 13:00:07 +00:00
|
|
|
// TODO: Remove these hardcoded values
|
|
|
|
Channel = "#sparkletest";
|
2010-09-12 22:31:35 +00:00
|
|
|
Server = "irc.gnome.org";
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-10-02 17:11:03 +00:00
|
|
|
Client = new IrcClient () {
|
2010-10-04 09:21:20 +00:00
|
|
|
PingTimeout = 120,
|
|
|
|
SocketSendTimeout = 120,
|
|
|
|
SocketReceiveTimeout = 120,
|
2010-10-03 10:43:28 +00:00
|
|
|
AutoRetry = true,
|
|
|
|
AutoReconnect = true,
|
|
|
|
AutoRejoin = true
|
2010-10-02 17:11:03 +00:00
|
|
|
};
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 13:00:07 +00:00
|
|
|
}
|
2010-09-11 18:11:34 +00:00
|
|
|
|
|
|
|
|
2010-09-12 13:00:07 +00:00
|
|
|
// Starts a new thread and listens to the channel
|
2010-09-14 19:11:55 +00:00
|
|
|
public void ListenForChanges ()
|
2010-09-12 13:00:07 +00:00
|
|
|
{
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 22:29:26 +00:00
|
|
|
Thread = new Thread (
|
|
|
|
new ThreadStart (delegate {
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 22:29:26 +00:00
|
|
|
try {
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 17:15:24 +00:00
|
|
|
// Connect to the server
|
2010-09-14 19:11:55 +00:00
|
|
|
Client.Connect (new string [] {Server}, 6667);
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 17:15:24 +00:00
|
|
|
// Login to the server
|
|
|
|
Client.Login (Nick, Nick);
|
|
|
|
|
|
|
|
// Join the channel
|
|
|
|
Client.RfcJoin (Channel);
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 13:00:07 +00:00
|
|
|
Client.Listen ();
|
2010-09-12 17:15:24 +00:00
|
|
|
|
|
|
|
Client.Disconnect ();
|
|
|
|
|
2010-09-14 19:11:55 +00:00
|
|
|
} catch (Meebey.SmartIrc4net.ConnectionException e) {
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 22:29:26 +00:00
|
|
|
Console.WriteLine ("Could not connect: " + e.Message);
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2010-09-12 22:29:26 +00:00
|
|
|
}
|
2010-09-11 16:03:28 +00:00
|
|
|
|
2010-09-12 22:29:26 +00:00
|
|
|
})
|
|
|
|
);
|
2010-09-11 16:50:30 +00:00
|
|
|
|
2010-09-12 22:29:26 +00:00
|
|
|
Thread.Start ();
|
2010-09-12 13:00:07 +00:00
|
|
|
|
2010-09-11 16:50:30 +00:00
|
|
|
}
|
2010-09-11 16:03:28 +00:00
|
|
|
|
2010-09-14 19:11:55 +00:00
|
|
|
|
2010-10-03 10:43:28 +00:00
|
|
|
// Frees all resources for this Listener
|
2010-09-14 19:11:55 +00:00
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
|
|
|
|
Thread.Abort ();
|
|
|
|
Thread.Join ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-09-11 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|