kmail functions - send meat, items with specific message and useful return value

Status
Not open for further replies.

zarqon

Well-known member
Here are some manual kmail functions in case you want to send specific text along with the items/meat. I wrote these for our clan tradebot/treasurer and decided others might find them useful.

It mirrors the behavior of cli_execute("send xx to xx"), in that it will send gifts if the target is in Ronin/hardcore.

Code:
boolean send_gift(string to, string message, int meat, int[item] goodies) {
 // parse items into query string
  string itemstring = "";
  int j = 0;
  int[item] extra;
  if (count(goodies) > 0)
  foreach i in goodies {
   if (is_tradeable(i)) {
     j = j+1;
     if (j < 4)
      itemstring = itemstring + "&howmany"+j+"="+goodies[i]+"&whichitem"+j+"="+to_int(i);
     else extra[i] = goodies[i];
   }
  }
  int shipping = 200;
  int pnum = 3;
  if (count(goodies) < 3) {
   shipping = 50*count(goodies);
   pnum = count(goodies);
  }
  if (my_meat() < meat+shipping) {
   print("Not enough meat to send the package.","red");
   return false;
  }
 // send gift
  string url = visit_url("town_sendgift.php?pwd=&towho="+to+"&note="+message+"&insidenote=&whichpackage="+pnum+"&fromwhere=0&sendmeat="+meat+"&action=Yep."+itemstring);
  if (!contains_text(url,"Package sent.")) {
   print("The message didn't send for some reason.","red");
   return false;
  }
  if (count(extra) > 0) return send_gift(to,"Here are your remaining items that would not fit in the previous message.",0,extra);
  return true;
}

boolean kmail(string to, string message, int meat, int[item] goodies) {
 // parse items into query strings
  string itemstring = "";
  int j = 0;
  string[int] itemstrings;
  if (count(goodies) > 0)
  foreach i in goodies {
   if (is_tradeable(i)) {
    j = j+1;
    itemstring = itemstring + "&howmany"+j+"="+goodies[i]+"&whichitem"+j+"="+to_int(i);
    if (j > 10) {
      itemstrings[count(itemstrings)] = itemstring;
      itemstring = '';
      j = 0;
    }
   }
  }
  if (itemstring != "") itemstrings[count(itemstrings)] = itemstring;
  if (count(itemstrings) == 0) itemstrings[0] = "";
 // send message(s)
  string msg;
  foreach q in itemstrings {
   if (q == 0) { msg = message; } else msg = 'Here are your remaining items that would not fit in the previous message.';
   string url = visit_url("sendmessage.php?pwd=&action=send&towho="+url_encode(to)+"&message="+url_encode(msg)+"&sendmeat="+meat+itemstrings[q]);
   if (contains_text(url,"That player cannot receive Meat or items"))
     return send_gift(to, message, meat, goodies);
   if (!contains_text(url,"Message sent.")) {
     print("The message didn't send for some reason.","red");
     return false;
   }
  }
  return true;
}
 
Last edited:

macman104

Member
Re: kmail functions - send meat, items with specific message

