msgs.ssjs 10.7 KB
Newer Older
1
/* $Id: msgs.ssjs,v 1.44 2018/06/30 19:41:49 rswindell Exp $ */
runemaster's avatar
runemaster committed
2

3 4 5
load("../web/lib/msgslib.ssjs");
load("../web/lib/mime_decode.ssjs");

6 7
if(!msgbase)
    error("msgbase not defined");
8
if(msgbase.open!=undefined && msgbase.open()==false) {
9
    error(msgbase.last_error);
10 11 12
}

var ShowMsgs=GET_ALL_MESSAGES;
13
var MAX_NAME_LEN=25;
14
var MAX_SUBJ_LEN=70;
15

runemaster's avatar
runemaster committed
16 17
if(file_exists(prefs_dir +format("%04d.html_prefs",user.number))) {
  prefsfile=new File(prefs_dir +format("%04d.html_prefs",user.number));
18
  if(prefsfile.open("r",false)) {
runemaster's avatar
runemaster committed
19
  SortDate=prefsfile.iniGetValue('Messaging', 'SortDate', 'descending');
20 21
  prefsfile.close();
  }
22
} else {
runemaster's avatar
runemaster committed
23
  SortDate="descending";
24 25
}

26
if(SortDate=="ascending")
27 28 29 30
  DateDescending=false;
else
  DateDescending=true;

31
if(http_request.query.show_messages != undefined) {
32 33 34 35 36 37 38 39 40 41 42
    switch(http_request.query.show_messages[0]) {
        case 'All':
            ShowMsgs=GET_ALL_MESSAGES;
            break;
        case 'Yours':
            ShowMsgs=GET_MY_MESSAGES;
            break;
        case 'YourUnread':
            ShowMsgs=GET_MY_UNREAD_MESSAGES;
            break;
    }
43 44 45 46
}

var new_query='';
for(key in http_request.query) {
47 48 49 50 51 52 53
    if(key != 'show_messages') {
        if(new_query.length>0)
            new_query+='&';
        new_query+=encodeURIComponent(key);
        new_query+='=';
        new_query+=encodeURIComponent(http_request.query[key]);
    }
54 55
}
if(user.security.restrictions&UFLAG_G)
56
    ShowMsg=GET_ALL_MESSAGES;
57
else {
58 59 60
    if(new_query.length>0)
        new_query+='&';
    new_query+='show_messages=';
61 62
    if(sub=='mail') {
        switch(ShowMsgs) {
63 64 65 66 67 68 69 70
            case GET_ALL_MESSAGES:
            case GET_MY_MESSAGES:
                template.show_choice='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'YourUnread">'+show_messages_your_unread_html+'</a>';
                break;
            case GET_MY_UNREAD_MESSAGES:
                template.show_choice='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'All">'+show_messages_all_html+'</a>';
                break;
        }
71 72
    } else {
        switch(ShowMsgs) {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
            case GET_ALL_MESSAGES:
                template.show_choice='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'Yours">'+show_messages_yours_html+'</a>';
                template.show_choice+=show_messages_spacer_html;
                template.show_choice+='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'YourUnread">'+show_messages_your_unread_html+'</a>';
                break;
            case GET_MY_MESSAGES:
                template.show_choice='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'All">'+show_messages_all_html+'</a>';
                template.show_choice+=show_messages_spacer_html;
                template.show_choice+='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'YourUnread">'+show_messages_your_unread_html+'</a>';
                break;
            case GET_MY_UNREAD_MESSAGES:
                template.show_choice='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'All">'+show_messages_all_html+'</a>';
                template.show_choice+=show_messages_spacer_html;
                template.show_choice+='<a class="tlink2" href="'+http_request.virtual_path+'?'+new_query+'Yours">'+show_messages_yours_html+'</a>';
                break;
        }
89 90 91 92 93 94 95
    }
}

/* Ensure that offset is an even multiple of max_messages */
offset-=offset%max_messages;

if(offset<0)
96
    offset=0;
97 98

if(offset > 0)  {
99 100 101
    if(offset<max_messages)  {
        offset=max_messages;
    }
102 103 104 105 106 107 108
}
var currpage=Math.floor(offset/max_messages);
var msgcount=0;
var msgarray;
msgarray=get_message_offsets(ShowMsgs);

if(sub=='mail')
109
    template.can_delete=true;
110
else
111
    template.can_delete=msg_area.sub[sub].is_operator;
112 113
var total_pages=Math.floor(msgarray.length/max_messages+(msgarray.length%max_messages?1:0));
if(total_pages==0)
114
    total_pages=1;
115 116 117
var firstpage=0;
var lastpage=firstpage+max_pages-1;
if(lastpage>=total_pages)
118
    lastpage=total_pages-1;
119 120 121
/* Ensure currpage is inside first/last */
var lcount=0;
while(currpage>lastpage) {
122 123 124 125 126 127 128
    lcount++;
    firstpage++;
    lastpage=firstpage+max_pages-1;
    if(lastpage>=total_pages)
        lastpage=total_pages-1;
    if(lcount>5000)
        currpage=lastpage;
129 130 131 132 133
}

/* Try adjust so currpage is in the middle of firstpage and lastpage */
lcount=0;
while(currpage>firstpage+(max_pages/2) && lastpage<(total_pages-1)) {
134 135 136 137 138 139 140
    lcount++;
    firstpage++;
    lastpage=firstpage+max_pages-1;
    if(lastpage>=total_pages)
        lastpage=total_pages-1;
    if(lcount>5000)
        break;
141 142
}

143 144 145 146 147 148 149
/* I could not get this to work with:           */
/* file.open(file_exists(fname) ? "r+":"w+");   */

if(http_request.query.sort_date != undefined) {
  switch(http_request.query.sort_date[0]) {
    case "ascending":
      DateDescending=false;
150
      SortDate="ascending"
runemaster's avatar
runemaster committed
151
      prefsfile=new File(prefs_dir + format("%04d.html_prefs",user.number));
152
      if(prefsfile.open("r+",false)) {
runemaster's avatar
runemaster committed
153
          prefsfile.iniSetValue('Messaging', 'SortDate', 'ascending');
154 155 156
          prefsfile.close();
      } else {
      if(prefsfile.open("w+",false)) {
runemaster's avatar
runemaster committed
157
          prefsfile.iniSetValue('Messaging', 'SortDate', 'ascending');
158 159 160 161 162 163
          prefsfile.close();
        }
      }
      break;
    case "descending":
      DateDescending=true;
164
      SortDate="descending"
runemaster's avatar
runemaster committed
165
      prefsfile=new File(prefs_dir + format("%04d.html_prefs",user.number));
166
      if(prefsfile.open("r+",false)) {
runemaster's avatar
runemaster committed
167
          prefsfile.iniSetValue('Messaging', 'SortDate', 'descending');
168 169 170
          prefsfile.close();
        } else {
      if(prefsfile.open("w+",false)) {
runemaster's avatar
runemaster committed
171
          prefsfile.iniSetValue('Messaging', 'SortDate', 'descending');
172 173 174 175 176 177 178 179 180 181 182 183
          prefsfile.close();
        }
      }
      break;
  }
}

if(DateDescending)
  template.sort_date='<a href="' + path + '?msg_sub=' + encodeURIComponent(sub) + '&amp;offset=' + (offset) + '&amp;sort_date=ascending">Date</a>';
else
  template.sort_date='<a href="' + path + '?msg_sub=' + encodeURIComponent(sub) + '&amp;offset=' + (offset) + '&amp;sort_date=descending">Date</a>';

184
/* Build the links now */
185

186 187
template.pagelinks='';
if(total_pages>1) {
188 189 190 191
    for(var page=firstpage;page<=lastpage;page++) {
        if(currpage==page)
            template.pagelinks += '<b>'+(page+1)+'</b> ';
        else
192
            template.pagelinks += "<a href=\""+path+'?msg_sub='+encodeURIComponent(sub)+'&amp;offset='+(page*max_messages)+'&amp;sort_date=' + SortDate +'">'+(page+1)+'</a> ';
193 194 195
    }
    if(DateDescending) {
    if(offset+max_messages < msgarray.length)  {
196
        template.pagelinks+='<a href="'+path+'?msg_sub='+encodeURIComponent(sub)+'&amp;offset='+(offset+max_messages)+'&amp;sort_date=' + SortDate +'">'+next_page_html+'</a>';
197 198
    }
    if(offset>0) {
199
        template.pagelinks='<a href="'+path+'?msg_sub='+encodeURIComponent(sub)+'&amp;offset='+(offset-max_messages)+'&amp;sort_date=' + SortDate +'">'+prev_page_html+'</a> '+template.pagelinks;
200 201 202
    }
} else {
    if(offset+max_messages < msgarray.length)  {
203
        template.pagelinks+='<a href="'+path+'?msg_sub='+encodeURIComponent(sub)+'&amp;offset='+(offset+max_messages)+'&amp;sort_date=' + SortDate +'">'+next_page_html+'</a>';
204 205
    }
    if(offset>0) {
206
        template.pagelinks='<a href="'+path+'?msg_sub='+encodeURIComponent(sub)+'&amp;offset='+(offset-max_messages)+'&amp;sort_date=' + SortDate +'">'+prev_page_html+'</a> '+template.pagelinks;
207 208
    }
  }
209 210 211
}

