The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IRCClient.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using IRCSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using IRCSharp.Messaging;
  8. using Microsoft.AspNet.SignalR;
  9. using Teknik.Hubs;
  10. using Microsoft.AspNet.SignalR.Hubs;
  11. using Teknik.Configuration;
  12. using System.Net;
  13. using Teknik.Utilities;
  14. using System.Timers;
  15. using IRCSharp.Commanding;
  16. namespace Teknik.SignalR
  17. {
  18. public class IRCClient
  19. {
  20. private IRC _IRC;
  21. private dynamic _CallerContext;
  22. public dynamic CallerContext
  23. {
  24. get
  25. {
  26. return _CallerContext;
  27. }
  28. set
  29. {
  30. _CallerContext = value;
  31. }
  32. }
  33. private bool _Guest;
  34. // State Variables
  35. private bool _Connected = false;
  36. public bool Connected
  37. {
  38. get
  39. {
  40. return _Connected;
  41. }
  42. set
  43. {
  44. _Connected = value;
  45. }
  46. }
  47. public IRCClient(dynamic callerContext)
  48. {
  49. _Guest = false;
  50. Config config = Config.Load();
  51. _CallerContext = callerContext;
  52. _IRC = new IRC(config.IRCConfig.MaxMessageLength);
  53. _IRC.ConnectEvent += HandleConnectEvent;
  54. _IRC.DisconnectEvent += HandleDisconnectEvent;
  55. _IRC.ExceptionThrown += HandleIrcExceptionThrown;
  56. // Handle Message Events
  57. _IRC.Message.RawMessageEvent += Message_RawMessageEvent;
  58. _IRC.Message.ChannelMessageReceivedEvent += Message_ChannelMessageReceivedEvent;
  59. _IRC.Message.ChannelNoticeReceivedEvent += Message_ChannelNoticeReceivedEvent;
  60. _IRC.Message.ChannelModeChangeEvent += Message_ChannelModeChangeEvent;
  61. _IRC.Message.PrivateMessageReceivedEvent += Message_PrivateMessageReceivedEvent;
  62. _IRC.Message.PrivateNoticeReceivedEvent += Message_PrivateNoticeReceivedEvent;
  63. _IRC.Message.CTCPMessageReceivedEvent += Message_CTCPMessageReceivedEvent;
  64. _IRC.Message.CTCPNoticeReceivedEvent += Message_CTCPNoticeReceivedEvent;
  65. _IRC.Message.JoinChannelEvent += Message_JoinChannelEvent;
  66. _IRC.Message.InviteChannelEvent += Message_InviteChannelEvent;
  67. _IRC.Message.PartChannelEvent += Message_PartChannelEvent;
  68. _IRC.Message.QuitEvent += Message_QuitEvent;
  69. _IRC.Message.KickEvent += Message_KickEvent;
  70. _IRC.Message.TopicChangeEvent += Message_TopicChangeEvent;
  71. _IRC.Message.UserModeChangeEvent += Message_UserModeChangeEvent;
  72. _IRC.Message.NickChangeEvent += Message_NickChangeEvent;
  73. _IRC.Message.ServerReplyEvent += Message_ServerReplyEvent;
  74. // Handle Command Events
  75. _IRC.Command.PrivateMessageCommandEvent += Command_PrivateMessageCommandEvent;
  76. _IRC.Command.PrivateNoticeCommandEvent += Command_PrivateNoticeCommandEvent;
  77. }
  78. public void Connect()
  79. {
  80. // Create guest username
  81. string username = "Guest_" + StringHelper.RandomString(6, "0123456789");
  82. Connect(username, string.Empty);
  83. }
  84. public void Connect(string username, string password)
  85. {
  86. _Guest = string.IsNullOrEmpty(password);
  87. if (!_Connected)
  88. {
  89. Config config = Config.Load();
  90. IPAddress[] ipList = Dns.GetHostAddresses(config.IRCConfig.Host);
  91. foreach (IPAddress ip in ipList)
  92. {
  93. _Connected = _IRC.Connect(ip, config.IRCConfig.Port);
  94. if (_Connected)
  95. {
  96. break;
  97. }
  98. }
  99. if (_Connected)
  100. {
  101. // Login
  102. _IRC.Login(config.Title + "-WebClient", new Nick()
  103. {
  104. Nickname = username,
  105. Host = config.Host,
  106. Realname = username,
  107. Username = username
  108. });
  109. // Fire off event about current nick
  110. _CallerContext.nickChanged(username);
  111. // Try to identify if not guest
  112. if (!_Guest)
  113. {
  114. // Identify to NickServ if need be
  115. _IRC.Command.SendPrivateMessage("NickServ", string.Format("IDENTIFY {0}", password));
  116. }
  117. }
  118. }
  119. }
  120. public void Disconnect()
  121. {
  122. if (_Connected)
  123. {
  124. _IRC.Disconnect();
  125. }
  126. }
  127. public void SendRawMessage(string message)
  128. {
  129. if (_Connected)
  130. {
  131. _IRC.Command.SendRaw(message);
  132. }
  133. }
  134. /// <summary>
  135. /// Process the message and for commands, execute their command, otherwise send a private message
  136. /// </summary>
  137. /// <param name="message"></param>
  138. public void SendMessage(string location, string message)
  139. {
  140. if (_Connected)
  141. {
  142. // Process Command
  143. if (message.StartsWith("/"))
  144. {
  145. string[] msgSplit = message.Split(' ');
  146. string cmd = msgSplit[0].Remove(0, 1);
  147. string actualMessage = string.Empty;
  148. if (msgSplit.Length > 1)
  149. {
  150. actualMessage = string.Join(" ", msgSplit.Skip(1).Take(msgSplit.Length - 1));
  151. }
  152. ProcessCommand(cmd, actualMessage, location);
  153. }
  154. else
  155. {
  156. _IRC.Command.SendPrivateMessage(location, message);
  157. }
  158. }
  159. }
  160. private void ProcessCommand(string command, string message, string location)
  161. {
  162. switch (command.ToLower())
  163. {
  164. case "join":
  165. _IRC.Command.SendJoin(message);
  166. break;
  167. case "part":
  168. _IRC.Command.SendPart(message);
  169. break;
  170. case "invite":
  171. _IRC.Command.SendInvite(location, message);
  172. break;
  173. case "list":
  174. _IRC.Command.SendList(message);
  175. break;
  176. case "action":
  177. _IRC.Command.SendCTCPMessage(location, "ACTION", message);
  178. break;
  179. case "query":
  180. string rec = string.Empty;
  181. string msg = string.Empty;
  182. string[] querySplit = message.Split(' ');
  183. if (querySplit.Length > 0)
  184. {
  185. rec = querySplit[0];
  186. }
  187. if (querySplit.Length > 1)
  188. {
  189. msg = string.Join(" ", querySplit.Skip(1).Take(querySplit.Length - 1));
  190. }
  191. _IRC.Command.SendPrivateMessage(rec, msg);
  192. break;
  193. case "notice":
  194. string notRec = string.Empty;
  195. string notMsg = string.Empty;
  196. string[] notSplit = message.Split(' ');
  197. if (notSplit.Length > 0)
  198. {
  199. notRec = notSplit[0];
  200. }
  201. if (notSplit.Length > 1)
  202. {
  203. notMsg = string.Join(" ", notSplit.Skip(1).Take(notSplit.Length - 1));
  204. }
  205. _IRC.Command.SendNotice(notRec, notMsg);
  206. break;
  207. case "raw":
  208. _IRC.Command.SendRaw(message);
  209. break;
  210. default:
  211. _CallerContext.exception($"Command not recognized: {command}");
  212. break;
  213. }
  214. }
  215. // Handle Message Events
  216. private void Message_RawMessageEvent(object sender, string message)
  217. {
  218. _CallerContext.rawMessage(message);
  219. }
  220. private void Message_ServerReplyEvent(object sender, IReply e)
  221. {
  222. if (e.GetType() == typeof(ServerReplyMessage))
  223. {
  224. ServerReplyMessage reply = (ServerReplyMessage)e;
  225. _CallerContext.serverReply(reply);
  226. }
  227. else if (e.GetType() == typeof(ServerErrorMessage))
  228. {
  229. ServerErrorMessage error = (ServerErrorMessage)e;
  230. _CallerContext.serverError(error);
  231. }
  232. }
  233. private void Message_NickChangeEvent(object sender, NickChangeInfo e)
  234. {
  235. _CallerContext.nickChange(e);
  236. }
  237. private void Message_UserModeChangeEvent(object sender, UserModeChangeInfo e)
  238. {
  239. _CallerContext.userModeChange(e, e.Modes.ModesToString());
  240. }
  241. private void Message_TopicChangeEvent(object sender, TopicChangeInfo e)
  242. {
  243. _CallerContext.topicChange(e);
  244. }
  245. private void Message_KickEvent(object sender, KickInfo e)
  246. {
  247. _CallerContext.kick(e);
  248. }
  249. private void Message_QuitEvent(object sender, QuitInfo e)
  250. {
  251. _CallerContext.quit(e);
  252. }
  253. private void Message_PartChannelEvent(object sender, PartChannelInfo e)
  254. {
  255. _CallerContext.partChannel(e);
  256. }
  257. private void Message_InviteChannelEvent(object sender, InviteChannelInfo e)
  258. {
  259. _CallerContext.inviteChannel(e);
  260. }
  261. private void Message_JoinChannelEvent(object sender, JoinChannelInfo e)
  262. {
  263. _CallerContext.joinChannel(e);
  264. }
  265. private void Message_CTCPNoticeReceivedEvent(object sender, CTCPMessage e)
  266. {
  267. _CallerContext.ctcpMessage(e);
  268. }
  269. private void Message_CTCPMessageReceivedEvent(object sender, CTCPMessage e)
  270. {
  271. _CallerContext.ctcpMessage(e);
  272. }
  273. private void Message_PrivateNoticeReceivedEvent(object sender, PrivateNotice e)
  274. {
  275. _CallerContext.privateNotice(e);
  276. }
  277. private void Message_PrivateMessageReceivedEvent(object sender, PrivateMessage e)
  278. {
  279. _CallerContext.privateMessage(e);
  280. }
  281. private void Message_ChannelModeChangeEvent(object sender, ChannelModeChangeInfo e)
  282. {
  283. _CallerContext.channelModeChange(e, e.Modes.ModesToString());
  284. }
  285. private void Message_ChannelNoticeReceivedEvent(object sender, ChannelNotice e)
  286. {
  287. _CallerContext.channelNotice(e);
  288. }
  289. private void Message_ChannelMessageReceivedEvent(object sender, ChannelMessage e)
  290. {
  291. _CallerContext.channelMessage(e);
  292. }
  293. // Handle Command Events
  294. private void Command_PrivateNoticeCommandEvent(object sender, PrivateNoticeCommand e)
  295. {
  296. _CallerContext.privateNoticeCommand(e);
  297. }
  298. private void Command_PrivateMessageCommandEvent(object sender, PrivateMessageCommand e)
  299. {
  300. _CallerContext.privateMessageCommand(e);
  301. }
  302. // Handle IRC events
  303. private void HandleIrcExceptionThrown(Exception obj)
  304. {
  305. _CallerContext.exception(obj.GetFullMessage(true));
  306. }
  307. private void HandleDisconnectEvent()
  308. {
  309. _CallerContext.disconnected();
  310. }
  311. private void HandleConnectEvent()
  312. {
  313. _CallerContext.connected();
  314. }
  315. }
  316. }