diff options
Diffstat (limited to 'other/fun/make-funny-savefile.py')
-rwxr-xr-x | other/fun/make-funny-savefile.py | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/other/fun/make-funny-savefile.py b/other/fun/make-funny-savefile.py new file mode 100755 index 00000000..43f14a6e --- /dev/null +++ b/other/fun/make-funny-savefile.py | |||
@@ -0,0 +1,122 @@ | |||
1 | #!/usr/bin/python | ||
2 | |||
3 | """ | ||
4 | Generate a new (and empty) save file with predefined keys. Used to play | ||
5 | with externally generated keys. | ||
6 | |||
7 | (c) 2015 Alexandre Erwin Ittner | ||
8 | |||
9 | Distributed under the GNU GPL v3 or later, WITHOUT ANY WARRANTY. See the | ||
10 | file "COPYING" for license information. | ||
11 | |||
12 | |||
13 | Usage: | ||
14 | |||
15 | ./make-funny-savefile.py <public key> <private key> <user name> <out file> | ||
16 | |||
17 | The new user profile will be saved to <out file>. | ||
18 | |||
19 | The keys must be an hex-encoded valid key pair generated by any means | ||
20 | (eg. strkey.c); username may be anything. A random nospam value will be | ||
21 | generated. | ||
22 | |||
23 | Once the savefile is done, load it in your favorite client to get some | ||
24 | DHT nodes, set status and status messages, add friends, etc. | ||
25 | |||
26 | |||
27 | Example (of course, do not try using this key for anything real): | ||
28 | |||
29 | ./make-funny-savefile.py 123411DC8B1A4760B648E0C7243B65F01069E4858F45C612CE1A6F673B603830 CC39440CFC063E4A95B7F2FB2580210558BE5C073AFC1C9604D431CCA3132238 "Test user" test.tox | ||
30 | |||
31 | |||
32 | """ | ||
33 | |||
34 | |||
35 | PUBLIC_KEY_LENGTH = 32 | ||
36 | PRIVATE_KEY_LENGTH = 32 | ||
37 | |||
38 | # Constants taken from messenger.c | ||
39 | MESSENGER_STATE_COOKIE_GLOBAL = 0x15ed1b1f | ||
40 | MESSENGER_STATE_COOKIE_TYPE = 0x01ce | ||
41 | MESSENGER_STATE_TYPE_NOSPAMKEYS = 1 | ||
42 | MESSENGER_STATE_TYPE_DHT = 2 | ||
43 | MESSENGER_STATE_TYPE_FRIENDS = 3 | ||
44 | MESSENGER_STATE_TYPE_NAME = 4 | ||
45 | MESSENGER_STATE_TYPE_STATUSMESSAGE = 5 | ||
46 | MESSENGER_STATE_TYPE_STATUS = 6 | ||
47 | MESSENGER_STATE_TYPE_TCP_RELAY = 10 | ||
48 | MESSENGER_STATE_TYPE_PATH_NODE = 11 | ||
49 | |||
50 | STATUS_MESSAGE = "New user".encode("utf-8") | ||
51 | |||
52 | |||
53 | |||
54 | import sys | ||
55 | import struct | ||
56 | import os | ||
57 | |||
58 | def abort(msg): | ||
59 | print(msg) | ||
60 | exit(1) | ||
61 | |||
62 | |||
63 | |||
64 | if len(sys.argv) != 5: | ||
65 | abort("Usage: %s <public key> <private key> <user name> <out file>" % (sys.argv[0])) | ||
66 | |||
67 | try: | ||
68 | public_key = sys.argv[1].decode("hex") | ||
69 | except: | ||
70 | abort("Bad public key") | ||
71 | |||
72 | try: | ||
73 | private_key = sys.argv[2].decode("hex") | ||
74 | except: | ||
75 | abort("Bad private key") | ||
76 | |||
77 | if len(public_key) != PUBLIC_KEY_LENGTH: | ||
78 | abort("Public key with wrong length") | ||
79 | |||
80 | if len(private_key) != PRIVATE_KEY_LENGTH: | ||
81 | abort("Private key with wrong length") | ||
82 | |||
83 | user_name = sys.argv[3].encode("utf-8") | ||
84 | |||
85 | if len(user_name) > 32: | ||
86 | abort("User name too long (for this script, at least)") | ||
87 | |||
88 | out_file_name = sys.argv[4] | ||
89 | nospam = os.urandom(4) | ||
90 | |||
91 | |||
92 | def make_subheader(h_type, h_length): | ||
93 | return ( | ||
94 | struct.pack("<I", h_length) + | ||
95 | struct.pack("<H", h_type) + | ||
96 | struct.pack("<H", MESSENGER_STATE_COOKIE_TYPE)) | ||
97 | |||
98 | data = ( | ||
99 | # Main header | ||
100 | struct.pack("<I", 0) + | ||
101 | struct.pack("<I", MESSENGER_STATE_COOKIE_GLOBAL) + | ||
102 | |||
103 | # Keys | ||
104 | make_subheader(MESSENGER_STATE_TYPE_NOSPAMKEYS, | ||
105 | len(nospam) + PUBLIC_KEY_LENGTH + PRIVATE_KEY_LENGTH) + | ||
106 | nospam + public_key + private_key + | ||
107 | |||
108 | # Name (not really needed, but helps) | ||
109 | make_subheader(MESSENGER_STATE_TYPE_NAME, len(user_name)) + | ||
110 | user_name + | ||
111 | |||
112 | # Status message (not really needed, but helps) | ||
113 | make_subheader(MESSENGER_STATE_TYPE_STATUSMESSAGE, len(STATUS_MESSAGE)) + | ||
114 | STATUS_MESSAGE | ||
115 | ) | ||
116 | |||
117 | |||
118 | try: | ||
119 | with open(out_file_name, "wb") as fp: | ||
120 | fp.write(data) | ||
121 | except Exception as e: | ||
122 | abort(str(e)) | ||