* Now command like /c and /j works in any tabs (previously you had to be in the serve...
[circ.git] / Circ.Lib / Backend / Rfc.cs
blob752fea45634741d24e83549fd8234d17e4fe4082
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.Collections.Generic;
12 using System.Text;
14 namespace Circ.Backend
16 public static class Rfc
18 public const string Crlf = "\r\n";
19 public const int MaxSize = 512;
20 public const char OperatorChar = '@';
21 public const int IdentDefaultPort = 113;
22 public const string IdentAnswer = " : USERID : UNIX : ";
24 // Consts reply code
25 public const int NamesReply = 353;
26 public const int TopicReply = 332;
28 private static StringBuilder sb = new StringBuilder(30);
30 public static bool IsMessageTooLong(string message)
32 if (string.IsNullOrEmpty(message))
33 throw new ArgumentNullException("message");
35 return message.Length > MaxSize;
38 public static string[] AdaptMessageSize(string message)
40 if (!IsMessageTooLong(message))
41 return new string[] { message };
43 int overflow = message.Length - MaxSize;
44 IList<string> messageList = new List<string>();
45 string[] temp = null;
47 messageList.Add(message.Substring(0, MaxSize));
49 if (overflow > MaxSize)
50 foreach (string s in AdaptMessageSize(message.Substring(0, MaxSize + 1)))
51 messageList.Add(s);
52 else {
53 messageList.Add(message.Substring(0, message.LastIndexOf(':') + 1) + message.Substring(MaxSize));
54 temp = new string[messageList.Count];
55 messageList.CopyTo(temp, 0);
57 return temp;
60 /// <summary>
61 /// Concatenate a command and its parameter and add a trailing CRLF sequence.
62 /// </summary>
63 private static string FormatString(params object[] param)
65 sb.Remove(0, sb.Length);
66 int i = 0;
67 foreach (object s in param) {
68 sb.Append(s.ToString());
69 if (++i < param.Length)
70 sb.Append(' ');
72 sb.Append(Crlf);
73 return sb.ToString();
76 #region Standard command
77 public static string Pass(string password)
79 return FormatString("PASS", password);
82 public static string Nick(string nickname)
84 return FormatString("NICK", nickname);
87 public static string User(string name, string realName)
89 return FormatString("USER", name, name, "irc.epiknet.org", ':'+realName);
92 public static string Quit(string quitMessage)
94 if (string.IsNullOrEmpty(quitMessage))
95 return FormatString("QUIT");
96 return FormatString("QUIT", quitMessage);
99 public static string Pong(string args)
101 return FormatString("PONG", args);
104 public static string Join(string chan)
106 return FormatString("JOIN", chan);
109 public static string Part(string channel, string quitMessage)
111 if (string.IsNullOrEmpty(quitMessage))
112 return FormatString("PART", channel);
113 return FormatString("PART", channel, quitMessage);
116 public static string PrivMsg(string target, string mess)
118 return FormatString("PRIVMSG", target, ':'+mess);
120 #endregion