Newer
Older
/* js_msgbase.c */
/* Synchronet JavaScript "MsgBase" Object */
/* $Id$ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
* *
* Copyright 2006 Rob Swindell - http://www.synchro.net/copyright.html *
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
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* See the GNU General Public License for more details: gpl.txt or *
* http://www.fsf.org/copyleft/gpl.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
#include "sbbs.h"
#ifdef JAVASCRIPT
static scfg_t* scfg=NULL;
typedef struct
{
smb_t smb;
int status;
BOOL debug;
} private_t;
typedef struct
{
private_t *p;
BOOL expand_fields;
smbmsg_t msg;
} privatemsg_t;
static const char* getprivate_failure = "line %d %s JS_GetPrivate failed";
/* Destructor */
static void js_finalize_msgbase(JSContext *cx, JSObject *obj)
{
private_t* p;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL)
return;
if(SMB_IS_OPEN(&(p->smb)))
smb_close(&(p->smb));
free(p);
JS_SetPrivate(cx, obj, NULL);
}
/* Methods */
static JSBool
js_open(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
private_t* p;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
JS_ReportError(cx,getprivate_failure,WHERE);
return(JS_FALSE);
}
*rval = JSVAL_FALSE;
if(p->smb.subnum==INVALID_SUB
&& strchr(p->smb.file,'/')==NULL
&& strchr(p->smb.file,'\\')==NULL) {
JS_ReportError(cx,"Unrecognized msgbase code: %s",p->smb.file);
return(JS_TRUE);
}
if((p->status=smb_open(&(p->smb)))!=SMB_SUCCESS)
return(JS_TRUE);
*rval = JSVAL_TRUE;
return(JS_TRUE);
}
static JSBool
js_close(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
private_t* p;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
JS_ReportError(cx,getprivate_failure,WHERE);
smb_close(&(p->smb));
return(JS_TRUE);
}
static BOOL parse_recipient_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg)
char to[256];
ushort nettype=NET_UNKNOWN;
ushort agent;
int32 i32;
jsval val;
if(JS_GetProperty(cx, hdr, "to", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
} else {
if(p->smb.status.attr&SMB_EMAIL) /* e-mail */
return(FALSE); /* "to" property required */
cp="All";
}
if((p->status=smb_hfield_str(msg, RECIPIENT, cp))!=SMB_SUCCESS)
return(FALSE);
if(!(p->smb.status.attr&SMB_EMAIL)) {
SAFECOPY(to,cp);
strlwr(to);
msg->idx.to=crc16(to,0);
}
if(JS_GetProperty(cx, hdr, "to_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, RECIPIENTEXT, cp))!=SMB_SUCCESS)
return(FALSE);
if(p->smb.status.attr&SMB_EMAIL)
msg->idx.to=atoi(cp);
}
if(JS_GetProperty(cx, hdr, "to_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, RECIPIENTORG, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "to_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
nettype=(ushort)i32;
}
if(JS_GetProperty(cx, hdr, "to_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_netaddr(msg, RECIPIENTNETADDR, cp, &nettype))!=SMB_SUCCESS)
return(FALSE);
}
if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
if(p->smb.status.attr&SMB_EMAIL)
msg->idx.to=0;
if((p->status=smb_hfield_bin(msg, RECIPIENTNETTYPE, nettype))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "to_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
agent=(ushort)i32;
if((p->status=smb_hfield_bin(msg, RECIPIENTAGENT, agent))!=SMB_SUCCESS)
return(FALSE);
return(TRUE);
}
static BOOL parse_header_object(JSContext* cx, private_t* p, JSObject* hdr, smbmsg_t* msg
,BOOL recipient)
{
char* cp;
char from[256];
ushort nettype=NET_UNKNOWN;
ushort type;
ushort agent;
ushort port;
JSObject* array;
JSObject* field;
jsuint i,len;
if(hdr==NULL)
return(FALSE);
if(recipient && !parse_recipient_object(cx,p,hdr,msg))
return(FALSE);
if(JS_GetProperty(cx, hdr, "subject", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
} else
cp="";
if((p->status=smb_hfield_str(msg, SUBJECT, cp))!=SMB_SUCCESS)
return(FALSE);
msg->idx.subj=smb_subject_crc(cp);
if(JS_GetProperty(cx, hdr, "from", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
} else
return(FALSE); /* "from" property required */
if((p->status=smb_hfield_str(msg, SENDER, cp))!=SMB_SUCCESS)
return(FALSE);
if(!(p->smb.status.attr&SMB_EMAIL)) {
SAFECOPY(from,cp);
strlwr(from);
msg->idx.from=crc16(from,0);
}
if(JS_GetProperty(cx, hdr, "from_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, SENDEREXT, cp))!=SMB_SUCCESS)
return(FALSE);
if(p->smb.status.attr&SMB_EMAIL)
if(JS_GetProperty(cx, hdr, "from_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, SENDERORG, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "from_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
nettype=(ushort)i32;
if(JS_GetProperty(cx, hdr, "from_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_netaddr(msg, SENDERNETADDR, cp, &nettype))!=SMB_SUCCESS)
return(FALSE);
}
if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
if(p->smb.status.attr&SMB_EMAIL)
msg->idx.from=0;
if((p->status=smb_hfield_bin(msg, SENDERNETTYPE, nettype))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "from_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
agent=(ushort)i32;
if((p->status=smb_hfield_bin(msg, SENDERAGENT, agent))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "from_ip_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, SENDERIPADDR, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "from_host_name", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, SENDERHOSTNAME, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "from_protocol", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, SENDERPROTOCOL, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "from_port", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
port=(ushort)i32;
if((p->status=smb_hfield_bin(msg, SENDERPORT, port))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "replyto", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, REPLYTO, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "replyto_ext", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, REPLYTOEXT, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "replyto_org", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, REPLYTOORG, cp))!=SMB_SUCCESS)
return(FALSE);
nettype=NET_UNKNOWN;
if(JS_GetProperty(cx, hdr, "replyto_net_type", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
nettype=(ushort)i32;
if(JS_GetProperty(cx, hdr, "replyto_net_addr", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_netaddr(msg, REPLYTONETADDR, cp, &nettype))!=SMB_SUCCESS)
return(FALSE);
}
if(nettype!=NET_UNKNOWN && nettype!=NET_NONE) {
if((p->status=smb_hfield_bin(msg, REPLYTONETTYPE, nettype))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "replyto_agent", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
agent=(ushort)i32;
if((p->status=smb_hfield_bin(msg, REPLYTOAGENT, agent))!=SMB_SUCCESS)
return(FALSE);
/* RFC822 headers */
if(JS_GetProperty(cx, hdr, "id", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, RFC822MSGID, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "reply_id", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, RFC822REPLYID, cp))!=SMB_SUCCESS)
return(FALSE);
}
/* SMTP headers */
if(JS_GetProperty(cx, hdr, "reverse_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, SMTPREVERSEPATH, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "forward_path", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, SMTPFORWARDPATH, cp))!=SMB_SUCCESS)
return(FALSE);
}
/* USENET headers */
if(JS_GetProperty(cx, hdr, "path", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, USENETPATH, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "newsgroups", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, USENETNEWSGROUPS, cp))!=SMB_SUCCESS)
return(FALSE);
}
/* FTN headers */
if(JS_GetProperty(cx, hdr, "ftn_msgid", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, FIDOMSGID, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "ftn_reply", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, FIDOREPLYID, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "ftn_area", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, FIDOAREA, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "ftn_flags", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, FIDOFLAGS, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "ftn_pid", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
if((p->status=smb_hfield_str(msg, FIDOPID, cp))!=SMB_SUCCESS)
return(FALSE);
if(JS_GetProperty(cx, hdr, "ftn_tid", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, FIDOTID, cp))!=SMB_SUCCESS)
return(FALSE);
}
if(JS_GetProperty(cx, hdr, "date", &val) && !JSVAL_NULL_OR_VOID(val)) {
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
msg->hdr.when_written=rfc822date(cp);
}
if(JS_GetProperty(cx, hdr, "attr", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.attr=(ushort)i32;
msg->idx.attr=msg->hdr.attr;
}
if(JS_GetProperty(cx, hdr, "auxattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.auxattr=i32;
}
if(JS_GetProperty(cx, hdr, "netattr", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.netattr=i32;
}
if(JS_GetProperty(cx, hdr, "when_written_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.when_written.time=i32;
}
if(JS_GetProperty(cx, hdr, "when_written_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.when_written.zone=(short)i32;
}
if(JS_GetProperty(cx, hdr, "when_imported_time", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.when_imported.time=i32;
}
if(JS_GetProperty(cx, hdr, "when_imported_zone", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.when_imported.zone=(short)i32;
}
if((JS_GetProperty(cx, hdr, "thread_orig", &val)
|| JS_GetProperty(cx, hdr, "thread_back", &val)) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.thread_back=i32;
if(JS_GetProperty(cx, hdr, "thread_next", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.thread_next=i32;
}
if(JS_GetProperty(cx, hdr, "thread_first", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.thread_first=i32;
}
if(JS_GetProperty(cx, hdr, "field_list", &val) && JSVAL_IS_OBJECT(val)) {
array=JSVAL_TO_OBJECT(val);
len=0;
if(!JS_GetArrayLength(cx, array, &len))
return(FALSE);
for(i=0;i<len;i++) {
if(!JS_GetElement(cx, array, i, &val))
continue;
if(!JSVAL_IS_OBJECT(val))
continue;
field=JSVAL_TO_OBJECT(val);
if(!JS_GetProperty(cx, field, "type", &val))
continue;
if(JSVAL_IS_STRING(val))
type=smb_hfieldtypelookup(JS_GetStringBytes(JS_ValueToString(cx,val)));
else {
JS_ValueToInt32(cx,val,&i32);
type=(ushort)i32;
}
if(!JS_GetProperty(cx, field, "data", &val))
continue;
if((cp=JS_GetStringBytes(JS_ValueToString(cx,val)))==NULL)
return(FALSE);
if((p->status=smb_hfield_str(msg, type, cp))!=SMB_SUCCESS)
return(FALSE);
}
}
if(msg->hdr.number==0 && JS_GetProperty(cx, hdr, "number", &val) && !JSVAL_NULL_OR_VOID(val)) {
JS_ValueToInt32(cx,val,&i32);
msg->hdr.number=i32;
}
return(TRUE);
}
/* obj must've been previously returned from get_msg_header() */
BOOL DLLCALL js_ParseMsgHeaderObject(JSContext* cx, JSObject* obj, smbmsg_t* msg)
{
private_t* p;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
JS_ReportError(cx,getprivate_failure,WHERE);
return(FALSE);
}
if(!parse_header_object(cx, p, obj, msg, /* recipient */ TRUE)) {
smb_freemsgmem(msg);
return(FALSE);
}
static BOOL msg_offset_by_id(private_t* p, char* id, int32_t* offset)
{
smbmsg_t msg;
if((p->status=smb_getmsgidx_by_msgid(&(p->smb),&msg,id))!=SMB_SUCCESS)
return(FALSE);
*offset = msg.offset;
return(TRUE);
}
static JSBool
js_get_msg_index(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
uintN n;
smbmsg_t msg;
JSObject* idxobj;
JSBool by_offset=JS_FALSE;
private_t* p;
*rval = JSVAL_NULL;
if((p=(private_t*)JS_GetPrivate(cx,obj))==NULL) {
JS_ReportError(cx,getprivate_failure,WHERE);
return(JS_FALSE);
}
if(!SMB_IS_OPEN(&(p->smb)))
return(JS_TRUE);
memset(&msg,0,sizeof(msg));
for(n=0;n<argc;n++) {
if(JSVAL_IS_BOOLEAN(argv[n]))
by_offset=JSVAL_TO_BOOLEAN(argv[n]);
else if(JSVAL_IS_NUM(argv[n])) {
if(by_offset) /* Get by offset */
JS_ValueToInt32(cx,argv[n],(int32*)&msg.offset);
else /* Get by number */
JS_ValueToInt32(cx,argv[n],(int32*)&msg.hdr.number);
if((p->status=smb_getmsgidx(&(p->smb), &msg))!=SMB_SUCCESS)
return(JS_TRUE);
break;
}
}
if((idxobj=JS_NewObject(cx,NULL,NULL,obj))==NULL)
return(JS_TRUE);
JS_NewNumberValue(cx, msg.idx.number ,&val);
JS_DefineProperty(cx, idxobj, "number" ,val
,NULL,NULL,JSPROP_ENUMERATE);
JS_NewNumberValue(cx, msg.idx.to ,&val);
JS_DefineProperty(cx, idxobj, "to" ,val
,NULL,NULL,JSPROP_ENUMERATE);
JS_NewNumberValue(cx, msg.idx.from ,&val);
JS_DefineProperty(cx, idxobj, "from" ,val
,NULL,NULL,JSPROP_ENUMERATE);
JS_NewNumberValue(cx, msg.idx.subj ,&val);
JS_DefineProperty(cx, idxobj, "subject" ,val
,NULL,NULL,JSPROP_ENUMERATE);
JS_NewNumberValue(cx, msg.idx.attr ,&val);
JS_DefineProperty(cx, idxobj, "attr" ,val
,NULL,NULL,JSPROP_ENUMERATE);
JS_NewNumberValue(cx, msg.offset ,&val);
JS_DefineProperty(cx, idxobj, "offset" ,val
,NULL,NULL,JSPROP_ENUMERATE);
JS_NewNumberValue(cx, msg.idx.time ,&val);
JS_DefineProperty(cx, idxobj, "time" ,val
,NULL,NULL,JSPROP_ENUMERATE);
*rval = OBJECT_TO_JSVAL(idxobj);
return(JS_TRUE);
}
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static JSBool js_get_msg_header_resolve(JSContext *cx, JSObject *obj, jsval id)
{
char date[128];
char msg_id[256];
char reply_id[256];
char* val;
ushort* port;
int i;
uintN n;
smbmsg_t remsg;
JSObject* array;
JSObject* field;
JSString* js_str;
jsint items;
jsval v;
privatemsg_t* p;
char* name=NULL;
if(id != JSVAL_NULL)
name=JS_GetStringBytes(JSVAL_TO_STRING(id));
/* If we have already enumerated, we're done here... */
if((p=(privatemsg_t*)JS_GetPrivate(cx,obj))==NULL)
return(JS_TRUE);
if((p->msg).hdr.number==0) /* No valid message number/id/offset specified */
return(JS_TRUE);
if(name==NULL || strcmp(name,"number")==0) {
JS_NewNumberValue(cx,(p->msg).hdr.number,&v);
JS_DefineProperty(cx, obj, "number", v, NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"offset")==0) {
JS_NewNumberValue(cx,(p->msg).offset,&v);
JS_DefineProperty(cx, obj, "offset", v, NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"to")==0) {
if((js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).to)))!=NULL) {
JS_DefineProperty(cx, obj, "to"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from")==0) {
if((js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).from)))!=NULL) {
JS_DefineProperty(cx, obj, "from"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"subject")==0) {
if((js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).subj)))!=NULL) {
JS_DefineProperty(cx, obj, "subject"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"summary")==0) {
if((p->msg).summary!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).summary)))!=NULL) {
JS_DefineProperty(cx, obj, "summary"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"to_ext")==0) {
if((p->msg).to_ext!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).to_ext)))!=NULL) {
JS_DefineProperty(cx, obj, "to_ext"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_ext")==0) {
if((p->msg).from_ext!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).from_ext)))!=NULL) {
JS_DefineProperty(cx, obj, "from_ext"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_org")==0) {
if((p->msg).from_org!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).from_org)))!=NULL) {
JS_DefineProperty(cx, obj, "from_org"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"replyto")==0) {
if((p->msg).replyto!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).replyto)))!=NULL) {
JS_DefineProperty(cx, obj, "replyto"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"replyto_ext")==0) {
if((p->msg).replyto_ext!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).replyto_ext)))!=NULL) {
JS_DefineProperty(cx, obj, "replyto_ext"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"reverse_path")==0) {
if((p->msg).reverse_path!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).reverse_path)))!=NULL) {
JS_DefineProperty(cx, obj, "reverse_path"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"forward_path")==0) {
if((p->msg).forward_path!=NULL
&& (js_str=JS_NewStringCopyZ(cx,truncsp((p->msg).forward_path)))!=NULL) {
JS_DefineProperty(cx, obj, "forward_path"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if(name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"to_agent")==0) {
if(p->expand_fields || (p->msg).to_agent) {
JS_DefineProperty(cx, obj, "to_agent",INT_TO_JSVAL((p->msg).to_agent)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_agent")==0) {
if(p->expand_fields || (p->msg).from_agent) {
JS_DefineProperty(cx, obj, "from_agent",INT_TO_JSVAL((p->msg).from_agent)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"replyto_agent")==0) {
if(p->expand_fields || (p->msg).replyto_agent) {
JS_DefineProperty(cx, obj, "replyto_agent",INT_TO_JSVAL((p->msg).replyto_agent)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"to_net_type")==0) {
if(p->expand_fields || (p->msg).to_net.type) {
JS_DefineProperty(cx, obj, "to_net_type",INT_TO_JSVAL((p->msg).to_net.type)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"to_net_addr")==0) {
if((p->msg).to_net.type
&& (js_str=JS_NewStringCopyZ(cx,smb_netaddr(&(p->msg).to_net)))!=NULL) {
JS_DefineProperty(cx, obj, "to_net_addr"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_net_type")==0) {
if(p->expand_fields || (p->msg).from_net.type) {
JS_DefineProperty(cx, obj, "from_net_type",INT_TO_JSVAL((p->msg).from_net.type)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_net_addr")==0) {
if((p->msg).from_net.type
&& (js_str=JS_NewStringCopyZ(cx,smb_netaddr(&(p->msg).from_net)))!=NULL) {
JS_DefineProperty(cx, obj, "from_net_addr"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"replyto_net_type")==0) {
if(p->expand_fields || (p->msg).replyto_net.type) {
JS_DefineProperty(cx, obj, "replyto_net_type",INT_TO_JSVAL((p->msg).replyto_net.type)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"replyto_net_addr")==0) {
if((p->msg).replyto_net.type
&& (js_str=JS_NewStringCopyZ(cx,smb_netaddr(&(p->msg).replyto_net)))!=NULL) {
JS_DefineProperty(cx, obj, "replyto_net_addr"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_ip_addr")==0) {
if((val=smb_get_hfield(&(p->msg),SENDERIPADDR,NULL))!=NULL
&& (js_str=JS_NewStringCopyZ(cx,val))!=NULL) {
JS_DefineProperty(cx, obj, "from_ip_addr"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_host_name")==0) {
if((val=smb_get_hfield(&(p->msg),SENDERHOSTNAME,NULL))!=NULL
&& (js_str=JS_NewStringCopyZ(cx,val))!=NULL) {
JS_DefineProperty(cx, obj, "from_host_name"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_protocol")==0) {
if((val=smb_get_hfield(&(p->msg),SENDERPROTOCOL,NULL))!=NULL
&& (js_str=JS_NewStringCopyZ(cx,val))!=NULL) {
JS_DefineProperty(cx, obj, "from_protocol"
,STRING_TO_JSVAL(js_str)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"from_port")==0) {
if((port=smb_get_hfield(&(p->msg),SENDERPORT,NULL))!=NULL) {
JS_DefineProperty(cx, obj, "from_port"
,INT_TO_JSVAL(*port)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"forwarded")==0) {
if(p->expand_fields || (p->msg).forwarded) {
JS_DefineProperty(cx, obj, "forwarded",INT_TO_JSVAL((p->msg).forwarded)
,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"expiration")==0) {
if(p->expand_fields || (p->msg).expiration) {
JS_NewNumberValue(cx,(p->msg).expiration,&v);
JS_DefineProperty(cx, obj, "expiration",v,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}
if(name==NULL || strcmp(name,"priority")==0) {
if(p->expand_fields || (p->msg).priority) {
JS_NewNumberValue(cx,(p->msg).priority,&v);
JS_DefineProperty(cx, obj, "priority",v,NULL,NULL,JSPROP_ENUMERATE);
if(name)
return(JS_TRUE);
}
else if (name)
return(JS_TRUE);
}