Forums

Some help needed

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Some help needed

Postby PrairieEagle » Tue Nov 02, 2010 6:17 pm

OK. To put it simply I am a code dabbler, I don't stay put in one coding language for to long, but I get the basics of most languages. But I don't know where to even start with cobra, I need some questions answered.
My first question is that the 'debugger' and function list for cobra created for Notepad++ doesn't work, I can get the syntax to work, but every time I try adding the files to the appropriate location, notepad++ comes up with a very vague window that just keeps popping up saying error. Could this be because I didn't actually properly install notepad?
The second is that the command line (or cmd.exe) keeps saying that 'cobra' isn't a command, I did install cobra properly, but has something like this come up before? Or is it just me and I should try re installing it?
The third is that I need some serious help with converting some C# code to Cobra. What this thing is going to be used for is to make a bot for a chat at deviantart.com, there is help for this sort of thing there, but not for Cobra language, this specific script does is it Grabs a Cookie on the deviantart login screen because it will need that to be allowed into the chat. Now what I actually did was use a converter to convert VB.NET to C# so the source code itself may contain errors but here is the source:

Code: Select all
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.web;
using System.Threading;
using System.Windows.Forms;
public struct dAmnAuth
{
   public string un;
   public string authtok;
   public string Password;
}
public class GetCookie
{
   const string SC = "Set-Cookie:";
   string PK = "";
   bool GotPK = false;
   WebBrowser WebBrowser1 = new WebBrowser();
   ManualResetEvent t;
   private void DC(System.Object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
   {
      lock (typeof(WebBrowser)) {
         if (GotPK != true) {
            string lpk = GetPK(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie));
            PK = lpk;
            t.Set();
         }
      }
   }
   public dAmnAuth GetdAmnAuth()
   {
      t = new ManualResetEvent(false);
      int x = 0;
      WebBrowser1.DocumentCompleted += DC;
      WebBrowser1.Navigate(new System.Uri("http://www.deviantart.com/"));
      while (WebBrowser1.Document == null) {
         System.Threading.Thread.Sleep(100);
         //My.Application.DoEvents()
         System.Windows.Forms.Application.DoEvents();
         x = x + 1;
         if (x >= 150) {
            return null;
         }
      }
      string lpk = GetPK(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie));
      PK = lpk;
      dAmnAuth rval = new dAmnAuth();
      rval.authtok = lpk;
      rval.un = GetUN(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie));

      //While Not t.WaitOne(1000, False)
      //    If x >= 20 Then
      //        Return ""
      //    End If
      //End While
      return rval;
   }
   public string GetUN()
   {
      t = new ManualResetEvent(false);
      int x = 0;
      WebBrowser1.DocumentCompleted += DC;
      WebBrowser1.Navigate(new System.Uri("http://www.deviantart.com/"));
      while (WebBrowser1.Document == null) {
         System.Threading.Thread.Sleep(100);
         //My.Application.DoEvents()
         System.Windows.Forms.Application.DoEvents();
         x = x + 1;
         if (x >= 150) {
            return "";
         }
      }
      string lpk = GetUN(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie));
      PK = lpk;
      t.Set();
      //While Not t.WaitOne(1000, False)
      //    If x >= 20 Then
      //        Return ""
      //    End If
      //End While
      return PK;
   }
   public string GetAT()
   {
      t = new ManualResetEvent(false);
      int x = 0;
      WebBrowser1.DocumentCompleted += DC;
      WebBrowser1.Navigate(new System.Uri("http://www.deviantart.com/"));
      while (WebBrowser1.Document == null) {
         System.Threading.Thread.Sleep(100);
         //My.Application.DoEvents()
         System.Windows.Forms.Application.DoEvents();
         x = x + 1;
         if (x >= 150) {
            return "";
         }
      }
      Interaction.MsgBox(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie));
      string lpk = GetPK(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie));
      PK = lpk;
      t.Set();
      //While Not t.WaitOne(1000, False)
      //    If x >= 20 Then
      //        Return ""
      //    End If
      //End While
      return PK;
   }
   private string GetUN(string DecodedData)
   {
      string[] sArray = null;
      string tString = null;
      string setCookie = "";
      string atkRaw = "";
      string atkFinal = "";
      bool isNext = false;
      sArray = Strings.Split(DecodedData, Constants.vbCrLf);
      foreach (string tString_loopVariable in sArray) {
         tString = tString_loopVariable;
         if ((Strings.InStr(tString, "Set-Cookie:") != 0)) {
            setCookie = tString;
            break; // TODO: might not be correct. Was : Exit For
         }
      }
      if (string.IsNullOrEmpty(setCookie)) {
         return "";
      }
      sArray = Strings.Split(setCookie, ";");
      foreach (string tString_loopVariable in sArray) {
         tString = tString_loopVariable;
         if (isNext) {
            atkRaw = tString;
            break; // TODO: might not be correct. Was : Exit For
         }
         if (Strings.InStr(tString, "username")) {
            isNext = true;
         }
      }
      if (string.IsNullOrEmpty(atkRaw)) {
         return "";
      }
      try {
         sArray = Strings.Split(atkRaw, ":");
         atkFinal = Strings.Mid(Strings.Mid(sArray[2], 2), 1, Strings.Len(Strings.Mid(sArray[2], 2)) - 1);
      } catch {
         return "";
      }
      return atkFinal;
   }
   private string GetPK(string decodedData)
   {
      string[] sArray = null;
      string tString = null;
      string setCookie = "";
      string atkRaw = "";
      string atkFinal = "";
      bool isNext = false;
      sArray = Strings.Split(decodedData, Constants.vbCrLf);
      foreach (string tString_loopVariable in sArray) {
         tString = tString_loopVariable;
         if ((Strings.InStr(tString, "Set-Cookie:") != 0)) {
            setCookie = tString;
            break; // TODO: might not be correct. Was : Exit For
         }
      }
      if (string.IsNullOrEmpty(setCookie)) {
         return "";
      }
      sArray = Strings.Split(setCookie, ";");
      foreach (string tString_loopVariable in sArray) {
         tString = tString_loopVariable;
         if (isNext) {
            atkRaw = tString;
            break; // TODO: might not be correct. Was : Exit For
         }
         if (Strings.InStr(tString, "authtoken")) {
            isNext = true;
         }
      }
      if (string.IsNullOrEmpty(atkRaw)) {
         return "";
      }
      try {
         sArray = Strings.Split(atkRaw, ":");
         atkFinal = Strings.Mid(Strings.Mid(sArray[2], 2), 1, Strings.Len(Strings.Mid(sArray[2], 2)) - 1);
      } catch {
         return "";
      }
      return atkFinal;
   }
}



And here is what I translated it to,

use Microsoft.VisualBasic
use System.Collections
use System.Collections.Generic
use System.Data
use System.Diagnostics
use System.Web
use System.Threading
use System.Windows.Forms
use Cobra.Lang

struct dAmnAuth
var un as String
var authtok as String
var password as String

class GetCookie as public
const SC as String = "Set-Cookie:"
var pK as String = ""
var gotPK as Boolean = false
var webBrowser1 as WebBrowser
var t as ManualResetEvent
def DC(sender as Object, e as System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
lock(typeOf(WebBrowser))
if (gotPK <> true)
var lpk as String = getPK(Httputility.UrlDecode(SC + WebBrowser.Document.Cookie))
pK = lpk
t.Set()

def GetdAmnAuth() is shared has dAmnAuth
var t = new ManualResetEvent(false)
var x as int = 0
WebBrowser1.DocumentCompleted += DC
WebBrowser1.Navigate(new System.Uri('http://www.deviantart.com/'))
while (WebBrowser1.Document == nil)
System.Threading.Thread.Sleep(100)
System.Windows.Forms.Application.DoEvents()
int x = x + 1
if (x >= 150)
return nil

var lpk as String = getPK(HttpUtility.UrlDecode(sC + webBrowser1.Document.Cookie))
pK = lpk
rval as dAmnAuth = new dAmnAuth
rval.authtok = lpk
rval.un = GetUN(HttpUtility.UrlDecode(sC + webBrowser1.Document.Cookie))
return rval
print [.rval]

def GetUN() as String
var t = new ManualResetEvent(false)
int x = 0
webBrowser1.DocumentCompleted += DC
webBrowser1.Navigate(new System.Uri('http://www.deviantart.com/'))
while (WebBrowser1.Document == nil)
System.Threading.Thread.Sleep(100)
System.Windows.Forms.Application.DoEvents()
int x = x + 1
if (x >= 150)
return ""
lpk as String = GetPK(HttpUtility.UrlDecode(sC + webBrowser1.Document.Cookie))
PK = lpk
t.Set()
return PK

def GetAT() as String
var t = new ManualResetEvent(false)
int x = 0
WebBrowser1.DocumentCompleted += DC
WebBrowser1.Navigate(new System.Uri('http://www.deviantart.com/'))
while (WebBrowser1.Document == nil)
System.Threading.Thread.Sleep(100)
System.Windows.Forms.Application.DoEvents()
int x = x + 1
if (x >= 150)
return ""
Interaction.MsgBox(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie))
lpk as String = GetPK(HttpUtility.UrlDecode(SC + WebBrowser1.Document.Cookie))
PK = lpk
t.Set()
return PK

