71f89ad636db0ffb91aa47c4fd3dff425d6e2e01
[citadel.git] / citadel / modules / newuser / serv_newuser.c
1 /*
2  * $Id$
3  *
4  * Automatically copies the contents of a "New User Greetings" room to the
5  * inbox of any new user upon account creation.
6  *
7  * Copyright (c) 1987-2010 by the citadel.org team
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /*
25  * Name of the New User Greetings room.
26  */
27 #define NEWUSERGREETINGS        "New User Greetings"
28
29
30 #include "sysdep.h"
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <signal.h>
36 #include <pwd.h>
37 #include <errno.h>
38 #include <sys/types.h>
39
40 #if TIME_WITH_SYS_TIME
41 # include <sys/time.h>
42 # include <time.h>
43 #else
44 # if HAVE_SYS_TIME_H
45 #  include <sys/time.h>
46 # else
47 #  include <time.h>
48 # endif
49 #endif
50
51 #include <sys/wait.h>
52 #include <string.h>
53 #include <limits.h>
54
55 #include "ctdl_module.h"
56
57 #include "citadel.h"
58 #include "server.h"
59 #include "citserver.h"
60 #include "support.h"
61 #include "config.h"
62 #include "user_ops.h"
63 #include "database.h"
64 #include "msgbase.h"
65
66
67
68
69
70
71 /*
72  * Copy the contents of the New User Greetings> room to the user's Mail> room.
73  */
74 void CopyNewUserGreetings(void) {
75         struct cdbdata *cdbfr;
76         long *msglist = NULL;
77         int num_msgs = 0;
78         char mailboxname[ROOMNAMELEN];
79
80
81         /* Only do this for new users. */
82         if (CC->user.timescalled != 1) return;
83
84         /* This user's mailbox. */
85         CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
86
87         /* Go to the source room ... bail out silently if it's not there,
88          * or if it's not private.
89          */
90         if (CtdlGetRoom(&CC->room, NEWUSERGREETINGS) != 0) return;
91         if ((CC->room.QRflags & QR_PRIVATE) == 0) return;
92
93         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
94
95         if (cdbfr != NULL) {
96                 msglist = malloc(cdbfr->len);
97                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
98                 num_msgs = cdbfr->len / sizeof(long);
99                 cdb_free(cdbfr);
100         }
101
102         if (num_msgs > 0) {
103                 CtdlSaveMsgPointersInRoom(mailboxname, msglist, num_msgs, 1, NULL, 0);
104         }
105
106         /* Now free the memory we used, and go away. */
107         if (msglist != NULL) free(msglist);
108 }
109
110
111 CTDL_MODULE_INIT(newuser)
112 {
113         if (!threading)
114         {
115                 CtdlRegisterSessionHook(CopyNewUserGreetings, EVT_LOGIN);
116         }
117         
118         /* return our Subversion id for the Log */
119         return "$Id$";
120 }