Added some of my new algorithm stubs, worked on general presentation of DateTime...
[circ.git] / Circ.Lib / Backend / DefaultParser.cs
bloba84e412175ca4ce6355531b54c80ee4d6c8c68cb
1 #region License
2 /* Circ.Lib : main library behind Circ
3 * Copyright (C) 2007 LAVAL Jérémie
5 * This file is licensed under the terms of the LGPL.
7 * For the complete licence see the file COPYING.
8 */
9 #endregion
10 using System;
11 using System.Text.RegularExpressions;
13 namespace Circ.Backend
15 /// <summary>
16 /// A very basic parser. Not optimized at all !
17 /// </summary>
18 public class DefaultParser: IParser
20 static Regex privmsg = new Regex(@"^:.+!.+ PRIVMSG .*", RegexOptions.Compiled | RegexOptions.CultureInvariant);
21 static Regex reply = new Regex(@"^:.+ \d{3}.*", RegexOptions.Compiled | RegexOptions.CultureInvariant);
23 /// <summary>
24 /// Retrieve the Author of a message
25 /// </summary>
26 /// <remarks>You MUST make sure that the raw irc message is a private message before calling this</remarks>
27 /// <seealso cref="RetrieveMessageType(string)">For checking if the message is a classic message</seealso>
28 public string RetrieveAuthor(string rawMessage)
30 return rawMessage.Substring(1, rawMessage.IndexOf('!')-1);
33 public string RetrieveMessage(string rawMessage)
35 string param = RetrieveParameters(rawMessage);
36 return param.Substring(param.IndexOf(':')+1);
39 public string RetrieveMessageTarget(string rawMessage)
41 string param = RetrieveParameters(rawMessage);
42 return param.Substring(0, param.IndexOf(' '));
45 /// <summary>
46 /// Retrieve the type of the raw irc message
47 /// </summary>
48 /// <remarks>The raw message returned by a IrcConnection instance</remarks>
49 public MessageType RetrieveMessageType(string rawMessage)
51 if (rawMessage.Substring(rawMessage.IndexOf(' ')+1, 6).ToUpperInvariant() == "NOTICE")
52 return MessageType.Notice;
53 else if (privmsg.IsMatch(rawMessage))
54 return MessageType.PrivateMessage;
55 else if (reply.IsMatch(rawMessage))
56 return MessageType.Reply;
58 return MessageType.Action;
61 public ActionType RetrieveAction(string rawMessage)
63 string action = StripFirstColon(rawMessage);
64 action = action.Substring(0, action.IndexOf(' ')).ToUpperInvariant();
65 ActionType temp;
67 switch (action) {
68 case "PING":
69 temp = ActionType.Ping;
70 break;
71 default:
72 temp = (ActionType)0;
73 break;
75 return temp;
78 public string RetrieveParameters(string rawMessage)
80 string param = StripFirstColon(rawMessage);
81 param = param.Substring(param.IndexOf(' ')+1);
82 return param;
85 public string RetrieveNoticeText(string rawMessage)
87 string noticeMessage = StripFirstColon(rawMessage);
88 return noticeMessage.Substring(noticeMessage.IndexOf(':')+1);
91 public int RetrieveNumericalReply(string rawMessage)
93 string num = StripFirstColon(rawMessage).Substring(0, 3);
94 int result;
95 int.TryParse(num, out result);
96 return result;
99 public string[] RetrieveUsersList(string rawMessage)
101 string namesList = StripFirstColon(rawMessage);
102 namesList = namesList.Substring(namesList.IndexOf(':') + 1);
103 return namesList.Split(' ');
106 public string RetrieveTopic(string rawMessage)
108 string top = rawMessage.Substring(rawMessage.IndexOf('#'));
109 return top.Substring(top.IndexOf(':') + 1);
112 public string RetrieveChannelTarget(string rawMessage)
114 string temp = rawMessage.Substring(rawMessage.IndexOf('#'));
115 return temp.Substring(0, temp.IndexOf(' '));
118 public string StripFirstColon(string rawString)
120 if (rawString[0] != ':')
121 return rawString;
123 return rawString.Substring(rawString.IndexOf(' ')+1);