forked from Teknikode/Teknik
17 changed files with 403 additions and 8 deletions
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Web; |
||||
using System.Web.Mvc; |
||||
using Teknik.Controllers; |
||||
using Teknik.Attributes; |
||||
using Teknik.Areas.IRC.ViewModels; |
||||
|
||||
namespace Teknik.Areas.IRC.Controllers |
||||
{ |
||||
[TeknikAuthorize] |
||||
public class IRCController : DefaultController |
||||
{ |
||||
public ActionResult Index() |
||||
{ |
||||
ClientViewModel model = new ClientViewModel(); |
||||
return View(model); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic; |
||||
using System.Web.Mvc; |
||||
using System.Web.Optimization; |
||||
using Teknik.Configuration; |
||||
using Teknik.Utilities; |
||||
|
||||
namespace Teknik.Areas.IRC |
||||
{ |
||||
public class IRCAreaRegistration : AreaRegistration |
||||
{ |
||||
public override string AreaName |
||||
{ |
||||
get |
||||
{ |
||||
return "IRC"; |
||||
} |
||||
} |
||||
|
||||
public override void RegisterArea(AreaRegistrationContext context) |
||||
{ |
||||
Config config = Config.Load(); |
||||
context.MapSubdomainRoute( |
||||
"IRC.Index", // Route name
|
||||
new List<string>() { "irc" }, |
||||
new List<string>() { config.Host }, |
||||
"", // URL with parameters
|
||||
new { controller = "IRC", action = "Index" }, // Parameter defaults
|
||||
new[] { typeof(Controllers.IRCController).Namespace } |
||||
); |
||||
|
||||
// Register Script Bundle
|
||||
BundleTable.Bundles.Add(new CdnScriptBundle("~/bundles/irc", config.CdnHost).Include( |
||||
"~/Areas/IRC/Scripts/IRC.js")); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
$(document).ready(function () { |
||||
/* ---------------------------------------- |
||||
Websocket for the irc client |
||||
-----------------------------------------*/ |
||||
var irc = $.connection.ircClient; |
||||
|
||||
irc.client.rawMessageReceived = function (message) { |
||||
var txt = $('#clientOutput'); |
||||
txt.val(txt.val() + '\n' + message); |
||||
}; |
||||
|
||||
$.connection.hub.start(); |
||||
|
||||
$('#sendMessage').click(function () { |
||||
irc.server.sendRawMessage($('#message').val()); |
||||
$('#message').val(''); |
||||
}); |
||||
}); |
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Web; |
||||
using Teknik.ViewModels; |
||||
|
||||
namespace Teknik.Areas.IRC.ViewModels |
||||
{ |
||||
public class ClientViewModel : ViewModelBase |
||||
{ |
||||
} |
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
@model Teknik.Areas.IRC.ViewModels.ClientViewModel |
||||
|
||||
@using Teknik.Utilities |
||||
|
||||
@Scripts.Render("~/bundles/signalr") |
||||
@Scripts.Render("~/signalr/hubs") |
||||
@Scripts.Render("~/bundles/irc") |
||||
|
||||
<div class="container"> |
||||
@if (User.Identity.IsAuthenticated) |
||||
{ |
||||
<div class="row"> |
||||
<div class="col-sm-10"> |
||||
<textarea class="form-control" name="clientOutput" id="clientOutput" rows="20"></textarea> |
||||
</div> |
||||
</div> |
||||
<br /> |
||||
<div class="row"> |
||||
<div class="col-sm-10"> |
||||
<input type="text" class="form-control" name="message" id="message"> |
||||
</div> |
||||
<div class="col-sm-2"> |
||||
<button type="button" class="btn btn-primary" id="sendMessage">Submit</button> |
||||
</div> |
||||
</div> |
||||
} |
||||
else |
||||
{ |
||||
<div class="row"> |
||||
<div class="col-sm-10 col-sm-offset-1"> |
||||
<p>You need to be authenticated to view this page.</p> |
||||
</div> |
||||
</div> |
||||
} |
||||
</div> |
@ -0,0 +1,3 @@
@@ -0,0 +1,3 @@
|
||||
@{ |
||||
Layout = "~/Views/Shared/_Layout.cshtml"; |
||||
} |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
<?xml version="1.0"?> |
||||
|
||||
<configuration> |
||||
<configSections> |
||||
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> |
||||
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> |
||||
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> |
||||
</sectionGroup> |
||||
</configSections> |
||||
|
||||
<system.web.webPages.razor> |
||||
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> |
||||
<pages pageBaseType="Teknik.BaseViewPage"> |
||||
<namespaces> |
||||
<add namespace="System.Web.Mvc" /> |
||||
<add namespace="System.Web.Mvc.Ajax" /> |
||||
<add namespace="System.Web.Mvc.Html" /> |
||||
<add namespace="System.Web.Routing" /> |
||||
<add namespace="System.Web.Optimization" /> |
||||
<add namespace="Teknik" /> |
||||
|
||||
</namespaces> |
||||
</pages> |
||||
</system.web.webPages.razor> |
||||
|
||||
<appSettings> |
||||
<add key="webpages:Enabled" value="false" /> |
||||
</appSettings> |
||||
|
||||
<system.webServer> |
||||
<handlers> |
||||
<remove name="BlockViewHandler"/> |
||||
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> |
||||
</handlers> |
||||
</system.webServer> |
||||
</configuration> |
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
using Microsoft.AspNet.SignalR; |
||||
using Microsoft.AspNet.SignalR.Hubs; |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Web; |
||||
using Teknik.Attributes; |
||||
using Teknik.SignalR; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Teknik.Hubs |
||||
{ |
||||
[HubName("ircClient")] |
||||
[TeknikAuthorize] |
||||
public class IRCClientHub : Hub |
||||
{ |
||||
private static Dictionary<string, IRCClient> _Clients = new Dictionary<string, IRCClient>(); |
||||
|
||||
public IRCClientHub() |
||||
{ |
||||
} |
||||
|
||||
public override Task OnConnected() |
||||
{ |
||||
if (Context != null) |
||||
{ |
||||
if (!_Clients.ContainsKey(Context.ConnectionId)) |
||||
{ |
||||
if (Context.User.Identity.IsAuthenticated) |
||||
{ |
||||
// Add the new connection to the main list
|
||||
_Clients.Add(Context.ConnectionId, new IRCClient(Clients.Caller)); |
||||
|
||||
// Auto Connect to the server at startup
|
||||
Connect(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return base.OnConnected(); |
||||
} |
||||
|
||||
public override Task OnDisconnected(bool stopCalled) |
||||
{ |
||||
if (_Clients.ContainsKey(Context.ConnectionId)) |
||||
{ |
||||
// Disconnect from the IRC session
|
||||
_Clients[Context.ConnectionId].Disconnect(); |
||||
|
||||
// Remove the irc client
|
||||
_Clients.Remove(Context.ConnectionId); |
||||
} |
||||
return base.OnDisconnected(stopCalled); |
||||
} |
||||
|
||||
public void Connect() |
||||
{ |
||||
if (_Clients.ContainsKey(Context.ConnectionId)) |
||||
{ |
||||
_Clients[Context.ConnectionId].Connect(Context.User.Identity.Name); |
||||
} |
||||
} |
||||
|
||||
public void SendRawMessage(string message) |
||||
{ |
||||
if (_Clients.ContainsKey(Context.ConnectionId)) |
||||
{ |
||||
_Clients[Context.ConnectionId].SendRawMessage(message); |
||||
} |
||||
} |
||||
} |
||||
} |
Binary file not shown.
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
using IRCSharp; |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Threading.Tasks; |
||||
using System.Web; |
||||
using IRCSharp.Messaging; |
||||
using Microsoft.AspNet.SignalR; |
||||
using Teknik.Hubs; |
||||
using Microsoft.AspNet.SignalR.Hubs; |
||||
using Teknik.Configuration; |
||||
using System.Net; |
||||
using Teknik.Utilities; |
||||
|
||||
namespace Teknik.SignalR |
||||
{ |
||||
public class IRCClient |
||||
{ |
||||
private IRC _IRC; |
||||
private dynamic _CallerContext; |
||||
|
||||
// State Variables
|
||||
private bool _Connected = false; |
||||
public bool Connected |
||||
{ |
||||
get |
||||
{ |
||||
return _Connected; |
||||
} |
||||
set |
||||
{ |
||||
_Connected = value; |
||||
} |
||||
} |
||||
|
||||
public IRCClient(dynamic callerContext) |
||||
{ |
||||
Config config = Config.Load(); |
||||
_CallerContext = callerContext; |
||||
|
||||
_IRC = new IRC(config.IRCConfig.MaxMessageLength); |
||||
|
||||
_IRC.ConnectEvent += HandleConnectEvent; |
||||
_IRC.DisconnectEvent += HandleDisconnectEvent; |
||||
_IRC.ExceptionThrown += HandleIrcExceptionThrown; |
||||
|
||||
_IRC.Message.RawMessageEvent += Message_RawMessageEvent; |
||||
} |
||||
|
||||
public void Connect(string username) |
||||
{ |
||||
if (!_Connected) |
||||
{ |
||||
Config config = Config.Load(); |
||||
IPAddress[] ipList = Dns.GetHostAddresses(config.IRCConfig.Host); |
||||
foreach (IPAddress ip in ipList) |
||||
{ |
||||
_Connected = _IRC.Connect(ip, config.IRCConfig.Port); |
||||
if (_Connected) |
||||
{ |
||||
break; |
||||
} |
||||
} |
||||
|
||||
if (_Connected) |
||||
{ |
||||
_IRC.Login("Teknik-WebClient", new Nick() |
||||
{ |
||||
Nickname = username, |
||||
Host = Dns.GetHostName(), |
||||
Realname = username, |
||||
Username = username |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void Disconnect() |
||||
{ |
||||
if (_Connected) |
||||
{ |
||||
_IRC.Disconnect(); |
||||
} |
||||
} |
||||
|
||||
public void SendRawMessage(string message) |
||||
{ |
||||
if (_Connected) |
||||
{ |
||||
_IRC.Command.SendRaw(message); |
||||
} |
||||
} |
||||
|
||||
private void Message_RawMessageEvent(object sender, string message) |
||||
{ |
||||
_CallerContext.rawMessageReceived(message); |
||||
} |
||||
|
||||
private void HandleIrcExceptionThrown(Exception obj) |
||||
{ |
||||
_CallerContext.exception(obj.GetFullMessage(true)); |
||||
} |
||||
|
||||
private void HandleDisconnectEvent() |
||||
{ |
||||
_CallerContext.disconnected(); |
||||
} |
||||
|
||||
private void HandleConnectEvent() |
||||
{ |
||||
_CallerContext.connected(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Teknik.Configuration |
||||
{ |
||||
public class IRCConfig |
||||
{ |
||||
public bool Enabled { get; set; } |
||||
|
||||
public string Host { get; set; } |
||||
|
||||
public int Port { get; set; } |
||||
|
||||
public int MaxMessageLength { get; set; } |
||||
|
||||
public IRCConfig() |
||||
{ |
||||
Host = string.Empty; |
||||
Port = 0; |
||||
MaxMessageLength = 400; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue