File: role.c

package info (click to toggle)
prayer 1.3.5-dfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,596 kB
  • sloc: ansic: 43,163; makefile: 817; sh: 445; perl: 166
file content (190 lines) | stat: -rw-r--r-- 5,771 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* $Cambridge: hermes/src/prayer/session/role.c,v 1.3 2008/09/16 09:59:58 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */

#include "prayer_session.h"

/* Little support routine, seems to turn up all over the place! */

static char *maybe_strdup(struct pool *p, char *s)
{
    return ((s) ? pool_strdup(p, s) : "");
}

/* role_add() ***********************************************************
 *
 * Add role to list
 *        rl: Role list
 *  personal: Personal name
 *      from: From address
 *  reply_to: Reply to address
 *       fcc: Default Fcc
 * signature: Signature
 *
 ***********************************************************************/

static void
role_add(struct list *rl,
         char *name,
         char *personal,
         char *from, char *reply_to, char *fcc, char *signature)
{
    struct role *new = pool_alloc(NIL, sizeof(struct role));

    new->next = NIL;
    new->personal = maybe_strdup(rl->pool, personal);
    new->from = maybe_strdup(rl->pool, from);
    new->reply_to = maybe_strdup(rl->pool, reply_to);
    new->fcc = maybe_strdup(rl->pool, fcc);
    new->signature = maybe_strdup(rl->pool, signature);

    list_push(rl, (struct list_item *) new, name);
}

/* role_update() ********************************************************
 *
 * Update existing role, or add new role to next of list if none exists.
 *        rl: Role list
 *  personal: Personal name
 *      from: From address
 *  reply_to: Reply to address
 *       fcc: Default Fcc
 * signature: Signature
 *
 ***********************************************************************/

void
role_update(struct list *rl,
            char *name,
            char *personal,
            char *from, char *reply_to, char *fcc, char *signature)
{
    struct list_item *li;

    if (!name)
        return;

    for (li = rl->head; li; li = li->next) {
        struct role *current = (struct role *) li;

        if (!strcmp(current->name, name)) {
            /* Replace existing entry */
            current->name = maybe_strdup(rl->pool, name);
            current->personal = maybe_strdup(rl->pool, personal);
            current->from = maybe_strdup(rl->pool, from);
            current->reply_to = maybe_strdup(rl->pool, reply_to);
            current->fcc = maybe_strdup(rl->pool, fcc);
            current->signature = maybe_strdup(rl->pool, signature);
            break;
        }
    }
    if (li == NIL)
        role_add(rl, name, personal, from, reply_to, fcc, signature);
}

/* ====================================================================== */

/* role_delete() ********************************************************
 *
 * Delete role
 *    rl: Role list
 *  name: Name of role to delete
 *
 ***********************************************************************/

void role_delete(struct list *rl, char *name)
{
    list_remove_byname(rl, name);
}

/* role_find() **********************************************************
 *
 * Lookup role
 *    rl: Role list
 *  name: Name of role to find
 *
 ***********************************************************************/

struct role *role_find(struct list *rl, char *name)
{
    return ((struct role *) list_lookup_byname(rl, name));
}

/* ====================================================================== */

/* role_parse_line() ****************************************************
 *
 * Parse a role from line of user preferences file
 *      rl: role list
 *    line: Line to parse
 * session: For logging purposes only
 *
 ***********************************************************************/

void role_parse_line(struct list *rl, char *line, struct session *session)
{
    char *list[6];
    char *name, *personal, *from, *reply_to, *fcc, *signature;

    if (!((list[0] = options_decode(string_get_token(&line))) &&
          (list[1] = options_decode(string_get_token(&line))) &&
          (list[2] = options_decode(string_get_token(&line))) &&
          (list[3] = options_decode(string_get_token(&line)))))
        return;

    if (line[0]) {
        list[4] = options_decode(string_get_token(&line));
        list[5] = options_decode(string_get_token(&line));

        name = list[0];
        personal = list[1];
        from = list[2];
        reply_to = list[3];
        fcc = list[4];
        signature = list[5];

    } else {
        name = list[0];         /* Backwards compatibility with old format */
        personal = list[1];
        from = "";
        reply_to = list[2];
        fcc = "";
        signature = list[3];

        session->options->save = T;
    }

    role_add(rl, name, personal, from, reply_to, fcc, signature);
}

/* role_print_options() *************************************************
 *
 * Convert role list to textual representation suitable for user
 * preferences file on IMAP server.
 *   rl: Role list to print
 *    b: Output buffer
 *
 ***********************************************************************/

void role_print_options(struct list *rl, struct buffer *b)
{
    struct list_item *li;
    struct role *role;

    for (li = rl->head; li; li = li->next) {
        unsigned long offset = 0L;
        role = (struct role *) li;

        options_print_token(b, role->name, &offset);
        options_print_token(b, role->personal, &offset);
        options_print_token(b, role->from, &offset);
        options_print_token(b, role->reply_to, &offset);
        options_print_token(b, role->fcc, &offset);
        options_print_token(b, role->signature, &offset);
        bputs(b, "" CRLF);
    }
}