aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/control/pad/pad_control.js
blob: 3c322024a8a242b727c2b18d57c2e152971d12f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("funhtml.*");
import("comet");
import("email.sendEmail");
import("fastJSON");
import("jsutils.eachProperty");
import("sqlbase.sqlbase");
import("stringutils.{toHTML,md5}");
import("stringutils");

import("etherpad.collab.collab_server");
import("etherpad.debug.dmesg");
import("etherpad.globals.*");
import("etherpad.helpers");
import("etherpad.licensing");
import("etherpad.quotas");
import("etherpad.log");
import("etherpad.log.{logRequest,logException}");
import("etherpad.sessions");
import("etherpad.sessions.getSession");
import("etherpad.utils.*");
import("etherpad.pro.pro_padmeta");
import("etherpad.pro.pro_pad_db");
import("etherpad.pro.pro_utils");
import("etherpad.pro.pro_accounts.getSessionProAccount");
import("etherpad.pro.domains");
import("etherpad.pro.pro_config");
import("etherpad.pne.pne_utils");
import("etherpad.pro.pro_quotas");

import("etherpad.pad.revisions");
import("etherpad.pad.chatarchive");
import("etherpad.pad.model");
import("etherpad.pad.padutils");
import("etherpad.pad.padusers");
import("etherpad.control.pad.pad_view_control");
import("etherpad.control.pad.pad_changeset_control");
import("etherpad.control.pad.pad_importexport_control");
import("etherpad.collab.readonly_server");

import("dispatch.{Dispatcher,PrefixMatcher,DirMatcher,forward}");

jimport("java.lang.System.out.println");

var DISABLE_PAD_CREATION = false;

function onStartup() {
  sqlbase.createJSONTable("PAD_DIAGNOSTIC");
}

function onRequest() {

  // TODO: take a hard look at /ep/pad/FOO/BAR/ dispatching.
  //      Perhaps standardize on /ep/pad/<pad-id>/foo
  if (request.path.indexOf('/ep/pad/auth/') == 0) {
    if (request.isGet) {
      return render_auth_get();
    }
    if (request.isPost) {
      return render_auth_post();
    }
  }

  if (pro_utils.isProDomainRequest()) {
    pro_quotas.perRequestBillingCheck();
  }

  var disp = new Dispatcher();
  disp.addLocations([
    [PrefixMatcher('/ep/pad/view/'), forward(pad_view_control)],
    [PrefixMatcher('/ep/pad/changes/'), forward(pad_changeset_control)],
    [PrefixMatcher('/ep/pad/impexp/'), forward(pad_importexport_control)],
    [PrefixMatcher('/ep/pad/export/'), pad_importexport_control.renderExport]
  ]);
  return disp.dispatch();
}

//----------------------------------------------------------------
// utils
//----------------------------------------------------------------

function getDefaultPadText() {
  if (pro_utils.isProDomainRequest()) {
    return pro_config.getConfig().defaultPadText;
  }
  return renderTemplateAsString("misc/pad_default.ejs", {padUrl: request.url.split("?", 1)[0]});
}

function assignName(pad, userId) {
  if (padusers.isGuest(userId)) {
    // use pad-specific name if possible
    var userData = pad.getAuthorData(userId);
    var nm = (userData && userData.name) || padusers.getUserName() || null;

    // don't let name guest typed in last once we've assigned a name
    // for this pad, so the user can change it
    delete getSession().guestDisplayName;

    return nm;
  }
  else {
    return padusers.getUserName();
  }
}

function assignColorId(pad, userId) {
  // use pad-specific color if possible
  var userData = pad.getAuthorData(userId);
  if (userData && ('colorId' in userData)) {
    return userData.colorId;
  }

  // assign random unique color
  function r(n) {
    return Math.floor(Math.random() * n);
  }
  var colorsUsed = {};
  var users = collab_server.getConnectedUsers(pad);
  var availableColors = [];
  users.forEach(function(u) {
    colorsUsed[u.colorId] = true;
  });
  for (var i = 0; i < COLOR_PALETTE.length; i++) {
    if (!colorsUsed[i]) {
      availableColors.push(i);
    }
  }
  if (availableColors.length > 0) {
    return availableColors[r(availableColors.length)];
  } else {
    return r(COLOR_PALETTE.length);
  }
}

