Add to git.
[monolith.git] / chat / chatroom.h
1 /* Monolith chatroom.
2  * - by Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: chatroom.h,v 1.6 2003/02/22 15:34:27 rich Exp $
19  */
20
21 #ifndef CHATROOM_H
22 #define CHATROOM_H
23
24 #include <pool.h>
25 #include <hash.h>
26 #include <vector.h>
27 #include <pthr_dbi.h>
28 #include <monolith.h>
29
30 #include "message.h"
31
32 struct chatroom;
33 typedef struct chatroom *chatroom;
34
35 /* Get (or create) the shared chatroom object for resource resid. */
36 extern chatroom get_chatroom (db_handle dbh, int resid);
37
38 /* Get the resid of a particular chatroom. */
39 extern int chatroom_get_resid (const chatroom r);
40
41 /* Get the index of the first and last message + 1 available in the room.
42  * NB. The '+ 1'.
43  */
44 extern int chatroom_first_message_index (const chatroom r);
45 extern int chatroom_last_message_index (const chatroom r);
46
47 /* Get a message. If you try to get a message < first index, then this
48  * function returns NULL. If you try to get a message >= last index, then
49  * this function sleeps until the message becomes available. Before
50  * going to sleep, the function "pre_sleep (session, data)" is called.
51  */
52 extern message chatroom_get_message (const chatroom r, int index, void (*pre_sleep) (ml_session, void *data), ml_session session, void *data);
53
54 /* Get the next message across several chatrooms. This is used by the
55  * chatbot interface. 'rooms' is a vector of chatroom objects.
56  * 'next_msgids' is a hash from chatroom resid -> message ID
57  * (equivalent to the 'index' parameter to the chatroom_get_message
58  * function above). On success, the function sets *msg_rtn and
59  * *room_rtn and returns 0. On failure (basically, message < first
60  * index as above), then this sets *room_rtn only and returns -1.
61  */
62 extern int chatroom_get_message_multiple (const vector rooms, const hash next_msgids, message *msg_rtn, chatroom *room_rtn);
63
64 /* Post a message into the chatroom. Assumes the current time and the
65  * current user identity from the session.
66  */
67 extern void chatroom_post (chatroom r, ml_session session, const char *conninfo, const char *body);
68
69 /* Post a blank message to all chatrooms. */
70 extern void chatroom_spam_blank (pool thread_pool);
71
72 /* Enter and leave the chatroom. The chatroom object uses these functions
73  * to maintain a list of who is in the room at a moment in time.
74  */
75 extern void chatroom_enter (chatroom r, int userid, const char *username);
76 extern void chatroom_leave (chatroom r);
77
78 /* Get the current list of users. This returns a hash of userid -> count
79  * where count is the number of times this user is in the room. In
80  * particular, element 0 is the number of anonymous users in the room.
81  */
82 extern const hash chatroom_users (chatroom r);
83
84 /* This function is used to wait for changes in the list of users. It
85  * sleeps until a user enters or leaves the room and then returns.
86  */
87 extern void chatroom_wait_enter_leave_event (chatroom r, ml_session session);
88
89 #endif /* CHATROOM_H */