if(sub=='mail') {
212 213 214 215
    template.title="Messages in E-Mail";
    template.sub=new Object;
    template.sub.description="Personal E-Mail";
    template.sub.code="mail";
216 217
}
else {
218 219
    template.title="Messages in "+msg_area.sub[sub].description;
    template.sub=msg_area.sub[sub];
220 221 222
}

if(sub!='mail')  {
223 224 225
    if(! msg_area.sub[sub].can_read)  {
        error("You don't have sufficient rights to read this sub");
    }
226 227 228
}

last_offset=msgarray.length-1-offset;
229 230 231 232
first_offset = offset;
max_first_offset= offset+msgarray.length-1-offset;
max_last_offset = last_offset;

233 234 235

template.messages=new Array;
if(sub=='mail') {
236 237 238
    template.group=new Object;
    template.group.name="E-Mail";
    template.group.description="E-Mail";
239 240
}
else {
241
    template.group=msg_area.grp[msg_area.sub[sub].grp_name];
242 243
}

244 245 246 247 248 249 250
if(DateDescending) {

  for(displayed=0;displayed<max_messages && last_offset >= 0;last_offset--) {
     var hdr=clean_msg_headers(msgbase.get_msg_header(true,msgarray[last_offset].offset),0);
     if(hdr==null)
          continue;
     template.messages[displayed.toString()]=hdr;
251 252 253
	 template.messages[displayed.toString()].to=template.messages[displayed.toString()].to.substr(0,MAX_NAME_LEN);
	 template.messages[displayed.toString()].from=template.messages[displayed.toString()].from.substr(0,MAX_NAME_LEN);
	 template.messages[displayed.toString()].subject=template.messages[displayed.toString()].subject.substr(0,MAX_SUBJ_LEN);
254
     template.messages[displayed.toString()].attachments=count_attachments(hdr,msgbase.get_msg_body(true,msgarray[last_offset].offset,hdr));
255 256 257 258 259 260 261 262 263
     template.messages[displayed.toString()].offset=msgarray[last_offset].offset;
     displayed++;
  }
} else {

  for(displayed = 0;displayed < max_messages && first_offset <= max_first_offset; first_offset++) {
      var hdr=clean_msg_headers(msgbase.get_msg_header(true,msgarray[first_offset].offset),0);
      if(hdr==null)
          continue;
runemaster's avatar
runemaster committed
264
	  template.messages[displayed.toString()]=hdr;
265 266 267
	  template.messages[displayed.toString()].to=template.messages[displayed.toString()].to.substr(0,MAX_NAME_LEN);
	  template.messages[displayed.toString()].from=template.messages[displayed.toString()].from.substr(0,MAX_NAME_LEN);
	  template.messages[displayed.toString()].subject=template.messages[displayed.toString()].subject.substr(0,MAX_SUBJ_LEN);
268
	  template.messages[displayed.toString()].attachments=count_attachments(hdr,msgbase.get_msg_body(true,msgarray[first_offset].offset,hdr));
269 270 271
      template.messages[displayed.toString()].offset=msgarray[first_offset].offset;
      displayed++;
  }
272 273 274
}

if(sub=='mail') {
runemaster's avatar
runemaster committed
275 276
    template.can_post=!(user.security.restrictions&UFLAG_E);
    template.post_button_image="new_e-mail.gif";
277
} else {
runemaster's avatar
runemaster committed
278 279
    template.can_post=msg_area.sub[sub].can_post;
    template.post_button_image="new_message.gif";
280
}           
281

runemaster's avatar
runemaster committed
282 283 284 285 286 287
if(http_request.query["sort"]!=undefined)
	template.messages.sort(alphasort);

if(do_header)
	write_template("header.inc");
if(do_topnav)
288
	load(topnav_html);
runemaster's avatar
runemaster committed
289
if(do_leftnav)
290
	load(leftnav_html);
runemaster's avatar
runemaster committed
291 292
if(do_rightnav)
	write_template("rightnav.inc");
293
write_template("msgs/msgs.inc");
runemaster's avatar
runemaster committed
294 295
if(do_footer)
	write_template("footer.inc");
296
msgs_done();
runemaster's avatar
runemaster committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

function alphasort (a,b)
{
	if(http_request.query["sort"]==undefined)
		return(0);
	var sortby=http_request.query["sort"]
	var au;
	var bu;
	if(a[sortby].toUpperCase!=undefined)
		au=a[sortby].toUpperCase();
	else
		au=a[sortby];
	if(b[sortby].toUpperCase!=undefined)
		bu=b[sortby].toUpperCase();
	else
		bu=b[sortby];
	if(au<bu)
		return -1;
	if(bu>au)
		return 1;
	return 0;
318
}