Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

forum.ssjs 9.2 KB
Newer Older
1 2 3 4 5
/*  This script is an interface between HTTP clients and the functions defined
    in web/lib/forum.js.  A basic check for an authenticated, non-guest user
    is done here; otherwise all permission checking is done at the function
    level. */

6
var settings = load('modopts.js', 'web');
7 8 9 10 11 12 13 14

load(settings.web_directory + '/lib/init.js');
load(settings.web_lib + 'auth.js');
load(settings.web_lib + 'forum.js');

var reply = {};

// There must be an API call, and the user must not be a guest or unknown
echicken's avatar
echicken committed
15
if (http_request.query.call !== undefined && (http_request.method === 'GET' || http_request.method === 'POST')) {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

    var handled = false;

    // Authenticated calls
    if (user.number > 0 && user.alias !== settings.guest) {

        handled = true;

        switch(http_request.query.call[0].toLowerCase()) {

            case 'get-mail-unread-count':
                reply.count = user.stats.mail_waiting;
                break;

            case 'get-mail-body':
                if (typeof http_request.query.number !== 'undefined') {
                    reply = getMailBody(http_request.query.number[0]);
                }
                break;

            case 'get-signature':
                reply.signature = getSignature();
                break;

            case 'post-reply':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.body !== 'undefined' &&
                    typeof http_request.query.pid !== 'undefined'
                ) {
                    reply.success = postReply(
                        http_request.query.sub[0],
                        http_request.query.body[0],
                        Number(http_request.query.pid[0])
                    );
                } else {
                    reply.success = false;
                }
                break;

            case 'post':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.to !== 'undefined' &&
                    typeof http_request.query.subject !== 'undefined' &&
                    typeof http_request.query.body !== 'undefined'
                ) {
                    reply.success = postNew(
                        http_request.query.sub[0],
                        http_request.query.to[0],
                        http_request.query.subject[0],
                        http_request.query.body[0]
                    );
                } else {
                    reply.success = false;
                }
                break;

            case 'delete-message':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.number !== 'undefined'
                ) {
                    reply.success = deleteMessage(
                        http_request.query.sub[0],
                        http_request.query.number[0]
                    );
                } else {
                    reply.success = false;
                }
                break;

            case 'delete-mail':
                if (typeof http_request.query.number !== 'undefined') {
                    reply.success = deleteMail(http_request.query.number);
                } else {
                    reply.success = false;
                }
                break;

            case 'set-scan-cfg':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.cfg !== 'undefined'
                ) {
                    reply.success = setScanCfg(
                        http_request.query.sub[0],
                        http_request.query.cfg[0]
                    );
                } else {
                    reply.success = false;
                }
                break;

            case 'vote':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.id !== 'undefined' &&
                    typeof http_request.query.up !== 'undefined' &&
                    !(user.security.restrictions&UFLAG_V)
                ) {
                    reply.success = voteMessage(
                        http_request.query.sub[0],
                        http_request.query.id[0],
                        http_request.query.up[0]
                    );
                } else {
                    reply.success = false;
                }
                break;

            case 'submit-poll-answers':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.id !== 'undefined' &&
                    typeof http_request.query.answer !== 'undefined'
                ) {
                    reply.success = submitPollAnswers(
                        http_request.query.sub[0],
                        http_request.query.id[0],
                        http_request.query.answer
                    );
                }
                break;

            case 'submit-poll':
                if (typeof http_request.query.subject !== 'undefined' &&
                    typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.votes !== 'undefined' &&
                    typeof http_request.query.results !== 'undefined' &&
                    typeof http_request.query.answer !== 'undefined'
                ) {
                    reply.success = postPoll(
                        http_request.query.sub[0],
                        http_request.query.subject[0],
                        http_request.query.votes[0],
                        http_request.query.results[0],
                        http_request.query.answer,
                        http_request.query.comment || []
                    );
                }
                break;

echicken's avatar
echicken committed
153 154 155 156 157 158 159 160
            case 'block-sender':
                if (user.is_sysop) {
                    if (http_request.query.from) addTwit(decodeURIComponent(http_request.query.from[0]));
                    if (http_request.query.from_net) addTwit(decodeURIComponent(http_request.query.from_net[0]));
                    reply.err = null;
                }
                break;

161 162 163
            default:
                handled = false;
                break;
164

165
        }
166

167
    }
168

169 170
    // Unauthenticated calls
    if (!handled) {
171

172
        switch(http_request.query.call[0].toLowerCase()) {
173

174 175 176 177 178 179 180
            case 'get-thread-votes':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.id !== 'undefined'
                ) {
                    var id = parseInt(http_request.query.id[0]);
                    if (!isNaN(id)) {
                        reply = getVotesInThread(
181
                            http_request.query.sub[0],
182
                            id
183 184
                        );
                    }
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                }
                break;

            case 'get-sub-votes':
                if (typeof http_request.query.sub !== 'undefined') {
                    reply = getVotesInThreads(http_request.query.sub[0]);
                }
                break;

            case 'get-poll-results':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.id !== 'undefined'
                ) {
                    reply = getUserPollData(
                        http_request.query.sub[0],
                        http_request.query.id[0]
                    );
                }
                break;

            case 'list-groups':
                reply = listGroups();
                break;

            case 'list-subs':
                if (typeof http_request.query.group !== 'undefined') {
                    reply = listSubs(http_request.query.group[0]);
                }
                break;

            case 'list-threads':
                if (typeof http_request.query.sub !== 'undefined' &&
                    typeof http_request.query.offset !== 'undefined'
                ) {
                    if (typeof http_request.query.count !== 'undefined') {
                        var count = http_request.query.count[0];
221
                    }
222 223 224 225 226 227 228 229 230 231 232 233 234
                    reply = listThreads(
                        http_request.query.sub[0],
                        http_request.query.offset[0],
                        count || settings.page_size
                    ).threads;
                }
                break;

            case 'get-group-unread-count':
                if (typeof http_request.query.group !== 'undefined') {
                    http_request.query.group.forEach(
                        function(group) {
                            reply[group] = getGroupUnreadCount(group);
235
                        }
236 237 238 239 240 241 242 243 244
                    );
                }
                break;

            case 'get-sub-unread-count':
                if (typeof http_request.query.sub !== 'undefined') {
                    http_request.query.sub.forEach(
                        function(sub) {
                            reply[sub] = getSubUnreadCount(sub);
245
                        }
246 247 248
                    );
                }
                break;
249

250 251
            default:
                break;
252

253
        }
254

echicken's avatar
echicken committed
255
    }
echicken's avatar
echicken committed
256

257 258 259 260 261 262
}

reply = JSON.stringify(reply);
http_reply.header['Content-Type'] = 'application/json';
http_reply.header['Content-Length'] = reply.length;
write(reply);