function _getPrivs() {
  return {
    maxRevisions: quotas.getMaxSavedRevisionsPerPad()
  };
}

//----------------------------------------------------------------
// linkfile (a file that users can save that redirects them to
// a particular pad; auto-download)
//----------------------------------------------------------------
function render_linkfile() {
  var padId = request.params.padId;

  renderHtml("pad/pad_download_link.ejs", {
    padId: padId
  });

  response.setHeader("Content-Disposition", "attachment; filename=\""+padId+".html\"");
}

//----------------------------------------------------------------
// newpad
//----------------------------------------------------------------

function render_newpad() {
  var session = getSession();
  var padId;

  if (pro_utils.isProDomainRequest()) {
    padId = pro_pad_db.getNextPadId();
  } else {
    padId = randomUniquePadId();
  }

  session.instantCreate = padId;
  response.redirect("/"+padId);
}

// Tokbox
function render_newpad_xml_post() {
  var localPadId;
  if (pro_utils.isProDomainRequest()) {
    localPadId = pro_pad_db.getNextPadId();
  } else {
    localPadId = randomUniquePadId();
  }
  // <RAFTER>
  if (DISABLE_PAD_CREATION) {
    if (! pro_utils.isProDomainRequest()) {
      utils.render500();
      return;
    }
  }
  // </RAFTER>

  padutils.accessPadLocal(localPadId, function(pad) {
    if (!pad.exists()) {
      pad.create(getDefaultPadText());
    }
  });
  response.setContentType('text/plain; charset=utf-8');
  response.write([
    '<newpad>',
    '<url>http://'+request.host+'/'+localPadId+'</url>',
    '</newpad>'
  ].join('\n'));
}

//----------------------------------------------------------------
// pad
//----------------------------------------------------------------

function _createIfNecessary(localPadId, pad) {
  if (pad.exists()) {
    delete getSession().instantCreate;
    return;
  }
  // make sure localPadId is valid.
  var validPadId = padutils.makeValidLocalPadId(localPadId);
  if (localPadId != validPadId) {
    response.redirect('/'+validPadId);
  }
  // <RAFTER>
  if (DISABLE_PAD_CREATION) {
    if (! pro_utils.isProDomainRequest()) {
      response.redirect("/ep/pad/create?padId="+encodeURIComponent(localPadId));
      return;
    }
  }
  // </RAFTER>
  // tokbox may use createImmediately
  if (request.params.createImmediately || getSession().instantCreate == localPadId) {
    pad.create(getDefaultPadText());
    delete getSession().instantCreate;
    return;
  }
  response.redirect("/ep/pad/create?padId="+encodeURIComponent(localPadId));
}

function _promptForMobileDevices(pad) {
  // TODO: also work with blackbery and windows mobile and others
  if (request.userAgent.isIPhone() && (!request.params.skipIphoneCheck)) {
    renderHtml("pad/pad_iphone_body.ejs", {padId: pad.getLocalId()});
    response.stop();
  }
}

function _checkPadQuota(pad) {
  var numConnectedUsers = collab_server.getNumConnections(pad);
  var maxUsersPerPad = quotas.getMaxSimultaneousPadEditors(pad.getId());

  if (numConnectedUsers >= maxUsersPerPad) {
    log.info("rendered-padfull");
    renderFramed('pad/padfull_body.ejs',
                  {maxUsersPerPad: maxUsersPerPad, padId: pad.getLocalId()});
    response.stop();
  }

  if (pne_utils.isPNE()) {
    if (!licensing.canSessionUserJoin()) {
      renderFramed('pad/total_users_exceeded.ejs', {
        userQuota: licensing.getActiveUserQuota(),
        activeUserWindowHours: licensing.getActiveUserWindowHours()
      });
      response.stop();
    }
  }
}

function _checkIfDeleted(pad) {
  // TODO: move to access control check on access?
  if (pro_utils.isProDomainRequest()) {
    pro_padmeta.accessProPad(pad.getId(), function(propad) {
      if (propad.exists() && propad.isDeleted()) {
        renderNoticeString("This pad has been deleted.");
        response.stop();
      }
    });
  }
}

