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