| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/textproto" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // The various control port StatusCode constants. |
| 10 | const ( |
| 11 | StatusOk = 250 |
| 12 | StatusOkUnnecessary = 251 |
| 13 | |
| 14 | StatusErrResourceExhausted = 451 |
| 15 | StatusErrSyntaxError = 500 |
| 16 | StatusErrUnrecognizedCmd = 510 |
| 17 | StatusErrUnimplementedCmd = 511 |
| 18 | StatusErrSyntaxErrorArg = 512 |
| 19 | StatusErrUnrecognizedCmdArg = 513 |
| 20 | StatusErrAuthenticationRequired = 514 |
| 21 | StatusErrBadAuthentication = 515 |
| 22 | StatusErrUnspecifiedTorError = 550 |
| 23 | StatusErrInternalError = 551 |
| 24 | StatusErrUnrecognizedEntity = 552 |
| 25 | StatusErrInvalidConfigValue = 553 |
| 26 | StatusErrInvalidDescriptor = 554 |
| 27 | StatusErrUnmanagedEntity = 555 |
| 28 | |
| 29 | StatusAsyncEvent = 650 |
| 30 | ) |
| 31 | |
| 32 | var statusCodeStringMap = map[int]string{ |
| 33 | StatusOk: "OK", |
| 34 | StatusOkUnnecessary: "Operation was unnecessary", |
| 35 | |
| 36 | StatusErrResourceExhausted: "Resource exhausted", |
| 37 | StatusErrSyntaxError: "Syntax error: protocol", |
| 38 | StatusErrUnrecognizedCmd: "Unrecognized command", |
| 39 | StatusErrUnimplementedCmd: "Unimplemented command", |
| 40 | StatusErrSyntaxErrorArg: "Syntax error in command argument", |
| 41 | StatusErrUnrecognizedCmdArg: "Unrecognized command argument", |
| 42 | StatusErrAuthenticationRequired: "Authentication required", |
| 43 | StatusErrBadAuthentication: "Bad authentication", |
| 44 | StatusErrUnspecifiedTorError: "Unspecified Tor error", |
| 45 | StatusErrInternalError: "Internal error", |
| 46 | StatusErrUnrecognizedEntity: "Unrecognized entity", |
| 47 | StatusErrInvalidConfigValue: "Invalid configuration value", |
| 48 | StatusErrInvalidDescriptor: "Invalid descriptor", |
| 49 | StatusErrUnmanagedEntity: "Unmanaged entity", |
| 50 | |
| 51 | StatusAsyncEvent: "Asynchronous event notification", |
| 52 | } |
| 53 | |
| 54 | func statusCodeToError(code int, reply string) *textproto.Error { |
| 55 | err := new(textproto.Error) |
| 56 | err.Code = code |
| 57 | if msg, ok := statusCodeStringMap[code]; ok { |
| 58 | trimmedReply := strings.TrimSpace(strings.TrimPrefix(reply, msg)) |
| 59 | err.Msg = fmt.Sprintf("%s: %s", msg, trimmedReply) |
| 60 | } else { |
| 61 | err.Msg = fmt.Sprintf("Unknown status code (%03d): %s", code, reply) |
| 62 | } |
| 63 | return err |
| 64 | } |