function render_pad(localPadId) {
  var proTitle = null, documentBarTitle, initialPassword = null;
  var isPro = isProDomainRequest();
  var userId = padusers.getUserId();

  var opts = {};
  var globalPadId;

  if (isPro) {
    pro_quotas.perRequestBillingCheck();
  }

  padutils.accessPadLocal(localPadId, function(pad) {
    globalPadId = pad.getId();
    request.cache.globalPadId = globalPadId;
    _createIfNecessary(localPadId, pad);
    _promptForMobileDevices(pad);
    _checkPadQuota(pad);
    _checkIfDeleted(pad);

    if (request.params.inviteTo) {
      getSession().nameGuess = request.params.inviteTo;
      response.redirect('/'+localPadId);
    }
    var displayName;
    if (request.params.displayName) { // tokbox
      displayName = String(request.params.displayName);
    }
    else {
      displayName = assignName(pad, userId);
    }

    if (isProDomainRequest()) {
      pro_padmeta.accessProPadLocal(localPadId, function(propad) {
        proTitle = propad.getDisplayTitle();
        initialPassword = propad.getPassword();
      });
    }
    documentBarTitle = (proTitle || "Public Pad");

    var specialKey = request.params.specialKey ||
      (sessions.isAnEtherpadAdmin() ? collab_server.getSpecialKey('invisible') :
       null);
    if (request.params.fullScreen) { // tokbox, embedding
      opts.fullScreen = true;
    }
    if (request.params.tokbox) {
      opts.tokbox = true;
    }
    if (request.params.sidebar) {
      opts.sidebar = Boolean(Number(request.params.sidebar));
    }

    helpers.addClientVars({
      padId: localPadId,
      globalPadId: globalPadId,
      userAgent: request.headers["User-Agent"],
      collab_client_vars: collab_server.getCollabClientVars(pad),
      debugEnabled: request.params.djs,
      clientIp: request.clientAddr,
      colorPalette: COLOR_PALETTE,
      nameGuess: (getSession().nameGuess || null),
      initialRevisionList: revisions.getRevisionList(pad),
      serverTimestamp: +(new Date),
      accountPrivs: _getPrivs(),
      chatHistory: chatarchive.getRecentChatBlock(pad, 30),
      numConnectedUsers: collab_server.getNumConnections(pad),
      isProPad: isPro,
      initialTitle: documentBarTitle,
      initialPassword: initialPassword,
      initialOptions: pad.getPadOptionsObj(),
      userIsGuest: padusers.isGuest(userId),
      userId: userId,
      userName: displayName,
      userColor: assignColorId(pad, userId),
      specialKey: specialKey,
      specialKeyTranslation: collab_server.translateSpecialKey(specialKey),
      opts: opts
    });
  });

  var isProUser = (isPro && ! padusers.isGuest(userId));

  var isFullWidth = false;
  var hideSidebar = false;
  var cookiePrefs = padutils.getPrefsCookieData();
  if (cookiePrefs) {
    isFullWidth = !! cookiePrefs.fullWidth;
    hideSidebar = !! cookiePrefs.hideSidebar;
  }
  if (opts.fullScreen) {
    isFullWidth = true;
    if (opts.tokbox) {
      hideSidebar = true;
    }
  }
  if ('sidebar' in opts) {
    hideSidebar = ! opts.sidebar;
  }
  var bodyClass = (isFullWidth ? "fullwidth" : "limwidth")+
    " "+(isPro ? "propad" : "nonpropad")+" "+
    (isProUser ? "prouser" : "nonprouser");

  var cookiePrefsToSet = {fullWidth:isFullWidth, hideSidebar:hideSidebar};
  helpers.addClientVars({cookiePrefsToSet: cookiePrefsToSet});

  renderHtml("pad/pad_body2.ejs",
             {localPadId:localPadId,
              pageTitle:toHTML(proTitle || localPadId),
              initialTitle:toHTML(documentBarTitle),
              bodyClass: bodyClass,
              hasOffice: hasOffice(),
              isPro: isPro,
              isProAccountHolder: isProUser,
              account: getSessionProAccount(), // may be falsy
              toHTML: toHTML,
              prefs: {isFullWidth:isFullWidth, hideSidebar:hideSidebar},
              signinUrl: '/ep/account/sign-in?cont='+
                encodeURIComponent(request.url),
              fullSuperdomain: pro_utils.getFullSuperdomainHost()
             });
  return true;
}

