randpost.php 6.93 KB
Newer Older
1 2 3
<?php
/**
 * Name: Randpost
4
 * Description: Make random posts/replies (requires fortunate and/or a fortunate server)
5 6
 * Version: 1.0
 * Author: Mike Macgirvin
7
 * Maintainer: none
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 */

require_once('include/crypto.php');

function randpost_load() {
	register_hook('cron', 'addon/randpost/randpost.php', 'randpost_fetch');
	register_hook('enotify_store', 'addon/randpost/randpost.php', 'randpost_enotify_store');
}

function randpost_unload() {
	unregister_hook('cron', 'addon/randpost/randpost.php', 'randpost_fetch');
	unregister_hook('enotify_store', 'addon/randpost/randpost.php', 'randpost_enotify_store');
}


function randpost_enotify_store(&$a,&$b) {

redmatrix's avatar
redmatrix committed
25
	if(! ($b['ntype'] == NOTIFY_COMMENT || $b['ntype'] == NOTIFY_TAGSELF))
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
		return;

	if(! get_pconfig($b['uid'],'randpost','enable'))
		return;


	$fort_server = get_config('fortunate','server');
	if(! $fort_server)
		return;


	$c = q("select * from channel where channel_id = %d limit 1",
		intval($b['uid'])
	);
	if(! $c)
		return;

43
	$my_conversation = false;
44

45
	$p = q("select id, item_flags, author_xchan from item where parent_mid = mid and parent_mid = '%s' and uid = %d limit 1",
46 47 48 49 50 51
		dbesc($b['item']['parent_mid']),
		intval($b['uid'])
	);
	if(! $p)
		return;

52
	$p = fetch_post_tags($p,true);
friendica's avatar
friendica committed
53

redmatrix's avatar
redmatrix committed
54
	if(intval($p[0]['item_obscured']))
55 56
		return;

57

redmatrix's avatar
redmatrix committed
58
	if($b['ntype'] == NOTIFY_TAGSELF)
59 60 61 62
		$my_conversation = true;
	elseif($p[0]['author_xchan'] === $c[0]['channel_hash'])
		$my_conversation = true;
	elseif($p[0]['term']) {
friendica's avatar
friendica committed
63
		$v = get_terms_oftype($p[0]['term'],TERM_MENTION);
redmatrix's avatar
redmatrix committed
64
		$link = normalise_link(z_root() . '/channel/' . $c[0]['channel_address']);
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		if($v) {
			foreach($v as $vv) {
				if(link_compare($vv['url'],$link)) {			
					$my_conversation = true;
					break;
				}
			}
		}				
	}
	
	// don't hijack somebody else's conversation, but respond (once) if invited to. 

	if(! $my_conversation)
		return;

friendica's avatar
friendica committed
80 81 82
	// This conversation is boring me.

	$limit = mt_rand(5,20);
83 84

	$h = q("select id, body from item where author_xchan = '%s' and parent_mid = '%s' and uid = %d",
friendica's avatar
friendica committed
85 86 87 88 89 90
		dbesc($c[0]['channel_hash']),
		dbesc($b['item']['parent_mid']),
		intval($b['uid'])
	);
	if($h && count($h) > $limit)
		return;
91

friendica's avatar
friendica committed
92 93 94 95
 
	
	// Be gracious and not obnoxious if thanked

96 97 98 99 100 101 102 103
	$replies = array(
		t('You\'re welcome.'),
		t('Ah shucks...'),
		t('Don\'t mention it.'),
		t('&lt;blush&gt;'),
		':like'
	);

friendica's avatar
friendica committed
104 105 106 107

	// TODO: if you really want to freak somebody out, add a relevance search function to mod_zotfeed and
	// use somebody's own words from long ago to craft a reply to them....

108 109 110 111
	require_once('include/bbcode.php');
	require_once('include/html2plain.php');

	if($b['item'] && $b['item']['body']) {
112 113
		if(stristr($b['item']['body'],'nocomment'))
			return;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		$txt = preg_replace('/\@\[z(.*?)\[\/zrl\]/','',$b['item']['body']);
		$txt = html2plain(bbcode($txt));
		$pattern = substr($txt,0,255);
	}

	if($b['item']['author_xchan']) {
		$z = q("select * from xchan where xchan_hash = '%s' limit 1",
			dbesc($b['item']['author_xchan'])
		);
		if($z) {
			$mention = '@' . '[zrl=' . $z[0]['xchan_url'] . ']' . $z[0]['xchan_name'] . '[/zrl]' . "\n\n";
		}
	}

	if(stristr($b['item']['body'],$c[0]['channel_name']) && mb_strlen($pattern) < 36 && stristr($pattern,'thank')) {
		$reply = $replies[mt_rand(0,count($replies)-1)];
	}

	$x = array();

	if($reply) {
		$x['body'] = $mention . $reply;
	}
	else {
friendica's avatar
friendica committed
138 139
		require_once('include/html2bbcode.php');

140
		$valid = false;
friendica's avatar
friendica committed
141

142 143
		do {
			$url = 'http://' . $fort_server . '/cookie.php?f=&lang=any&off=a&pattern=' . urlencode($pattern);
friendica's avatar
friendica committed
144

145 146 147 148 149 150 151
			$s = z_fetch_url($url);

			if($s['success'] && (! $s['body']))
				$s = z_fetch_url('http://' . $fort_server . '/cookie.php');

			if((! $s['success']) || (! $s['body']))
				return;
friendica's avatar
friendica committed
152

153 154
			// if it might be a quote make it a quote
			if(strpos($s['body'],'--'))
155
				$x['body'] = '[quote]' . html2bbcode($s['body']) . '[/quote]';
156
			else
157
				$x['body'] = html2bbcode($s['body']);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

			$found_text = false;

			if($h) {
				foreach($h as $hh) {
					if(stripos($hh['body'],$x['body']) !== false) {
						$pattern = '';
						$found_text = true;
						break;
					}
				}
			}
			if(! $found_text)
				$valid = true;

		}
		while(! $valid);
175 176 177
	}

	if($mention) {
178 179
		$x['body'] = $mention . $x['body'];

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
		$x['term'] = array(array(
			'uid' => $c[0]['channel_id'],
			'type' => TERM_MENTION,
			'otype' => TERM_OBJ_POST,
			'term' => $z[0]['xchan_name'],
			'url' => $z[0]['xchan_url']
		));
	}

	$x['uid'] = $c[0]['channel_id'];
	$x['aid'] = $c[0]['channel_account_id'];
	$x['mid'] = item_message_id();
	$x['parent'] = $p[0]['id'];
	$x['parent_mid'] = $b['item']['parent_mid'];
	$x['author_xchan'] = $c[0]['channel_hash'];
	$x['owner_xchan'] = $b['item']['owner_xchan'];

redmatrix's avatar
redmatrix committed
197 198
	$x['item_origin'] = 1;
	$x['item_verified'] = 1;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

	// You can't pass a Turing test if you reply in milliseconds. 
	// Also I believe we've got ten minutes fudge before we declare a post as time traveling.
	// Otherwise we'll just set it to now and it will still go out in milliseconds. 
	// So set the reply to post sometime in the next 15-45 minutes (depends on poller interval)

	$fudge = mt_rand(15,30);
	$x['created'] = $x['edited'] = datetime_convert('UTC','UTC','now + ' . $fudge . ' minutes');

	$x['body'] = trim($x['body']);
	$x['sig'] = base64url_encode(rsa_sign($x['body'],$c[0]['channel_prvkey']));

	$post = item_store($x);
	$post_id = $post['item_id'];

	$x['id'] = $post_id;

	call_hooks('post_local_end', $x);

redmatrix's avatar
redmatrix committed
218
	Zotlabs\Daemon\Master::Summon(array('Notifier','comment-new',$post_id));	
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

}



