config.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* $Id$
  2. *
  3. * isync - IMAP4 to maildir mailbox synchronizer
  4. * Copyright (C) 2000 Michael R. Elkins <me@mutt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <unistd.h>
  21. #include <limits.h>
  22. #include <errno.h>
  23. #include <pwd.h>
  24. #include <sys/types.h>
  25. #include <string.h>
  26. #include "isync.h"
  27. static config_t *box = 0;
  28. /* set defaults from the global configuration section */
  29. void
  30. config_defaults (config_t * conf)
  31. {
  32. conf->user = global.user;
  33. conf->pass = global.pass;
  34. conf->port = global.port;
  35. conf->box = global.box;
  36. conf->host = global.host;
  37. conf->max_size = global.max_size;
  38. conf->copy_deleted_to = global.copy_deleted_to;
  39. conf->use_namespace = global.use_namespace;
  40. conf->expunge = global.expunge;
  41. #if HAVE_LIBSSL
  42. conf->require_ssl = global.require_ssl;
  43. conf->use_imaps = global.use_imaps;
  44. conf->cert_file = global.cert_file;
  45. conf->use_sslv2 = global.use_sslv2;
  46. conf->use_sslv3 = global.use_sslv3;
  47. conf->use_tlsv1 = global.use_tlsv1;
  48. #endif
  49. }
  50. void
  51. load_config (const char *where)
  52. {
  53. char path[_POSIX_PATH_MAX];
  54. char buf[1024];
  55. struct passwd *pw;
  56. config_t **cur = &box;
  57. int line = 0;
  58. FILE *fp;
  59. char *p, *cmd, *val;
  60. if (!where)
  61. {
  62. pw = getpwuid (getuid ());
  63. snprintf (path, sizeof (path), "%s/.isyncrc", pw->pw_dir);
  64. where = path;
  65. }
  66. printf ("Reading %s\n", where);
  67. fp = fopen (where, "r");
  68. if (!fp)
  69. {
  70. if (errno != ENOENT)
  71. {
  72. perror ("fopen");
  73. return;
  74. }
  75. }
  76. buf[sizeof buf - 1] = 0;
  77. while ((fgets (buf, sizeof (buf) - 1, fp)))
  78. {
  79. p = buf;
  80. cmd = next_arg (&p);
  81. val = next_arg (&p);
  82. line++;
  83. if (!cmd || *cmd == '#')
  84. continue;
  85. if (!strncasecmp ("mailbox", cmd, 7))
  86. {
  87. if (*cur)
  88. cur = &(*cur)->next;
  89. *cur = calloc (1, sizeof (config_t));
  90. config_defaults (*cur);
  91. (*cur)->path = strdup (val);
  92. }
  93. else if (!strncasecmp ("host", cmd, 4))
  94. {
  95. #if HAVE_LIBSSL
  96. if (!strncasecmp ("imaps:", val, 6))
  97. {
  98. val += 6;
  99. if (*cur)
  100. {
  101. (*cur)->use_imaps = 1;
  102. (*cur)->port = 993;
  103. }
  104. else
  105. {
  106. global.use_imaps = 1;
  107. global.port = 993;
  108. }
  109. }
  110. #endif
  111. if (*cur)
  112. (*cur)->host = strdup (val);
  113. else
  114. global.host = strdup (val);
  115. }
  116. else if (!strncasecmp ("user", cmd, 4))
  117. {
  118. if (*cur)
  119. (*cur)->user = strdup (val);
  120. else
  121. global.user = strdup (val);
  122. }
  123. else if (!strncasecmp ("pass", cmd, 4))
  124. {
  125. if (*cur)
  126. (*cur)->pass = strdup (val);
  127. else
  128. global.pass = strdup (val);
  129. }
  130. else if (!strncasecmp ("port", cmd, 4))
  131. {
  132. if (*cur)
  133. (*cur)->port = atoi (val);
  134. else
  135. global.port = atoi (val);
  136. }
  137. else if (!strncasecmp ("box", cmd, 3))
  138. {
  139. if (*cur)
  140. (*cur)->box = strdup (val);
  141. else
  142. global.box = strdup (val);
  143. }
  144. else if (!strncasecmp ("alias", cmd, 5))
  145. {
  146. if (*cur)
  147. (*cur)->alias = strdup (val);
  148. }
  149. else if (!strncasecmp ("maxsize", cmd, 7))
  150. {
  151. if (*cur)
  152. (*cur)->max_size = atol (val);
  153. else
  154. global.max_size = atol (val);
  155. }
  156. else if (!strncasecmp ("UseNamespace", cmd, 12))
  157. {
  158. if (*cur)
  159. (*cur)->use_namespace = (strcasecmp (val, "yes") == 0);
  160. else
  161. global.use_namespace = (strcasecmp (val, "yes") == 0);
  162. }
  163. else if (!strncasecmp ("CopyDeletedTo", cmd, 13))
  164. {
  165. if (*cur)
  166. (*cur)->copy_deleted_to = strdup (val);
  167. else
  168. global.copy_deleted_to = strdup (val);
  169. }
  170. else if (!strncasecmp ("Expunge", cmd, 7))
  171. {
  172. if (*cur)
  173. (*cur)->expunge = (strcasecmp (val, "yes") == 0);
  174. else
  175. global.expunge = (strcasecmp (val, "yes") == 0);
  176. }
  177. #if HAVE_LIBSSL
  178. else if (!strncasecmp ("CertificateFile", cmd, 15))
  179. {
  180. if (*cur)
  181. (*cur)->cert_file = strdup (val);
  182. else
  183. global.cert_file = strdup (val);
  184. }
  185. else if (!strncasecmp ("RequireSSL", cmd, 10))
  186. {
  187. if (*cur)
  188. (*cur)->require_ssl = (strcasecmp (val, "yes") == 0);
  189. else
  190. global.require_ssl = (strcasecmp (val, "yes") == 0);
  191. }
  192. else if (!strncasecmp ("UseSSLv2", cmd, 8))
  193. {
  194. if (*cur)
  195. (*cur)->use_sslv2 = (strcasecmp (val, "yes") == 0);
  196. else
  197. global.use_sslv2 = (strcasecmp (val, "yes") == 0);
  198. }
  199. else if (!strncasecmp ("UseSSLv3", cmd, 8))
  200. {
  201. if (*cur)
  202. (*cur)->use_sslv3 = (strcasecmp (val, "yes") == 0);
  203. else
  204. global.use_sslv3 = (strcasecmp (val, "yes") == 0);
  205. }
  206. else if (!strncasecmp ("UseTLSv1", cmd, 8))
  207. {
  208. if (*cur)
  209. (*cur)->use_tlsv1 = (strcasecmp (val, "yes") == 0);
  210. else
  211. global.use_tlsv1 = (strcasecmp (val, "yes") == 0);
  212. }
  213. else if (!strncasecmp ("RequireCRAM", cmd, 11))
  214. {
  215. if (*cur)
  216. (*cur)->require_cram = (strcasecmp (val, "yes") == 0);
  217. else
  218. global.require_cram = (strcasecmp (val, "yes") == 0);
  219. }
  220. #endif
  221. else if (buf[0])
  222. printf ("%s:%d:unknown command:%s", path, line, cmd);
  223. }
  224. fclose (fp);
  225. }
  226. config_t *
  227. find_box (const char *s)
  228. {
  229. config_t *p = box;
  230. for (; p; p = p->next)
  231. if (!strcmp (s, p->path) || (p->alias && !strcmp (s, p->alias)))
  232. return p;
  233. return 0;
  234. }