~hkdb/geary/disco-3.34.1

« back to all changes in this revision

Viewing changes to src/engine/imap/response/imap-response-code.vala

  • Committer: hkdb
  • Date: 2019-10-08 10:54:21 UTC
  • Revision ID: hkdb@3df.io-20191008105421-3dkwnpnhcamm77to
Tags: upstream-3.34.1-disco
ImportĀ upstreamĀ versionĀ 3.34.1-disco

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2016 Software Freedom Conservancy Inc.
 
2
 *
 
3
 * This software is licensed under the GNU Lesser General Public License
 
4
 * (version 2.1 or later).  See the COPYING file in this distribution.
 
5
 */
 
6
 
 
7
/**
 
8
 * A response code and additional information that optionally accompanies a {@link StatusResponse}.
 
9
 *
 
10
 * See [[http://tools.ietf.org/html/rfc3501#section-7.1]] for more information.
 
11
 */
 
12
 
 
13
public class Geary.Imap.ResponseCode : Geary.Imap.ListParameter {
 
14
    public ResponseCode() {
 
15
    }
 
16
 
 
17
    public ResponseCodeType get_response_code_type() throws ImapError {
 
18
        return new ResponseCodeType.from_parameter(get_as_string(0));
 
19
    }
 
20
 
 
21
    /**
 
22
     * Converts the {@link ResponseCode} into a UIDNEXT {@link UID}, if possible.
 
23
     *
 
24
     * @throws ImapError.INVALID if not UIDNEXT.
 
25
     */
 
26
    public UID get_uid_next() throws ImapError {
 
27
        if (!get_response_code_type().is_value(ResponseCodeType.UIDNEXT))
 
28
            throw new ImapError.INVALID("Not UIDNEXT: %s", to_string());
 
29
 
 
30
        return new UID.checked(get_as_string(1).as_int64());
 
31
    }
 
32
 
 
33
    /**
 
34
     * Converts the {@link ResponseCode} into a {@link UIDValidity}, if possible.
 
35
     *
 
36
     * @throws ImapError.INVALID if not UIDVALIDITY.
 
37
     */
 
38
    public UIDValidity get_uid_validity() throws ImapError {
 
39
        if (!get_response_code_type().is_value(ResponseCodeType.UIDVALIDITY))
 
40
            throw new ImapError.INVALID("Not UIDVALIDITY: %s", to_string());
 
41
 
 
42
        return new UIDValidity.checked(get_as_string(1).as_int64());
 
43
    }
 
44
 
 
45
    /**
 
46
     * Converts the {@link ResponseCode} into an UNSEEN value, if possible.
 
47
     *
 
48
     * @throws ImapError.INVALID if not UNSEEN.
 
49
     */
 
50
    public int get_unseen() throws ImapError {
 
51
        if (!get_response_code_type().is_value(ResponseCodeType.UNSEEN))
 
52
            throw new ImapError.INVALID("Not UNSEEN: %s", to_string());
 
53
 
 
54
        return get_as_string(1).as_int32(0, int.MAX);
 
55
    }
 
56
 
 
57
    /**
 
58
     * Converts the {@link ResponseCode} into PERMANENTFLAGS {@link MessageFlags}, if possible.
 
59
     *
 
60
     * @throws ImapError.INVALID if not PERMANENTFLAGS.
 
61
     */
 
62
    public MessageFlags get_permanent_flags() throws ImapError {
 
63
        if (!get_response_code_type().is_value(ResponseCodeType.PERMANENT_FLAGS))
 
64
            throw new ImapError.INVALID("Not PERMANENTFLAGS: %s", to_string());
 
65
 
 
66
        return MessageFlags.from_list(get_as_list(1));
 
67
    }
 
68
 
 
69
    /**
 
70
     * Parses the {@link ResponseCode} into {@link Capabilities}, if possible.
 
71
     *
 
72
     * Since Capabilities are revised with various {@link ClientSession} states, this method accepts
 
73
     * a ref to an int that will be incremented after handed to the Capabilities constructor.  This
 
74
     * can be used to track the revision of capabilities seen on the connection.
 
75
     *
 
76
     * @throws ImapError.INVALID if Capability was not specified.
 
77
     */
 
78
    public Capabilities get_capabilities(ref int next_revision) throws ImapError {
 
79
        if (!get_response_code_type().is_value(ResponseCodeType.CAPABILITY))
 
80
            throw new ImapError.INVALID("Not CAPABILITY response code: %s", to_string());
 
81
 
 
82
        Capabilities capabilities = new Capabilities(next_revision++);
 
83
        for (int ctr = 1; ctr < size; ctr++) {
 
84
            StringParameter? param = get_if_string(ctr);
 
85
            if (param != null)
 
86
                capabilities.add_parameter(param);
 
87
        }
 
88
 
 
89
        return capabilities;
 
90
    }
 
91
 
 
92
    /**
 
93
     * Parses the {@link ResponseCode} into UIDPLUS' COPYUID response, if possible.
 
94
     *
 
95
     * Note that the {@link UID}s are returned from the server in the order the messages
 
96
     * were copied.
 
97
     *
 
98
     * See [[http://tools.ietf.org/html/rfc4315#section-3]]
 
99
     *
 
100
     * @throws ImapError.INVALID if not COPYUID.
 
101
     */
 
102
    public void get_copyuid(out UIDValidity uidvalidity, out Gee.List<UID>? source_uids,
 
103
        out Gee.List<UID>? destination_uids) throws ImapError {
 
104
        if (!get_response_code_type().is_value(ResponseCodeType.COPYUID))
 
105
            throw new ImapError.INVALID("Not COPYUID response code: %s", to_string());
 
106
 
 
107
        uidvalidity = new UIDValidity.checked(get_as_number(1).as_int64());
 
108
        source_uids = MessageSet.uid_parse(get_as_string(2).ascii);
 
109
        destination_uids = MessageSet.uid_parse(get_as_string(3).ascii);
 
110
    }
 
111
 
 
112
    public override string to_string() {
 
113
        return "[%s]".printf(stringize_list());
 
114
    }
 
115
 
 
116
    public override void serialize(Serializer ser, GLib.Cancellable cancellable)
 
117
        throws GLib.Error {
 
118
        ser.push_ascii('[', cancellable);
 
119
        serialize_list(ser, cancellable);
 
120
        ser.push_ascii(']', cancellable);
 
121
    }
 
122
}