Diffstat (limited to 'noncore/net/mail/libmailwrapper/imapwrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.cpp | 84 |
1 files changed, 78 insertions, 6 deletions
diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.cpp b/noncore/net/mail/libmailwrapper/imapwrapper.cpp index 098dbdc..1dfcc4c 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/imapwrapper.cpp | |||
@@ -40,8 +40,60 @@ void IMAPwrapper::imap_progress( size_t current, size_t maximum ) | |||
40 | { | 40 | { |
41 | qDebug( "IMAP: %i of %i", current, maximum ); | 41 | qDebug( "IMAP: %i of %i", current, maximum ); |
42 | } | 42 | } |
43 | 43 | ||
44 | bool IMAPwrapper::start_tls(bool force_tls) | ||
45 | { | ||
46 | int err; | ||
47 | bool try_tls; | ||
48 | mailimap_capability_data * cap_data = 0; | ||
49 | |||
50 | err = mailimap_capability(m_imap,&cap_data); | ||
51 | if (err != MAILIMAP_NO_ERROR) { | ||
52 | Global::statusMessage("error getting capabilities!"); | ||
53 | qDebug("error getting capabilities!"); | ||
54 | return false; | ||
55 | } | ||
56 | clistiter * cur; | ||
57 | for(cur = clist_begin(cap_data->cap_list) ; cur != NULL;cur = clist_next(cur)) { | ||
58 | struct mailimap_capability * cap; | ||
59 | cap = (struct mailimap_capability *)clist_content(cur); | ||
60 | if (cap->cap_type == MAILIMAP_CAPABILITY_NAME) { | ||
61 | if (strcasecmp(cap->cap_data.cap_name, "STARTTLS") == 0) { | ||
62 | try_tls = true; | ||
63 | break; | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | if (cap_data) { | ||
68 | mailimap_capability_data_free(cap_data); | ||
69 | } | ||
70 | if (try_tls) { | ||
71 | err = mailimap_starttls(m_imap); | ||
72 | if (err != MAILIMAP_NO_ERROR && force_tls) { | ||
73 | Global::statusMessage(tr("Server has no TLS support!")); | ||
74 | qDebug("Server has no TLS support!"); | ||
75 | try_tls = false; | ||
76 | } else { | ||
77 | mailstream_low * low; | ||
78 | mailstream_low * new_low; | ||
79 | low = mailstream_get_low(m_imap->imap_stream); | ||
80 | if (!low) { | ||
81 | try_tls = false; | ||
82 | } else { | ||
83 | int fd = mailstream_low_get_fd(low); | ||
84 | if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { | ||
85 | mailstream_low_free(low); | ||
86 | mailstream_set_low(m_imap->imap_stream, new_low); | ||
87 | } else { | ||
88 | try_tls = false; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | return try_tls; | ||
94 | } | ||
95 | |||
44 | void IMAPwrapper::login() | 96 | void IMAPwrapper::login() |
45 | { | 97 | { |
46 | const char *server, *user, *pass; | 98 | const char *server, *user, *pass; |
47 | uint16_t port; | 99 | uint16_t port; |
@@ -79,17 +131,19 @@ void IMAPwrapper::login() | |||
79 | } | 131 | } |
80 | 132 | ||
81 | m_imap = mailimap_new( 20, &imap_progress ); | 133 | m_imap = mailimap_new( 20, &imap_progress ); |
82 | 134 | ||
83 | |||
84 | |||
85 | /* connect */ | 135 | /* connect */ |
86 | |||
87 | bool ssl = false; | 136 | bool ssl = false; |
137 | bool try_tls = false; | ||
138 | bool force_tls = false; | ||
88 | 139 | ||
89 | if ( account->ConnectionType() == 2 ) { | 140 | if ( account->ConnectionType() == 2 ) { |
90 | ssl = true; | 141 | ssl = true; |
91 | } | 142 | } |
143 | if (account->ConnectionType()==1) { | ||
144 | force_tls = true; | ||
145 | } | ||
92 | 146 | ||
93 | if ( ssl ) { | 147 | if ( ssl ) { |
94 | qDebug( "using ssl" ); | 148 | qDebug( "using ssl" ); |
95 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); | 149 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); |
@@ -111,12 +165,30 @@ void IMAPwrapper::login() | |||
111 | m_imap = 0; | 165 | m_imap = 0; |
112 | return; | 166 | return; |
113 | } | 167 | } |
114 | 168 | ||
169 | if (!ssl) { | ||
170 | try_tls = start_tls(force_tls); | ||
171 | } | ||
172 | |||
173 | bool ok = true; | ||
174 | if (force_tls && !try_tls) { | ||
175 | Global::statusMessage(tr("Server has no TLS support!")); | ||
176 | qDebug("Server has no TLS support!"); | ||
177 | ok = false; | ||
178 | } | ||
179 | |||
180 | |||
115 | /* login */ | 181 | /* login */ |
116 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); | 182 | |
117 | if ( err != MAILIMAP_NO_ERROR ) { | 183 | if (ok) { |
118 | Global::statusMessage(tr("error logging in imap server: %1").arg(m_imap->imap_response)); | 184 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); |
185 | if ( err != MAILIMAP_NO_ERROR ) { | ||
186 | Global::statusMessage(tr("error logging in imap server: %1").arg(m_imap->imap_response)); | ||
187 | ok = false; | ||
188 | } | ||
189 | } | ||
190 | if (!ok) { | ||
119 | err = mailimap_close( m_imap ); | 191 | err = mailimap_close( m_imap ); |
120 | mailimap_free( m_imap ); | 192 | mailimap_free( m_imap ); |
121 | m_imap = 0; | 193 | m_imap = 0; |
122 | } | 194 | } |