PageRenderTime 37ms CodeModel.GetById 16ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/ArgsXMPP/protocol/client/Error.cs

https://bitbucket.org/Lavir_the_Whiolet/yobabot
C# | 419 lines | 263 code | 25 blank | 131 comment | 42 complexity | 3da550baed4bf20f17401165c30407ae MD5 | raw file
  1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2 * Copyright (c) 2003-2008 by AG-Software 											 *
  3 * All Rights Reserved.																 *
  4 * Contact information for AG-Software is available at http://www.ag-software.de	 *
  5 *																					 *
  6 * Licence:																			 *
  7 * The agsXMPP SDK is released under a dual licence									 *
  8 * agsXMPP can be used under either of two licences									 *
  9 * 																					 *
 10 * A commercial licence which is probably the most appropriate for commercial 		 *
 11 * corporate use and closed source projects. 										 *
 12 *																					 *
 13 * The GNU Public License (GPL) is probably most appropriate for inclusion in		 *
 14 * other open source projects.														 *
 15 *																					 *
 16 * See README.html for details.														 *
 17 *																					 *
 18 * For general enquiries visit our website at:										 *
 19 * http://www.ag-software.de														 *
 20 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 21using System;
 22
 23using agsXMPP.Xml.Dom;
 24
 25// JEP-0086: Error Condition Mappings
 26
 27// <stanza-kind to='sender' type='error'>
 28// [RECOMMENDED to include sender XML here]
 29// <error type='error-type'>
 30// <defined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
 31// <text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'
 32// xml:lang='langcode'>
 33// OPTIONAL descriptive text
 34// </text>
 35// [OPTIONAL application-specific condition element]
 36// </error>
 37// </stanza-kind>
 38
 39// Legacy Error
 40// <error code="501">Not Implemented</error>
 41
 42// XMPP Style Error
 43// <error code='404' type='cancel'>
 44//		<item-not-found xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
 45// </error>
 46
 47namespace agsXMPP.protocol.client
 48{
 49	// XMPP error condition  		XMPP error type  	Legacy error code
 50	// <bad-request/> 				modify 				400
 51	// <conflict/> 					cancel 				409
 52	// <feature-not-implemented/> 	cancel 				501
 53	// <forbidden/> 				auth 				403
 54	// <gone/> 						modify 				302 (permanent)
 55	// <internal-server-error/> 	wait 				500
 56	// <item-not-found/> 			cancel 				404
 57	// <jid-malformed/> 			modify 				400
 58	// <not-acceptable/> 			modify 				406
 59	// <not-allowed/> 				cancel 				405
 60	// <not-authorized/> 			auth 				401
 61	// <payment-required/> 			auth 				402
 62	// <recipient-unavailable/> 	wait 				404
 63	// <redirect/> 					modify 				302 (temporary)
 64	// <registration-required/> 	auth 				407
 65	// <remote-server-not-found/> 	cancel 				404
 66	// <remote-server-timeout/> 	wait 				504
 67	// <resource-constraint/> 		wait 				500
 68	// <service-unavailable/> 		cancel 				503
 69	// <subscription-required/> 	auth 				407
 70	// <undefined-condition/> 		[any] 				500
 71	// <unexpected-request/> 		wait 				400
 72
 73	public enum ErrorCondition
 74	{
 75		BadRequest,
 76		Conflict,
 77		FeatureNotImplemented,
 78		Forbidden,
 79		Gone,
 80		InternalServerError,
 81		ItemNotFound,
 82		JidMalformed,
 83		NotAcceptable,
 84		NotAllowed,
 85		NotAuthorized,
 86		PaymentRequired,
 87		RecipientUnavailable,
 88		Redirect,
 89		RegistrationRequired,
 90		RemoteServerNotFound,
 91		RemoteServerTimeout,
 92		ResourceConstraint,
 93		ServiceUnavailable,
 94		SubscriptionRequired,
 95		UndefinedCondition,
 96		UnexpectedRequest		
 97	}
 98
 99	// The value of the <error/> element's 'type' attribute MUST be one of the following:
100	// * cancel -- do not retry (the error is unrecoverable)
101	// * continue -- proceed (the condition was only a warning)
102	// * modify -- retry after changing the data sent
103	// * auth -- retry after providing credentials
104	// * wait -- retry after waiting (the error is temporary)
105	public enum ErrorType
106	{
107		cancel,
108		@continue,
109		modify,
110		auth,
111		wait
112	}
113
114
115	/// <summary>
116	/// The legacy Error Code
117	/// </summary>
118	public enum ErrorCode
119	{		
120		/// <summary>
121		/// Bad request
122		/// </summary>
123		BadRequest				= 400,
124		/// <summary>
125		/// Unauthorized
126		/// </summary>
127		Unauthorized			= 401,
128		/// <summary>
129		/// Payment required
130		/// </summary>
131		PaymentRequired			= 402,
132		/// <summary>
133		/// Forbidden
134		/// </summary>
135		Forbidden				= 403,
136		/// <summary>
137		/// Not found
138		/// </summary>
139		NotFound				= 404,
140		/// <summary>
141		/// Not allowed
142		/// </summary>
143		NotAllowed				= 405,
144		/// <summary>
145		/// Not acceptable
146		/// </summary>
147		NotAcceptable			= 406,
148		/// <summary>
149		/// Registration required 
150		/// </summary>
151		RegistrationRequired	= 407,
152		/// <summary>
153		/// Request timeout
154		/// </summary>
155		RequestTimeout			= 408,
156		/// <summary>
157		/// Conflict
158		/// </summary>
159		Conflict                = 409,
160		/// <summary>
161		/// Internal server error
162		/// </summary>
163		InternalServerError		= 500,
164		/// <summary>
165		/// Not implemented
166		/// </summary>
167		NotImplemented			= 501,
168		/// <summary>
169		/// Remote server error
170		/// </summary>
171		RemoteServerError		= 502,
172		/// <summary>
173		/// Service unavailable
174		/// </summary>
175		ServiceUnavailable		= 503,
176		/// <summary>
177		/// Remote server timeout
178		/// </summary>
179		RemoteServerTimeout		= 504,
180		/// <summary>
181		/// Disconnected
182		/// </summary>
183		Disconnected            = 510
184	}
185
186	
187	/// <summary>
188	/// Summary description for Error.
189	/// </summary>
190	public class Error : Element
191	{
192
193		#region << Constructors >>
194		public Error()
195		{
196            this.Namespace  = Uri.CLIENT;
197			this.TagName    = "error";
198		}
199
200
201		public Error(int code) : this()
202		{			
203			this.SetAttribute("code", code.ToString());
204		}
205
206		public Error(ErrorCode code) : this()
207		{			
208			this.SetAttribute("code", (int) code);
209		}
210
211		public Error(ErrorType type) : this()
212		{
213			Type = type;
214		}
215
216		/// <summary>
217		/// Creates an error Element according the the condition
218		/// The type attrib as added automatically as decribed in the XMPP specs
219		/// This is the prefered way to create error Elements
220		/// </summary>
221		/// <param name="condition"></param>
222		public Error(ErrorCondition condition) : this()
223		{			
224			this.Condition	= condition;
225		}
226
227        public Error(ErrorType type, ErrorCondition condition) : this(type)
228        {
229            this.Condition = condition;
230        }
231		#endregion
232
233		/// <summary>
234		/// The error Description
235		/// </summary>
236		public string Message
237		{
238			get
239			{
240				return this.Value;
241			}
242			set
243			{
244				this.Value = value;
245			}
246		}
247
248		public ErrorCode Code
249		{
250			get
251			{
252				return (ErrorCode) GetAttributeInt("code");
253			}
254			set
255			{
256				SetAttribute("code", (int) value);
257			}
258		}
259       
260		public ErrorType Type
261		{
262			get
263			{
264				return (ErrorType) GetAttributeEnum("type", typeof(ErrorType));
265			}
266			set
267			{
268				SetAttribute("type", value.ToString());
269			}
270		}
271
272		public ErrorCondition Condition
273		{
274			get
275			{
276				if (HasTag("bad-request"))					// <bad-request/> 
277					return ErrorCondition.BadRequest;
278				else if (HasTag("conflict"))				// <conflict/> 
279					return ErrorCondition.Conflict;
280				else if  (HasTag("feature-not-implemented"))// <feature-not-implemented/>
281					return ErrorCondition.FeatureNotImplemented;
282				else if (HasTag("forbidden"))				// <forbidden/> 
283					return ErrorCondition.Forbidden;
284				else if (HasTag("gone"))					// <gone/>
285					return ErrorCondition.Gone;
286				else if (HasTag("internal-server-error"))	// <internal-server-error/>
287					return ErrorCondition.InternalServerError;
288				else if (HasTag("item-not-found"))			// <item-not-found/> 
289					return ErrorCondition.ItemNotFound;
290				else if (HasTag("jid-malformed"))			// <jid-malformed/>
291					return ErrorCondition.JidMalformed;
292				else if (HasTag("not-acceptable"))			// <not-acceptable/> 
293					return ErrorCondition.NotAcceptable;
294				else if (HasTag("not-authorized"))			// <not-authorized/>
295					return ErrorCondition.NotAuthorized;
296				else if (HasTag("payment-required"))		// <payment-required/>
297					return ErrorCondition.PaymentRequired;
298				else if (HasTag("recipient-unavailable"))	// <recipient-unavailable/>
299					return ErrorCondition.RecipientUnavailable;
300				else if (HasTag("redirect"))				// <redirect/>
301					return ErrorCondition.Redirect;
302				else if (HasTag("registration-required"))	// <registration-required/>
303					return ErrorCondition.RegistrationRequired;
304				else if (HasTag("remote-server-not-found"))	// <remote-server-not-found/> 
305					return ErrorCondition.RemoteServerNotFound;
306				else if (HasTag("remote-server-timeout"))	// <remote-server-timeout/> 
307					return ErrorCondition.RemoteServerTimeout;	
308				else if (HasTag("resource-constraint"))		// <resource-constraint/>
309					return ErrorCondition.ResourceConstraint;	
310				else if (HasTag("service-unavailable"))		// <service-unavailable/> 
311					return ErrorCondition.ServiceUnavailable;
312				else if (HasTag("subscription-required"))	// <subscription-required/> 
313					return ErrorCondition.SubscriptionRequired;
314				else if (HasTag("undefined-condition"))		// <undefined-condition/> 
315					return ErrorCondition.UndefinedCondition;
316				else if (HasTag("unexpected-request"))		// <unexpected-request/> 
317					return ErrorCondition.UnexpectedRequest;
318				else
319 					return ErrorCondition.UndefinedCondition;
320					
321			}
322			set
323			{
324				switch (value)
325				{
326					case ErrorCondition.BadRequest:
327						SetTag("bad-request",				"", Uri.STANZAS);
328						Type = ErrorType.modify;
329						break;
330					case ErrorCondition.Conflict:
331						SetTag("conflict",					"", Uri.STANZAS);
332						Type = ErrorType.cancel;
333						break;
334					case ErrorCondition.FeatureNotImplemented:
335						SetTag("feature-not-implemented",	"", Uri.STANZAS);
336						Type = ErrorType.cancel;
337						break;
338					case ErrorCondition.Forbidden:
339						SetTag("forbidden",					"", Uri.STANZAS);
340						Type = ErrorType.auth;
341						break;
342					case ErrorCondition.Gone:
343						SetTag("gone",						"", Uri.STANZAS);
344						Type = ErrorType.modify;
345						break;
346					case ErrorCondition.InternalServerError:
347						SetTag("internal-server-error",		"", Uri.STANZAS);
348						Type = ErrorType.wait;
349						break;
350					case ErrorCondition.ItemNotFound:
351						SetTag("item-not-found",			"", Uri.STANZAS);
352						Type = ErrorType.cancel;
353						break;
354					case ErrorCondition.JidMalformed:
355						SetTag("jid-malformed",				"", Uri.STANZAS);
356						Type = ErrorType.modify;
357						break;
358					case ErrorCondition.NotAcceptable:
359						SetTag("not-acceptable",			"", Uri.STANZAS);
360						Type = ErrorType.modify;
361						break;
362					case ErrorCondition.NotAllowed:
363						SetTag("not-allowed",				"", Uri.STANZAS);
364						Type = ErrorType.cancel;
365						break;
366					case ErrorCondition.NotAuthorized:
367						SetTag("not-authorized",			"", Uri.STANZAS);
368						Type = ErrorType.auth;
369						break;
370					case ErrorCondition.PaymentRequired:
371						SetTag("payment-required",			"", Uri.STANZAS);
372						Type = ErrorType.auth;
373						break;
374					case ErrorCondition.RecipientUnavailable:
375						SetTag("recipient-unavailable",		"", Uri.STANZAS);
376						Type = ErrorType.wait;
377						break;
378					case ErrorCondition.Redirect:
379						SetTag("redirect",					"", Uri.STANZAS);
380						Type = ErrorType.modify;
381						break;
382					case ErrorCondition.RegistrationRequired:
383						SetTag("registration-required",		"", Uri.STANZAS);
384						Type = ErrorType.auth;
385						break;
386					case ErrorCondition.RemoteServerNotFound:
387						SetTag("remote-server-not-found",	"", Uri.STANZAS);
388						Type = ErrorType.cancel;
389						break;
390					case ErrorCondition.RemoteServerTimeout:
391						SetTag("remote-server-timeout",		"", Uri.STANZAS);
392						Type = ErrorType.wait;
393						break;
394					case ErrorCondition.ResourceConstraint:	
395						SetTag("resource-constraint",		"", Uri.STANZAS);
396						Type = ErrorType.wait;
397						break;
398					case ErrorCondition.ServiceUnavailable:
399						SetTag("service-unavailable",		"", Uri.STANZAS);
400						Type = ErrorType.cancel;
401						break;
402					case ErrorCondition.SubscriptionRequired:
403						SetTag("subscription-required",		"", Uri.STANZAS);
404						Type = ErrorType.auth;
405						break;
406					case ErrorCondition.UndefinedCondition:
407						SetTag("undefined-condition",		"", Uri.STANZAS);
408						// could be any
409						break;
410					case ErrorCondition.UnexpectedRequest:
411						SetTag("unexpected-request",		"", Uri.STANZAS);
412						Type = ErrorType.wait;
413						break;
414
415				}
416			}
417		}		
418	}
419}