function render_create_get() {
  var padId = request.params.padId;
  // <RAFTER>
  var template = (DISABLE_PAD_CREATION && ! pro_utils.isProDomainRequest()) ?
    "pad/create_body_rafter.ejs" :
    "pad/create_body.ejs";
  // </RAFTER>
  renderFramed(template, {padId: padId,
                          fullSuperdomain: pro_utils.getFullSuperdomainHost()});
}

function render_create_post() {
  var padId = request.params.padId;
  getSession().instantCreate = padId;
  response.redirect("/"+padId);
}

//----------------------------------------------------------------
// saverevision
//----------------------------------------------------------------

function render_saverevision_post() {
  var padId = request.params.padId;
  var savedBy = request.params.savedBy;
  var savedById = request.params.savedById;
  var revNum = request.params.revNum;
  var privs = _getPrivs();
  padutils.accessPadLocal(padId, function(pad) {
    if (! pad.exists()) { response.notFound(); }
    var currentRevs = revisions.getRevisionList(pad);
    if (currentRevs.length >= privs.maxRevisions) {
      response.forbid();
    }
    var savedRev = revisions.saveNewRevision(pad, savedBy, savedById,
                                             revNum);
    readonly_server.broadcastNewRevision(pad, savedRev);
    response.setContentType('text/x-json');
    response.write(fastJSON.stringify(revisions.getRevisionList(pad)));
  });
}

function render_saverevisionlabel_post() {
  var userId = request.params.userId;
  var padId = request.params.padId;
  var revId = request.params.revId;
  var newLabel = request.params.newLabel;
  padutils.accessPadLocal(padId, function(pad) {
    revisions.setLabel(pad, revId, userId, newLabel);
    response.setContentType('text/x-json');
    response.write(fastJSON.stringify(revisions.getRevisionList(pad)));
  });
}

function render_getrevisionatext_get() {
  var padId = request.params.padId;
  var revId = request.params.revId;
  var result = null;

  var rev = padutils.accessPadLocal(padId, function(pad) {
    var r = revisions.getStoredRevision(pad, revId);
    var forWire = collab_server.getATextForWire(pad, r.revNum);
    result = {atext:forWire.atext, apool:forWire.apool,
              historicalAuthorData:forWire.historicalAuthorData};
    return r;
  }, "r");

  response.setContentType('text/plain; charset=utf-8');
  response.write(fastJSON.stringify(result));
}

//----------------------------------------------------------------
// reconnect
//----------------------------------------------------------------

function _recordDiagnosticInfo(padId, diagnosticInfoJson) {

  var diagnosticInfo = {};
  try {
    diagnosticInfo = fastJSON.parse(diagnosticInfoJson);
  } catch (ex) {
    log.warn("Error parsing diagnosticInfoJson: "+ex);
    diagnosticInfo = {error: "error parsing JSON"};
  }

  // ignore userdups, unauth
  if (diagnosticInfo.disconnectedMessage == "userdup" ||
      diagnosticInfo.disconnectedMessage == "unauth") {
    return;
  }

  var d = new Date();

  diagnosticInfo.date = +d;
  diagnosticInfo.strDate = String(d);
  diagnosticInfo.clientAddr = request.clientAddr;
  diagnosticInfo.padId = padId;
  diagnosticInfo.headers = {};
  eachProperty(request.headers, function(k,v) {
    diagnosticInfo.headers[k] = v;
  });

  var uid = diagnosticInfo.uniqueId;

  sqlbase.putJSON("PAD_DIAGNOSTIC", (diagnosticInfo.date)+"-"+uid, diagnosticInfo);

}

function recordMigratedDiagnosticInfo(objArray) {
  objArray.forEach(function(obj) {
    sqlbase.putJSON("PAD_DIAGNOSTIC", (obj.date)+"-"+obj.uniqueId, obj);
  });
}

