| 1085 | |
| 1086 | |
| 1087 | def _command(self, name, *args): |
| 1088 | |
| 1089 | if self.state not in Commands[name]: |
| 1090 | self.literal = None |
| 1091 | raise self.error("command %s illegal in state %s, " |
| 1092 | "only allowed in states %s" % |
| 1093 | (name, self.state, |
| 1094 | ', '.join(Commands[name]))) |
| 1095 | |
| 1096 | for typ in ('OK', 'NO', 'BAD'): |
| 1097 | if typ in self.untagged_responses: |
| 1098 | del self.untagged_responses[typ] |
| 1099 | |
| 1100 | if 'READ-ONLY' in self.untagged_responses \ |
| 1101 | and not self.is_readonly: |
| 1102 | raise self.readonly('mailbox status changed to READ-ONLY') |
| 1103 | |
| 1104 | tag = self._new_tag() |
| 1105 | name = bytes(name, self._encoding) |
| 1106 | data = tag + b' ' + name |
| 1107 | for arg in args: |
| 1108 | if arg is None: continue |
| 1109 | if isinstance(arg, str): |
| 1110 | arg = bytes(arg, self._encoding) |
| 1111 | data = data + b' ' + arg |
| 1112 | |
| 1113 | literal = self.literal |
| 1114 | if literal is not None: |
| 1115 | self.literal = None |
| 1116 | if type(literal) is type(self._command): |
| 1117 | literator = literal |
| 1118 | else: |
| 1119 | literator = None |
| 1120 | if self.utf8_enabled: |
| 1121 | data = data + bytes(' UTF8 (~{%s}' % len(literal), self._encoding) |
| 1122 | literal = literal + b')' |
| 1123 | else: |
| 1124 | data = data + bytes(' {%s}' % len(literal), self._encoding) |
| 1125 | |
| 1126 | if __debug__: |
| 1127 | if self.debug >= 4: |
| 1128 | self._mesg('> %r' % data) |
| 1129 | else: |
| 1130 | self._log('> %r' % data) |
| 1131 | |
| 1132 | try: |
| 1133 | self.send(data + CRLF) |
| 1134 | except OSError as val: |
| 1135 | raise self.abort('socket error: %s' % val) |
| 1136 | |
| 1137 | if literal is None: |
| 1138 | return tag |
| 1139 | |
| 1140 | while 1: |
| 1141 | # Wait for continuation response |
| 1142 | |
| 1143 | while self._get_response(): |
| 1144 | if self.tagged_commands[tag]: # BAD/NO? |