PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/MessengerBot.Core/Session.cs

#
C# | 265 lines | 137 code | 25 blank | 103 comment | 7 complexity | d6a1ed084bd89d130a1a29298cb8f9ca MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using MSNPSharp;
  6. namespace MessengerBot
  7. {
  8. /// <summary>
  9. /// This class encapsulates a session with a user.
  10. /// </summary>
  11. public sealed class Session
  12. {
  13. /// <summary>
  14. /// This is the backing field for the <see cref="Bot"/> property.
  15. /// </summary>
  16. private readonly Bot _Bot;
  17. /// <summary>
  18. /// This is the backing field for the <see cref="Conversation"/> property.
  19. /// </summary>
  20. private readonly Conversation _Conversation;
  21. /// <summary>
  22. /// This field holds a collection of system state objects for the registered systems.
  23. /// </summary>
  24. private readonly List<Tuple<ISystem, object>> _SystemState = new List<Tuple<ISystem, object>>();
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="Session"/> class.
  27. /// </summary>
  28. /// <param name="bot">
  29. /// The bot that manages this <see cref="Session"/>.
  30. /// </param>
  31. /// <param name="conversation">
  32. /// The <see cref="Conversation"/> object this <see cref="Session"/> will encapsulate.
  33. /// </param>
  34. /// <exception cref="ArgumentNullException">
  35. /// <para><paramref name="bot"/> is <c>null</c>.</para>
  36. /// <para>- or -</para>
  37. /// <para><paramref name="conversation"/> is <c>null</c>.</para>
  38. /// </exception>
  39. public Session(Bot bot, Conversation conversation)
  40. {
  41. if (bot == null)
  42. throw new ArgumentNullException("bot");
  43. if (conversation == null)
  44. throw new ArgumentNullException("conversation");
  45. _Bot = bot;
  46. _Conversation = conversation;
  47. _Conversation.ContactJoined += ContactJoined;
  48. _Conversation.ContactLeft += ContactLeft;
  49. _Conversation.ConversationEnded += ConversationEnded;
  50. _Conversation.EmoticonDefinitionReceived += EmoticonDefinitionReceived;
  51. _Conversation.ExceptionOccurred += ExceptionOccurred;
  52. _Conversation.MSNObjectDataTransferCompleted += MSNObjectDataTransferCompleted;
  53. _Conversation.NudgeReceived += NudgeReceived;
  54. _Conversation.RemoteOwnerChanged += RemoteOwnerChanged;
  55. _Conversation.ServerErrorReceived += ServerErrorReceived;
  56. _Conversation.SessionClosed += SessionClosed;
  57. _Conversation.SessionEstablished += SessionEstablished;
  58. _Conversation.TextMessageReceived += TextMessageReceived;
  59. _Conversation.UserTyping += UserTyping;
  60. _Conversation.WinkReceived += WinkReceived;
  61. }
  62. /// <summary>
  63. /// Gets the <see cref="Bot"/> that manages this <see cref="Session"/>.
  64. /// </summary>
  65. public Bot Bot
  66. {
  67. get
  68. {
  69. return _Bot;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the <see cref="Conversation"/> that this <see cref="Session"/> encapsulates.
  74. /// </summary>
  75. public Conversation Conversation
  76. {
  77. get
  78. {
  79. return _Conversation;
  80. }
  81. }
  82. /// <summary>
  83. /// Event, WinkReceived.
  84. /// </summary>
  85. /// <param name="sender">The sender.</param>
  86. /// <param name="e">The <see cref="MSNPSharp.WinkEventArgs"/> instance containing the event data.</param>
  87. private void WinkReceived(object sender, WinkEventArgs e)
  88. {
  89. }
  90. /// <summary>
  91. /// Event, UserTyping.
  92. /// </summary>
  93. /// <param name="sender">The sender.</param>
  94. /// <param name="e">The <see cref="MSNPSharp.ContactEventArgs"/> instance containing the event data.</param>
  95. private void UserTyping(object sender, ContactEventArgs e)
  96. {
  97. }
  98. /// <summary>
  99. /// Event, TextMessageReceived.
  100. /// </summary>
  101. /// <param name="sender">The sender.</param>
  102. /// <param name="e">The <see cref="MSNPSharp.TextMessageEventArgs"/> instance containing the event data.</param>
  103. private void TextMessageReceived(object sender, TextMessageEventArgs e)
  104. {
  105. foreach (var systemState in _SystemState)
  106. systemState.Item1.MessageReceived(Bot, this, systemState.Item2, e.Message.Text);
  107. // TODO: Handle quotes
  108. string[] parts = e.Message.Text.Split(
  109. new[]
  110. {
  111. ' '
  112. }, StringSplitOptions.RemoveEmptyEntries);
  113. string[] arguments;
  114. ICommand command = CommandParser.GetCommand(_Bot.Commands, parts, out arguments);
  115. if (command != null)
  116. {
  117. _Conversation.SendTypingMessage();
  118. Type commandType = command.GetType();
  119. ICommandBasedSystem ownerSystem = (from system in _Bot.Configuration.Systems.OfType<ICommandBasedSystem>()
  120. where system.GetCommands().Contains(commandType)
  121. select system).FirstOrDefault();
  122. Debug.Assert(ownerSystem != null, "ownerSystem should not be null here");
  123. try
  124. {
  125. command.Execute(ownerSystem, this, arguments);
  126. }
  127. catch (CommandException ex)
  128. {
  129. _Conversation.SendTextMessage(new TextMessage("Error: " + ex.Message));
  130. _Conversation.SendTextMessage(new TextMessage("If you need assistance, use the command 'help'"));
  131. }
  132. catch (Exception ex)
  133. {
  134. _Conversation.SendTextMessage(new TextMessage("An exception occured :("));
  135. _Conversation.SendTextMessage(new TextMessage(ex.GetType().FullName + ": " + ex.Message));
  136. _Conversation.SendTextMessage(new TextMessage(ex.StackTrace));
  137. }
  138. }
  139. else
  140. _Conversation.SendTextMessage(new TextMessage("Unknown command, use the command 'help' for help"));
  141. }
  142. /// <summary>
  143. /// Event, SessionEstablished.
  144. /// </summary>
  145. /// <param name="sender">The sender.</param>
  146. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  147. private void SessionEstablished(object sender, EventArgs e)
  148. {
  149. _SystemState.Clear();
  150. foreach (ISystem system in Bot.Configuration.Systems)
  151. {
  152. object state = system.SessionStarted(Bot, this);
  153. _SystemState.Add(Tuple.Create(system, state));
  154. }
  155. }
  156. /// <summary>
  157. /// Event, SessionClosed.
  158. /// </summary>
  159. /// <param name="sender">The sender.</param>
  160. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  161. private void SessionClosed(object sender, EventArgs e)
  162. {
  163. foreach (var systemState in _SystemState)
  164. systemState.Item1.SessionEnded(Bot, this, systemState.Item2);
  165. _SystemState.Clear();
  166. }
  167. /// <summary>
  168. /// Event, ServerErrorReceived.
  169. /// </summary>
  170. /// <param name="sender">The sender.</param>
  171. /// <param name="e">The <see cref="MSNPSharp.MSNErrorEventArgs"/> instance containing the event data.</param>
  172. private void ServerErrorReceived(object sender, MSNErrorEventArgs e)
  173. {
  174. }
  175. /// <summary>
  176. /// Event, RemoteOwnerChanged.
  177. /// </summary>
  178. /// <param name="sender">The sender.</param>
  179. /// <param name="e">The <see cref="MSNPSharp.ConversationRemoteOwnerChangedEventArgs"/> instance containing the event data.</param>
  180. private void RemoteOwnerChanged(object sender, ConversationRemoteOwnerChangedEventArgs e)
  181. {
  182. }
  183. /// <summary>
  184. /// Event, NudgeReceived.
  185. /// </summary>
  186. /// <param name="sender">The sender.</param>
  187. /// <param name="e">The <see cref="MSNPSharp.ContactEventArgs"/> instance containing the event data.</param>
  188. private void NudgeReceived(object sender, ContactEventArgs e)
  189. {
  190. }
  191. /// <summary>
  192. /// Event, MSNObjectDataTransferCompleted.
  193. /// </summary>
  194. /// <param name="sender">The sender.</param>
  195. /// <param name="e">The <see cref="MSNPSharp.ConversationMSNObjectDataTransferCompletedEventArgs"/> instance containing the event data.</param>
  196. private void MSNObjectDataTransferCompleted(object sender, ConversationMSNObjectDataTransferCompletedEventArgs e)
  197. {
  198. }
  199. /// <summary>
  200. /// Event, ExceptionOccurred.
  201. /// </summary>
  202. /// <param name="sender">The sender.</param>
  203. /// <param name="e">The <see cref="MSNPSharp.ExceptionEventArgs"/> instance containing the event data.</param>
  204. private void ExceptionOccurred(object sender, ExceptionEventArgs e)
  205. {
  206. }
  207. /// <summary>
  208. /// Event, EmoticonDefinitionReceived.
  209. /// </summary>
  210. /// <param name="sender">The sender.</param>
  211. /// <param name="e">The <see cref="MSNPSharp.EmoticonDefinitionEventArgs"/> instance containing the event data.</param>
  212. private void EmoticonDefinitionReceived(object sender, EmoticonDefinitionEventArgs e)
  213. {
  214. }
  215. /// <summary>
  216. /// Event, ConversationEnded.
  217. /// </summary>
  218. /// <param name="sender">The sender.</param>
  219. /// <param name="e">The <see cref="MSNPSharp.ConversationEndEventArgs"/> instance containing the event data.</param>
  220. private void ConversationEnded(object sender, ConversationEndEventArgs e)
  221. {
  222. }
  223. /// <summary>
  224. /// Event, ContactLeft.
  225. /// </summary>
  226. /// <param name="sender">The sender.</param>
  227. /// <param name="e">The <see cref="MSNPSharp.ContactConversationEventArgs"/> instance containing the event data.</param>
  228. private void ContactLeft(object sender, ContactConversationEventArgs e)
  229. {
  230. }
  231. /// <summary>
  232. /// Event, ContactJoined.
  233. /// </summary>
  234. /// <param name="sender">The sender.</param>
  235. /// <param name="e">The <see cref="MSNPSharp.ContactConversationEventArgs"/> instance containing the event data.</param>
  236. private void ContactJoined(object sender, ContactConversationEventArgs e)
  237. {
  238. }
  239. }
  240. }