Hate to burst your bubble for all of your hard work but mafia already allows you to send a specific message
Code:
cli_execute("send ...... to ...... || "My message here");
 

zarqon

Well-known member
Re: kmail functions - send meat, items with specific message

The CLI "send" hasn't been working for me... I keep getting blank messages, using either send or csend. I needed greater control over it (I sometimes adjust clannies' credit scores by the cost of packaging if they request item while they're in Ronin/HC), and I needed to know whether or not it worked, i.e. a useful return value (did it send?). My bubble is not burst.

All the same, that's good to know.
Another useful application of this could be

Code:
kmail("MyMulti","Here you go!",my_meat(),get_inventory());

for when you are ascending into HC and want to send all your stuff to another account.

Although, thinking about it, a per-item is_tradeable() check would probably be a good idea. I do the check elsewhere in my itembot script. I'll put that in the first post.
 

macman104

Member
Re: kmail functions - send meat, items with specific message

[quote author=zarqon link=topic=1683.msg7892#msg7892 date=1208404868]Another useful application of this could be

Code:
kmail("MyMulti","Here you go!",my_meat(),get_inventory());

for when you are ascending into HC and want to send all your stuff to another account.

Although, thinking about it, a per-item is_tradeable() check would probably be a good idea. I do the check elsewhere in my itembot script. I'll put that in the first post.[/quote]Oooo, I really like this! This would be awesome!
 

Leperconartist

New member
Re: kmail functions - send meat, items with specific message and useful return v

I copied and pasted this just to see how the variables work. I saved it and called it "Kmailer.ash", and then I made a new script to send 10 000 meat to Zarqon(If that is your real name) my script looks like this:

Code:
Void Main(){
import {Kmailer.ash};
kmail("Zarqon","nice script it worked",9999999999999,0);
}

*Changed the number to 9999999999999 for no reason*
Then I loaded the script into into KolMafia, and this is what happened.

> call scripts\Copy of New.ash

Unknown variable 'else' (Kmailer.ash, line 44)

I think the problem is that you put an else where you needed an if, I'd fix it myself but I don't quite understand how your program works, and don't know what you need to check, but perhaps it's just that I am using the variables wrong? Please help me if you have time.

And since I'm trying to figure out scripting, how did you get the full URL for the message
("sendmessage.php?pwd=&action=send&towho="+url_encode(to)+"&message="+url_encode(message)+
"&sendmeat="+meat+itemstring)
 

zarqon

Well-known member
Re: kmail functions - send meat, items with specific message and useful return v

Oops, that turned out to be a completely extraneous "else", which I forgot to delete when I added the is_tradeable() check. You can just delete the else from line 44. I've made the change in the first post too.

For how I got the URL: you'll need to research HTML forms and query strings. To get you started: Loathing uses HTML forms all over the place (everything inside the < form ></ form > tags) to get user input. To make a URL suitable for visit_url() out of this you have to use the target page in the form's "action" property, then make a query string out of all the input fields (including the hidden ones), as name=value, separated by ampersands:

variable1=value1&variable2=value2

Good luck.
 

Leperconartist

New member
Re: kmail functions - send meat, items with specific message and useful return v

Thanks for fixing the script and for the explanation, I'll start researching it right away.

#A few more questions about the code for whenever you or somebody else has time.
1)How do I edit my message code so that it leaves a space (As if I had hit the enter button)?
This isn't really needed if you don't know, then just nvm.

2)I've seen it all the time, but could never quite figure out what it does. What exactly does Return do?

3)Why did you set up your kmail, and send_gift as Booleans instead of Voids?

#Questions about how to use the program.
I'm pretty sure I got the first parts of it working, but I don't know how to use the last part
[size=12pt]kmail("Zarqon","Nice script it works, I Just don't know how to use it ;)",10000,(This part here));[/size]

1) How do I say that I don't want to add an item
2) How do I name an item, and the amount of it?

PS. This is the last time I'll post here, unless I find a problem in the program. Thanks for your help hope I didn't bother you too much.
 

zarqon

Well-known member
1) use \n, for example "This is on line one,\nbut this is on line two."

2) return tells a function to return the following value. If the function returns void, return simply exits the function.

3) These functions return booleans so they have a useful return value. If you try to send a kmail and it doesn't send, the function returns false. I use this return value in my itembot/treasurer script for our clan... he keeps a credit score for each clan member, based on donations. They can use their credit to request items. When I send them an item, I deduct the value of that item from their credit. If there is an error sending the item, I don't want to deduct from their credit anyway.

So I do something like
PHP:
if (kmail(clannie,"Here are your items",0,items)) reduce_credit(clannie, amount).
Very useful.

1) To send no items, just pass an empty int[item] variable. For example:

Code:
int[item] nothing;
kmail("Zarqon","It works!",10000,nothing);

2) To send items, populate the map first:

Code:
int[item] stuff;
stuff[$item[rewinged stab bat]] = 11;
stuff[$item[batgut]] = 11;
kmail("Zarqon","It works!",10000,stuff);    // this will send me 11 bats and 11 batguts
 

Leperconartist

New member
Re: kmail functions - send meat, items with specific message and useful return v

:D That's ingenious, you've taught me a lot and the script rocks. TYVM

