Beispiel #1
0
        public CJParameterInfo(ITypeDatabaseReader typeDb, Dictionary<string, object> parameterTable)
        {
            if (parameterTable != null) {
                object typeObj;

                if (parameterTable.TryGetValue("type", out typeObj)) {
                    typeDb.LookupType(typeObj, (value, fromInstanceDb) => _type = value);
                }
                _typeObj = typeObj;

                object nameObj;
                if (parameterTable.TryGetValue("name", out nameObj)) {
                    _name = nameObj as string;
                }

                object docObj;
                if (parameterTable.TryGetValue("doc", out docObj)) {
                    _doc = docObj as string;
                }

                object defaultValueObj;
                if (parameterTable.TryGetValue("default_value", out defaultValueObj)) {
                    _defaultValue = defaultValueObj as string;
                }

                object argFormatObj;
                if (parameterTable.TryGetValue("arg_format", out argFormatObj)) {
                    switch (argFormatObj as string) {
                        case "*": _isSplat = true; break;
                        case "**": _isKeywordSplat = true; break;
                    }

                }
            }
        }
Beispiel #2
0
        public static byte[] GetJsonEncoded(Context context, IJType message, long receiverId, byte[] attechment = null)
        {
            byte[] aesBinKey = context.Contacts
                               .Where(u => u.PublicId == receiverId)
                               .Select(u => u.SendAesKey)
                               .SingleOrDefault();

            AESPassword key = new AESPassword(aesBinKey);

            MemoryStream stream = new MemoryStream();

            BinaryEncoder.SendInt(stream, (int)message.GetJsonType());

            string json = JsonConvert.SerializeObject(message);

            TextEncoder.SendString(stream, json);

            if (attechment == null)
            {
                BinaryEncoder.SendInt(stream, 0);
            }
            else
            {
                BinaryEncoder.SendInt(stream, 1);
                BinaryEncoder.SendBytes(stream, attechment);
            }

            byte[] notEncrypted = stream.ToArray();
            return(key.Encrypt(notEncrypted));
        }
Beispiel #3
0
        public CJFunction(ITypeDatabaseReader typeDb, string name, Dictionary<string, object> functionTable, IMemberContainer declaringType, bool isMethod = false)
        {
            _name = name;

            object doc;
            if (functionTable.TryGetValue("doc", out doc)) {
                _doc = doc as string;
            }

            object value;
            if (functionTable.TryGetValue("builtin", out value)) {
                _isBuiltin = Convert.ToBoolean(value);
            } else {
                _isBuiltin = true;
            }

            if (functionTable.TryGetValue("static", out value)) {
                _isStatic = Convert.ToBoolean(value);
            } else {
                _isStatic = true;
            }

            _hasLocation = JTypeDatabase.TryGetLocation(functionTable, ref _line, ref _column);

            _declaringModule = CJModule.GetDeclaringModuleFromContainer(declaringType);
            object overloads;
            functionTable.TryGetValue("overloads", out overloads);
            _overloads = LoadOverloads(typeDb, overloads, isMethod);
            _declaringType = declaringType as IJType;
        }
Beispiel #4
0
        public CJFunctionOverload(ITypeDatabaseReader typeDb, Dictionary<string, object> argInfo, bool isMethod)
        {
            if (argInfo != null) {
                object args;
                IList<object> argList;
                if (argInfo.TryGetValue("args", out args)) {
                    argList = (IList<object>)args;
                    if (argList != null) {
                        if (argList.Count == 0 || (isMethod && argList.Count == 1)) {
                            _parameters = EmptyParameters;
                        } else {
                            _parameters = new CJParameterInfo[isMethod ? argList.Count - 1 : argList.Count];
                            for (int i = 0; i < _parameters.Length; i++) {
                                _parameters[i] = new CJParameterInfo(typeDb, (isMethod ? argList[i + 1] : argList[i]) as Dictionary<string, object>);
                            }
                        }
                    }
                }

                object docObj;
                if (argInfo.TryGetValue("doc", out docObj)) {
                    _doc = docObj as string;
                }

                if (argInfo.TryGetValue("return_doc", out docObj)) {
                    _returnDoc = docObj as string;
                }

                object retTypeObj;
                argInfo.TryGetValue("ret_type", out retTypeObj);

                typeDb.LookupType(retTypeObj, (value, fromInstanceDb) => _retType = value);
            }
        }
Beispiel #5
0
        public static void SendIJType(Context context, IJType toSend, long recepientId, long myUserId)
        {
            if (toSend == null)
            {
                return;
            }

            var recepient = context.Contacts
                            .Where(u => u.PublicId == recepientId)
                            .SingleOrDefault();

            if (recepient == null)
            {
                throw new Exception($"User is not downloaded in local database.");
            }
            else if (recepient.Trusted != 1)
            {
                throw new Exception($"User {recepient.PublicId} ({recepient.UserName}) is not trusted.");
            }

            long?blobId = null;

            if (myUserId == recepientId)
            {
                var blobMessage = new BlobMessages()
                {
                    SenderId = myUserId,
                    PublicId = null,
                    DoDelete = 0,
                    Failed   = 0
                };
                context.BlobMessages.Add(blobMessage);
                context.SaveChanges();

                blobId = blobMessage.Id;
                PullMessageParser.ParseIJTypeMessage(context, toSend, myUserId, blobMessage.Id, myUserId);
                context.SaveChanges();
            }

            context.ToSendMessages.Add(new ToSendMessages()
            {
                RecepientId    = recepientId,
                BlobMessagesId = blobId,
                Blob           = JsonEncoder.GetJsonEncoded(context, toSend, recepientId)
            });

            context.SaveChanges();
        }
Beispiel #6
0
        public CJProperty(ITypeDatabaseReader typeDb, Dictionary<string, object> valueDict, IMemberContainer container)
        {
            _declaringModule = CJModule.GetDeclaringModuleFromContainer(container);

            object value;
            if (valueDict.TryGetValue("doc", out value)) {
                _doc = value as string;
            }

            object type;
            valueDict.TryGetValue("type", out type);

            _hasLocation = JTypeDatabase.TryGetLocation(valueDict, ref _line, ref _column);

            typeDb.LookupType(type, (typeValue, fromInstanceDb) => _type = typeValue);
        }
Beispiel #7
0
        public static byte[] GetJsonEncoded(Context context, IJType message, long receiverId)
        {
            byte[] aesBinKey = context.Contacts
                               .Where(u => u.PublicId == receiverId)
                               .Select(u => u.SendAesKey)
                               .SingleOrDefault();

            AESPassword key = new AESPassword(aesBinKey);

            MemoryStream stream = new MemoryStream();

            stream.WriteByte((byte)message.GetJsonType());

            string       json   = JsonConvert.SerializeObject(message);
            StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);

            writer.Write(json);
            writer.Close();

            byte[] notEncrypted = stream.ToArray();
            return(key.Encrypt(notEncrypted));
        }
Beispiel #8
0
        public KnownTypes(JAnalyzer state)
        {
            var interpreter = state.Interpreter;

            None = interpreter.GetBuiltinType(BuiltinTypeId.NoneType);
            Set = interpreter.GetBuiltinType(BuiltinTypeId.Set);
            Function = interpreter.GetBuiltinType(BuiltinTypeId.Function);
            Generator = interpreter.GetBuiltinType(BuiltinTypeId.Generator);
            Dict = interpreter.GetBuiltinType(BuiltinTypeId.Dict);
            Bool = interpreter.GetBuiltinType(BuiltinTypeId.Bool);
            List = interpreter.GetBuiltinType(BuiltinTypeId.List);
            Tuple = interpreter.GetBuiltinType(BuiltinTypeId.Tuple);
            BuiltinFunction = interpreter.GetBuiltinType(BuiltinTypeId.BuiltinFunction);
            BuiltinMethodDescriptor = interpreter.GetBuiltinType(BuiltinTypeId.BuiltinMethodDescriptor);
            Object = interpreter.GetBuiltinType(BuiltinTypeId.Object);
            Float = interpreter.GetBuiltinType(BuiltinTypeId.Float);
            Int = interpreter.GetBuiltinType(BuiltinTypeId.Int);
            Str = interpreter.GetBuiltinType(BuiltinTypeId.Str);
            Bytes = interpreter.GetBuiltinType(BuiltinTypeId.Bytes);
            Complex = interpreter.GetBuiltinType(BuiltinTypeId.Complex);
            Module = interpreter.GetBuiltinType(BuiltinTypeId.Module);
            if (!state.LanguageVersion.Is7x()) {
                Long = interpreter.GetBuiltinType(BuiltinTypeId.Long);
            }
            Ellipsis = interpreter.GetBuiltinType(BuiltinTypeId.Ellipsis);
            Type = interpreter.GetBuiltinType(BuiltinTypeId.Type);
            DictItems = interpreter.GetBuiltinType(BuiltinTypeId.DictItems);
            ListIterator = interpreter.GetBuiltinType(BuiltinTypeId.ListIterator);
            TupleIterator = interpreter.GetBuiltinType(BuiltinTypeId.TupleIterator);
            SetIterator = interpreter.GetBuiltinType(BuiltinTypeId.SetIterator);
            StrIterator = interpreter.GetBuiltinType(BuiltinTypeId.StrIterator);
            if (state.LanguageVersion.Is7x()) {
                BytesIterator = interpreter.GetBuiltinType(BuiltinTypeId.BytesIterator);
            }
            CallableIterator = interpreter.GetBuiltinType(BuiltinTypeId.CallableIterator);
        }
