stable now but there are GIANT PIECES MISSING
[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  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 /*
17  * Name of the New User Greetings room.
18  */
19 #define NEWUSERGREETINGS        "New User Greetings"
20
21 #include "sysdep.h"
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <time.h>
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <limits.h>
34 #include "ctdl_module.h"
35 #include "citadel.h"
36 #include "server.h"
37 #include "citserver.h"
38 #include "support.h"
39 #include "config.h"
40 #include "user_ops.h"
41 #include "database.h"
42 #include "msgbase.h"
43
44 /*
45  * Copy the contents of the New User Greetings> room to the user's Mail> room.
46  */
47 void CopyNewUserGreetings(void) {
48         struct cdbdata *cdbfr;
49         long *msglist = NULL;
50         int num_msgs = 0;
51         char mailboxname[ROOMNAMELEN];
52
53
54         /* Only do this for new users. */
55         if (CC->user.timescalled != 1) return;
56
57         /* This user's mailbox. */
58         CtdlMailboxName(mailboxname, sizeof mailboxname, &CC->user, MAILROOM);
59
60         /* Go to the source room ... bail out silently if it's not there,
61          * or if it's not private.
62          */
63         if (CtdlGetRoom(&CC->room, NEWUSERGREETINGS) != 0) return;
64         if ((CC->room.QRflags & QR_PRIVATE) == 0) return;
65
66         cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
67
68         if (cdbfr != NULL) {
69                 msglist = malloc(cdbfr->len);
70                 memcpy(msglist, cdbfr->ptr, cdbfr->len);
71                 num_msgs = cdbfr->len / sizeof(long);
72                 cdb_free(cdbfr);
73         }
74
75         if (num_msgs > 0) {
76                 CtdlSaveMsgPointersInRoom(mailboxname, msglist, num_msgs, 1, NULL, 0);
77         }
78
79         /* Now free the memory we used, and go away. */
80         if (msglist != NULL) free(msglist);
81 }
82
83
84 CTDL_MODULE_INIT(newuser)
85 {
86         if (!threading)
87         {
88                 CtdlRegisterSessionHook(CopyNewUserGreetings, EVT_LOGIN, PRIO_LOGIN + 1);
89         }
90         
91         /* return our module name for the log */
92         return "newuser";
93 }