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.
11 using System
.Text
.RegularExpressions
;
13 namespace Circ
.Backend
16 /// A very basic parser. Not optimized at all !
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
);
24 /// Retrieve the Author of a message
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(' '));
46 /// Retrieve the type of the raw irc message
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();
69 temp
= ActionType
.Ping
;
78 public string RetrieveParameters(string rawMessage
)
80 string param
= StripFirstColon(rawMessage
);
81 param
= param
.Substring(param
.IndexOf(' ')+1);
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);
95 int.TryParse(num
, out 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] != ':')
123 return rawString
.Substring(rawString
.IndexOf(' ')+1);