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
|
2010-11-21 12:33:24 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-09-11 16:03:28 +00:00
|
|
|
// 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/>.
|
|
|
|
|
2011-02-26 14:20:32 +00:00
|
|
|
|
2010-09-11 16:03:28 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
2010-09-11 18:11:34 +00:00
|
|
|
using System.Threading;
|
2011-02-24 01:43:06 +00:00
|
|
|
using System.Security.Cryptography;
|
2010-09-11 16:03:28 +00:00
|
|
|
|
2011-03-16 14:48:15 +00:00
|
|
|
using Meebey.SmartIrc4net;
|
|
|
|
|
2010-09-11 16:03:28 +00:00
|
|
|
namespace SparkleLib {
|
|
|
|
|
2011-03-22 13:44:52 +00:00
|
|
|
public enum NotificationServerType
|
|
|
|
{
|
|
|
|
|
|
|
|
Own,
|
|
|
|
Central
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-03 10:43:28 +00:00
|
|
|
// A persistent connection to the server that
|
|
|
|
// listens for change notifications
|
2011-02-26 14:20:32 +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;
|
2011-03-02 00:24:18 +00:00
|
|
|
public readonly string FallbackServer;
|
2010-09-14 19:11:55 +00:00
|
|
|
public readonly string Channel;
|
|
|
|
public readonly string Nick;
|
|
|
|
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2011-03-22 13:44:52 +00:00
|
|
|
public SparkleListener (string server, string folder_name,
|
|
|
|
string user_email, NotificationServerType type)
|
2010-09-12 13:00:07 +00:00
|
|
|
{
|
2010-09-11 18:11:34 +00:00
|
|
|
|
2011-03-22 13:44:52 +00:00
|
|
|
if (type == NotificationServerType.Own) {
|
|
|
|
|
|
|
|
Server = server;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// This is SparkleShare's centralized notification service.
|
|
|
|
// Don't worry, we only use this server as a backup if you
|
|
|
|
// don't have your own. All data needed to connect is hashed and
|
|
|
|
// we don't store any personal information ever.
|
|
|
|
Server = "204.62.14.135";
|
|
|
|
|
|
|
|
}
|
2011-02-24 01:43:06 +00:00
|
|
|
|
|
|
|
if (!user_email.Equals ("") && user_email != null)
|
2011-03-01 23:13:43 +00:00
|
|
|
Nick = GetSHA1 (folder_name + user_email + "sparkles");
|
2010-11-19 01:07:59 +00:00
|
|
|
else
|
2011-02-24 01:43:06 +00:00
|
|
|
Nick = GetSHA1 (DateTime.Now.ToString () + "sparkles");
|
|
|
|
|
2011-03-01 23:13:43 +00:00
|
|
|
Nick = "s" + Nick.Substring (0, 7);
|
2011-03-13 02:07:47 +00:00
|
|
|
Channel = "#" + GetSHA1 (server + folder_name + "sparkles");
|
|
|
|
|
2010-10-02 17:11:03 +00:00
|
|
|
Client = new IrcClient () {
|
2011-03-01 23:13:43 +00:00
|
|
|
PingTimeout = 180,
|
|
|
|
PingInterval = 90
|
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
|
2011-02-27 01:01:13 +00:00
|
|
|
public void Listen ()
|
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
|
|
|
}
|
2011-02-27 01:01:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void Announce (string message)
|
|
|
|
{
|
|
|
|
|
|
|
|
Client.SendMessage (SendType.Message, Channel, message);
|
|
|
|
|
|
|
|
}
|
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 ();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-24 01:43:06 +00:00
|
|
|
|
|
|
|
// Creates an SHA-1 hash of input
|
2011-02-27 01:01:13 +00:00
|
|
|
private static string GetSHA1 (string s)
|
2011-02-24 01:43:06 +00:00
|
|
|
{
|
|
|
|
SHA1 sha1 = new SHA1CryptoServiceProvider ();
|
|
|
|
Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
|
|
|
|
Byte[] encoded_bytes = sha1.ComputeHash (bytes);
|
|
|
|
return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
|
|
|
|
}
|
|
|
|
|
2010-09-11 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|