2011-06-01 23:08:05 +00:00
|
|
|
// SparkleShare, a collaboration and sharing tool.
|
2011-05-14 17:10:24 +00:00
|
|
|
// 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 System;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
|
|
using Meebey.SmartIrc4net;
|
|
|
|
|
|
|
|
namespace SparkleLib {
|
|
|
|
|
|
|
|
public class SparkleListenerIrc : SparkleListenerBase {
|
|
|
|
|
|
|
|
private Thread thread;
|
|
|
|
private IrcClient client;
|
|
|
|
private string nick;
|
2011-10-16 18:46:37 +00:00
|
|
|
private string announcements_password;
|
|
|
|
private bool allow_passwordless_join;
|
2011-05-14 17:10:24 +00:00
|
|
|
|
2011-05-19 15:41:46 +00:00
|
|
|
|
2011-10-16 18:46:37 +00:00
|
|
|
public SparkleListenerIrc (Uri server, string folder_identifier) :
|
2011-06-28 18:36:47 +00:00
|
|
|
base (server, folder_identifier)
|
2011-05-14 17:10:24 +00:00
|
|
|
{
|
|
|
|
// Try to get a uniqueish nickname
|
|
|
|
this.nick = SHA1 (DateTime.Now.ToString ("ffffff") + "sparkles");
|
|
|
|
|
|
|
|
// Most irc servers don't allow nicknames starting
|
|
|
|
// with a number, so prefix an alphabetic character
|
|
|
|
this.nick = "s" + this.nick.Substring (0, 7);
|
|
|
|
|
2011-10-17 17:54:41 +00:00
|
|
|
// Optional password to make the channel access more safe
|
|
|
|
this.announcements_password =
|
|
|
|
SparkleConfig.DefaultConfig.GetConfigOption ("announcements_password");
|
|
|
|
|
|
|
|
// Option to allow access to channel when no password is defined
|
|
|
|
try {
|
2011-10-22 13:51:57 +00:00
|
|
|
string option = SparkleConfig.DefaultConfig.GetConfigOption ("allow_passwordless_join");
|
|
|
|
this.allow_passwordless_join = (option == null || Convert.ToBoolean (option));
|
2011-10-17 17:54:41 +00:00
|
|
|
} catch (Exception) {
|
|
|
|
this.allow_passwordless_join = true;
|
2011-10-16 18:46:37 +00:00
|
|
|
}
|
2011-10-11 15:11:12 +00:00
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
base.channels.Add ("#" + folder_identifier);
|
2011-05-14 17:10:24 +00:00
|
|
|
|
|
|
|
this.client = new IrcClient () {
|
2011-06-05 22:08:17 +00:00
|
|
|
PingTimeout = 180,
|
2011-05-28 23:52:46 +00:00
|
|
|
PingInterval = 60
|
2011-05-14 17:10:24 +00:00
|
|
|
};
|
|
|
|
|
2011-09-12 14:13:41 +00:00
|
|
|
string proxy = Environment.GetEnvironmentVariable ("http_proxy");
|
|
|
|
Uri proxy_uri = null;
|
|
|
|
if (!String.IsNullOrEmpty (proxy) &&
|
|
|
|
Uri.TryCreate (proxy, UriKind.Absolute, out proxy_uri)) {
|
|
|
|
|
|
|
|
if (proxy_uri.Scheme == "http") {
|
2011-09-12 13:59:17 +00:00
|
|
|
this.client.ProxyType = ProxyType.Http;
|
2011-09-12 15:05:10 +00:00
|
|
|
this.client.ProxyHost = proxy_uri.Host;
|
|
|
|
this.client.ProxyPort = proxy_uri.Port;
|
2011-09-12 13:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-14 17:10:24 +00:00
|
|
|
this.client.OnConnected += delegate {
|
2011-05-22 00:02:16 +00:00
|
|
|
base.is_connecting = false;
|
2011-05-14 17:10:24 +00:00
|
|
|
OnConnected ();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.client.OnDisconnected += delegate {
|
2011-06-07 23:38:09 +00:00
|
|
|
base.is_connecting = false;
|
2011-05-14 17:10:24 +00:00
|
|
|
OnDisconnected ();
|
|
|
|
};
|
|
|
|
|
2011-05-28 23:52:46 +00:00
|
|
|
this.client.OnError += delegate {
|
2011-06-07 23:38:09 +00:00
|
|
|
base.is_connecting = false;
|
2011-05-28 23:52:46 +00:00
|
|
|
OnDisconnected ();
|
|
|
|
};
|
|
|
|
|
2011-05-14 17:10:24 +00:00
|
|
|
this.client.OnChannelMessage += delegate (object o, IrcEventArgs args) {
|
|
|
|
string message = args.Data.Message.Trim ();
|
2011-05-23 03:06:07 +00:00
|
|
|
string folder_id = args.Data.Channel.Substring (1); // remove the starting hash
|
2011-05-28 23:52:46 +00:00
|
|
|
OnAnnouncement (new SparkleAnnouncement (folder_id, message));
|
2011-05-14 17:10:24 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
public override bool IsConnected {
|
|
|
|
get {
|
|
|
|
return this.client.IsConnected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-14 17:10:24 +00:00
|
|
|
// Starts a new thread and listens to the channel
|
|
|
|
public override void Connect ()
|
|
|
|
{
|
2011-05-22 00:02:16 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Connecting to " + Server);
|
|
|
|
|
|
|
|
base.is_connecting = true;
|
|
|
|
|
2011-05-14 17:10:24 +00:00
|
|
|
this.thread = new Thread (
|
|
|
|
new ThreadStart (delegate {
|
|
|
|
try {
|
|
|
|
// Connect, login, and join the channel
|
2011-06-28 18:36:47 +00:00
|
|
|
int port = base.server.Port;
|
2011-10-17 17:54:41 +00:00
|
|
|
|
|
|
|
if (port < 0)
|
|
|
|
port = 6667;
|
|
|
|
|
2011-06-28 18:36:47 +00:00
|
|
|
this.client.Connect (base.server.Host, port);
|
2011-09-11 18:24:17 +00:00
|
|
|
this.client.Login (this.nick, this.nick, 8, this.nick);
|
2011-05-22 00:02:16 +00:00
|
|
|
|
2011-05-28 23:52:46 +00:00
|
|
|
foreach (string channel in base.channels) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Joining channel " + channel);
|
2011-10-17 17:54:41 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty (this.announcements_password)) {
|
2011-10-16 18:46:37 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Password set to access the channel");
|
|
|
|
this.client.RfcJoin (channel, this.announcements_password);
|
|
|
|
this.client.RfcMode (channel, "+k " + this.announcements_password);
|
2011-10-17 17:54:41 +00:00
|
|
|
|
2011-09-20 18:11:38 +00:00
|
|
|
} else {
|
2011-10-17 17:54:41 +00:00
|
|
|
if (this.allow_passwordless_join) {
|
2011-10-22 13:51:57 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Accessing unprotected channel, change the setting to not access");
|
2011-10-11 15:11:12 +00:00
|
|
|
this.client.RfcJoin (channel);
|
2011-10-22 13:51:57 +00:00
|
|
|
|
2011-10-11 15:11:12 +00:00
|
|
|
} else {
|
2011-10-22 13:51:57 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Unprotected channel, change the setting to access");
|
|
|
|
base.is_connecting = false;
|
|
|
|
OnDisconnected ();
|
|
|
|
throw new ConnectionException ("Unprotected channel, change the setting to access");
|
2011-10-11 15:11:12 +00:00
|
|
|
}
|
2011-09-20 18:11:38 +00:00
|
|
|
}
|
2011-10-17 17:54:41 +00:00
|
|
|
|
2011-09-11 18:29:25 +00:00
|
|
|
this.client.RfcMode (channel, "+s");
|
2011-05-28 23:52:46 +00:00
|
|
|
}
|
2011-05-14 17:10:24 +00:00
|
|
|
|
|
|
|
// List to the channel, this blocks the thread
|
|
|
|
this.client.Listen ();
|
|
|
|
|
|
|
|
// Disconnect when we time out
|
|
|
|
this.client.Disconnect ();
|
2011-05-28 23:52:46 +00:00
|
|
|
|
|
|
|
} catch (ConnectionException e) {
|
2011-05-22 00:02:16 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Could not connect to " + Server + ": " + e.Message);
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.thread.Start ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
public override void AlsoListenTo (string folder_identifier)
|
2011-05-14 17:10:24 +00:00
|
|
|
{
|
2011-05-22 00:02:16 +00:00
|
|
|
string channel = "#" + folder_identifier;
|
2011-06-01 22:59:18 +00:00
|
|
|
if (!base.channels.Contains (channel)) {
|
|
|
|
base.channels.Add (channel);
|
2011-06-07 23:38:09 +00:00
|
|
|
|
|
|
|
if (IsConnected) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Joining channel " + channel);
|
2011-10-16 18:46:37 +00:00
|
|
|
if (this.announcements_password != null) {
|
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Password set to access the channel");
|
|
|
|
this.client.RfcJoin (channel, this.announcements_password);
|
|
|
|
this.client.RfcMode (channel, "+k " + this.announcements_password);
|
2011-09-20 18:11:38 +00:00
|
|
|
} else {
|
2011-10-16 18:46:37 +00:00
|
|
|
if (allow_passwordless_join) {
|
2011-10-11 15:11:12 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Accessing a dangerous channel change the setting to not access");
|
|
|
|
this.client.RfcJoin (channel);
|
|
|
|
} else {
|
2011-10-16 18:46:37 +00:00
|
|
|
SparkleHelpers.DebugInfo ("ListenerIrc", "Dangerous channel, change the setting to access");
|
2011-10-11 15:11:12 +00:00
|
|
|
}
|
2011-09-20 18:11:38 +00:00
|
|
|
}
|
2011-10-16 18:46:37 +00:00
|
|
|
this.client.RfcMode (channel, "+s");
|
2011-06-07 23:38:09 +00:00
|
|
|
}
|
2011-06-01 22:59:18 +00:00
|
|
|
}
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-22 00:02:16 +00:00
|
|
|
public override void Announce (SparkleAnnouncement announcement)
|
|
|
|
{
|
|
|
|
string channel = "#" + announcement.FolderIdentifier;
|
|
|
|
this.client.SendMessage (SendType.Message, channel, announcement.Message);
|
|
|
|
|
|
|
|
// Also announce to ourselves for debugging purposes
|
2011-05-30 15:22:09 +00:00
|
|
|
// base.OnAnnouncement (announcement);
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Dispose ()
|
|
|
|
{
|
|
|
|
this.thread.Abort ();
|
|
|
|
this.thread.Join ();
|
2011-05-29 19:01:56 +00:00
|
|
|
base.Dispose ();
|
2011-05-14 17:10:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a SHA-1 hash of input
|
|
|
|
private string SHA1 (string s)
|
|
|
|
{
|
|
|
|
SHA1 sha1 = new SHA1CryptoServiceProvider ();
|
|
|
|
Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
|
|
|
|
Byte[] encoded_bytes = sha1.ComputeHash (bytes);
|
|
|
|
return BitConverter.ToString (encoded_bytes).ToLower ().Replace ("-", "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|