annotate src/pop3/client.c @ 6940:414c9d631a81 HEAD

Replaced t_push/t_pop calls with T_FRAME*() macros.
author Timo Sirainen <tss@iki.fi>
date Wed, 05 Dec 2007 17:47:44 +0200
parents c62f30cc1153
children 7ed926ed7aa4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6429
65c69a53a7be Replaced my Copyright notices. The year range always ends with 2007 now.
Timo Sirainen <tss@iki.fi>
parents: 6414
diff changeset
1 /* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "common.h"
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
4 #include "buffer.h"
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "ioloop.h"
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "network.h"
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7 #include "istream.h"
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 #include "ostream.h"
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
9 #include "str.h"
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
10 #include "var-expand.h"
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 #include "mail-storage.h"
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
12 #include "commands.h"
1845
bd8b6ed35327 Removed fetch_init/fetch_next from mail-storage. search_* makes it
Timo Sirainen <tss@iki.fi>
parents: 1672
diff changeset
13 #include "mail-search.h"
5500
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
14 #include "mail-namespace.h"
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 #include <stdlib.h>
3960
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
17 #include <unistd.h>
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18
1053
28f606ada24a Reduce input buffer size
Timo Sirainen <tss@iki.fi>
parents: 1044
diff changeset
19 /* max. length of input command line (spec says 512) */
28f606ada24a Reduce input buffer size
Timo Sirainen <tss@iki.fi>
parents: 1044
diff changeset
20 #define MAX_INBUF_SIZE 2048
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
21
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
22 /* Stop reading input when output buffer has this many bytes. Once the buffer
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
23 size has dropped to half of it, start reading input again. */
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
24 #define OUTBUF_THROTTLE_SIZE 4096
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
25
2927
6e2129a4a595 Timeout changes. Default idle timeout is now 10 minutes instead of 30
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
26 /* If we can't send anything for 10 minutes, disconnect the client */
6e2129a4a595 Timeout changes. Default idle timeout is now 10 minutes instead of 30
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
27 #define CLIENT_OUTPUT_TIMEOUT (10*60)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 /* Disconnect client when it sends too many bad commands in a row */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 #define CLIENT_MAX_BAD_COMMANDS 20
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
31
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32 /* Disconnect client after idling this many seconds */
2927
6e2129a4a595 Timeout changes. Default idle timeout is now 10 minutes instead of 30
Timo Sirainen <tss@iki.fi>
parents: 2790
diff changeset
33 #define CLIENT_IDLE_TIMEOUT (10*60)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 static struct client *my_client; /* we don't need more than one currently */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 static struct timeout *to_idle;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
37
4907
5b4c9b20eba0 Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
38 static void client_input(struct client *client);
5b4c9b20eba0 Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
39 static int client_output(struct client *client);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
40
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
41 static bool init_mailbox(struct client *client, const char **error_r)
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
42 {
1845
bd8b6ed35327 Removed fetch_init/fetch_next from mail-storage. search_* makes it
Timo Sirainen <tss@iki.fi>
parents: 1672
diff changeset
43 struct mail_search_arg search_arg;
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents: 1845
diff changeset
44 struct mailbox_transaction_context *t;
1845
bd8b6ed35327 Removed fetch_init/fetch_next from mail-storage. search_* makes it
Timo Sirainen <tss@iki.fi>
parents: 1672
diff changeset
45 struct mail_search_context *ctx;
2976
96a4ab34c8f1 Added pop3_uidl_format setting.
Timo Sirainen <tss@iki.fi>
parents: 2927
diff changeset
46 struct mailbox_status status;
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
47 struct mail *mail;
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
48 buffer_t *message_sizes_buf;
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
49 uint32_t failed_uid = 0;
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6279
diff changeset
50 uoff_t size;
3863
55df57c028d4 Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents: 3517
diff changeset
51 int i;
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
52 bool failed, expunged;
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
53
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
54 message_sizes_buf = buffer_create_dynamic(default_pool, 512);
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
55
1845
bd8b6ed35327 Removed fetch_init/fetch_next from mail-storage. search_* makes it
Timo Sirainen <tss@iki.fi>
parents: 1672
diff changeset
56 memset(&search_arg, 0, sizeof(search_arg));
bd8b6ed35327 Removed fetch_init/fetch_next from mail-storage. search_* makes it
Timo Sirainen <tss@iki.fi>
parents: 1672
diff changeset
57 search_arg.type = SEARCH_ALL;
bd8b6ed35327 Removed fetch_init/fetch_next from mail-storage. search_* makes it
Timo Sirainen <tss@iki.fi>
parents: 1672
diff changeset
58
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
59 for (i = 0; i < 2; i++) {
6652
c62f30cc1153 Fixed using uninitialized variable in error handling.
Timo Sirainen <tss@iki.fi>
parents: 6512
diff changeset
60 expunged = FALSE;
6463
aeee5076f99f Use mailbox_sync() instead of our own implementation.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
61 if (mailbox_sync(client->mailbox, MAILBOX_SYNC_FLAG_FULL_READ,
aeee5076f99f Use mailbox_sync() instead of our own implementation.
Timo Sirainen <tss@iki.fi>
parents: 6429
diff changeset
62 STATUS_UIDVALIDITY, &status) < 0) {
2322
aae574ed7f4c Broke mailbox_sync() into iterator.
Timo Sirainen <tss@iki.fi>
parents: 2238
diff changeset
63 client_send_storage_error(client);
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
64 break;
2092
952a4106d3b8 assert fix
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
65 }
2976
96a4ab34c8f1 Added pop3_uidl_format setting.
Timo Sirainen <tss@iki.fi>
parents: 2927
diff changeset
66 client->uid_validity = status.uidvalidity;
2092
952a4106d3b8 assert fix
Timo Sirainen <tss@iki.fi>
parents: 2039
diff changeset
67
3209
923ff19873d4 Major mail-storage API changes. It's now a bit cleaner and much more plugin
Timo Sirainen <tss@iki.fi>
parents: 3016
diff changeset
68 t = mailbox_transaction_begin(client->mailbox, 0);
923ff19873d4 Major mail-storage API changes. It's now a bit cleaner and much more plugin
Timo Sirainen <tss@iki.fi>
parents: 3016
diff changeset
69 ctx = mailbox_search_init(t, NULL, &search_arg, NULL);
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
70
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
71 client->last_seen = 0;
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
72 client->total_size = 0;
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
73 buffer_set_used_size(message_sizes_buf, 0);
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
74
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
75 failed = FALSE;
3209
923ff19873d4 Major mail-storage API changes. It's now a bit cleaner and much more plugin
Timo Sirainen <tss@iki.fi>
parents: 3016
diff changeset
76 mail = mail_alloc(t, MAIL_FETCH_VIRTUAL_SIZE, NULL);
923ff19873d4 Major mail-storage API changes. It's now a bit cleaner and much more plugin
Timo Sirainen <tss@iki.fi>
parents: 3016
diff changeset
77 while (mailbox_search_next(ctx, mail) > 0) {
6280
eb7c9d8ece54 mail_*() APIs changed to return int and return the actual data as pointer.
Timo Sirainen <tss@iki.fi>
parents: 6279
diff changeset
78 if (mail_get_virtual_size(mail, &size) < 0) {
6312
05aabdc581b1 Fix to last change: Don't continue if we can't get message's size.
Timo Sirainen <tss@iki.fi>
parents: 6311
diff changeset
79 expunged = mail->expunged;
05aabdc581b1 Fix to last change: Don't continue if we can't get message's size.
Timo Sirainen <tss@iki.fi>
parents: 6311
diff changeset
80 failed = TRUE;
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
81 if (failed_uid == mail->uid) {
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
82 i_error("Getting size of message "
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
83 "UID=%u failed", mail->uid);
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
84 break;
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
85 }
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
86 failed_uid = mail->uid;
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
87 break;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
88 }
2621
c6cc163344c3 Added pop3_enable_last setting to enable deprecated LAST command.
Timo Sirainen <tss@iki.fi>
parents: 2525
diff changeset
89
3209
923ff19873d4 Major mail-storage API changes. It's now a bit cleaner and much more plugin
Timo Sirainen <tss@iki.fi>
parents: 3016
diff changeset
90 if ((mail_get_flags(mail) & MAIL_SEEN) != 0)
2621
c6cc163344c3 Added pop3_enable_last setting to enable deprecated LAST command.
Timo Sirainen <tss@iki.fi>
parents: 2525
diff changeset
91 client->last_seen = mail->seq;
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
92 client->total_size += size;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
93
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
94 buffer_append(message_sizes_buf, &size, sizeof(size));
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
95 }
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
96 client->messages_count =
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
97 message_sizes_buf->used / sizeof(uoff_t);
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
98
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
99 mail_free(&mail);
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
100 if (mailbox_search_deinit(&ctx) < 0 || (failed && !expunged)) {
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
101 client_send_storage_error(client);
6512
1a3604c8ee05 mailbox_transaction_commit*() doesn't sync the mailbox anymore, so it
Timo Sirainen <tss@iki.fi>
parents: 6463
diff changeset
102 (void)mailbox_transaction_commit(&t);
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
103 break;
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
104 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
105
2010
01d2eb10a1c9 index wasn't unlocked initially
Timo Sirainen <tss@iki.fi>
parents: 1994
diff changeset
106 if (!failed) {
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
107 client->trans = t;
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
108 client->message_sizes =
6414
a6a49d5efc59 Changed buffer_free() and buffer_free_without_data() APIs to take ** pointer
Timo Sirainen <tss@iki.fi>
parents: 6411
diff changeset
109 buffer_free_without_data(&message_sizes_buf);
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
110 return TRUE;
2010
01d2eb10a1c9 index wasn't unlocked initially
Timo Sirainen <tss@iki.fi>
parents: 1994
diff changeset
111 }
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
112
5761
8008fbb660c1 Even if mailbox sync fails, commit the transaction so that cache file gets
Timo Sirainen <tss@iki.fi>
parents: 5613
diff changeset
113 /* well, sync and try again. we might have cached virtual
8008fbb660c1 Even if mailbox sync fails, commit the transaction so that cache file gets
Timo Sirainen <tss@iki.fi>
parents: 5613
diff changeset
114 sizes, make sure they get committed. */
6512
1a3604c8ee05 mailbox_transaction_commit*() doesn't sync the mailbox anymore, so it
Timo Sirainen <tss@iki.fi>
parents: 6463
diff changeset
115 (void)mailbox_transaction_commit(&t);
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
116 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
117
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
118 if (expunged) {
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
119 client_send_line(client,
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
120 "-ERR [IN-USE] Couldn't sync mailbox.");
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
121 *error_r = "Can't sync mailbox: Messages keep getting expunged";
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
122 } else {
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
123 struct mail_storage *storage = client->inbox_ns->storage;
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
124 enum mail_error error;
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
125
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
126 *error_r = mail_storage_get_last_error(storage, &error);
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
127 }
6414
a6a49d5efc59 Changed buffer_free() and buffer_free_without_data() APIs to take ** pointer
Timo Sirainen <tss@iki.fi>
parents: 6411
diff changeset
128 buffer_free(&message_sizes_buf);
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
129 return FALSE;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
130 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
131
3960
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
132 struct client *client_create(int fd_in, int fd_out,
5500
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
133 struct mail_namespace *namespaces)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
134 {
5500
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
135 struct mail_storage *storage;
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
136 const char *inbox;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 struct client *client;
2039
f0925b2271e1 Added pop3_mails_keep_recent option. Fixed recent assert crash.
Timo Sirainen <tss@iki.fi>
parents: 2020
diff changeset
138 enum mailbox_open_flags flags;
5178
bbe25ef34f03 If INBOX can't be opened, give better error messages. The reason is never
Timo Sirainen <tss@iki.fi>
parents: 5102
diff changeset
139 const char *errmsg;
5613
f717fb4b31c0 Error handling rewrite.
Timo Sirainen <tss@iki.fi>
parents: 5608
diff changeset
140 enum mail_error error;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
141
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
142 /* always use nonblocking I/O */
3960
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
143 net_set_nonblock(fd_in, TRUE);
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
144 net_set_nonblock(fd_out, TRUE);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
145
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 client = i_new(struct client, 1);
3960
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
147 client->fd_in = fd_in;
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
148 client->fd_out = fd_out;
6162
896cc473c1f0 Renamed i_stream_create_file() to i_stream_create_fd().
Timo Sirainen <tss@iki.fi>
parents: 6161
diff changeset
149 client->input = i_stream_create_fd(fd_in, MAX_INBUF_SIZE, FALSE);
6161
c62f7ee79446 Split o_stream_create_file() to _create_fd() and _create_fd_file().
Timo Sirainen <tss@iki.fi>
parents: 6142
diff changeset
150 client->output = o_stream_create_fd(fd_out, (size_t)-1, FALSE);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
151 o_stream_set_flush_callback(client->output, client_output, client);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152
3960
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
153 client->io = io_add(fd_in, IO_READ, client_input, client);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
154 client->last_input = ioloop_time;
5500
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
155
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
156 client->namespaces = namespaces;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
157
5500
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
158 inbox = "INBOX";
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
159 client->inbox_ns = mail_namespace_find(namespaces, &inbox);
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
160 if (client->inbox_ns == NULL) {
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
161 client_send_line(client, "-ERR No INBOX namespace for user.");
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
162 client_destroy(client, "No INBOX namespace for user.");
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
163 return NULL;
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
164 }
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
165
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
166 storage = client->inbox_ns->storage;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167
2039
f0925b2271e1 Added pop3_mails_keep_recent option. Fixed recent assert crash.
Timo Sirainen <tss@iki.fi>
parents: 2020
diff changeset
168 flags = 0;
2719
f8adc5cb2508 Renamed pop3_mails_keep_recent to pop3_no_flag_updates which includes
Timo Sirainen <tss@iki.fi>
parents: 2712
diff changeset
169 if (no_flag_updates)
2039
f0925b2271e1 Added pop3_mails_keep_recent option. Fixed recent assert crash.
Timo Sirainen <tss@iki.fi>
parents: 2020
diff changeset
170 flags |= MAILBOX_OPEN_KEEP_RECENT;
4153
690c72358cd5 Added pop3_lock_session setting.
Timo Sirainen <tss@iki.fi>
parents: 4118
diff changeset
171 if (lock_session)
690c72358cd5 Added pop3_lock_session setting.
Timo Sirainen <tss@iki.fi>
parents: 4118
diff changeset
172 flags |= MAILBOX_OPEN_KEEP_LOCKED;
3245
6491dab63e54 Added input stream parameter to mailbox_open(). With mbox it now allows
Timo Sirainen <tss@iki.fi>
parents: 3209
diff changeset
173 client->mailbox = mailbox_open(storage, "INBOX", NULL, flags);
1363
383e4b9e347c Crashfix if there was some errors while opening mailbox
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
174 if (client->mailbox == NULL) {
5178
bbe25ef34f03 If INBOX can't be opened, give better error messages. The reason is never
Timo Sirainen <tss@iki.fi>
parents: 5102
diff changeset
175 errmsg = t_strdup_printf("Couldn't open INBOX: %s",
bbe25ef34f03 If INBOX can't be opened, give better error messages. The reason is never
Timo Sirainen <tss@iki.fi>
parents: 5102
diff changeset
176 mail_storage_get_last_error(storage,
5613
f717fb4b31c0 Error handling rewrite.
Timo Sirainen <tss@iki.fi>
parents: 5608
diff changeset
177 &error));
5178
bbe25ef34f03 If INBOX can't be opened, give better error messages. The reason is never
Timo Sirainen <tss@iki.fi>
parents: 5102
diff changeset
178 i_error("%s", errmsg);
bbe25ef34f03 If INBOX can't be opened, give better error messages. The reason is never
Timo Sirainen <tss@iki.fi>
parents: 5102
diff changeset
179 client_send_line(client, "-ERR [IN-USE] %s", errmsg);
bbe25ef34f03 If INBOX can't be opened, give better error messages. The reason is never
Timo Sirainen <tss@iki.fi>
parents: 5102
diff changeset
180 client_destroy(client, "Couldn't open INBOX");
1363
383e4b9e347c Crashfix if there was some errors while opening mailbox
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
181 return NULL;
383e4b9e347c Crashfix if there was some errors while opening mailbox
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
182 }
383e4b9e347c Crashfix if there was some errors while opening mailbox
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
183
6311
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
184 if (!init_mailbox(client, &errmsg)) {
e84fad8d3987 Handle initial syncing errors better.
Timo Sirainen <tss@iki.fi>
parents: 6280
diff changeset
185 i_error("Couldn't init INBOX: %s", errmsg);
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
186 client_destroy(client, "Mailbox init failed");
1363
383e4b9e347c Crashfix if there was some errors while opening mailbox
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
187 return NULL;
383e4b9e347c Crashfix if there was some errors while opening mailbox
Timo Sirainen <tss@iki.fi>
parents: 1059
diff changeset
188 }
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
189
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
190 i_assert(my_client == NULL);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
191 my_client = client;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192
1646
a7c742b940e5 Added pop3 hooks
Timo Sirainen <tss@iki.fi>
parents: 1640
diff changeset
193 if (hook_client_created != NULL)
a7c742b940e5 Added pop3 hooks
Timo Sirainen <tss@iki.fi>
parents: 1640
diff changeset
194 hook_client_created(&client);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 return client;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
196 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
197
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
198 static const char *client_stats(struct client *client)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 {
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
200 static struct var_expand_table static_tab[] = {
4118
b66da5c1a94b Replaced %T and %R with %p and %b. %R was already used by string-reversion,
Timo Sirainen <tss@iki.fi>
parents: 4096
diff changeset
201 { 'p', NULL },
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
202 { 't', NULL },
4118
b66da5c1a94b Replaced %T and %R with %p and %b. %R was already used by string-reversion,
Timo Sirainen <tss@iki.fi>
parents: 4096
diff changeset
203 { 'b', NULL },
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
204 { 'r', NULL },
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
205 { 'd', NULL },
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
206 { 'm', NULL },
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
207 { 's', NULL },
5979
433745f10290 Added %i and %o for input/output bytes to pop3_logout_format.
Timo Sirainen <tss@iki.fi>
parents: 5761
diff changeset
208 { 'i', NULL },
433745f10290 Added %i and %o for input/output bytes to pop3_logout_format.
Timo Sirainen <tss@iki.fi>
parents: 5761
diff changeset
209 { 'o', NULL },
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
210 { '\0', NULL }
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
211 };
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
212 struct var_expand_table *tab;
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
213 string_t *str;
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
214
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
215 tab = t_malloc(sizeof(static_tab));
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
216 memcpy(tab, static_tab, sizeof(static_tab));
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
217
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
218 tab[0].value = dec2str(client->top_bytes);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
219 tab[1].value = dec2str(client->top_count);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
220 tab[2].value = dec2str(client->retr_bytes);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
221 tab[3].value = dec2str(client->retr_count);
4566
aa9109a17db6 Show number of actually expunged messages in logout message, instead of
Timo Sirainen <tss@iki.fi>
parents: 4153
diff changeset
222 tab[4].value = dec2str(client->expunged_count);
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
223 tab[5].value = dec2str(client->messages_count);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
224 tab[6].value = dec2str(client->total_size);
5979
433745f10290 Added %i and %o for input/output bytes to pop3_logout_format.
Timo Sirainen <tss@iki.fi>
parents: 5761
diff changeset
225 tab[7].value = dec2str(client->input->v_offset);
433745f10290 Added %i and %o for input/output bytes to pop3_logout_format.
Timo Sirainen <tss@iki.fi>
parents: 5761
diff changeset
226 tab[8].value = dec2str(client->output->offset);
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
227
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
228 str = t_str_new(128);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
229 var_expand(str, logout_format, tab);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
230 return str_c(str);
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
231 }
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
232
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
233 void client_destroy(struct client *client, const char *reason)
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
234 {
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
235 if (!client->disconnected) {
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
236 if (reason == NULL)
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
237 reason = "Disconnected";
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
238 i_info("%s %s", reason, client_stats(client));
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
239 }
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
240
2502
e628e0ab8924 Deinitialize pop3 commands if connection is closed unexpectedly.
Timo Sirainen <tss@iki.fi>
parents: 2494
diff changeset
241 if (client->cmd != NULL) {
e628e0ab8924 Deinitialize pop3 commands if connection is closed unexpectedly.
Timo Sirainen <tss@iki.fi>
parents: 2494
diff changeset
242 /* deinitialize command */
e628e0ab8924 Deinitialize pop3 commands if connection is closed unexpectedly.
Timo Sirainen <tss@iki.fi>
parents: 2494
diff changeset
243 i_stream_close(client->input);
e628e0ab8924 Deinitialize pop3 commands if connection is closed unexpectedly.
Timo Sirainen <tss@iki.fi>
parents: 2494
diff changeset
244 o_stream_close(client->output);
e628e0ab8924 Deinitialize pop3 commands if connection is closed unexpectedly.
Timo Sirainen <tss@iki.fi>
parents: 2494
diff changeset
245 client->cmd(client);
2695
Timo Sirainen <tss@iki.fi>
parents: 2665
diff changeset
246 i_assert(client->cmd == NULL);
2502
e628e0ab8924 Deinitialize pop3 commands if connection is closed unexpectedly.
Timo Sirainen <tss@iki.fi>
parents: 2494
diff changeset
247 }
5102
f8c2cf9e3267 Commit the transaction even if client didn't QUIT so cached data gets saved.
Timo Sirainen <tss@iki.fi>
parents: 5091
diff changeset
248 if (client->trans != NULL) {
f8c2cf9e3267 Commit the transaction even if client didn't QUIT so cached data gets saved.
Timo Sirainen <tss@iki.fi>
parents: 5091
diff changeset
249 /* client didn't QUIT, but we still want to save any changes
f8c2cf9e3267 Commit the transaction even if client didn't QUIT so cached data gets saved.
Timo Sirainen <tss@iki.fi>
parents: 5091
diff changeset
250 done in this transaction. especially the cached virtual
f8c2cf9e3267 Commit the transaction even if client didn't QUIT so cached data gets saved.
Timo Sirainen <tss@iki.fi>
parents: 5091
diff changeset
251 message sizes. */
6512
1a3604c8ee05 mailbox_transaction_commit*() doesn't sync the mailbox anymore, so it
Timo Sirainen <tss@iki.fi>
parents: 6463
diff changeset
252 (void)mailbox_transaction_commit(&client->trans);
5102
f8c2cf9e3267 Commit the transaction even if client didn't QUIT so cached data gets saved.
Timo Sirainen <tss@iki.fi>
parents: 5091
diff changeset
253 }
2722
8c5bcd6585a4 Use only a single transaction for the whole duration of pop3 session. Avoids
Timo Sirainen <tss@iki.fi>
parents: 2719
diff changeset
254 if (client->mailbox != NULL)
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
255 mailbox_close(&client->mailbox);
5500
4862cb37106c Moved namespace handling to lib-storage. Beginnings of namespace support for
Timo Sirainen <tss@iki.fi>
parents: 5178
diff changeset
256 mail_namespaces_deinit(&client->namespaces);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
258 i_free(client->message_sizes);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
259 i_free(client->deleted_bitmask);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
260
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
261 if (client->io != NULL)
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
262 io_remove(&client->io);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
263
4070
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 3970
diff changeset
264 i_stream_destroy(&client->input);
71b8faa84ec6 Added i_stream_destroy() and o_stream_destroy() and used them instead of
Timo Sirainen <tss@iki.fi>
parents: 3970
diff changeset
265 o_stream_destroy(&client->output);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
266
3960
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
267 if (close(client->fd_in) < 0)
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
268 i_error("close(client in) failed: %m");
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
269 if (client->fd_in != client->fd_out) {
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
270 if (close(client->fd_out) < 0)
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
271 i_error("close(client out) failed: %m");
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
272 }
aeb424e64f24 Call io_remove() before closing the fd. It's required by kqueue.
Timo Sirainen <tss@iki.fi>
parents: 3879
diff changeset
273
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
274 i_free(client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
275
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
276 /* quit the program */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
277 my_client = NULL;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
278 io_loop_stop(ioloop);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
279 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
280
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
281 void client_disconnect(struct client *client, const char *reason)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
282 {
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
283 if (client->disconnected)
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
284 return;
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
285
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
286 client->disconnected = TRUE;
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
287 i_info("Disconnected: %s %s", reason, client_stats(client));
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
288
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
289 (void)o_stream_flush(client->output);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
290
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
291 i_stream_close(client->input);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
292 o_stream_close(client->output);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
295 int client_send_line(struct client *client, const char *fmt, ...)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297 va_list va;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
298 ssize_t ret;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
299
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
300 if (client->output->closed)
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
301 return -1;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
302
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
303 va_start(va, fmt);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
304
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
305 T_FRAME(
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
306 string_t *str;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
307
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
308 str = t_str_new(256);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
309 str_vprintfa(str, fmt, va);
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
310 str_append(str, "\r\n");
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
311
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
312 ret = o_stream_send(client->output,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
313 str_data(str), str_len(str));
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
314 i_assert(ret < 0 || (size_t)ret == str_len(str));
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
315 );
2735
25113dcc9705 Crashfix
Timo Sirainen <tss@iki.fi>
parents: 2722
diff changeset
316 if (ret >= 0) {
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
317 if (o_stream_get_buffer_used_size(client->output) <
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
318 OUTBUF_THROTTLE_SIZE) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
319 ret = 1;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
320 client->last_output = ioloop_time;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
321 } else {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
322 ret = 0;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
323 if (client->io != NULL) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
324 /* no more input until client has read
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
325 our output */
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
326 io_remove(&client->io);
3352
75cfcf7736a7 Connection could have gotten stuck sometimes, doing nothing until idle
Timo Sirainen <tss@iki.fi>
parents: 3336
diff changeset
327
75cfcf7736a7 Connection could have gotten stuck sometimes, doing nothing until idle
Timo Sirainen <tss@iki.fi>
parents: 3336
diff changeset
328 /* If someone happens to flush output,
75cfcf7736a7 Connection could have gotten stuck sometimes, doing nothing until idle
Timo Sirainen <tss@iki.fi>
parents: 3336
diff changeset
329 we want to get our IO handler back in
75cfcf7736a7 Connection could have gotten stuck sometimes, doing nothing until idle
Timo Sirainen <tss@iki.fi>
parents: 3336
diff changeset
330 flush callback */
75cfcf7736a7 Connection could have gotten stuck sometimes, doing nothing until idle
Timo Sirainen <tss@iki.fi>
parents: 3336
diff changeset
331 o_stream_set_flush_pending(client->output,
75cfcf7736a7 Connection could have gotten stuck sometimes, doing nothing until idle
Timo Sirainen <tss@iki.fi>
parents: 3336
diff changeset
332 TRUE);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
333 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
334 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
335 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
336
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
337 va_end(va);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
338 return (int)ret;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
339 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
340
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
341 void client_send_storage_error(struct client *client)
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
342 {
5613
f717fb4b31c0 Error handling rewrite.
Timo Sirainen <tss@iki.fi>
parents: 5608
diff changeset
343 enum mail_error error;
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
344
1915
79790750c349 importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents: 1845
diff changeset
345 if (mailbox_is_inconsistent(client->mailbox)) {
1415
c1a7da406bbd Handle inconsistency error separately.
Timo Sirainen <tss@iki.fi>
parents: 1412
diff changeset
346 client_send_line(client, "-ERR Mailbox is in inconsistent "
c1a7da406bbd Handle inconsistency error separately.
Timo Sirainen <tss@iki.fi>
parents: 1412
diff changeset
347 "state, please relogin.");
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
348 client_disconnect(client, "Mailbox is in inconsistent state.");
1415
c1a7da406bbd Handle inconsistency error separately.
Timo Sirainen <tss@iki.fi>
parents: 1412
diff changeset
349 return;
c1a7da406bbd Handle inconsistency error separately.
Timo Sirainen <tss@iki.fi>
parents: 1412
diff changeset
350 }
c1a7da406bbd Handle inconsistency error separately.
Timo Sirainen <tss@iki.fi>
parents: 1412
diff changeset
351
5613
f717fb4b31c0 Error handling rewrite.
Timo Sirainen <tss@iki.fi>
parents: 5608
diff changeset
352 client_send_line(client, "-ERR %s",
f717fb4b31c0 Error handling rewrite.
Timo Sirainen <tss@iki.fi>
parents: 5608
diff changeset
353 mail_storage_get_last_error(client->inbox_ns->storage,
f717fb4b31c0 Error handling rewrite.
Timo Sirainen <tss@iki.fi>
parents: 5608
diff changeset
354 &error));
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
355 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
356
4907
5b4c9b20eba0 Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
357 static void client_input(struct client *client)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
358 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
359 char *line, *args;
3469
7e39590da48a Call t_push/t_pop around client command execution function, so if client
Timo Sirainen <tss@iki.fi>
parents: 3441
diff changeset
360 int ret;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
361
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
362 if (client->cmd != NULL) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
363 /* we're still processing a command. wait until it's
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
364 finished. */
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
365 io_remove(&client->io);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
366 client->waiting_input = TRUE;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
367 return;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
368 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
369
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
370 client->waiting_input = FALSE;
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
371 client->last_input = ioloop_time;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
372
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
373 switch (i_stream_read(client->input)) {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
374 case -1:
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
375 /* disconnected */
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
376 client_destroy(client, NULL);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
377 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
378 case -2:
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
379 /* line too long, kill it */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
380 client_send_line(client, "-ERR Input line too long.");
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
381 client_destroy(client, "Input line too long");
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
382 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
383 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
384
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
385 o_stream_cork(client->output);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
386 while (!client->output->closed &&
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
387 (line = i_stream_next_line(client->input)) != NULL) {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
388 args = strchr(line, ' ');
4816
8ac2a2d27364 Cleanup: Don't put string literals into non-const pointers.
Timo Sirainen <tss@iki.fi>
parents: 4566
diff changeset
389 if (args != NULL)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
390 *args++ = '\0';
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
391
6940
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
392 T_FRAME(
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
393 ret = client_command_execute(client, line,
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
394 args != NULL ? args : "");
414c9d631a81 Replaced t_push/t_pop calls with T_FRAME*() macros.
Timo Sirainen <tss@iki.fi>
parents: 6652
diff changeset
395 );
3469
7e39590da48a Call t_push/t_pop around client command execution function, so if client
Timo Sirainen <tss@iki.fi>
parents: 3441
diff changeset
396 if (ret >= 0) {
1056
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
397 client->bad_counter = 0;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
398 if (client->cmd != NULL) {
3336
a3a72d5bdfce o_stream_uncork() was previously always setting IO_WRITE handler even if
Timo Sirainen <tss@iki.fi>
parents: 3260
diff changeset
399 o_stream_set_flush_pending(client->output,
a3a72d5bdfce o_stream_uncork() was previously always setting IO_WRITE handler even if
Timo Sirainen <tss@iki.fi>
parents: 3260
diff changeset
400 TRUE);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
401 client->waiting_input = TRUE;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
402 break;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
403 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
404 } else if (++client->bad_counter > CLIENT_MAX_BAD_COMMANDS) {
1056
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
405 client_send_line(client, "-ERR Too many bad commands.");
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
406 client_disconnect(client, "Too many bad commands.");
1056
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
407 }
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
408 }
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
409 o_stream_uncork(client->output);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
410
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
411 if (client->output->closed)
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
412 client_destroy(client, NULL);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
413 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
414
4907
5b4c9b20eba0 Replaced void *context from a lot of callbacks with the actual context
Timo Sirainen <tss@iki.fi>
parents: 4848
diff changeset
415 static int client_output(struct client *client)
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
416 {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
417 int ret;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
418
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
419 if ((ret = o_stream_flush(client->output)) < 0) {
3384
3b75956d20c4 Added configurable logging for login process. Added configurable pop3 logout
Timo Sirainen <tss@iki.fi>
parents: 3372
diff changeset
420 client_destroy(client, NULL);
2790
02c0b8d532c2 Changed ostream's flush callback to have return value which can tell if
Timo Sirainen <tss@iki.fi>
parents: 2735
diff changeset
421 return 1;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
422 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
423
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
424 client->last_output = ioloop_time;
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
425
3394
30099e1ccf97 Small optimization
Timo Sirainen <tss@iki.fi>
parents: 3391
diff changeset
426 if (client->cmd != NULL) {
30099e1ccf97 Small optimization
Timo Sirainen <tss@iki.fi>
parents: 3391
diff changeset
427 o_stream_cork(client->output);
2494
a2e2c76021b9 Fixes for nonblocking changes.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
428 client->cmd(client);
3394
30099e1ccf97 Small optimization
Timo Sirainen <tss@iki.fi>
parents: 3391
diff changeset
429 o_stream_uncork(client->output);
30099e1ccf97 Small optimization
Timo Sirainen <tss@iki.fi>
parents: 3391
diff changeset
430 }
2494
a2e2c76021b9 Fixes for nonblocking changes.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
431
3391
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
432 if (client->cmd == NULL) {
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
433 if (o_stream_get_buffer_used_size(client->output) <
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
434 OUTBUF_THROTTLE_SIZE/2 && client->io == NULL) {
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
435 /* enable input again */
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
436 client->io = io_add(i_stream_get_fd(client->input),
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
437 IO_READ, client_input, client);
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
438 }
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
439 if (client->io != NULL && client->waiting_input)
d01de9d362c1 Code cleanup. Removed useless previous "fix".
Timo Sirainen <tss@iki.fi>
parents: 3384
diff changeset
440 client_input(client);
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
441 }
2494
a2e2c76021b9 Fixes for nonblocking changes.
Timo Sirainen <tss@iki.fi>
parents: 2421
diff changeset
442
2790
02c0b8d532c2 Changed ostream's flush callback to have return value which can tell if
Timo Sirainen <tss@iki.fi>
parents: 2735
diff changeset
443 return client->cmd == NULL;
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
444 }
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
445
6411
6a64e64fa3a3 Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents: 6312
diff changeset
446 static void idle_timeout(void *context ATTR_UNUSED)
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
447 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
448 if (my_client == NULL)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
449 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
450
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
451 if (my_client->cmd != NULL) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
452 if (ioloop_time - my_client->last_output >=
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
453 CLIENT_OUTPUT_TIMEOUT) {
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
454 client_destroy(my_client, "Disconnected for inactivity "
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
455 "in reading our output");
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
456 }
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
457 } else {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
458 if (ioloop_time - my_client->last_input >=
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
459 CLIENT_IDLE_TIMEOUT) {
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
460 client_send_line(my_client,
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
461 "-ERR Disconnected for inactivity.");
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
462 client_destroy(my_client, "Disconnected for inactivity");
2421
d141e1bfdd63 We never do blocking reads/writes to network anymore. Changed imap and pop3
Timo Sirainen <tss@iki.fi>
parents: 2322
diff changeset
463 }
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
464 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
465 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
466
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
467 void clients_init(void)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
468 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
469 my_client = NULL;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
470 to_idle = timeout_add(10000, idle_timeout, NULL);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
471 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
472
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
473 void clients_deinit(void)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
474 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
475 if (my_client != NULL) {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
476 client_send_line(my_client, "-ERR Server shutting down.");
4096
904c53275e83 Log a line when IMAP client disconnects with a reason why it happened.
Timo Sirainen <tss@iki.fi>
parents: 4070
diff changeset
477 client_destroy(my_client, "Server shutting down");
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
478 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
479
3879
928229f8b3e6 deinit, unref, destroy, close, free, etc. functions now take a pointer to
Timo Sirainen <tss@iki.fi>
parents: 3863
diff changeset
480 timeout_remove(&to_idle);
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
481 }