1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 // This code can be compiled and used to test the otr package against libotr.
6 // See otr_test.go.
7 
8 // +build ignore
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 #include <proto.h>
15 #include <message.h>
16 
17 static int g_session_established = 0;
18 
policy(void * opdata,ConnContext * context)19 OtrlPolicy policy(void *opdata, ConnContext *context) {
20   return OTRL_POLICY_ALWAYS;
21 }
22 
is_logged_in(void * opdata,const char * accountname,const char * protocol,const char * recipient)23 int is_logged_in(void *opdata, const char *accountname, const char *protocol, const char *recipient) {
24   return 1;
25 }
26 
inject_message(void * opdata,const char * accountname,const char * protocol,const char * recipient,const char * message)27 void inject_message(void *opdata, const char *accountname, const char *protocol, const char *recipient, const char *message) {
28   printf("%s\n", message);
29   fflush(stdout);
30   fprintf(stderr, "libotr helper sent: %s\n", message);
31 }
32 
notify(void * opdata,OtrlNotifyLevel level,const char * accountname,const char * protocol,const char * username,const char * title,const char * primary,const char * secondary)33 void notify(void *opdata, OtrlNotifyLevel level, const char *accountname, const char *protocol, const char *username, const char *title, const char *primary, const char *secondary) {
34   fprintf(stderr, "NOTIFY: %s %s %s %s\n", username, title, primary, secondary);
35 }
36 
display_otr_message(void * opdata,const char * accountname,const char * protocol,const char * username,const char * msg)37 int display_otr_message(void *opdata, const char *accountname, const char *protocol, const char *username, const char *msg) {
38   fprintf(stderr, "MESSAGE: %s %s\n", username, msg);
39   return 1;
40 }
41 
update_context_list(void * opdata)42 void update_context_list(void *opdata) {
43 }
44 
protocol_name(void * opdata,const char * protocol)45 const char *protocol_name(void *opdata, const char *protocol) {
46         return "PROTOCOL";
47 }
48 
protocol_name_free(void * opdata,const char * protocol_name)49 void protocol_name_free(void *opdata, const char *protocol_name) {
50 }
51 
new_fingerprint(void * opdata,OtrlUserState us,const char * accountname,const char * protocol,const char * username,unsigned char fingerprint[20])52 void new_fingerprint(void *opdata, OtrlUserState us, const char *accountname, const char *protocol, const char *username, unsigned char fingerprint[20]) {
53         fprintf(stderr, "NEW FINGERPRINT\n");
54         g_session_established = 1;
55 }
56 
write_fingerprints(void * opdata)57 void write_fingerprints(void *opdata) {
58 }
59 
gone_secure(void * opdata,ConnContext * context)60 void gone_secure(void *opdata, ConnContext *context) {
61 }
62 
gone_insecure(void * opdata,ConnContext * context)63 void gone_insecure(void *opdata, ConnContext *context) {
64 }
65 
still_secure(void * opdata,ConnContext * context,int is_reply)66 void still_secure(void *opdata, ConnContext *context, int is_reply) {
67 }
68 
log_message(void * opdata,const char * message)69 void log_message(void *opdata, const char *message) {
70         fprintf(stderr, "MESSAGE: %s\n", message);
71 }
72 
max_message_size(void * opdata,ConnContext * context)73 int max_message_size(void *opdata, ConnContext *context) {
74   return 99999;
75 }
76 
account_name(void * opdata,const char * account,const char * protocol)77 const char *account_name(void *opdata, const char *account, const char *protocol) {
78         return "ACCOUNT";
79 }
80 
account_name_free(void * opdata,const char * account_name)81 void account_name_free(void *opdata, const char *account_name) {
82 }
83 
84 OtrlMessageAppOps uiops = {
85   policy,
86   NULL,
87   is_logged_in,
88   inject_message,
89   notify,
90   display_otr_message,
91   update_context_list,
92   protocol_name,
93   protocol_name_free,
94   new_fingerprint,
95   write_fingerprints,
96   gone_secure,
97   gone_insecure,
98   still_secure,
99   log_message,
100   max_message_size,
101   account_name,
102   account_name_free,
103 };
104 
105 static const char kPrivateKeyData[] = "(privkeys (account (name \"account\") (protocol proto) (private-key (dsa (p #00FC07ABCF0DC916AFF6E9AE47BEF60C7AB9B4D6B2469E436630E36F8A489BE812486A09F30B71224508654940A835301ACC525A4FF133FC152CC53DCC59D65C30A54F1993FE13FE63E5823D4C746DB21B90F9B9C00B49EC7404AB1D929BA7FBA12F2E45C6E0A651689750E8528AB8C031D3561FECEE72EBB4A090D450A9B7A857#) (q #00997BD266EF7B1F60A5C23F3A741F2AEFD07A2081#) (g #535E360E8A95EBA46A4F7DE50AD6E9B2A6DB785A66B64EB9F20338D2A3E8FB0E94725848F1AA6CC567CB83A1CC517EC806F2E92EAE71457E80B2210A189B91250779434B41FC8A8873F6DB94BEA7D177F5D59E7E114EE10A49CFD9CEF88AE43387023B672927BA74B04EB6BBB5E57597766A2F9CE3857D7ACE3E1E3BC1FC6F26#) (y #0AC8670AD767D7A8D9D14CC1AC6744CD7D76F993B77FFD9E39DF01E5A6536EF65E775FCEF2A983E2A19BD6415500F6979715D9FD1257E1FE2B6F5E1E74B333079E7C880D39868462A93454B41877BE62E5EF0A041C2EE9C9E76BD1E12AE25D9628DECB097025DD625EF49C3258A1A3C0FF501E3DC673B76D7BABF349009B6ECF#) (x #14D0345A3562C480A039E3C72764F72D79043216#)))))\n";
106 
107 int
main()108 main() {
109   OTRL_INIT;
110 
111   // We have to write the private key information to a file because the libotr
112   // API demands a filename to read from.
113   const char *tmpdir = "/tmp";
114   if (getenv("TMP")) {
115     tmpdir = getenv("TMP");
116   }
117 
118   char private_key_file[256];
119   snprintf(private_key_file, sizeof(private_key_file), "%s/libotr_test_helper_privatekeys-XXXXXX", tmpdir);
120   int fd = mkstemp(private_key_file);
121   if (fd == -1) {
122     perror("creating temp file");
123   }
124   write(fd, kPrivateKeyData, sizeof(kPrivateKeyData)-1);
125   close(fd);
126 
127   OtrlUserState userstate = otrl_userstate_create();
128   otrl_privkey_read(userstate, private_key_file);
129   unlink(private_key_file);
130 
131   fprintf(stderr, "libotr helper started\n");
132 
133   char buf[4096];
134 
135   for (;;) {
136     char* message = fgets(buf, sizeof(buf), stdin);
137     if (strlen(message) == 0) {
138       break;
139     }
140     message[strlen(message) - 1] = 0;
141     fprintf(stderr, "libotr helper got: %s\n", message);
142 
143     char *newmessage = NULL;
144     OtrlTLV *tlvs;
145     int ignore_message = otrl_message_receiving(userstate, &uiops, NULL, "account", "proto", "peer", message, &newmessage, &tlvs, NULL, NULL);
146     if (tlvs) {
147             otrl_tlv_free(tlvs);
148     }
149 
150     if (newmessage != NULL) {
151       fprintf(stderr, "libotr got: %s\n", newmessage);
152       otrl_message_free(newmessage);
153 
154       gcry_error_t err;
155       char *newmessage = NULL;
156 
157       err = otrl_message_sending(userstate, &uiops, NULL, "account", "proto", "peer", "test message", NULL, &newmessage, NULL, NULL);
158       if (newmessage == NULL) {
159         fprintf(stderr, "libotr didn't encrypt message\n");
160         return 1;
161       }
162       write(1, newmessage, strlen(newmessage));
163       write(1, "\n", 1);
164       g_session_established = 0;
165       otrl_message_free(newmessage);
166       write(1, "?OTRv2?\n", 8);
167     }
168   }
169 
170   return 0;
171 }
172