def GetUN(DecodeData) as String
var sArray() as String = nil
var tString as String = nil
var setCooking as String = ""
var atkRaw as String = ""
var atkFinal as String = ""
var isNext as bool = false
sArray = Strings.Split(DecodedData, Constants.vbCrLf)
for (tString_loopVariable as String in sArray)
tString = tString_loopVariable
if (String.InStr(tString, "Set-Cookie:") <> 0)
setCookie = tString
break

if (string.IsNullOrEmpty(setCookie))
return ""

sArray = Strings.Split(setCookie, ";")
for (tString_loopVariable as String in sArray)
tString = tString_loopVariable
if (isNext)
atkRaw = tString
break

if (Strings.InStr(tString, "authtoken"))
isNext = true

if (string.IsNullOrEmpty(atkRaw))
return ""

try
sArray = Strings.Split(atkRaw, ":")
atkFinal = Strings.Mid(Strings.Mid(sArray[2], 2), 1, Strings.Len(Strings.Mid(sArray[2], 2)) - 1)
catch
return ""

return atkFinal


Any help with these problems is greatly appreciated, If you need more information about the original files, click this http://botdom.com/wiki/DAmnDOTnet and download the file on that page to see the original VB.NET source code. I thank you in advance for any help given.
PrairieEagle
 
Posts: 5

Re: Some help needed

Postby hopscc » Wed Nov 03, 2010 12:46 am

Cant help ya with Notepad++.

re cmd.exe
- make sure that the directory to where cobra.exe was installed is in your PATH
- can you run cobra.exe using a fullpath to where it was installed ?
e.g.
Code: Select all
 \Cobra-0.8.0\bin\cobra.exe -help


alternatively copy your source file to the cobra installation bin directory and compile it in there

re source code:
your c# xlation looks pretty good - off the top of my head tho'
- dont need use Cobra.Lang - cobra assumes that for you ( ditto System.Collections{.Generic} )
- cobra has no typeof keyword/statement use WebBrowser.typeOf or WebBrowser.getType
- 'var' is only used to declare instance variables/attributes on classes/structs;declare local variables by first assignment/inference or directly
e.g.
Code: Select all
var lpk as String = getPK(Httputility.UrlDecode(SC + WebBrowser.Document.Cookie))
# becomes
lpk as String = getPK(Httputility.urlDecode(SC + WebBrowser.Document.cookie))
#or simply
lpk  = getPK(Httputility.urlDecode(SC + WebBrowser.Document.cookie))


- method calls are always started with lower case in cobra (converted for .Net libs)
Code: Select all
    t.Set -> t.set
    System.Threading.Thread.Sleep(100) -> System.Threading.Thread.sleep(100)
   


- calls to methods without params and declaration of methods without params dont need empty () - you;ll get a warning telling you this
Code: Select all
t.Set() -> t.set
def GetdAmnAuth() is shared has dAmnAuth
 #becomes
def getdAmnAuth is shared has dAmnAuth


- dont need 'new' to instantiate a class instance here you just explicitly call the class name ( with explicit trailing arglist even if empty)
Code: Select all
var t = new ManualResetEvent(false)
# becomes
 t =  ManualResetEvent(false)
#
rval as dAmnAuth = new dAmnAuth
# should be
rval as dAmnAuth =  dAmnAuth()
#or just
rval = dAmnAuth()


- eventhandlers/delegates dont use the '+=' syntax
Code: Select all
  WebBrowser1.DocumentCompleted += DC
 # becomes
   listen WebBrowser1.documentCompleted,  dC
# since def DC(..) should be declared something like def dC(...


- matching try and catch keywords need to be aligned at the same indentation level.

Theres probably more but if you can correct these and get the compiler executing it should be pretty clear.

FWiW if you've not seen it there are some notes on translating c# code to cobra on the wiki at
Porting C# code to cobra and
something of the differences C#/Cobra at Cobra differences from C#.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Some help needed

Postby PrairieEagle » Wed Nov 03, 2010 1:48 pm

hopscc wrote:FWiW if you've not seen it there are some notes on translating c# code to cobra on the wiki at
Porting C# code to cobra and
something of the differences C#/Cobra at Cobra differences from C#.

:D
Thank you for your help. And yes I have seen the differences and the how to port code from C#.
Oh and if anyone else reads this, I still do need help either getting notepad++ to work or help making an actually editor for cobra...I have a feeling that the later option would probably be less time consuming.
PrairieEagle
 
Posts: 5

Re: Some help needed

