annotate src/pop3/client.c @ 1056:a9b499b2611e HEAD

Disconnect after too many bad commands. We also crashed if there were no mails in mailbox.
author Timo Sirainen <tss@iki.fi>
date Thu, 30 Jan 2003 20:05:33 +0200
parents 28f606ada24a
children d805c2f1d6a9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
1 /* Copyright (C) 2002 Timo Sirainen */
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"
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #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
5 #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
6 #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
7 #include "ostream.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 "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
9 #include "commands.h"
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10
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 <stdlib.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
1053
28f606ada24a Reduce input buffer size
Timo Sirainen <tss@iki.fi>
parents: 1044
diff changeset
13 /* max. length of input command line (spec says 512) */
28f606ada24a Reduce input buffer size
Timo Sirainen <tss@iki.fi>
parents: 1044
diff changeset
14 #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
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 /* If we can't send a buffer in a minute, disconnect the client */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
17 #define CLIENT_OUTPUT_TIMEOUT (60*1000)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
18
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
19 /* 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
20 #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
21
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
22 /* Disconnect client after idling this many seconds */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 #define CLIENT_IDLE_TIMEOUT (60*30)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 extern struct mail_storage_callbacks mail_storage_callbacks;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 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
28 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
29
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
30 static void client_input(void *context);
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 static void client_output_timeout(void *context)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 struct client *client = context;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
36 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
37 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
38 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
39
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
40 static int init_mailbox(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
41 {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
42 struct mail_fetch_context *ctx;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
43 struct mail *mail;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
44 struct mailbox_status status;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
45 const char *messageset;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
46 int i, all_found, failed;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
47
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
48 if (!client->mailbox->get_status(client->mailbox,
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
49 STATUS_MESSAGES | STATUS_UIDVALIDITY,
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
50 &status)) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
51 client_send_storage_error(client);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
52 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
53 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
54
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
55 client->messages_count = status.messages;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
56 client->uidvalidity = status.uidvalidity;
1056
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
57
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
58 if (client->messages_count == 0)
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
59 return TRUE;
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
60
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
61 client->message_sizes = i_new(uoff_t, client->messages_count);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
62 messageset = t_strdup_printf("1:%u", client->messages_count);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
63 for (i = 0; i < 2; i++) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
64 ctx = client->mailbox->fetch_init(client->mailbox,
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
65 MAIL_FETCH_SIZE,
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
66 NULL, messageset, FALSE);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
67 if (ctx == NULL) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
68 client_send_storage_error(client);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
69 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
70 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
71
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
72 client->total_size = 0;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
73 failed = FALSE;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
74 while ((mail = client->mailbox->fetch_next(ctx)) != NULL) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
75 uoff_t size = mail->get_size(mail);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
76
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
77 if (size == (uoff_t)-1) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
78 failed = TRUE;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
79 break;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
80 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
81 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
82
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
83 i_assert(mail->seq <= client->messages_count);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
84 client->message_sizes[mail->seq-1] = size;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
85 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
86
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
87 if (!client->mailbox->fetch_deinit(ctx, &all_found)) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
88 client_send_storage_error(client);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
89 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
90 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
91
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
92 if (!failed && all_found)
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
93 return TRUE;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
94
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
95 /* well, sync and try again */
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
96 if (!client->mailbox->sync(client->mailbox, TRUE)) {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
97 client_send_storage_error(client);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
98 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
99 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
100 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
101
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
102 client_send_line(client, "-ERR Couldn't sync mailbox.");
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
103 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
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
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
106 struct client *client_create(int hin, int hout, struct mailbox *mailbox)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
107 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
108 struct 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
109
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
110 client = i_new(struct client, 1);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
111 client->input = i_stream_create_file(hin, default_pool,
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
112 MAX_INBUF_SIZE, FALSE);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
113 client->output = o_stream_create_file(hout, default_pool, 4096,
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
114 IO_PRIORITY_DEFAULT, FALSE);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
115
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
116 /* set timeout for sending data */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
117 o_stream_set_blocking(client->output, CLIENT_OUTPUT_TIMEOUT,
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
118 client_output_timeout, client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
119
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
120 client->io = io_add(hin, IO_READ, client_input, client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
121 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
122
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
123 client->storage = mailbox->storage;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
124 client->mailbox = mailbox;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
125
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
126 mailbox->storage->set_callbacks(mailbox->storage,
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
127 &mail_storage_callbacks, client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
128
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
129 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
130 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
131
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
132 if (!init_mailbox(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
133 client_destroy(client);
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
134 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
135 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
136
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
137 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
138 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
139
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
140 void client_destroy(struct 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
141 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
142 o_stream_flush(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
143
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
144 if (client->mailbox != NULL)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
145 client->mailbox->close(client->mailbox);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
146 mail_storage_destroy(client->storage);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
148 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
149 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
150
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
151 io_remove(client->io);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
152
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
153 i_stream_unref(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
154 o_stream_unref(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
155
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
156 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
157
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 /* 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
159 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
160 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
161 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 void client_disconnect(struct 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
164 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
165 o_stream_flush(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
166
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 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
168 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
169 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
170
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
171 void client_send_line(struct client *client, const char *fmt, ...)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
172 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
173 va_list va;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
174
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
175 if (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
176 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
177
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
178 va_start(va, fmt);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
179 (void)o_stream_send_str(client->output, t_strdup_vprintf(fmt, va));
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
180 (void)o_stream_send(client->output, "\r\n", 2);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
181 va_end(va);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
182 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
183
1044
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
184 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
185 {
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
186 const char *error;
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
187
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
188 error = client->storage->get_last_error(client->storage, NULL);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
189 client_send_line(client, "-ERR %s", error);
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
190 }
50d258907c99 Read the sizes of all messages to memory at startup. More failsafe and
Timo Sirainen <tss@iki.fi>
parents: 1043
diff changeset
191
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
192 static void client_input(void *context)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
193 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
194 struct client *client = context;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
195 char *line, *args;
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 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
198
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 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
200 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
201 /* disconnected */
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202 client_destroy(client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
203 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
204 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
205 /* 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
206 client_send_line(client, "-ERR Input line too long.");
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
207 client_destroy(client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
208 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
209 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
210
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
211 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
212 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
213 (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
214 args = strchr(line, ' ');
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
215 if (args == NULL)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
216 args = "";
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
217 else
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
218 *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
219
1056
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
220 if (client_command_execute(client, line, args))
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
221 client->bad_counter = 0;
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
222 else if (++client->bad_counter > CLIENT_MAX_BAD_COMMANDS) {
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
223 client_send_line(client, "-ERR Too many bad commands.");
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
224 client_disconnect(client);
a9b499b2611e Disconnect after too many bad commands. We also crashed if there were no
Timo Sirainen <tss@iki.fi>
parents: 1053
diff changeset
225 }
1043
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
226 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
227 o_stream_flush(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
228
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
229 if (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
230 client_destroy(client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
231 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
232
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
233 static void idle_timeout(void *context __attr_unused__)
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
234 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
235 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
236 return;
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
237
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
238 if (ioloop_time - my_client->last_input >= CLIENT_IDLE_TIMEOUT) {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
239 client_send_line(my_client,
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
240 "-ERR Disconnected for inactivity.");
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
241 client_destroy(my_client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
242 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
243 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
244
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
245 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
246 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
247 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
248 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
249 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
250
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
251 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
252 {
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
253 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
254 client_send_line(my_client, "-ERR Server shutting down.");
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
255 client_destroy(my_client);
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
256 }
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
257
cacabd33c68a Initial code for POP3 server. RETR isn't working right yet, there's some
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
258 timeout_remove(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
259 }