/[imapfilter]/imapfilter/imap.c
ViewVC logotype

Diff of /imapfilter/imap.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.38.2.2 by lefcha, Tue Jan 20 01:49:44 2004 UTC revision 1.46 by lefcha, Sat Feb 14 22:48:04 2004 UTC
# Line 3  Line 3 
3    
4  #include "config.h"  #include "config.h"
5  #include "imapfilter.h"  #include "imapfilter.h"
6    #include "filter.h"
7  #include "buffer.h"  #include "buffer.h"
8    
9    
10  extern conn_t connpri, connaux;  extern options_t opts;
11    extern connection_t connpri, connaux;
12    
13  buffer_t obuf;                  /* Output buffer. */  buffer_t obuf;                  /* Output buffer. */
14    
# Line 14  static unsigned int tag = 0x1000;      /* Eve Line 16  static unsigned int tag = 0x1000;      /* Eve
16                                           * a unique [:alnum:] string. */                                           * a unique [:alnum:] string. */
17    
18    
19  unsigned int send_command(conn_t * conn, char *cmd);  unsigned int send_command(connection_t * conn, char *cmd, char *alt);
20  void send_command_cont(conn_t * conn, char *cmd);  void send_command_cont(connection_t * conn, char *cmd);
21    
22    
23  /*  /*
24   * Send to server data; a command.   * Send to server data; a command.
25   */   */
26  unsigned int  unsigned int
27  send_command(conn_t * conn, char *cmd)  send_command(connection_t * conn, char *cmd, char *alt)
28  {  {
29  #ifdef DEBUG  
30          fprintf(stderr, "debug: sending command (%s):\n\n%s\n",          debug("sending command (%s):\n\n%s\n",
31              (conn == &connpri ? "primary" : "auxiliary"), cmd);              (conn == &connpri ? "primary" : "auxiliary"),
32  #endif              (opts.debug == 1 && alt ? alt : cmd));
33          verbose("%s: %s", (conn == &connpri ? "C" : "c"), cmd);  
34            verbose("%s: %s", (conn == &connpri ? "C" : "c"), (alt ? alt : cmd));
35    
36          socket_write(conn, cmd);          socket_write(conn, cmd);
37    
# Line 44  send_command(conn_t * conn, char *cmd) Line 47  send_command(conn_t * conn, char *cmd)
47   * Send to server data: a continuation command.   * Send to server data: a continuation command.
48   */   */
49  void  void
50  send_command_cont(conn_t * conn, char *cmd)  send_command_cont(connection_t * conn, char *cmd)
51  {  {
52  #ifdef DEBUG  
53          fprintf(stderr, "debug: sending command (%s):\n\n%s\r\n\n",          debug("sending command (%s):\n\n%s\r\n\n",
54              (conn == &connpri ? "primary" : "auxiliary"), cmd);              (conn == &connpri ? "primary" : "auxiliary"), cmd);
 #endif  
55    
56          socket_write(conn, cmd);          socket_write(conn, cmd);
57          socket_write(conn, "\r\n");          socket_write(conn, "\r\n");
# Line 57  send_command_cont(conn_t * conn, char *c Line 59  send_command_cont(conn_t * conn, char *c
59  #endif  #endif
60    
61    
 #ifdef DEBUG  
62  /*  /*
63   * IMAP NOOP: does nothing always succeeds.   * IMAP NOOP: does nothing always succeeds.
64   */   */
65  int  int
66  imap_noop(conn_t * conn)  imap_noop(connection_t * conn)
67  {  {
68          reset_buffer(&obuf);  
69          check_buffer(&obuf, strlen("NOOP") + 12);          buffer_reset(&obuf);
70            buffer_check(&obuf, strlen("NOOP") + 12);
71    
72          snprintf(obuf.data, obuf.size, "%04X NOOP\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X NOOP\r\n", tag);
73    
74          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
75  }  }
 #endif  
76    
77    
78  /*  /*
79   * IMAP CAPABILITY: requests listing of capabilities that the server supports.   * IMAP CAPABILITY: requests listing of capabilities that the server supports.
80   */   */
81  int  int
82  imap_capability(conn_t * conn)  imap_capability(connection_t * conn)
83  {  {
84          reset_buffer(&obuf);  
85          check_buffer(&obuf, strlen("CAPABILITY") + 12);          buffer_reset(&obuf);
86            buffer_check(&obuf, strlen("CAPABILITY") + 12);
87    
88          snprintf(obuf.data, obuf.size, "%04X CAPABILITY\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X CAPABILITY\r\n", tag);
89    
90          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
91  }  }
92    
93    
94  /*  /*
95   * IMAP NAMESPACE: discovers the prefix and delimeter of namespaces used by   * IMAP NAMESPACE: discovers the prefix and delimeter of namespaces used by the
96   * the server for mailboxes (RFC 2342).   * server for mailboxes (RFC 2342).
97   */   */
98  int  int
99  imap_namespace(conn_t * conn)  imap_namespace(connection_t * conn)
100  {  {
101          reset_buffer(&obuf);  
102          check_buffer(&obuf, strlen("NAMESPACE") + 12);          buffer_reset(&obuf);
103            buffer_check(&obuf, strlen("NAMESPACE") + 12);
104    
105          snprintf(obuf.data, obuf.size, "%04X NAMESPACE\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X NAMESPACE\r\n", tag);
106    
107          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
108  }  }
109    
110    
# Line 109  imap_namespace(conn_t * conn) Line 112  imap_namespace(conn_t * conn)
112   * IMAP LOGOUT: informs server that client is done.   * IMAP LOGOUT: informs server that client is done.
113   */   */
114  int  int
115  imap_logout(conn_t * conn)  imap_logout(connection_t * conn)
116  {  {
117          reset_buffer(&obuf);  
118          check_buffer(&obuf, strlen("LOGOUT") + 12);          buffer_reset(&obuf);
119            buffer_check(&obuf, strlen("LOGOUT") + 12);
120    
121          snprintf(obuf.data, obuf.size, "%04X LOGOUT\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X LOGOUT\r\n", tag);
122    
123          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
124  }  }
125    
126    
# Line 124  imap_logout(conn_t * conn) Line 128  imap_logout(conn_t * conn)
128   * IMAP STARTTLS: begin TLS negotiation.   * IMAP STARTTLS: begin TLS negotiation.
129   */   */
130  int  int
131  imap_starttls(conn_t * conn)  imap_starttls(connection_t * conn)
132  {  {
133          reset_buffer(&obuf);  
134          check_buffer(&obuf, strlen("STARTTLS") + 12);          buffer_reset(&obuf);
135            buffer_check(&obuf, strlen("STARTTLS") + 12);
136    
137          snprintf(obuf.data, obuf.size, "%04X STARTTLS\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X STARTTLS\r\n", tag);
138    
139          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
140  }  }
141    
142    
# Line 141  imap_starttls(conn_t * conn) Line 146  imap_starttls(conn_t * conn)
146   * authentication protocol exchange.   * authentication protocol exchange.
147   */   */
148  int  int
149  imap_authenticate(conn_t * conn, char *auth, int cont)  imap_authenticate(connection_t * conn, char *auth, int cont)
150  {  {
151          reset_buffer(&obuf);  
152          check_buffer(&obuf, strlen("AUTHENTICATE") + 12);          buffer_reset(&obuf);
153            buffer_check(&obuf, strlen("AUTHENTICATE") + 12);
154    
155          if (!cont) {          if (!cont) {
156                  snprintf(obuf.data, obuf.size, "%04X AUTHENTICATE %s\r\n",                  snprintf(obuf.data, obuf.size, "%04X AUTHENTICATE %s\r\n",
157                      tag, auth);                      tag, auth);
158                  return send_command(conn, obuf.data);                  return send_command(conn, obuf.data, NULL);
159          } else {          } else {
160                  send_command_cont(conn, auth);                  send_command_cont(conn, auth);
161                  return 0;                  return 0;
# Line 162  imap_authenticate(conn_t * conn, char *a Line 168  imap_authenticate(conn_t * conn, char *a
168   * IMAP LOGIN: identifies client to server.   * IMAP LOGIN: identifies client to server.
169   */   */
170  int  int
171  imap_login(conn_t * conn, char *user, char *pass)  imap_login(connection_t * conn, char *user, char *pass)
172  {  {
173          int r, n;          int r, n;
174          char *sbuf;          char *sbuf;
175    
176            /* Command to send to server. */
177          n = strlen("LOGIN") + strlen(user) + strlen(pass) + 18;          n = strlen("LOGIN") + strlen(user) + strlen(pass) + 18;
178          sbuf = (char *)smalloc(n);          sbuf = (char *)smalloc(n);
   
179          snprintf(sbuf, n, "%04X LOGIN \"%s\" \"%s\"\r\n", tag, user, pass);          snprintf(sbuf, n, "%04X LOGIN \"%s\" \"%s\"\r\n", tag, user, pass);
180    
181          r = send_command(conn, sbuf);          /* Alternate command with password shrouded for safe printing. */
182            buffer_reset(&obuf);
183            buffer_check(&obuf, strlen("LOGIN") + strlen(user) + 18);
184            snprintf(obuf.data, obuf.size, "%04X LOGIN \"%s\" *\r\n", tag, user);
185    
186            r = send_command(conn, sbuf, obuf.data);
187    
188          sfree(sbuf);          sfree(sbuf);
189    
# Line 184  imap_login(conn_t * conn, char *user, ch Line 195  imap_login(conn_t * conn, char *user, ch
195   * IMAP LIST: returns a subset of names from the complete set of names   * IMAP LIST: returns a subset of names from the complete set of names
196   * available to the client.   * available to the client.
197   *   *
198  int imap_list(conn_t *conn, char *refer, char *mbox)  int imap_list(connection_t *conn, char *refer, char *mbox)
199  {  {
200          int r;  
201                    buffer_reset(&obuf);
202          reset_buffer(&obuf);          buffer_check(&obuf, strlen("LIST") + strlen(refer) + strlen(mbox) + 18);
         check_buffer(&obuf, strlen("LIST") + strlen(refer) + strlen(mbox) + 18);  
203                    
204          snprintf(obuf.data, obuf.size, "%04X LIST \"%s\" \"%s\"\r\n", tag,          snprintf(obuf.data, obuf.size, "%04X LIST \"%s\" \"%s\"\r\n", tag,
205              refer, mbox);              refer, mbox);
206    
207          r = send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
   
         return r;  
208  }*/  }*/
209    
210    
211  /*  /*
212   * IMAP SUBSCRIBE: adds the specified mailbox name to the server's   * IMAP SUBSCRIBE: adds the specified mailbox name to the server's set of
213   * set of "active" or "subscribed" mailboxes.   * "active" or "subscribed" mailboxes.
214   */   */
215  int  int
216  imap_subscribe(conn_t * conn, char *mbox)  imap_subscribe(connection_t * conn, char *mbox)
217  {  {
218          reset_buffer(&obuf);  
219          check_buffer(&obuf, strlen("SUBSCRIBE") + strlen(mbox) + 15);          buffer_reset(&obuf);
220            buffer_check(&obuf, strlen("SUBSCRIBE") + strlen(mbox) + 15);
221    
222          snprintf(obuf.data, obuf.size, "%04X SUBSCRIBE \"%s\"\r\n", tag, mbox);          snprintf(obuf.data, obuf.size, "%04X SUBSCRIBE \"%s\"\r\n", tag, mbox);
223    
224          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
225  }  }
226    
227    
228  /*  /*
229   * IMAP EXAMINE: access a mailbox in READ-ONLY mode.   * IMAP EXAMINE: access a mailbox in READ-ONLY mode.
230   *   */
231  int  int
232  imap_examine(conn_t *conn, char *mbox)  imap_examine(connection_t * conn, char *mbox)
233  {  {
         reset_buffer(&obuf);  
         check_buffer(&obuf, strlen("EXAMINE") + strlen(mbox) + 15);  
234    
235          snprintf(obuf.data, MEDIUM_CMD, "%04X EXAMINE \"%s\"\r\n", tag, mbox);          buffer_reset(&obuf);
236            buffer_check(&obuf, strlen("EXAMINE") + strlen(mbox) + 15);
237    
238          return send_command(conn, obuf.data);          snprintf(obuf.data, obuf.size, "%04X EXAMINE \"%s\"\r\n", tag, mbox);
239  }*/  
240            return send_command(conn, obuf.data, NULL);
241    }
242    
243    
244  /*  /*
245   * IMAP SELECT: access a mailbox in READ-WRITE mode.   * IMAP SELECT: access a mailbox in READ-WRITE mode.
246   */   */
247  int  int
248  imap_select(conn_t * conn, char *mbox)  imap_select(connection_t * conn, char *mbox)
249  {  {
250          reset_buffer(&obuf);  
251          check_buffer(&obuf, strlen("SELECT") + strlen(mbox) + 15);          buffer_reset(&obuf);
252            buffer_check(&obuf, strlen("SELECT") + strlen(mbox) + 15);
253    
254          snprintf(obuf.data, obuf.size, "%04X SELECT \"%s\"\r\n", tag, mbox);          snprintf(obuf.data, obuf.size, "%04X SELECT \"%s\"\r\n", tag, mbox);
255    
256          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
257  }  }
258    
259    
# Line 250  imap_select(conn_t * conn, char *mbox) Line 261  imap_select(conn_t * conn, char *mbox)
261   * IMAP STATUS: requests status of the indicated mailbox.   * IMAP STATUS: requests status of the indicated mailbox.
262   */   */
263  int  int
264  imap_status(conn_t * conn, char *mbox, char *items)  imap_status(connection_t * conn, char *mbox, char *items)
265  {  {
266          reset_buffer(&obuf);  
267          check_buffer(&obuf, strlen("STATUS") + strlen(mbox) +          buffer_reset(&obuf);
268            buffer_check(&obuf, strlen("STATUS") + strlen(mbox) +
269              strlen(items) + 18);              strlen(items) + 18);
270    
271          snprintf(obuf.data, obuf.size, "%04X STATUS \"%s\" (%s)\r\n", tag, mbox, items);          snprintf(obuf.data, obuf.size, "%04X STATUS \"%s\" (%s)\r\n", tag,
272                mbox, items);
273    
274          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
275  }  }
276    
277    
# Line 266  imap_status(conn_t * conn, char *mbox, c Line 279  imap_status(conn_t * conn, char *mbox, c
279   * IMAP CREATE: create mailbox.   * IMAP CREATE: create mailbox.
280   */   */
281  int  int
282  imap_create(conn_t * conn, char *mbox)  imap_create(connection_t * conn, char *mbox)
283  {  {
284          reset_buffer(&obuf);  
285          check_buffer(&obuf, strlen("CREATE") + strlen(mbox) + 14);          buffer_reset(&obuf);
286            buffer_check(&obuf, strlen("CREATE") + strlen(mbox) + 14);
287    
288          snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox);          snprintf(obuf.data, obuf.size, "%04X CREATE \"%s\"\r\n", tag, mbox);
289    
290          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
291  }  }
292    
293    
# Line 281  imap_create(conn_t * conn, char *mbox) Line 295  imap_create(conn_t * conn, char *mbox)
295   * IMAP SEARCH: searches the mailbox for messages that match certain criteria.   * IMAP SEARCH: searches the mailbox for messages that match certain criteria.
296   */   */
297  int  int
298  imap_search(conn_t * conn, char *charset, char *search)  imap_search(connection_t * conn, char *charset, char *search)
299  {  {
300          reset_buffer(&obuf);  
301          check_buffer(&obuf, strlen("SEARCH CHARSET") + strlen(charset) +          buffer_reset(&obuf);
302            buffer_check(&obuf, strlen("SEARCH CHARSET") + strlen(charset) +
303              strlen(search) + 15);              strlen(search) + 15);
304    
305          if (*charset)          if (*charset)
306                  snprintf(obuf.data, obuf.size, "%04X SEARCH CHARSET \"%s\" %s\r\n",                  snprintf(obuf.data, obuf.size,
307                      tag, charset, search);                      "%04X SEARCH CHARSET \"%s\" %s\r\n", tag, charset, search);
308          else          else
309                  snprintf(obuf.data, obuf.size, "%04X SEARCH %s\r\n", tag, search);                  snprintf(obuf.data, obuf.size, "%04X SEARCH %s\r\n", tag,
310                        search);
311    
312          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
313  }  }
314    
315    
# Line 301  imap_search(conn_t * conn, char *charset Line 317  imap_search(conn_t * conn, char *charset
317   * IMAP FETCH: retrieves data associated with a message.   * IMAP FETCH: retrieves data associated with a message.
318   */   */
319  int  int
320  imap_fetch(conn_t * conn, char *mesg, char *items)  imap_fetch(connection_t * conn, char *mesg, char *items)
321  {  {
         reset_buffer(&obuf);  
         check_buffer(&obuf, strlen("FETCH") + strlen(mesg) + strlen(items) + 14);  
322    
323          snprintf(obuf.data, obuf.size, "%04X FETCH %s %s\r\n", tag, mesg, items);          buffer_reset(&obuf);
324            buffer_check(&obuf,
325                strlen("FETCH") + strlen(mesg) + strlen(items) + 14);
326    
327          return send_command(conn, obuf.data);          snprintf(obuf.data, obuf.size, "%04X FETCH %s %s\r\n", tag, mesg,
328                items);
329    
330            return send_command(conn, obuf.data, NULL);
331  }  }
332    
333    
# Line 316  imap_fetch(conn_t * conn, char *mesg, ch Line 335  imap_fetch(conn_t * conn, char *mesg, ch
335   * IMAP STORE: alters data associated with a message.   * IMAP STORE: alters data associated with a message.
336   */   */
337  int  int
338  imap_store(conn_t * conn, char *mesg, unsigned int mode, char *flags)  imap_store(connection_t * conn, char *mesg, unsigned int mode, char *flags)
339  {  {
340          reset_buffer(&obuf);  
341          check_buffer(&obuf, strlen("STORE") + strlen(mesg) +          buffer_reset(&obuf);
342            buffer_check(&obuf, strlen("STORE") + strlen(mesg) +
343              strlen("FLAGS.SILENT") + strlen(flags) + 18);              strlen("FLAGS.SILENT") + strlen(flags) + 18);
344    
345          snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n",          snprintf(obuf.data, obuf.size, "%04X STORE %s %sFLAGS.SILENT (%s)\r\n",
346              tag, mesg, (mode == STORE_FLAG_REPLACE ? "" :              tag, mesg, (mode == ACTION_FLAG_REPLACE ? "" :
347                  mode == STORE_FLAG_ADD ? "+" : "-"), flags);              mode == ACTION_FLAG_ADD ? "+" : "-"), flags);
348    
349          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
350  }  }
351    
352    
# Line 334  imap_store(conn_t * conn, char *mesg, un Line 354  imap_store(conn_t * conn, char *mesg, un
354   * IMAP COPY: copy messages to mailbox.   * IMAP COPY: copy messages to mailbox.
355   */   */
356  int  int
357  imap_copy(conn_t * conn, char *mesg, char *mbox)  imap_copy(connection_t * conn, char *mesg, char *mbox)
358  {  {
         reset_buffer(&obuf);  
         check_buffer(&obuf, strlen("COPY") + strlen(mesg) + strlen(mbox) + 16);  
359    
360          snprintf(obuf.data, obuf.size, "%04X COPY %s \"%s\"\r\n", tag, mesg, mbox);          buffer_reset(&obuf);
361            buffer_check(&obuf, strlen("COPY") + strlen(mesg) + strlen(mbox) + 16);
362    
363          return send_command(conn, obuf.data);          snprintf(obuf.data, obuf.size, "%04X COPY %s \"%s\"\r\n", tag, mesg,
364                mbox);
365    
366            return send_command(conn, obuf.data, NULL);
367  }  }
368    
369    
# Line 349  imap_copy(conn_t * conn, char *mesg, cha Line 371  imap_copy(conn_t * conn, char *mesg, cha
371   * IMAP APPEND: append message to the end of a mailbox.   * IMAP APPEND: append message to the end of a mailbox.
372   */   */
373  int  int
374  imap_append(conn_t * conn, char *mbox, char *flags, char *date, unsigned int size)  imap_append(connection_t * conn, char *mbox, char *flags, char *date, unsigned int size)
375  {  {
376          reset_buffer(&obuf);  
377          check_buffer(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) +          buffer_reset(&obuf);
378            buffer_check(&obuf, strlen("APPEND") + strlen(mbox) + strlen(flags) +
379              strlen(date) + strlen(ultostr(size, 10)) + 24);              strlen(date) + strlen(ultostr(size, 10)) + 24);
380    
381          snprintf(obuf.data, obuf.size, "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n",          snprintf(obuf.data, obuf.size,
382              tag, mbox, flags, date, size);              "%04X APPEND \"%s\" (%s) \"%s\" {%d}\r\n", tag, mbox, flags, date,
383                size);
384    
385          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
386  }  }
387    
388    
# Line 367  imap_append(conn_t * conn, char *mbox, c Line 391  imap_append(conn_t * conn, char *mbox, c
391   * IMAP CLOSE: delete messages and return to authenticated state.   * IMAP CLOSE: delete messages and return to authenticated state.
392   */   */
393  int  int
394  imap_close(conn_t * conn)  imap_close(connection_t * conn)
395  {  {
396          reset_buffer(&obuf);  
397          check_buffer(&obuf, strlen("CLOSE") + 12);          buffer_reset(&obuf);
398            buffer_check(&obuf, strlen("CLOSE") + 12);
399    
400          snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X CLOSE\r\n", tag);
401    
402          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
403  }  }
404    
405    
# Line 382  imap_close(conn_t * conn) Line 407  imap_close(conn_t * conn)
407   * IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set.   * IMAP EXPUNGE: permanently removes any messages with the \Deleted flag set.
408   */   */
409  int  int
410  imap_expunge(conn_t * conn)  imap_expunge(connection_t * conn)
411  {  {
412          reset_buffer(&obuf);  
413          check_buffer(&obuf, strlen("EXPUNGE") + 12);          buffer_reset(&obuf);
414            buffer_check(&obuf, strlen("EXPUNGE") + 12);
415    
416          snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag);          snprintf(obuf.data, obuf.size, "%04X EXPUNGE\r\n", tag);
417    
418          return send_command(conn, obuf.data);          return send_command(conn, obuf.data, NULL);
419  }  }

Legend:
Removed from v.1.38.2.2  
changed lines
  Added in v.1.46

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26