Init RSA repo.
[external/uw-imap-toolkit.git] / imap-2007e / c-client / lnx_mail.c
1 /* ========================================================================
2  * Copyright 1988-2006 University of Washington
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * 
11  * ========================================================================
12  */
13
14 #ifndef WIN32
15 #define tcp_getline   tcp_getline_lnx
16 #define tcp_getbuffer tcp_getbuffer_lnx
17 #define tcp_soutr     tcp_soutr_lnx
18 #define tcp_sout      tcp_sout_lnx
19 #endif
20
21 #define mail_lookup_auth_name      mail_lookup_auth_name_unused
22
23
24 #include "mail.c"
25 //#include "em-core-utils.h"
26 #include <errno.h>
27 #include <sys/time.h>
28 #include <unistd.h>
29
30 #include "imap-dbglog.h"
31
32 #ifndef WIN32
33 #undef tcp_getline
34 #undef tcp_getbuffer
35 #undef tcp_soutr
36 #undef tcp_sout
37 #endif
38
39 #undef mail_lookup_auth_name
40
41
42 #ifndef WIN32
43
44 #include "tcp_unix.h"
45
46
47 long 
48 tcp_getdata_lnx(TCPSTREAM* stream)
49 {
50         struct timeval tmout;
51         fd_set readfds;
52         int nread, sret, sockid, maxfd;
53         int max_timeout = 0;
54         
55         sockid = stream->tcpsi;
56         maxfd = sockid + 1;
57         
58         if (sockid < 0) return false;
59         
60         while (stream->ictr < 1) 
61         {
62                 tmout.tv_usec = 0;//1000*10;
63                 tmout.tv_sec = 1;
64                 
65                 FD_ZERO(&readfds);
66                 FD_SET(sockid, &readfds);
67                 
68                 sret = select(maxfd, &readfds, NULL, NULL, &tmout);
69                 
70                 if (sret < 0) {
71                         tcp_abort(stream);
72                         return false;
73                 }
74                 else if (!sret) {
75                         if (max_timeout >= 50) {
76                                 IMAP_DEBUG_EXCEPTION("max select timeout %d", max_timeout);
77                                 
78                                 //em_core_set_network_error(EMF_ERROR_NO_RESPONSE);
79                                 return false;
80                         }
81                         
82                         IMAP_DEBUG_EXCEPTION("%d select timeout", max_timeout);
83                         
84                         ++max_timeout;
85                         continue;
86                 }
87                 
88                 if ((nread = read(sockid, stream->ibuf, BUFLEN)) < 0) {
89                         IMAP_DEBUG_EXCEPTION("\t socket read failed...\n");
90                         tcp_abort(stream);
91                         return false;
92                 }
93                 
94                 if (!nread) {
95                         IMAP_DEBUG_EXCEPTION("\t socket read no data...\n");
96                         tcp_abort(stream);
97                         return false;
98                 }
99                 
100                 stream->ictr = nread;
101                 stream->iptr = stream->ibuf;
102         }
103
104         return true;
105 }
106
107 /* TCP/IP receive line
108  * Accepts: TCP/IP stream
109  * Returns: text line string or NIL if failure
110  */
111
112 char *tcp_getline_lnx (void *vstream)
113 {
114         TCPSTREAM *stream = (TCPSTREAM*)vstream;
115         int n,m;
116         char *st,*ret,*stp;
117         char c = '\0';
118         char d;
119         /* make sure have data */
120         if (!tcp_getdata_lnx (stream)) return NIL;
121         st = stream->iptr;              /* save start of string */
122         n = 0;                  /* init string count */
123         while (stream->ictr--) {        /* look for end of line */
124                 d = *stream->iptr++;    /* slurp another character */
125                 if ((c == '\015') && (d == '\012')) {
126                         ret = (char *) fs_get (n--);
127                         memcpy (ret,st,n);      /* copy into a free storage string */
128                         ret[n] = '\0';          /* tie off string with null */
129                         return ret;
130                 }
131                 n++;                    /* count another character searched */
132                 c = d;                  /* remember previous character */
133         }
134         /* copy partial string from buffer */
135         memcpy ((ret = stp = (char *) fs_get (n)),st,n);
136         /* get more data from the net */
137         if (!tcp_getdata_lnx (stream)) fs_give ((void **) &ret);
138         /* special case of newline broken by buffer */
139         else if ((c == '\015') && (*stream->iptr == '\012')) {
140                 stream->iptr++;         /* eat the line feed */
141                 stream->ictr--;
142                 ret[n - 1] = '\0';              /* tie off string with null */
143         }
144         /* else recurse to get remainder */
145         else if (st = tcp_getline_lnx (stream)) {
146                 ret = (char *) fs_get (n + 1 + (m = strlen (st)));
147                 memcpy (ret,stp,n);             /* copy first part */
148                 memcpy (ret + n,st,m);  /* and second part */
149                 fs_give ((void **) &stp);       /* flush first part */
150                 fs_give ((void **) &st);        /* flush second part */
151                 ret[n + m] = '\0';              /* tie off string with null */
152         }
153         return ret;
154 }
155 #endif
156
157
158 int try_auth = 0;
159 unsigned int mail_lookup_auth_name (char *mechanism,long flags)
160 {
161   int i;
162   AUTHENTICATOR *auth;
163
164   if(!try_auth) return 0;
165
166   for (i = 1, auth = mailauthenticators; auth; i++, auth = auth->next)
167     if (auth->client && !(flags & ~auth->flags) &&
168         !compare_cstring (auth->name,mechanism))
169       return i;
170   return 0;
171 }
172
173 // 22-Mar-2010 added
174 int try_auth_smtp = 0;
175 unsigned int mail_lookup_auth_name_smtp (char *mechanism,long flags)
176 {
177   int i;
178   AUTHENTICATOR *auth;
179
180   if(!try_auth_smtp) return 0;
181
182   for (i = 1, auth = mailauthenticators; auth; i++, auth = auth->next)
183     if (auth->client && !(flags & ~auth->flags) &&
184         !compare_cstring (auth->name,mechanism))
185       return i;
186   return 0;
187 }
188