nntp.h sys/nntp.h
Framework for NNTP.
Showing
2 changed files
with
226 additions
and
0 deletions
include/mailutils/nntp.h
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public | ||
15 | License along with this library; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #ifndef _MAILUTILS_NNTP_H | ||
19 | #define _MAILUTILS_NNTP_H | ||
20 | |||
21 | #include <mailutils/debug.h> | ||
22 | #include <mailutils/stream.h> | ||
23 | |||
24 | #ifdef __cplusplus | ||
25 | extern "C" { | ||
26 | #endif | ||
27 | |||
28 | struct _mu_nntp; | ||
29 | typedef struct _mu_nntp* mu_nntp_t; | ||
30 | |||
31 | extern int mu_nntp_create (mu_nntp_t *nntp); | ||
32 | extern void mu_nntp_destroy (mu_nntp_t *nntp); | ||
33 | |||
34 | extern int mu_nntp_set_carrier (mu_nntp_t nntp, stream_t carrier); | ||
35 | extern int mu_nntp_get_carrier (mu_nntp_t nntp, stream_t *pcarrier); | ||
36 | |||
37 | extern int mu_nntp_connect (mu_nntp_t nntp); | ||
38 | extern int mu_nntp_disconnect (mu_nntp_t nntp); | ||
39 | |||
40 | extern int mu_nntp_set_timeout (mu_nntp_t nntp, int timeout); | ||
41 | extern int mu_nntp_get_timeout (mu_nntp_t nntp, int *timeout); | ||
42 | |||
43 | extern int mu_nntp_set_debug (mu_nntp_t nntp, mu_debug_t debug); | ||
44 | |||
45 | extern int mu_nntp_stls (mu_nntp_t nntp); | ||
46 | |||
47 | |||
48 | extern int mu_nntp_article (mu_nntp_t nntp, long num, stream_t *stream); | ||
49 | extern int mu_nntp_article_id (mu_nntp_t nntp, const char *id, stream_t *stream); | ||
50 | |||
51 | extern int mu_nntp_header (mu_nntp_t nntp, long num, stream_t *stream); | ||
52 | extern int mu_nntp_header_id (mu_nntp_t nntp, const char *name, stream_t *stream); | ||
53 | |||
54 | extern int mu_nntp_body (mu_nntp_t nntp, long num, stream_t *stream); | ||
55 | extern int mu_nntp_body_id (mu_nntp_t nntp, const char *name, stream_t *stream); | ||
56 | |||
57 | extern int mu_nntp_stat (mu_nntp_t nntp, long num, char **id); | ||
58 | extern int mu_nntp_stat_id (mu_nntp_t nntp, const char *name, char **id); | ||
59 | |||
60 | extern int mu_nntp_group (mu_nntp_t nntp, const char *group, long *total, long *first, long *last, char **name); | ||
61 | |||
62 | |||
63 | /* Reads the multi-line response of the server, nread will be 0 when the termination octets | ||
64 | are detected. Clients should not use this function unless they are sending direct command. */ | ||
65 | extern int mu_nntp_readline (mu_nntp_t nntp, char *buffer, size_t buflen, size_t *nread); | ||
66 | |||
67 | /* Returns the last command acknowledge. If the server supports RESP-CODE, the message | ||
68 | could be retrieve, but it is up the caller to do the parsing. */ | ||
69 | extern int mu_nntp_response (mu_nntp_t nntp, char *buffer, size_t buflen, size_t *nread); | ||
70 | |||
71 | /* pop3_writeline copies the line in the internal buffer, a mu_pop3_send() is | ||
72 | needed to do the actual transmission. */ | ||
73 | extern int mu_nntp_writeline (mu_nntp_t nntp, const char *format, ...); | ||
74 | |||
75 | /* mu_pop3_sendline() is equivalent to: | ||
76 | mu_pop3_writeline (pop3, line); | ||
77 | mu_pop3_send (pop3); | ||
78 | */ | ||
79 | extern int mu_nntp_sendline (mu_nntp_t nntp, const char *line); | ||
80 | |||
81 | /* Transmit via the carrier the internal buffer data. */ | ||
82 | extern int mu_nntp_send (mu_nntp_t nntp); | ||
83 | |||
84 | #ifdef __cplusplus | ||
85 | } | ||
86 | #endif | ||
87 | |||
88 | #endif /* _MAILUTILS_POP3_H */ |
include/mailutils/sys/nntp.h
0 → 100644
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2004 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 2 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public | ||
15 | License along with this library; if not, write to the Free Software | ||
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ | ||
17 | |||
18 | #ifndef _MAILUTILS_SYS_NNTP_H | ||
19 | #define _MAILUTILS_SYS_NNTP_H | ||
20 | |||
21 | #include <sys/types.h> | ||
22 | #include <mailutils/nntp.h> | ||
23 | #include <mailutils/errno.h> | ||
24 | |||
25 | #ifdef DMALLOC | ||
26 | # include <dmalloc.h> | ||
27 | #endif | ||
28 | |||
29 | #ifdef __cplusplus | ||
30 | extern "C" { | ||
31 | #endif | ||
32 | |||
33 | enum mu_nntp_state | ||
34 | { | ||
35 | MU_NNTP_NO_STATE, | ||
36 | MU_NNTP_CONNECT, MU_NNTP_GREETINGS, | ||
37 | MU_NNTP_ARTICLE, MU_NNTP_ARTICLE_ACK, MU_NNTP_ARTICLE_RX, | ||
38 | MU_NNTP_HEADER, MU_NNTP_HEADER_ACK, MU_NNTP_HEADER_RX, | ||
39 | MU_NNTP_BODY, MU_NNTP_BODY_ACK, MU_NNTP_BODY_RX, | ||
40 | MU_NNTP_STAT, MU_NNTP_STAT_ACK, | ||
41 | MU_NNTP_STLS, MU_NNTP_STLS_ACK, MU_NNTP_STLS_CONNECT, | ||
42 | MU_NNTP_DONE, MU_NNTP_UNKNOWN, MU_NNTP_ERROR | ||
43 | }; | ||
44 | |||
45 | /* Structure holding the data necessary to do proper buffering. */ | ||
46 | struct mu_nntp_work_buf | ||
47 | { | ||
48 | char *buf; | ||
49 | char *ptr; | ||
50 | char *nl; | ||
51 | size_t len; | ||
52 | }; | ||
53 | |||
54 | /* Structure to hold things general to nntp connection, like its state, etc ... */ | ||
55 | struct _mu_nntp | ||
56 | { | ||
57 | /* Working I/O buffer. | ||
58 | io.buf: Working io buffer | ||
59 | io.ptr: Points to the end of the buffer, the non consumed chars | ||
60 | io.nl: Points to the '\n' char in the string | ||
61 | io.len: Len of io_buf. */ | ||
62 | struct mu_nntp_work_buf io; | ||
63 | |||
64 | /* Holds the first line response of the last command, i.e the ACK: | ||
65 | ack.buf: Buffer for the ack | ||
66 | ack.ptr: Working pointer, indicate the start of the non consumed chars | ||
67 | ack.len: Size 512 according to RFC2449. */ | ||
68 | struct mu_nntp_work_buf ack; | ||
69 | int acknowledge; | ||
70 | |||
71 | unsigned timeout; /* Default is 10 minutes. */ | ||
72 | |||
73 | mu_debug_t debug; /* debugging trace. */ | ||
74 | |||
75 | enum mu_nntp_state state; /* Indicate the state of the running command. */ | ||
76 | |||
77 | stream_t carrier; /* TCP Connection. */ | ||
78 | }; | ||
79 | |||
80 | extern int mu_nntp_debug_cmd (mu_nntp_t); | ||
81 | extern int mu_nntp_debug_ack (mu_nntp_t); | ||
82 | extern int mu_nntp_stream_create (mu_nntp_t pop3, stream_t *pstream); | ||
83 | extern int mu_nntp_carrier_is_ready (stream_t carrier, int flag, int timeout); | ||
84 | |||
85 | /* Check for non recoverable error. | ||
86 | The error is consider not recoverable if not part of the signal set: | ||
87 | EAGAIN, EINPROGRESS, EINTR. | ||
88 | For unrecoverable error we reset, by moving the working ptr | ||
89 | to the begining of the buffer and setting the state to error. | ||
90 | */ | ||
91 | #define MU_NNTP_CHECK_EAGAIN(nntp, status) \ | ||
92 | do \ | ||
93 | { \ | ||
94 | if (status != 0) \ | ||
95 | { \ | ||
96 | if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \ | ||
97 | { \ | ||
98 | nntp->io.ptr = nntp->io.buf; \ | ||
99 | nntp->state = MU_NNTP_ERROR; \ | ||
100 | } \ | ||
101 | return status; \ | ||
102 | } \ | ||
103 | } \ | ||
104 | while (0) | ||
105 | |||
106 | /* If error return. | ||
107 | Check status an reset(see MU_NNTP_CHECK_EAGAIN) the buffer. | ||
108 | */ | ||
109 | #define MU_NNTP_CHECK_ERROR(nntp, status) \ | ||
110 | do \ | ||
111 | { \ | ||
112 | if (status != 0) \ | ||
113 | { \ | ||
114 | nntp->io.ptr = nntp->io.buf; \ | ||
115 | nntp->state = MU_NNTP_ERROR; \ | ||
116 | return status; \ | ||
117 | } \ | ||
118 | } \ | ||
119 | while (0) | ||
120 | |||
121 | /* Check if we got "2xx". In NNTP protocol and ack of "2xx" means the command was successfull. | ||
122 | */ | ||
123 | #define MU_NNTP_CHECK_COMPLETE(nntp) \ | ||
124 | do \ | ||
125 | { \ | ||
126 | if (nntp->ack.buf[0] == '2') != 0) \ | ||
127 | { \ | ||
128 | nntp->state = MU_NNTP_NO_STATE; \ | ||
129 | return EACCES; \ | ||
130 | } \ | ||
131 | } \ | ||
132 | while (0) | ||
133 | |||
134 | #ifdef __cplusplus | ||
135 | } | ||
136 | #endif | ||
137 | |||
138 | #endif /* _MAILUTILS_SYS_NNTP_H */ |
-
Please register or sign in to post a comment