Scan each room's netconfig for remote POP3 account definitions
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * Aggregate remote POP3 accounts
3  */
4
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <stdio.h>
8
9 #if TIME_WITH_SYS_TIME
10 # include <sys/time.h>
11 # include <time.h>
12 #else
13 # if HAVE_SYS_TIME_H
14 #  include <sys/time.h>
15 # else
16 #  include <time.h>
17 # endif
18 #endif
19
20 #include <ctype.h>
21 #include <string.h>
22 #include <errno.h>
23 #include "citadel.h"
24 #include "server.h"
25 #include "citserver.h"
26 #include "support.h"
27 #include "config.h"
28 #include "tools.h"
29 #include "room_ops.h"
30 #include "ctdl_module.h"
31
32 struct pop3aggr {
33         struct pop3aggr *next;
34         char roomname[ROOMNAMELEN];
35         char pop3host[128];
36         char pop3user[128];
37         char pop3pass[128];
38 };
39
40 struct pop3aggr *palist = NULL;
41
42 #ifdef POP3_AGGREGATION
43
44
45 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
46 {
47         lprintf(CTDL_DEBUG, "POP3: %s %s %s %s\n", roomname, pop3host, pop3user, pop3pass);
48 }
49
50
51 /*
52  * Scan a room's netconfig to determine whether it requires POP3 aggregation
53  */
54 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
55 {
56         char filename[PATH_MAX];
57         char buf[1024];
58         char instr[32];
59         FILE *fp;
60         struct pop3aggr *pptr;
61
62         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
63
64         /* Only do net processing for rooms that have netconfigs */
65         fp = fopen(filename, "r");
66         if (fp == NULL) {
67                 return;
68         }
69
70         while (fgets(buf, sizeof buf, fp) != NULL) {
71                 buf[strlen(buf)-1] = 0;
72
73                 extract_token(instr, buf, 0, '|', sizeof instr);
74                 if (!strcasecmp(instr, "pop3client")) {
75                         pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
76                         if (pptr != NULL) {
77                                 extract_token(pptr->roomname, buf, 1, '|', sizeof pptr->roomname);
78                                 extract_token(pptr->pop3host, buf, 2, '|', sizeof pptr->pop3host);
79                                 extract_token(pptr->pop3user, buf, 3, '|', sizeof pptr->pop3user);
80                                 extract_token(pptr->pop3pass, buf, 4, '|', sizeof pptr->pop3pass);
81                                 pptr->next = palist;
82                                 palist = pptr;
83                         }
84                 }
85
86         }
87
88         fclose(fp);
89
90 }
91
92
93 void pop3client_scan(void) {
94         static time_t last_run = 0L;
95         static int doing_pop3client = 0;
96         struct pop3aggr *pptr;
97
98         /*
99          * Run POP3 aggregation no more frequently than once every n seconds
100          */
101         if ( (time(NULL) - last_run) < config.c_net_freq ) {
102                 return;
103         }
104
105         /*
106          * This is a simple concurrency check to make sure only one pop3client run
107          * is done at a time.  We could do this with a mutex, but since we
108          * don't really require extremely fine granularity here, we'll do it
109          * with a static variable instead.
110          */
111         if (doing_pop3client) return;
112         doing_pop3client = 1;
113
114         lprintf(CTDL_DEBUG, "pop3client started\n");
115         ForEachRoom(pop3client_scan_room, NULL);
116
117         while (palist != NULL) {
118                 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
119                 pptr = palist;
120                 palist = palist->next;
121                 free(pptr);
122         }
123
124         lprintf(CTDL_DEBUG, "pop3client ended\n");
125         doing_pop3client = 0;
126 }
127
128 #endif
129
130 CTDL_MODULE_INIT(pop3client)
131 {
132 #ifdef POP3_AGGREGATION
133         CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
134 #endif
135
136         /* return our Subversion id for the Log */
137         return "$Id:  $";
138 }
139