function render_reconnect() {
  var localPadId = request.params.padId;
  var globalPadId = padutils.getGlobalPadId(localPadId);
  var userId = (padutils.getPrefsCookieUserId() || undefined);
  var hasClientErrors = false;
  var uniqueId;
  try {
    var obj = fastJSON.parse(request.params.diagnosticInfo);
    uniqueId = obj.uniqueId;
    errorMessage = obj.disconnectedMessage;
    hasClientErrors = obj.collabDiagnosticInfo.errors.length > 0;
  } catch (e) {
    // guess it doesn't have errors.
  }

  log.custom("reconnect", {globalPadId: globalPadId, userId: userId,
                           uniqueId: uniqueId,
                           hasClientErrors: hasClientErrors,
                           errorMessage: errorMessage });

  try {
    _recordDiagnosticInfo(globalPadId, request.params.diagnosticInfo);
  } catch (ex) {
    log.warn("Error recording diagnostic info: "+ex+" / "+request.params.diagnosticInfo);
  }

  try {
    _applyMissedChanges(localPadId, request.params.missedChanges);
  } catch (ex) {
    log.warn("Error applying missed changes: "+ex+" / "+request.params.missedChanges);
  }

  response.redirect('/'+localPadId);
}

/* posted asynchronously by the client as soon as reconnect dialogue appears. */
function render_connection_diagnostic_info_post() {
  var localPadId = request.params.padId;
  var globalPadId = padutils.getGlobalPadId(localPadId);
  var userId = (padutils.getPrefsCookieUserId() || undefined);
  var hasClientErrors = false;
  var uniqueId;
  var errorMessage;
  try {
    var obj = fastJSON.parse(request.params.diagnosticInfo);
    uniqueId = obj.uniqueId;
    errorMessage = obj.disconnectedMessage;
    hasClientErrors = obj.collabDiagnosticInfo.errors.length > 0;
  } catch (e) {
    // guess it doesn't have errors.
  }
  log.custom("disconnected_autopost", {globalPadId: globalPadId, userId: userId,
                                       uniqueId: uniqueId,
                                       hasClientErrors: hasClientErrors,
                                       errorMessage: errorMessage});

  try {
    _recordDiagnosticInfo(globalPadId, request.params.diagnosticInfo);
  } catch (ex) {
    log.warn("Error recording diagnostic info: "+ex+" / "+request.params.diagnosticInfo);
  }
  response.setContentType('text/plain; charset=utf-8');
  response.write("OK");
}

function _applyMissedChanges(localPadId, missedChangesJson) {
  var missedChanges;
  try {
    missedChanges = fastJSON.parse(missedChangesJson);
  } catch (ex) {
    log.warn("Error parsing missedChangesJson: "+ex);
    return;
  }

  padutils.accessPadLocal(localPadId, function(pad) {
    if (pad.exists()) {
      collab_server.applyMissedChanges(pad, missedChanges);
    }
  });
}

//----------------------------------------------------------------
// feedback
//----------------------------------------------------------------

function render_feedback_post() {
  var feedback = request.params.feedback;
  var localPadId = request.params.padId;
  var globalPadId = padutils.getGlobalPadId(localPadId);
  var username = request.params.username;
  var email = request.params.email;
  var subject = 'EtherPad Feedback from '+request.clientAddr+' / '+globalPadId+' / '+username;

  if (feedback.indexOf("@") > 0) {
    subject = "@ "+subject;
  }

  feedback += "\n\n--\n";
  feedback += ("User Agent: "+request.headers['User-Agent'] + "\n");
  feedback += ("Session Referer: "+getSession().initialReferer + "\n");
  feedback += ("Email: "+email+"\n");

  // log feedback
  var userId = padutils.getPrefsCookieUserId();
  log.custom("feedback", {
    globalPadId: globalPadId,
    userId: userId,
    email: email,
    username: username,
    feedback: request.params.feedback});

  sendEmail(
    'feedback@pad.spline.inf.fu-berlin.de',
    'feedback@pad.spline.inf.fu-berlin.de',
    subject,
    {},
    feedback
  );
  response.write("OK");
}