Okay I lied, 1 more question, this script gets rid of my meat and items. But then KolMafia thinks that I still have those items and meat. Is there anyway to refresh KolMafia or something and get my item and meat amounts back to what they actually are?
 

zarqon

Well-known member
To refresh meat: just click the "reload character sheet" button in the bottom left.
To refresh items: type "inv refresh" in the CLI.
 

macman104

Member
Re: kmail functions - send meat, items with specific message and useful return v

Script worked great, sent all of my items over to my mall multi.

However, one thing for a future improvement might be to have an array of strings that contain your send commands and build it up like that (and then pass return the completed map and go through and send each string). Right now, if I have 75 items, it'll go through and once it hits 11, it'll still traverse the whole map and add the remaining items to a new one, and then go through those, and once it hits 11, go through the new map, and etc...this adds alot of time if you have more than just a handful of items.
 

zarqon

Well-known member
Re: kmail functions - send meat, items with specific message and useful return v

Yeah, I've realized recursion is suboptimal here, it was just easier to code. :)

I think you're talking about something like this?

Code:
boolean kmail(string to, string message, int meat, int[item] goodies) {
  // parse items into query strings
   string itemstring = "";
   int j = 0;
   string[int] itemstrings;
   if (count(goodies) > 0)
    foreach i in goodies {
      if (is_tradeable(i)) {
        j = j+1;
        itemstring = itemstring + "&howmany"+j+"="+goodies[i]+"&whichitem"+j+"="+to_int(i);
        if (j > 10) {
           itemstrings[count(itemstrings)] = itemstring;
           itemstring = '';
           j = 0;
        }
      }
    }
   if (itemstring != "") itemstrings[count(itemstrings)] = itemstring;
   if (count(itemstrings) == 0) itemstrings[0] = "";
  // send message(s)
   string msg;
   foreach q in itemstrings {
      if (q == 0) { msg = message; } else msg = 'Here are your remaining items that would not fit in the previous message.';
      string url = visit_url("sendmessage.php?pwd=&action=send&towho="+url_encode(to)+"&message="+url_encode(msg)+"&sendmeat="+meat+itemstrings[q]);
      if (contains_text(url,"That player cannot receive Meat or items"))
         return send_gift(to, message, meat, goodies);
      if (!contains_text(url,"Message sent.")) {
         print("The message didn't send for some reason.","red");
         return false;
      }
   }
   return true;
}
 

gaap

New member
Re: kmail functions - send meat, items with specific message and useful return v

So how do you really go about using posted scripts?

I made a file 'kmailer.ash' which held the script from the first post and nothing else. I called the file by typing 'using kmailer.ash' in the graphical CLI. From here on, I'm not sure how to proceed. I've got a simple farming script in which I'd like to make my farmers send meat to my main, I'm just not sure how to go about incorporating kmailer.ash into this.
 

Bale

Minion
Re: kmail functions - send meat, items with specific message and useful return v

[quote author=gaap link=topic=1683.msg11268#msg11268 date=1232589125]
I've got a simple farming script in which I'd like to make my farmers send meat to my main, I'm just not sure how to go about incorporating kmailer.ash into this.
[/quote]

Jeez. Couldn't you have just made up something that doesn't make everyone dislike you for being a dirty exploiter? Did you just not have a clue that you were doing something wrong?
 

Alphonse Kane

New member
Awesome script.

Our clan runs (#1818136), and uses this as part of a "stuffie cannon" script. The code has expanded to include a lot of chat_reply stuff and buffing, but it started with this. It works great- we just took out the part with the "is tradeable" check because stuffies are incorrectly labeled as non-tradeable. If only there were a seperate "is mall-able" check...
 

The Botter

New member
So I've got a simple farming script in which I'd like to make my farmers send meat to my main, I'm just not sure how to go about incorporating kmailer.ash into this.
Wow you just admitted to multi abuse. At the same time it angers me and others who play the game by the rules for the most part (we've all done our fair share of shady stuff).
 

Bale

Minion
That was several months ago, he's already been called out on it and hasn't dared to show his face since. I'd like to just forget him by now.
 
Status
Not open for further replies.
Top