function randpost_fetch(&$a,&$b) {

	$fort_server = get_config('fortunate','server');
	if(! $fort_server)
		return;

	$r = q("select * from pconfig where cat = 'randpost' and k = 'enable'");

	if($r) {
		foreach($r as $rr) {
			if(! $rr['v'])
				continue;
//			logger('randpost');

			// cronhooks run every 10-15 minutes typically
239
			// try to keep from posting frequently.
240

241
			$test = mt_rand(0,100);
242
			if($test == 25) {
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
				$c = q("select * from channel where channel_id = %d limit 1",
					intval($rr['uid'])
				);
				if(! $c)
					continue;

				$mention = '';

				require_once('include/html2bbcode.php');

				$s = z_fetch_url('http://' . $fort_server . '/cookie.php?numlines=2&equal=1&rand=' . mt_rand());
				if(! $s['success'])
					continue;

				$x = array();
				$x['uid'] = $c[0]['channel_id'];
				$x['aid'] = $c[0]['channel_account_id'];
				$x['mid'] = $x['parent_mid'] = item_message_id();
				$x['author_xchan'] = $x['owner_xchan'] = $c[0]['channel_hash'];
redmatrix's avatar
redmatrix committed
262 263 264
				$x['item_thread_top'] = 1;
				$x['item_origin'] = 1;
				$x['item_verified'] = 1;
redmatrix's avatar
redmatrix committed
265
				$x['item_wall'] = 1;
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

				// if it might be a quote make it a quote
				if(strpos($s['body'],'--'))
					$x['body'] = $mention . '[quote]' . html2bbcode($s['body']) . '[/quote]';
				else
					$x['body'] = $mention . html2bbcode($s['body']);

				$x['sig'] = base64url_encode(rsa_sign($x['body'],$c[0]['channel_prvkey']));

				$post = item_store($x);
				$post_id = $post['item_id'];

				$x['id'] = $post_id;

				call_hooks('post_local_end', $x);

redmatrix's avatar
redmatrix committed
282
				Zotlabs\Daemon\Master::Summon(array('Notifier','wall-new',$post_id));
283 284 285 286 287
			}
		}
	}
}