//----------------------------------------------------------------
// emailinvite
//----------------------------------------------------------------

function render_emailinvite_post() {
  var toEmails = String(request.params.toEmails).split(',');
  var padId = String(request.params.padId);
  var username = String(request.params.username);
  var subject = String(request.params.subject);
  var message = String(request.params.message);

  log.custom("padinvite",
             {toEmails: toEmails, padId: padId, username: username,
              subject: subject, message: message});

  var fromAddr = '"EtherPad" <noreply@pad.spline.inf.fu-berlin.de>';
  // client enforces non-empty subject and message
  var subj = '[EtherPad] '+subject;
  var body = renderTemplateAsString('email/padinvite.ejs',
                                    {body: message});
  var headers = {};
  var proAccount = getSessionProAccount();
  if (proAccount) {
    headers['Reply-To'] = proAccount.email;
  }

  response.setContentType('text/plain; charset=utf-8');
  try {
    sendEmail(toEmails, fromAddr, subj, headers, body);
    response.write("OK");
  } catch (e) {
    logException(e);
    response.setStatusCode(500);
    response.write("Error");
  }
}

//----------------------------------------------------------------
// time-slider
//----------------------------------------------------------------
function render_slider() {
  var parts = request.path.split('/');
  var padOpaqueRef = parts[4];

  helpers.addClientVars({padOpaqueRef:padOpaqueRef});

  renderHtml("pad/padslider_body.ejs", {
    // properties go here
  });

  return true;
}

//----------------------------------------------------------------
// auth
//----------------------------------------------------------------

function render_auth_get() {
  var parts = request.path.split('/');
  var localPadId = parts[4];
  var errDiv;
  if (getSession().padPassErr) {
    errDiv = DIV({style: "border: 1px solid #fcc; background: #ffeeee; padding: 1em; margin: 1em 0;"},
                  B(getSession().padPassErr));
    delete getSession().padPassErr;
  } else {
    errDiv = DIV();
  }
  renderFramedHtml(function() {
    return DIV({className: "fpcontent"},
           DIV({style: "margin: 1em;"},
            errDiv,
            FORM({style: "border: 1px solid #ccc; padding: 1em; background: #fff6cc;",
                  action: request.path+'?'+request.query,
                  method: "post"},
              LABEL(B("Please enter the password required to access this pad:")),
              BR(), BR(),
              INPUT({type: "text", name: "password"}), INPUT({type: "submit", value: "Submit"})
            /*DIV(BR(), "Or ", A({href: '/ep/account/sign-in'}, "sign in"), ".")*/
            )),
          DIV({style: "padding: 0 1em;"},
            P({style: "color: #444;"},
            "If you have forgotten a pad's password, contact your site administrator.",
            " Site administrators can recover lost pad text through the \"Admin\" tab.")
          )
        );
  });
  return true;
}

function render_auth_post() {
  var parts = request.path.split('/');
  var localPadId = parts[4];
  var domainId = domains.getRequestDomainId();
  if (!getSession().padPasswordAuth) {
    getSession().padPasswordAuth = {};
  }
  var currentPassword = pro_padmeta.accessProPadLocal(localPadId, function(propad) {
    return propad.getPassword();
  });
  if (request.params.password == currentPassword) {
    var globalPadId = padutils.getGlobalPadId(localPadId);
    getSession().padPasswordAuth[globalPadId] = true;
  } else {
    getSession().padPasswordAuth[globalPadId] = false;
    getSession().padPassErr = "Incorrect password.";
  }
  var cont = request.params.cont;
  if (!cont) {
    cont = '/'+localPadId;
  }
  response.redirect(cont);
}

//----------------------------------------------------------------
// chathistory
//----------------------------------------------------------------

function render_chathistory_get() {
  var padId = request.params.padId;
  var start = Number(request.params.start || 0);
  var end = Number(request.params.end || 0);
  var result = null;

  var rev = padutils.accessPadLocal(padId, function(pad) {
    result = chatarchive.getChatBlock(pad, start, end);
  }, "r");

  response.setContentType('text/plain; charset=utf-8');
  response.write(fastJSON.stringify(result));
}