Postby Charles » Wed Nov 03, 2010 7:25 pm

hopscc advice was great, but just one thing. I'm guessing that your "typeof(Foo)" in C# should simply be "Foo" in Cobra. The ".typeOf" method is normally used on instances to get their type, and when used on a class, will return System.Type which is probably not what you wanted.

C#'s "typeof(Foo)" is really just a parsing thing as they don't allow direct references to types to be passed around (whereas Cobra and Python do).

So in C#:
Code: Select all
obj.SomeMethod(typeof(SomeClass))

Would be in Cobra:
obj.someMethod(SomeClass)
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Some help needed

Postby PrairieEagle » Thu Nov 04, 2010 4:32 pm

Thanks Chuck. but I got my code and everything seems to be in order except for one thing, whenever I run it I get this error:

Code: Select all
GetCookie.cobra(52): error: Cannot find "WebbrowserDocumentCompleted" at component 4 of qualified type.
Compilation failed - 1 error, 0 warnings
Not running due to errors above.


Why won't this part work?

def dC(sender as Object, e as System.Windows.Forms.WebbrowserDocumentCompleted.EventArgs)
lock(WebBrowser)
if gotPK <> true
lpk as String = getPK(Httputility.UrlDecode(sC + WebBrowser.Document.cookie))
pK = lpk
t.set()

I don't quite understand the error here. am I doing something wrong?
PrairieEagle
 
Posts: 5

Re: Some help needed

Postby Charles » Thu Nov 04, 2010 4:40 pm

When I search Google for WebbrowserDocumentCompleted I get hits for:

WebBrowser.DocumentCompleted

Capital B. Period after Browser. Try that.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Some help needed

Postby PrairieEagle » Sat Nov 06, 2010 5:40 am

Chuck wrote:When I search Google for WebbrowserDocumentCompleted I get hits for:

WebBrowser.DocumentCompleted

Capital B. Period after Browser. Try that.

:lol: Yeah, thanks Chuck, I really find cobra to be easy once you get paste all the spelling errors. and actually someone at deviantart was able to help with that one, now I can fix the 45 other errors like forgetting to put in def getPK and forgetting to put a period in someplace.
But I have a question to you, Chuch, wouldn't it be easier to make the language non case-sensitive? After all it just seems better and easier to use then.
And also wouldn't it be easy to make a how to port from VB.NET? could I try to make one for everyone?

Thanks again Chuck for your help.

And Also no matter what I do,
(HttpUtility.UrlDecode(.sC + .webBrowser1.document.cookie))

comes up with an error
GetCookie.cobra(56): error: Cannot find "HttpUtility". There is a member named ".t" with a similar name.

why am I getting this error? I don't understand it.
PrairieEagle
 
Posts: 5

Re: Some help needed

Postby Charles » Sun Nov 07, 2010 10:06 am

-- You'll be interested to know that Cobra has a -correct-source option which will write the correct case back to the source file:

-correct-source[:none|case|all] default is none
-cs
Rewrite source files with corrections, only when unambiguous. For example, "string" --> "String". If possible, you should set your editor to
automatically reload files when using this option.

Although I don't think it works on methods yet.

-- Note that where you had "WebbrowserDocumentCompleted", case alone would not have fixed the issue; another dot was needed.

-- Regarding why I don't make it case-insensitive, Cobra favors a more uniform approach to syntax than many languages so that programs differ more by their semantics rather than their syntax. Once you get used to the syntax, it is fairly easy to read other people's code because of the uniform approach to case, indentation, etc.

-- HttpUtility is a type from the libraries that, like all types, resides in some namespace and a library. If you search the Cobra source tree for it, you'll find a file that uses it that starts with:
@ref 'System.Web'
use System.Web

So add the @ref to your sample.

I have added a wiki page about HttpUtility so that in the future if someone searches for it on the web site, they will find this information.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Some help needed

Postby PrairieEagle » Sun Nov 07, 2010 10:46 am

Chuck wrote:-- You'll be interested to know that Cobra has a -correct-source option which will write the correct case back to the source file:

-correct-source[:none|case|all] default is none
-cs
Rewrite source files with corrections, only when unambiguous. For example, "string" --> "String". If possible, you should set your editor to
automatically reload files when using this option.

Although I don't think it works on methods yet.


Though I didn't understand what you said at in the quote...I think I understood what I needed to, Thank you chuck for such a wonderful language...
the ref didn't fix the problem but that is OK, I think that is my fault, I'll be back to debugging now...
PrairieEagle
 
Posts: 5

Re: Some help needed

Postby Charles » Sun Nov 07, 2010 2:27 pm

I will post about -correct-source under a new topic.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 79 guests