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