Beispiel #9
0
 public JsonCapsula(IJType message)
 {
     this.Message    = message;
     this.Attechment = null;
 }
Beispiel #10
0
 internal CJConstant GetConstant(IJType type)
 {
     CJConstant constant;
     if (!_constants.TryGetValue(type, out constant)) {
         _constants[type] = constant = new CJConstant(type);
     }
     return constant;
 }
Beispiel #11
0
 public SequenceBuiltinClassInfo(IJType classObj, JAnalyzer projectState)
     : base(classObj, projectState)
 {
 }
Beispiel #12
0
 public RangeInfo(IJType seqType, JAnalyzer state)
     : base(state._listType)
 {
 }
Beispiel #13
0
 public IterBuiltinMethodInfo(IJType declaringType, JAnalyzer projectState)
     : base(new IterFunction(declaringType), JMemberType.Method, projectState)
 {
 }
Beispiel #14
0
 private void StoreBase(IJType type, bool isInstance)
 {
     if (type != null) {
         _bases.Add(type);
     }
 }
Beispiel #15
0
 private object[] GenerateTypeName(IJType type)
 {
     if (type != null) {
         return GetTypeName(type.DeclaringModule.Name, type.Name);
     }
     return null;
 }
Beispiel #16
0
        public static void ParseIJTypeMessage(Context context, IJType decoded, long senderId, long messageId, long myUserId)
        {
            if (decoded == null)
            {
                return;
            }
            if (context.Contacts
                .Where(u => u.PublicId == senderId)
                .Select(u => u.Trusted)
                .SingleOrDefault() != 1)
            {
                throw new Exception($"User with id {senderId} isn't trusted.");
            }

            bool permission = senderId == myUserId;

            switch (decoded.GetJsonType())
            {
            case JsonTypes.ALARM:
                JAlarm alarm = (JAlarm)decoded;
                permission = permission ||
                             context.ContactsDetail
                             .Where(u => u.ContactId == senderId)
                             .Select(u => u.AlarmPermission)
                             .SingleOrDefault() == 1;

                if (permission)
                {
                    context.Alarms.Add(new Alarms()
                    {
                        BlobMessagesId = messageId,
                        Text           = alarm.Text,
                        Time           = alarm.Date.GetChatovatkoString()
                    });
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception($"User with id {senderId} doesn't have permission to set alarm.");
                }
                break;

            case JsonTypes.CONTACT_DETAIL:
                JContactDetail detail = (JContactDetail)decoded;
                permission = permission ||
                             context.ContactsDetail
                             .Where(u => u.ContactId == senderId)
                             .Select(u => u.ChangeContactsPermission)
                             .SingleOrDefault() == 1;

                if (permission)
                {
                    var toUpdate = context.ContactsDetail
                                   .Where(u => u.ContactId == detail.ContactId)
                                   .SingleOrDefault();
                    if (toUpdate != null)
                    {
                        toUpdate.NickName                 = detail.NickName;
                        toUpdate.BlobMessagesId           = messageId;
                        toUpdate.AlarmPermission          = detail.AlarmPermission ? 1 : 0;
                        toUpdate.ChangeContactsPermission = detail.ChangeContactPermission;
                    }
                    else
                    {
                        context.ContactsDetail.Add(new ContactsDetail()
                        {
                            AlarmPermission          = detail.ChangeContactPermission,
                            NickName                 = detail.NickName,
                            BlobMessagesId           = messageId,
                            ContactId                = detail.ContactId,
                            ChangeContactsPermission = detail.ChangeContactPermission
                        });
                    }
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception($"User with id {senderId} doesn't have permission to set contact detail.");
                }
                break;

            case JsonTypes.MESSAGES:
                JMessage jmessage       = (JMessage)decoded;
                long     threadWithUser = (
                    from threads in context.MessagesThread
                    where threads.PublicId == jmessage.MessageThreadId
                    select threads.WithUser
                    ).SingleOrDefault();
                permission = permission || threadWithUser == senderId;

                if (permission)
                {
                    bool onlive = (from threads in context.MessagesThread
                                   where threads.PublicId == jmessage.MessageThreadId
                                   select threads.Onlive)
                                  .SingleOrDefault() == 1;

                    bool updated = false;
                    if (onlive)
                    {
                        var toUpdateInfo = (from bmessages in context.BlobMessages
                                            join messages in context.Messages on bmessages.Id equals messages.BlobMessagesId
                                            where bmessages.SenderId == senderId && messages.IdMessagesThread == jmessage.MessageThreadId
                                            select new { messages.BlobMessagesId, messages.Id })
                                           .SingleOrDefault();
                        if (toUpdateInfo != null)
                        {
                            var toUpdate = context.Messages
                                           .Where(m => m.Id == toUpdateInfo.Id)
                                           .SingleOrDefault();
                            updated = true;

                            toUpdate.Text           = jmessage.Text;
                            toUpdate.Date           = jmessage.Time.GetChatovatkoString();
                            toUpdate.BlobMessagesId = messageId;

                            context.SaveChanges();
                        }
                    }

                    if (!updated)
                    {
                        context.Messages.Add(new Messages()
                        {
                            Date             = jmessage.Time.GetChatovatkoString(),
                            Text             = jmessage.Text,
                            IdMessagesThread = jmessage.MessageThreadId,
                            BlobMessagesId   = messageId
                        });
                        context.SaveChanges();
                    }
                }
                else
                {
                    throw new Exception($"User with id {senderId} doesn't have permission to send this message.");
                }
                break;

            case JsonTypes.MESSAGES_THREAD:
                JMessageThread messageThread = (JMessageThread)decoded;
                permission = permission || (messageThread.WithUserId == senderId && !messageThread.DoOnlyDelete);
                if (permission)
                {
                    var old = context.MessagesThread
                              .Where(u => u.PublicId == messageThread.PublicId)
                              .SingleOrDefault();
                    if (messageThread.DoOnlyDelete && old != null)
                    {
                        context.Remove(old);
                    }
                    else if (messageThread.DoOnlyDelete)
                    {
                    }
                    else if (old != null)
                    {
                        old.Name           = messageThread.Name;
                        old.BlobMessagesId = messageId;
                        old.Archived       = messageThread.Archived;
                    }
                    else
                    {
                        context.MessagesThread.Add(new MessagesThread
                        {
                            Name           = messageThread.Name,
                            PublicId       = messageThread.PublicId,
                            Onlive         = messageThread.Onlive,
                            Archived       = messageThread.Archived,
                            WithUser       = messageThread.WithUserId,
                            BlobMessagesId = messageId
                        });
                    }
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception($"User with id {senderId} doesn't have permission to create/edit/delete this message thread.");
                }
                break;

            case JsonTypes.AES_KEY:
                JAESKey aesKey = (JAESKey)decoded;
                if (permission)
                {
                    var contact = context.Contacts
                                  .Where(c => c.PublicId == aesKey.UserId)
                                  .SingleOrDefault();
                    if (contact.SendAesKey != null)
                    {
                        throw new Exception($"AES key of user {contact.UserName} already exist.");
                    }
                    contact.SendAesKey = aesKey.AESKey;
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception($"User with id {senderId} doesn't have permission to create AES keys to send.");
                }
                break;
            }
        }
Beispiel #17
0
        public static void ParseEncryptedMessage(Context context, byte[] message, long senderId, long messageId, long myUserId)
        {
            IJType decoded = JsonEncoder.GetJsonDecoded(context, message, senderId);

            ParseIJTypeMessage(context, decoded, senderId, messageId, myUserId);
        }
Beispiel #18
0
 public IterFunction(IJType declaringType)
 {
     DeclaringType = declaringType;
 }
Beispiel #19
0
 private void StoreMro(IJType type, bool isInstance)
 {
     var cpt = type as CJType;
     if (cpt != null) {
         _mro.Add(cpt);
     }
 }
Beispiel #20
0
 public CJConstant(IJType type)
 {
     _type = type;
 }