VB wtf..



  • Well, i thought i'd share this with you since it still amuses me.. i wrote this a good few years ago when i was in my teenage years (if its any excuse)

    Private Sub Command1_Click()

    Dim strOld As Date
    Dim strDif As Date
    Dim strHours As Integer
    Dim strMinutes As Integer
    Dim strMins As Integer
    Dim strOldMinutes As Integer
    Dim strSeconds As Integer
    Dim strOldSeconds As Integer
    Dim strTotalSeconds As Integer
    Dim strOnline As String
    Dim strDate As Date
    Dim strOldDate As Date
    Dim strDays As Integer
    Dim strNew As Date
    Dim strOldDays As Integer
    Dim strDifDays As Integer
    Dim strMonth As String
    Dim strOldMonth As String
    Dim strMonthDif As String
    Dim strTest As Integer
    Dim strNum As Integer
    Dim strMod As Integer
    Dim strTest2 As Integer
    Dim strExtraHours As Integer
    Dim strXHours As Integer
    Dim strOldXHours As Integer
    Dim strTotalMinutes As Integer
    Dim strTotalMinutes2 As Integer
    Dim strXTotal As Integer
    Dim strNum2 As Integer
    Dim strNum3 As Integer
    Dim strSecTot  As Integer
    Dim strDayz As Integer
    Dim strOldDayz As Integer
    Dim strTotalDayz As Integer
    Dim intTmp As Integer
    Dim intTotal2 As Integer
    Dim intMonthDif As Integer
    Dim strDayz2 As Integer
    Dim strXMonth As Integer
    Dim strX2Total As Integer

    strDate = "25/03/05"
    strOldDate = lblDate.Caption
    strNew = "13:42:43"
    strOld = lblTime.Caption

    If strDate = strOldDate Then
    strDif = strNew - strOld

    strHours = Mid(strDif, 1, 2)
    strMinutes = Mid(strDif, 4, 2)
    strSeconds = Mid(strDif, 7, 2)

    strOnline = "I have been online for " & strHours & " hour(s), " & strMinutes & " minute(s) and " & strSeconds & " second(s)"

    sckIRC.SendData "PRIVMSG #test :" & strOnline & vbCrLf
    display Time & " [ CT ]: " & strOnline & vbCrLf

    ElseIf strDate <> strOldDate Then

    strMonth = Mid(strDate, 4, 2)
    strOldMonth = Mid(strOldDate, 4, 2)

    If strMonth = strOldMonth Then

    strDays = Mid(strDate, 1, 2)
    strOldDays = Mid(strOldDate, 1, 2)

    strDifDays = strDays - strOldDays
    strDifDays = strDifDays + 1
    strDifDays = strDifDays - 2
    strDifDays = strDifDays * 24

    strXHours = Mid(strOld, 1, 2)

    strXHours = 24 - strXHours

    strOldXHours = Mid(strNew, 1, 2)

    strTest = strXHours + strOldXHours + strDifDays
    strTest2 = strXHours + strOldXHours + strDifDays

    strNum = 0
    Do While strTest > 0
    strTest = strTest - 24
    If strTest > 0 Then
    strNum = strNum + 1
    Else
    'nothing
    End If
    Loop

    strExtraHours = strNum * 24
    strExtraHours = strTest2 - strExtraHours

    strMinutes = Mid(strNew, 4, 2)
    strOldMinutes = Mid(strOld, 4, 2)

    strTotalMinutes = strMinutes + strOldMinutes

    strTotalMinutes2 = strMinutes + strOldMinutes

    If strTotalMinutes > 59 Then
    strNum2 = 1
    strXTotal = strTotalMinutes - 60
    Else
    strNum2 = 0
    strXTotal = strTotalMinutes
    End If

    strExtraHours = strExtraHours + strNum2

    strSeconds = Mid(strNew, 7, 2)
    strOldSeconds = Mid(strOld, 7, 2)

    strTotalSeconds = strSeconds + strOldSeconds

    If strTotalSeconds > 59 Then
    strNum3 = 1
    strSecTot = strTotalSeconds - 60
    Else
    strNum3 = 0
    strSecTot = strTotalSeconds
    End If

    strXTotal = strXTotal + strNum3

    If strNum > 1 Then

    strOnline = strNum & " days, " & strExtraHours & " hours, " & strXTotal & " minutes and " & strSecTot & " seconds"
    sckIRC.SendData "PRIVMSG #test :" & strOnline & vbCrLf
    display Time & " [ CT ]: " & strOnline & vbCrLf
    Else

    strOnline = strNum & " day, " & strExtraHours & " hours, " & strXTotal & " minutes and " & strSecTot & " seconds"
    sckIRC.SendData "PRIVMSG #test :" & strOnline & vbCrLf
    display Time & " [ CT ]: " & strOnline & vbCrLf
    End If

    'lblTest.Caption =

    ElseIf strMonth <> strOldMonth Then

    strDayz = Mid(strDate, 1, 2)
    strOldDayz = Mid(strOldDate, 1, 2)

    If (strOldMonth = 2) Then
    strDays = 28
    ElseIf (strOldMonth = 4) Or (strOldMonth = 6) Or (strOldMonth = 9) Or (strOldMonth = 11) Then
    strDays = 30
    ElseIf (strOldMonth = 1) Or (strOldMonth = 3) Or (strOldMonth = 5) Or (strOldMonth = 7) Or (strOldMonth = 8) Or (strOldMonth = 10) Or (strOldMonth = 12) Then
    strDays = 31
    End If

    If ((strMonth - strOldMonth) = 1) Then

    strOldDayz = strDays - strOldDayz

    strTotalDayz = strOldDayz + strDayz

    Else

    intTmp = strMonth
    intTmp = intTmp - 1
    intTotal2 = 0
    Do While (intTmp < strMonth) And (intTmp > strOldMonth)

    If (intTmp = 2) Then
    strDayz2 = 28
    ElseIf (intTmp = 4) Or (intTmp = 6) Or (intTmp = 9) Or (intTmp = 11) Then
    strDayz2 = 30
    ElseIf (intTmp = 1) Or (intTmp = 3) Or (intTmp = 5) Or (intTmp = 7) Or (intTmp = 8) Or (intTmp = 10) Or (intTmp = 12) Then
    strDayz2 = 31
    End If
    intTotal2 = intTotal2 + strDayz2

    intTmp = intTmp - 1

    Loop

    strXMonth = strDays - strOldDayz
    strTotalDayz = strXMonth + intTotal2 + strDayz


    End If
    Dim num4 As Integer
    Dim intTot As Integer
    strMinutes = Mid(strNew, 4, 2)
    strOldMinutes = Mid(strOld, 4, 2)

    strXHours = Mid(strNew, 1, 2)
    strOldXHours = Mid(strOld, 1, 2)



    strOldXHours = 24 - strOldXHours

    strExtraHours = strOldXHours + strXHours

    strTotalMinutes = strMinutes + strOldMinutes

    If strTotalMinutes > 59 Then
    strNum2 = 1
    strXTotal = strTotalMinutes - 60
    Else
    strNum2 = 0
    strXTotal = strTotalMinutes
    End If

    strExtraHours = strExtraHours + strNum2

    strSeconds = Mid(strNew, 7, 2)
    strOldSeconds = Mid(strOld, 7, 2)

    strTotalSeconds = strSeconds + strOldSeconds

    If strTotalSeconds > 59 Then
    strNum3 = 1
    strSecTot = strTotalSeconds - 60
    Else
    strNum3 = 0
    strSecTot = strTotalSeconds
    End If

    strXTotal = strXTotal + strNum3

    If strTotalDayz > 1 Then

    strOnline = strTotalDayz & " days, " & strExtraHours & " hours, " & strXTotal & " minutes and " & strSecTot & " seconds"
    sckIRC.SendData "PRIVMSG #test :" & strOnline & vbCrLf
    display Time & " [ CT ]: " & strOnline & vbCrLf
    Else
    sckIRC.SendData "PRIVMSG #test :" & strOnline & vbCrLf
    display Time & " [ CT ]: " & strOnline & vbCrLf
    strOnline = strTotalDayz & " day, " & strExtraHours & " hours, " & strXTotal & " minutes and " & strSecTot & " seconds"

    End If


    End If

    End If



    End Sub




    It basically works out the duration between 2 times.. god knows why i did it like that, i'm sure it can be done with 1/2 lines or something, but at the time, well i was naive. It was for an irc bot (you don't want to see the rest of the code!), so i thought i'd post it here, since it wasn't exactly going to kill anyone. Nice huh?


  • I think the funniest part of this by far is the utterly meaningless Hungarian notation in the variable names. Who'd have thought that strDif was a date, strDifDays was an integer, and strMonthDif was a string?

    No, scratch that, just make that the variable names themselves. strDays versus strDayz versus strDayz2... strMinutes versus strMins...



  • Dim strTest As Integer
    Dim strTest2 As Integer
    Dim intTmp As Integer

    reminds me of my desktop at times...
     



  • Maybe someone thought "str" meant "store"?



  • I didn't even read it in detail past the huge list of variables.  Needing that many is bad enough by itself, thanks for the entertainment.

     

    Oh, by the way, being a teenager is no excuse.  I'm 18, and I haven't done anything as bad as that...well, at least for a couple years. ;-) 



  • The key to a good WTF story is focusing on the WTF. Don't post a whole damn program! No one has time to go spelunking in there, no matter how many gems there are. Brevity! Focus!

    I gave up after noting the use of str for everything.

     

    btw, using strFoo and intFoo and boolFoo is not the intent of hungarian notiation. When hungarian notation means to represent type, it means "kind" not literally object type (as in class).

    for a good summary and reference read the "I'm Hungary" Section about halfway down: http://www.joelonsoftware.com/articles/Wrong.html

     


     



  • Guess I'm not the only one that wrote a VB IRC bot as a teenager :)



  • @muon said:

    i wrote this a good few years ago when i was in my teenage years (if its any excuse)

    It is, really.  You're allowed to do monstrously stupid things while you're still a student, and just about everyone does.  When you're a $100/hr consultant, producing crap like this for *pay*, and you consider yourself a competent professional... *That* is Worse Than Failure.
     



  • josh@JoshMobile:~$ expr $vb == $wtf

    1



  • @burntfuse said:

    Oh, by the way, being a teenager is no excuse.  I'm 18, and I haven't done anything as bad as that...well, at least for a couple years. ;-) 



    Yeah, definitely... I'm 17 and try to focus on clean code (mainly using MVC and similar design patterns... I'm hoping that's not just me :P). I don't do Visual Basic any more, but mainly do PHP coding now.



  • @Daniel15 said:

    @burntfuse said:

    Oh, by the way, being a teenager is no excuse. I'm 18, and I haven't done anything as bad as that...well, at least for a couple years. ;-)



    Yeah, definitely... I'm 17 and try to focus on clean code (mainly using MVC and similar design patterns... I'm hoping that's not just me :P). I don't do Visual Basic any more, but mainly do PHP coding now.

    When I read 'teenager' and 'VB' together I think 13-year-old. In reality it seems more whether you can control the impatience of youth and do things the 'right way', which generally takes time when you start young. At least, that was my experience...



  • I have seen some badly designed irc bots (including one that pipes the data sending thrught one of the event handlers for a button), and this is sadly an averenge irc bot.

    The IRC protocol is too simple for people to learn, it should be binary to get the worst crap away.
     



  • @henke37 said:

    The IRC protocol is too simple for people to learn, it should be binary to get the worst crap away.

    The IRC protocol looks simple, but is not. It's actually riddled with undocumented flaws that almost nobody knows about, resulting in a lot of broken clients.

    Example: what is the maximum safe length for a message sent to a channel? (Answer: there isn't one, but if you go sending messages up to the 512 byte limit that the spec says, your messages will be mangled or dropped, and it's not actually possible to predict whether a message will be passed correctly before you send it. In practice you have to stay under about 450 bytes to be safe most of the time)



  • @asuffield said:

    @henke37 said:

    The IRC protocol is too simple for people to learn, it should be binary to get the worst crap away.

    The IRC protocol looks simple, but is not. It's actually riddled with undocumented flaws that almost nobody knows about, resulting in a lot of broken clients.

    Example: what is the maximum safe length for a message sent to a channel? (Answer: there isn't one, but if you go sending messages up to the 512 byte limit that the spec says, your messages will be mangled or dropped, and it's not actually possible to predict whether a message will be passed correctly before you send it. In practice you have to stay under about 450 bytes to be safe most of the time)

    I'm no expert in IRC but wouldn't the server send your message to all members of the channel, including you? Then you could just compare the server "echo" with the message you sent to find out if it has been mangled.  



  • I agree however that IRC is one of those lovely examples of "organic growth". The countless incompatible *serv implementations are just an example. I believe there are a few approaches (IRC2, IRC+) to fix the problems but as usual they don't have much success as of now...



  • @PSWorx said:

    @asuffield said:

    Example: what is the maximum safe length for a message sent to a channel? (Answer: there isn't one, but if you go sending messages up to the 512 byte limit that the spec says, your messages will be mangled or dropped, and it's not actually possible to predict whether a message will be passed correctly before you send it. In practice you have to stay under about 450 bytes to be safe most of the time)

    I'm no expert in IRC but wouldn't the server send your message to all members of the channel, including you? Then you could just compare the server "echo" with the message you sent to find out if it has been mangled.  

    That does work, yes. It's not predicting. It is possible to observe, after you send it, whether or not your message was passed correctly.



  • Example: what is the maximum safe length for a message sent to a channel? (Answer: there isn't one, but if you go sending messages up to the 512 byte limit that the spec says, your messages will be mangled or dropped, and it's not actually possible to predict whether a message will be passed correctly before you send it. In practice you have to stay under about 450 bytes to be safe most of the time)

    The maximum safe length is 496 minus the lengths of:

    • Your nickname
    • Your username (as reported back to you from the IRC server, which may have truncated or prepended something etc)
    • Your hostname (also as reported back to you)
    • The channel name

     This is because the message that others receive has to be 512 bytes or less and consists of

    :nick!user@host PRIVMSG #chan :(your message)[CR][LF]

    That you don't understand the spec does not mean it does not apply. You do not have 512 bytes to put in the message body and the spec does not purport to claim that you do.

    @asuffield said:

    @PSWorx said:
    I'm no expert in IRC but wouldn't the server send your message to all members of the channel, including you? Then you could just compare the server "echo" with the message you sent to find out if it has been mangled. 

    That does work, yes. It's not predicting. It is possible to observe, after you send it, whether or not your message was passed correctly.

    False and false. The server does not send the message back to the sender, and if you in fact see your message truncated or split on your own screen it's because the program knew how much room you had but happily let you type more into the input line anyway.



  • @Random832 said:

    ...if you in fact see your message truncated or split on your own screen it's because the program knew how much room you had...
    [bah, we need more time to edit] In other words, "almost nobody knows about" these "undocumented flaws" except the people writing REAL irc clients, as opposed to bots built on their scripting language. And splitting the line in two ("mangled", as you call it) is perfectly reasonable behavior when you consider this is a chat protocol. Though it'd be better if they would split on word boundaries, and if those that do not would limit the amount you can type in the input box.



  • @Random832 said:

    Example: what is the maximum safe length for a message sent to a
    channel? (Answer: there isn't one, but if you go sending messages up to
    the 512 byte limit that the spec says, your messages will be mangled or
    dropped, and it's not actually possible to predict whether a message
    will be passed correctly before you send it. In practice you have to
    stay under about 450 bytes to be safe most of the time)

    The maximum safe length is 496 minus the lengths of:

    • Your nickname
    • Your username (as reported back to you from the IRC server, which may have truncated or prepended something etc)
    • Your hostname (also as reported back to you)
    • The channel name

    This is not necessarily true, and varies depending on the server implementation. In reality, you cannot predict the size of the prefix that will be applied to the message during transport. It does not work on any of the major servers.

    In other words, "almost nobody knows about" these "undocumented flaws"
    except the people writing REAL irc clients, as opposed to bots built on
    their scripting language.

    The only reason that some of the "REAL" IRC clients get this "right" is because I personally went around checking them for this flaw and reporting the problems to the relevant people, five or six years ago. I did not actually find a single one that did not have this bug (including all forms of ircii, bitchx, irssi, and xchat). Most of them now adopt the approach of just using an arbitrarily shortened guess on the limit. There was some discussion about altering the protocol to provide better guarantees, but nothing ever came of it.



  • What about PRIVMSG'ing yourself once to find the length of the nick!user@host prefix - that's the only real unknown as far as I can tell, and that doesn't change (well, unless you change nick).

    I guess I assumed less knowledge of the protocol on your part because you thought (someone else thought, you agreed) that messages to channels would be echoed back by the server, or that it the cutoff length was random [i read this as "it may vary from one message to the next within the same channel"] (I guess I was wrong though, about the server explicitly reporting back the user@host it chooses for you - but you can get it by sending any message to yourself) 



  • @Random832 said:

    What about PRIVMSG'ing yourself once to find the length of the nick!user@host prefix - that's the only real unknown as far as I can tell, and that doesn't change (well, unless you change nick).

    I guess I assumed less knowledge of the protocol on your part because you thought (someone else thought, you agreed) that messages to channels would be echoed back by the server, or that it the cutoff length was random [i read this as "it may vary from one message to the next within the same channel"] (I guess I was wrong though, about the server explicitly reporting back the user@host it chooses for you - but you can get it by sending any message to yourself) 

    Unfortunately, it may vary over time and in unpredictable ways as servers split and rejoin. There are some really annoying failure modes in the IRC protocol. 



  • @asuffield said:

    @Random832 said:

    What about PRIVMSG'ing yourself once to find the length of the nick!user@host prefix - that's the only real unknown as far as I can tell, and that doesn't change (well, unless you change nick).

    I guess I assumed less knowledge of the protocol on your part because you thought (someone else thought, you agreed) that messages to channels would be echoed back by the server, or that it the cutoff length was random [i read this as "it may vary from one message to the next within the same channel"] (I guess I was wrong though, about the server explicitly reporting back the user@host it chooses for you - but you can get it by sending any message to yourself) 

    Unfortunately, it may vary over time and in unpredictable ways as servers split and rejoin.

    I don't think I believe you. Since I'm pretty sure I at least know enough about the IRC protocol that I won't be completely lost if you explain it, I'd like to hear an explanation of how this can happen. 



  • @Random832 said:

    I don't think I believe you. Since I'm pretty sure I at least know enough about the IRC protocol that I won't be completely lost if you explain it, I'd like to hear an explanation of how this can happen.
    Hint: services. As they go up and down, your host may get masked/unmasked, changed to an arbitrary string. And just for fun, you can also manually log out and log in to the nickserv, or request an arbitrary hostmask. Did I mention that doing these things (including receiving notifications that something has changed) varies between networks?



  • 512 bytes should be enough for anyone.



  • VB wtf

     Redundancy redundancy?
     



  • The Real WTF (yeah, yeah...) is VB.
     


Log in to reply